SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraFijos] @pmNumero INT
 AS 
 DELETE FROM TraFijos  WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraLiquidaAnu] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_TraLiquida SET  Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelEmpleados] @pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Empleados
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPucSub]  @pmIdCuenta VARCHAR(16)

AS
  SELECT  IdCuenta,NomCuenta   FROM  Puc  
 WHERE  SUBSTRING(IdCuenta ,1,LEN(@pmIdCuenta) )=@pmIdCuenta AND IdCuenta<>@pmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryConcDiversosNov] 
 AS 
 SELECT IdConcepto,Concepto,Grupo,Concepto+' '+IdConcepto AS DsCon
 FROM ConcDiversos 
 WHERE Grupo IN ('FACTURA','OTROS') AND Inactivo=0
 ORDER BY Concepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTarifasFleMay] @pmIdAgencia VARCHAR(16),@pmTipoTarifa VARCHAR(10),@pmNumero INT
 AS 
 SELECT IdAgencia,TipoTarifa,Numero,TipoVehic,NitEmpresa,CapacMin,CapacMax,VrTarifa,TipoLiquida,DescTipoVeh,Inactivo,FechaCrea,IdUsuario
 FROM TarifasFleMay
 WHERE IdAgencia=@pmIdAgencia AND TipoTarifa=@pmTipoTarifa AND Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNomNovedadId] @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(Numero) AS MXNumero
 FROM Trn_NomNovedad
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTercClienteSdo] @pmIdClie VARCHAR(16),@pmVrSaldo MONEY
 AS 
 UPDATE TercCliente SET VrSaldo=@pmVrSaldo
 WHERE IdClie=@pmIdClie
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryProdTarfItm] @pmTipo VARCHAR(3)
 AS 
 SELECT MAX(Numero) AS MXNUM
 FROM Trn_ProdTarf
 WHERE Tipo=@pmTipo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryCompaniasDso]
AS
  SELECT  IdCia, Compania,IdCia+'  '+Compania AS DsCia
  FROM Companias WHERE  Inactivo=0 ORDER BY IdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasAux] @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,@pmTipoAfiVehic INT
 AS 
 INSERT INTO AcuFisCuentasAux (nAnno,nMes,IdCia,nReg,IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,SubAuxiliar,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos,TipoAfiVehic)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmnReg,@pmIdCuenta,@pmIdTercero,@pmCodAgncia,@pmIdCCosto,@pmIdSubCos
,@pmSubAuxiliar,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos,@pmTipoAfiVehic)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_Comisiones] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_Comisiones  WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelJurRecOfrecmto] @pmTipDoc VARCHAR(3),@pmReclamac INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_JurRecOfrecmto
 WHERE TipDoc=@pmTipDoc AND Reclamac=@pmReclamac AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMudOrdenServ] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_MudOrdenServ
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomPrestId]
 AS 
 SELECT MAX(NPrestamo) AS MXPSTM
 FROM Trn_NomPrest
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPolizas] @pmNumPoliza VARCHAR(30)
 AS 
 DELETE FROM Polizas
 WHERE NumPoliza=@pmNumPoliza
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTraPedAnexo] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
 AS 
  DELETE FROM Trn_TraPedAnexo WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTarifasFleMay] @pmIdAgencia VARCHAR(16),@pmTipoTarifa VARCHAR(10),@pmNumero INT,@pmNitEmpresa VARCHAR(16)
,@pmCapacMin DECIMAL(14,4),@pmCapacMax DECIMAL(14,4),@pmVrTarifa DECIMAL(14,4),@pmTipoLiquida VARCHAR(10),@pmDescTipoVeh VARCHAR(50)
,@pmInactivo BIT,@pmTipoVehic INT,@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TarifasFleMay (IdAgencia,TipoTarifa,Numero,TipoVehic,NitEmpresa,CapacMin,CapacMax,VrTarifa,TipoLiquida,DescTipoVeh,Inactivo,FechaCrea,IdUsuario)
 VALUES (@pmIdAgencia,@pmTipoTarifa,@pmNumero,@pmTipoVehic,@pmNitEmpresa,@pmCapacMin,@pmCapacMax,@pmVrTarifa,@pmTipoLiquida,@pmDescTipoVeh,@pmInactivo,@pmFechaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelFormaspago] @pmIdForma VARCHAR(4)
 AS 
 DELETE FROM Formaspago
 WHERE IdForma=@pmIdForma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelMayContratos] @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_MayContratos
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposNom] @pmIdNom VARCHAR(4)
 AS 
 DELETE FROM TiposNom
 WHERE IdNom=@pmIdNom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_SerId] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM  FROM tm_Ser 
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpProdMciasIva] @pmIdSubgrupo VARCHAR(8),@pmIdTarIva VARCHAR(4),@pmIdNewTar VARCHAR(4)
 AS 
 UPDATE ProdMcias SET IdTarIva=@pmIdNewTar
 WHERE IdSubgrupo=@pmIdSubgrupo AND IdTarIva=@pmIdTarIva
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryRutasPlanItm] @pmIdRuta VARCHAR(4)
 AS 
 SELECT MAX(IdPunto) AS MXPUNTO
 FROM RutasPlan
 WHERE IdRuta=@pmIdRuta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelUndMed] @pmIdUnd VARCHAR(4)
 AS 
 DELETE FROM UndMed
 WHERE IdUnd=@pmIdUnd
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryAgenciasId]
 AS 
 SELECT COUNT(IdAgencia) AS CANTAGE
 FROM Agencias
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelmonedas] @pmIdMneda VARCHAR(5)
 AS 
 DELETE FROM adm_monedas
 WHERE IdMneda=@pmIdMneda
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDeltm_CajaTot] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_CajaTot
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelAreas] @pmIdArea VARCHAR(4)
 AS 
 DELETE FROM Areas
 WHERE IdArea=@pmIdArea
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_MovCue] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_MovCue  WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryObjetosApp] @pmIdObj VARCHAR(10)
 AS 
 SELECT IdObj,IdGrupo,SubModulo,IndObj,Nombre,Formulario,Permisos,NivelMinimo,NomArchivo
 FROM Sys_ObjetosApp
 WHERE IdObj=@pmIdObj
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomPrest] @pmNPrestamo INT
 AS 
 DELETE FROM Trn_NomPrest
 WHERE NPrestamo=@pmNPrestamo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelLineas] @pmIdLinea VARCHAR(8)
 AS 
 DELETE FROM Lineas
 WHERE IdLinea=@pmIdLinea
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelDependencias] @pmIdDep VARCHAR(4)
 AS 
 DELETE FROM Dependencias
 WHERE IdDep=@pmIdDep
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmTipoAfiVehic INT
 AS 
 INSERT INTO AcuCuentasAux (nAnno,nMes,IdCia,nReg,IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,SubAuxiliar,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos,TipoAfiVehic)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmnReg,@pmIdCuenta,@pmIdTercero,@pmCodAgncia,@pmIdCCosto,@pmIdSubCos
,@pmSubAuxiliar,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos,@pmTipoAfiVehic)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTarifasFleMay] @pmIdAgencia VARCHAR(16),@pmTipoTarifa VARCHAR(10),@pmNumero INT,@pmNitEmpresa VARCHAR(16),@pmCapacMin DECIMAL(14,4),@pmCapacMax DECIMAL(14,4),@pmVrTarifa DECIMAL(14,4)
,@pmTipoLiquida VARCHAR(10),@pmDescTipoVeh VARCHAR(50),@pmInactivo BIT,@pmTipoVehic INT
 AS 
 UPDATE TarifasFleMay SET NitEmpresa=@pmNitEmpresa,CapacMin=@pmCapacMin,CapacMax=@pmCapacMax,VrTarifa=@pmVrTarifa,TipoLiquida=@pmTipoLiquida,DescTipoVeh=@pmDescTipoVeh,Inactivo=@pmInactivo,TipoVehic=@pmTipoVehic
 WHERE IdAgencia=@pmIdAgencia AND TipoTarifa=@pmTipoTarifa AND Numero=@pmNumero
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTarifAnticipos] @pmNumero INT
 AS 
 DELETE FROM TarifAnticipos
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTanques] @pmIdTanque VARCHAR(4)
 AS 
 DELETE FROM Tanques
 WHERE IdTanque=@pmIdTanque
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelEmpaques] @pmIdEmp VARCHAR(4)
 AS 
 DELETE FROM Empaques
 WHERE IdEmp=@pmIdEmp
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomConAuto] @pmIdReg INT
 AS 
 DELETE FROM Trn_NomConAuto
 WHERE IdReg=@pmIdReg
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryProdComboFac] @pmIdProducto VARCHAR(16),@pmItem INT
 AS 
 SELECT SUM(Cant) AS SCANT
 FROM ProdCombo
 WHERE IdProducto=@pmIdProducto AND  Item<>@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_ComboLin] @pmtmNumero VARCHAR(5),@pmtmLinea INT
 AS 
 DELETE FROM tm_Combo
 WHERE tmNumero=@pmtmNumero AND tmLinea=@pmtmLinea
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomRetId] @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(Numero) AS MXNUM
 FROM Trn_NomRet
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelOilMediciones] @pmTipDoc VARCHAR(3),@pmNumInvent INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_OilMediciones
 WHERE TipDoc=@pmTipDoc AND NumInvent=@pmNumInvent AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelLocalidades] @pmIdLocal VARCHAR(8)
 AS 
 DELETE FROM Localidades
 WHERE IdLocal=@pmIdLocal
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraLiquida] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraLiquida
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryFormaspagoID]
 AS 
  SELECT MAX(Consecutivo) AS MXCON
  FROM Formaspago
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_BalComCue] @pmtmEst CHAR(2)
 AS
 SELECT  tmIdCuenta,COUNT(tmItem) AS StotItm
 FROM tm_BalCom
 WHERE tmEst=@pmtmEst
 GROUP BY tmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelSubGrupos] @pmIdSubgrupo VARCHAR(8)
 AS 
 DELETE FROM SubGrupos
 WHERE IdSubgrupo=@pmIdSubgrupo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraManifiesto] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2)
 AS 
 BEGIN
  DECLARE @TrnNombre VARCHAR(20)
  SET @TrnNombre = 'TrnManifiesto'
   BEGIN TRANSACTION @TrnNombre
   DELETE FROM  Trn_TraManifAnexo  WHERE  TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia 
   DELETE FROM Trn_TraManifiesto  WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia 
  COMMIT TRANSACTION  @TrnNombre
 END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraAnticiposAnu] @pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCia CHAR(2),@pmVrAbonado MONEY
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_TraAnticipos SET  VrAbonado=@pmVrAbonado,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Anticipo=@pmAnticipo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomConCuentasId] @pmIdConcepto VARCHAR(4)
 AS 
 SELECT MAX(Item) AS MXITEM
 FROM NomConCuentas
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomAportante] @pmIdAportnte VARCHAR(16)
 AS 
 DELETE FROM NomAportante
 WHERE IdAportnte=@pmIdAportnte
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTercCliePrestamo] @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16)
 AS 
 DELETE FROM TercCliePrestamo
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelCnxextna] @pmIdReg INT
 AS 
 DELETE FROM Sys_Cnxextna
 WHERE IdReg=@pmIdReg
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelDepartamentos] @pmIdDep VARCHAR(8)
 AS 
 DELETE FROM Departamentos
 WHERE IdDep=@pmIdDep
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelGruposMcia] @pmIdGrupo VARCHAR(10)
 AS 
 DELETE FROM GruposMcia
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTercProvee] @pmIdProv VARCHAR(16)
 AS 
 DELETE FROM TercProvee
 WHERE IdProv=@pmIdProv
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuCuentasAuxSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum VARCHAR(4),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuCuentasAux SET AcuCuentasAux.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuCuentasAux INNER JOIN (SELECT nAnno,nMes,IdCia,nReg,IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,SubAuxiliar,TipoAfiVehic,TipoAcum,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuCuentasAux WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt AND TipoAcum=@pmTipoAcum) AS SA ON AcuCuentasAux.IdCia=SA.IdCia AND AcuCuentasAux.IdCuenta=SA.IdCuenta
 AND AcuCuentasAux.IdTercero=SA.IdTercero AND AcuCuentasAux.CodAgncia=SA.CodAgncia AND AcuCuentasAux.IdCCosto=SA.IdCCosto AND AcuCuentasAux.IdSubCos=SA.IdSubCos
 AND AcuCuentasAux.SubAuxiliar=SA.SubAuxiliar AND AcuCuentasAux.TipoAfiVehic=SA.TipoAfiVehic
 WHERE AcuCuentasAux.nAnno=@pmnAnno AND AcuCuentasAux.nMes=@pmnMes AND AcuCuentasAux.TipoAcum=@pmTipoAcum
 AND (@pmIdCia IS NULL OR AcuCuentasAux.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuCuentasAux.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelOilEntrada] @pmTipEnt VARCHAR(3),@pmNumEntrada INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_OilEntrada
 WHERE TipEnt=@pmTipEnt AND NumEntrada=@pmNumEntrada AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraCauDetalle] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraCauDetalle
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEmp_ContratoId]
 AS 
 SELECT MAX(NContrato) AS MXNCON
 FROM Emp_Contrato
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTarifAnticipos] @pmNumero INT,@pmCuotaMax DECIMAL(14,4),@pmTipoCuota VARCHAR(10),@pmTipoAplica VARCHAR(10)
,@pmCdOrigen VARCHAR(8),@pmCdDestino VARCHAR(8),@pmNitCliente VARCHAR(16),@pmCdTipoVeh VARCHAR(4),@pmTipoAfilVeh VARCHAR(10)
,@pmInactivo BIT,@pmCdRuta VARCHAR(4),@pmNitPropietario VARCHAR(16),@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11) 
 AS 
 INSERT INTO TarifAnticipos (Numero,CuotaMax,TipoCuota,TipoAplica,CdOrigen,CdDestino,CdRuta,NitCliente,CdTipoVeh,TipoAfilVeh,Inactivo,FechaCrea,IdUsuario,NitPropietario)
 VALUES (@pmNumero,@pmCuotaMax,@pmTipoCuota,@pmTipoAplica,@pmCdOrigen,@pmCdDestino,@pmCdRuta,@pmNitCliente,@pmCdTipoVeh,@pmTipoAfilVeh,@pmInactivo,@pmFechaCrea,@pmIdUsuario,@pmNitPropietario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTraManifAnexo] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraManifAnexo
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTercerosPrv] @pmIdTercero VARCHAR(16),@pmEsProveedor BIT
 AS 
 UPDATE Terceros SET EsProveedor=@pmEsProveedor
 WHERE IdTercero=@pmIdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelProdEntrega] @pmTipDoc VARCHAR(3),@pmNumEntrega INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_ProdEntrega WHERE TipDoc=@pmTipDoc AND NumEntrega=@pmNumEntrega AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposDoc] @pmIdDoc VARCHAR(3)
 AS 
 DELETE FROM Sys_TiposDoc
 WHERE IdDoc=@pmIdDoc
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelBodegas] @pmIdBodega VARCHAR(4)
 AS 
 DELETE FROM Bodegas
 WHERE IdBodega=@pmIdBodega
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomVac] @pmIdEmpleado VARCHAR(16),@pmNumero INT
 AS 
 DELETE FROM Trn_NomVac
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmtmVrCompras MONEY
 AS 
 INSERT INTO tm_BalCom (tmEst,tmIdCuenta,tmItem,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmCodAgncia,tmTipo,tmSaldoAnterior
,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo,tmVrPresupto,tmVrCompras)
 VALUES (@pmtmEst,@pmtmIdCuenta,@pmtmItem,@pmtmIdTercero,@pmtmIdVehiculo,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmCodAgncia
,@pmtmTipo,@pmtmSaldoAnterior,@pmtmTotalDebitos,@pmtmTotalCreditos,@pmtmNuevoSaldo,@pmtmVrPresupto,@pmtmVrCompras)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryProdComboItm] @pmIdProducto VARCHAR(16)
 AS 
 SELECT Max(Item) AS MXITM
 FROM ProdCombo
 WHERE IdProducto=@pmIdProducto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMayLiqTotales] @pmNLiquida INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_MayLiqTotales
 WHERE NLiquida=@pmNLiquida AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelFondos] @pmIdFondo VARCHAR(8)
 AS 
 DELETE FROM Fondos
 WHERE IdFondo=@pmIdFondo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_MovInvent] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_MovInvent
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposVeh] @pmIdTipoVeh VARCHAR(4)
 AS 
 DELETE FROM TiposVeh
 WHERE IdTipoVeh=@pmIdTipoVeh
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNominaCed] @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT IdPeriodo,KeyRegistro,IdEmpleado,FecInicial,FecFinal
 FROM Trn_Nomina
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelEmp_Contrato] @pmNContrato INT
 AS 
 DELETE FROM Emp_Contrato
 WHERE NContrato=@pmNContrato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelConceptos] @pmIdConcepto VARCHAR(4)
 AS 
 DELETE FROM Conceptos
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraOrdenServAnu] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4)
AS 
 UPDATE Trn_TraOrdenServ SET Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomAportanteReg]
 AS 
 SELECT COUNT(IdAportnte) AS SCanApo
 FROM NomAportante
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelDocSoporte] @pmTipDoc VARCHAR(3),@pmNumDoc INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DocSoporte
 WHERE TipDoc=@pmTipDoc AND NumDoc=@pmNumDoc AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuCuentasAux_Uni]  @pmIdVehiculo VARCHAR(10),@pmIdVehiculoDos VARCHAR(10),@pmNewVehiculo VARCHAR(10)
 AS 
 IF EXISTS (SELECT * FROM AcuCuentasAux WHERE (SubAuxiliar=@pmIdVehiculo OR SubAuxiliar=@pmIdVehiculoDos) AND TipoAcum LIKE '%V%')
  INSERT INTO AcuCuentasAux (nAnno,nMes,IdCia,nReg,IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,SubAuxiliar,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos,TipoAfiVehic)
  SELECT nAnno,nMes,IdCia,MAX(nReg),IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,@pmNewVehiculo,TipoAcum,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos),TipoAfiVehic
  FROM AcuCuentasAux WHERE (SubAuxiliar=@pmIdVehiculo OR SubAuxiliar=@pmIdVehiculoDos) AND TipoAcum LIKE '%V%'
  GROUP BY nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,TipoAcum,TipoAfiVehic
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMercancias] @pmIdMercancia VARCHAR(16)
 AS 
 DELETE FROM Mercancias
 WHERE IdMercancia=@pmIdMercancia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEmpleadosReg] @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT IdEmpleado,Apellidos,Nombres,Codigo,Direccion,IdLocal,Telefono,e_mail,NContrato,FecVincula
 FROM Empleados
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryLocalidadesNom] @pmLocalidad VARCHAR(50)
 AS 
 SELECT IdLocal,Localidad,IdDep,TipLocal
 FROM Localidades
 WHERE Localidad=@pmLocalidad
 ORDER BY IdLocal
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPresDetalle] @pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_PresDetalle
 WHERE Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpEmp_ContratoCc]  @pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
 AS 
 UPDATE Emp_Contrato SET IdCCosto=@pmIdCCosto
 WHERE IdSubCos=@pmIdSubCos
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTercCndtoresLic] @pmLicencia VARCHAR(25)
 AS 
 SELECT TercCndtores.*
 FROM TercCndtores
 WHERE Licencia=@pmLicencia
 ORDER BY IdConductor
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelRutas] @pmIdRuta VARCHAR(4)
 AS 
 DELETE FROM Rutas
 WHERE IdRuta=@pmIdRuta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpEmpleadosVeh] @pmIdEmpleado VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Empleados SET pVehiculo=@pmpVehiculo,FechaUpdate=@pmFechaUpdate
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryEdsRangosId] @pmTipoRango VARCHAR(3)
 AS 
 SELECT MAX(IdRango) AS MXITM 
 FROM Trn_EdsRangos
 WHERE TipoRango=@pmTipoRango
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTarifAnticipos] @pmNumero INT
 AS 
 SELECT Numero,CuotaMax,TipoCuota,TipoAplica,CdOrigen,CdDestino,CdRuta,NitCliente
,CdTipoVeh,TipoAfilVeh,Inactivo,NitPropietario,FechaCrea,IdUsuario
 FROM TarifAnticipos
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpVehRemolqKmt] @pmIdRemque VARCHAR(10),@pmKilmetraje INT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE VehRemolq SET Kilmetraje=@pmKilmetraje,FecUpdate=@pmFecUpdate
 WHERE IdRemque=@pmIdRemque
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelFijos] @pmIdCargo VARCHAR(4)
AS
 DELETE FROM  Fijos  WHERE IdCargo=@pmIdCargo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpProdMciasRet] @pmIdSubgrupo VARCHAR(8),@pmCdTarRet VARCHAR(4),@pmIdNewRet VARCHAR(4)
 AS 
 UPDATE ProdMcias SET  CdTarRet=@pmIdNewRet
 WHERE IdSubgrupo=@pmIdSubgrupo AND CdTarRet=@pmCdTarRet
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuFisCuentasAuxSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum VARCHAR(4),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuFisCuentasAux SET AcuFisCuentasAux.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuFisCuentasAux INNER JOIN (SELECT nAnno,nMes,IdCia,nReg,IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,SubAuxiliar,TipoAfiVehic,TipoAcum,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuFisCuentasAux WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt AND TipoAcum=@pmTipoAcum) AS SA ON AcuFisCuentasAux.IdCia=SA.IdCia AND AcuFisCuentasAux.IdCuenta=SA.IdCuenta
 AND AcuFisCuentasAux.IdTercero=SA.IdTercero AND AcuFisCuentasAux.CodAgncia=SA.CodAgncia AND AcuFisCuentasAux.IdCCosto=SA.IdCCosto AND AcuFisCuentasAux.IdSubCos=SA.IdSubCos
 AND AcuFisCuentasAux.SubAuxiliar=SA.SubAuxiliar AND AcuFisCuentasAux.TipoAfiVehic=SA.TipoAfiVehic
 WHERE AcuFisCuentasAux.nAnno=@pmnAnno AND AcuFisCuentasAux.nMes=@pmnMes AND AcuFisCuentasAux.TipoAcum=@pmTipoAcum
 AND (@pmIdCia IS NULL OR AcuFisCuentasAux.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuFisCuentasAux.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposCom] @pmIdCom VARCHAR(3)
 AS 
 DELETE FROM TiposCom
 WHERE IdCom=@pmIdCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPuc] @pmIdCuenta VARCHAR(16)
 AS 
 DELETE FROM Puc
 WHERE IdCuenta=@pmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTercCndtores] @pmIdConductor VARCHAR(16)
 AS 
 DELETE FROM TercCndtores
 WHERE IdConductor=@pmIdConductor
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraOrdenServ] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraOrdenServ
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelMayTiposBonif] @pmIdBonif VARCHAR(4)
 AS 
 DELETE FROM MayTiposBonif
 WHERE IdBonif=@pmIdBonif
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomSueldosId] @pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT MAX(Numero) AS MXNUM
 FROM Trn_NomSueldos
 WHERE  IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelNomRet] @pmIdEmpleado VARCHAR(16),@pmNumero INT
 AS 
 DELETE FROM Trn_NomRet
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomCertf] @pmIdEmpleado VARCHAR(16),@pmNumCertf VARCHAR(20)
 AS 
 DELETE FROM Trn_NomCertf
 WHERE IdEmpleado=@pmIdEmpleado AND NumCertf LIKE ISNULL(@pmNumCertf,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrymonedas] @pmIdMneda VARCHAR(5)
 AS 
 SELECT IdMneda,Mneda,Simbolo,MneLocal,VrCambio,Inactivo
 FROM adm_monedas
 WHERE IdMneda=@pmIdMneda
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelConcDiversos] @pmIdConcepto VARCHAR(4)
 AS 
 DELETE FROM ConcDiversos
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpVehiculosCdt] @pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Vehiculos SET IdConductor=@pmIdConductor,FechaUpdate=@pmFechaUpdate
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelUsucaja] @pmIdUsuario VARCHAR(11),@pmnFormato VARCHAR(10)
 AS 
 DELETE FROM adm_Usucaja
 WHERE IdUsuario=@pmIdUsuario AND nFormato LIKE ISNULL(@pmnFormato,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPuestos] @pmIdPto VARCHAR(4)
 AS 
 DELETE FROM Puestos
 WHERE IdPto=@pmIdPto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryProdMciasCol]
 AS 
 SELECT Color,COUNT(IdProducto)  AS CantProd
 FROM ProdMcias
 GROUP BY Color
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryProdImptosItm] @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4)
 AS 
 SELECT MAX(Item) AS MXITM
 FROM ProdImptos
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_BalCom] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_BalCom
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuNiifCuentasAuxSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum VARCHAR(4),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuNiifCuentasAux SET AcuNiifCuentasAux.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuNiifCuentasAux INNER JOIN (SELECT nAnno,nMes,IdCia,nReg,IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,SubAuxiliar,TipoAfiVehic,TipoAcum,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuNiifCuentasAux WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt AND TipoAcum=@pmTipoAcum) AS SA ON AcuNiifCuentasAux.IdCia=SA.IdCia AND AcuNiifCuentasAux.IdCuenta=SA.IdCuenta
 AND AcuNiifCuentasAux.IdTercero=SA.IdTercero AND AcuNiifCuentasAux.CodAgncia=SA.CodAgncia AND AcuNiifCuentasAux.IdCCosto=SA.IdCCosto AND AcuNiifCuentasAux.IdSubCos=SA.IdSubCos
 AND AcuNiifCuentasAux.SubAuxiliar=SA.SubAuxiliar AND AcuNiifCuentasAux.TipoAfiVehic=SA.TipoAfiVehic
 WHERE AcuNiifCuentasAux.nAnno=@pmnAnno AND AcuNiifCuentasAux.nMes=@pmnMes AND AcuNiifCuentasAux.TipoAcum=@pmTipoAcum
 AND (@pmIdCia IS NULL OR AcuNiifCuentasAux.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuNiifCuentasAux.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelAgencias] @pmIdAgencia VARCHAR(16)
 AS 
 DELETE FROM Agencias
 WHERE IdAgencia=@pmIdAgencia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomVacId] @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(Numero) AS MXNUM
 FROM Trn_NomVac
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_ComboItm] @pmtmNumero VARCHAR(5)
 AS 
 SELECT  MAX(tmItem) AS  MXITM
 FROM tm_Combo
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelObjetosApp] @pmIdObj VARCHAR(10)
 AS 
 DELETE FROM Sys_ObjetosApp
 WHERE IdObj=@pmIdObj
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelProdOrden] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_ProdOrden
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMttoSistemas] @pmIdSistema VARCHAR(4)
 AS 
 DELETE FROM MttoSistemas WHERE IdSistema=@pmIdSistema
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_NiifMov] @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 INT,@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,@pmCodCuentaCG VARCHAR(16)
 AS 
 INSERT INTO tm_NiifMov (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,CodCuentaCG)
 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,@pmCodCuentaCG)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelInstalaciones] @pmIdInstala VARCHAR(4)
 AS 
 DELETE FROM Instalaciones
 WHERE IdInstala=@pmIdInstala
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPuntosPrg]  @pmIdProgrma INT
 AS 
 SELECT Fecha,IdCliente,IdAgencia,NumTarjeta
 FROM Trn_Puntos
 WHERE IdProgrma=@pmIdProgrma
 ORDER BY Fecha
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelSobEntidades] @pmIdEnterr VARCHAR(8)
 AS 
 DELETE FROM SobEntidades
 WHERE IdEnterr=@pmIdEnterr
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomSueldos] @pmNumero INT,@pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Trn_NomSueldos
 WHERE Numero=@pmNumero AND IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelRegistro] @pmIdCia CHAR(2)
 AS 
 DELETE FROM adm_Registro
 WHERE IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasAux] @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,@pmTipoAfiVehic INT
 AS 
 INSERT INTO AcuNiifCuentasAux (nAnno,nMes,IdCia,nReg,IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,SubAuxiliar,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos,TipoAfiVehic)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmnReg,@pmIdCuenta,@pmIdTercero,@pmCodAgncia,@pmIdCCosto,@pmIdSubCos
,@pmSubAuxiliar,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos,@pmTipoAfiVehic)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraCauDetalle] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdConcepto VARCHAR(4),@pmDescripcion VARCHAR(250),@pmTipoConc VARCHAR(10),@pmVrDebito MONEY,@pmVrCredito MONEY
,@pmCdCuenta VARCHAR(16),@pmNitTercero VARCHAR(16),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmTipoAfiVehic VARCHAR(10),@pmVrBase MONEY,@pmTarifBase DECIMAL(14,4)
,@pmReferencia VARCHAR(50),@pmTipDocRef VARCHAR(3),@pmDocRef INT,@pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCiaFac CHAR(2),@pmItemFac INT,@pmFecVence SMALLDATETIME
,@pmEstadoReg INT,@pmTipoLiq VARCHAR(3),@pmNumLiquida INT,@pmCiaLiquida CHAR(2),@pmVrAbonado MONEY
 AS 
 INSERT INTO Trn_TraCauDetalle (TipDoc,Causacion,IdCia,Item,IdConcepto,Descripcion,TipoConc,VrDebito,VrCredito,CdCuenta,NitTercero,CdCCosto,CdSubCos,pVehiculo,TipoAfiVehic,VrBase,TarifBase,Referencia
,TipDocRef,DocRef,TipFac,Factura,IdCiaFac,ItemFac,FecVence,EstadoReg,TipoLiq,NumLiquida,CiaLiquida,VrAbonado)
 VALUES (@pmTipDoc,@pmCausacion,@pmIdCia,@pmItem,@pmIdConcepto,@pmDescripcion,@pmTipoConc,@pmVrDebito,@pmVrCredito,@pmCdCuenta,@pmNitTercero,@pmCdCCosto,@pmCdSubCos,@pmpVehiculo,@pmTipoAfiVehic,@pmVrBase
,@pmTarifBase,@pmReferencia,@pmTipDocRef,@pmDocRef,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmItemFac,@pmFecVence,@pmEstadoReg,@pmTipoLiq,@pmNumLiquida,@pmCiaLiquida,@pmVrAbonado)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTercerosReg] @pmIdTercero VARCHAR(16)
 AS 
 SELECT IdTercero,RazonSocial,Codigo,TipoId,Dv,Direccion,IdLocal,Telefono,IdEstado,Inactivo
 FROM Terceros
 WHERE IdTercero=@pmIdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelAfAmtDetalle] @pmNumero INT,@pmIdDiferido VARCHAR(30)
 AS 
 DELETE FROM Trn_AfAmtDetalle
 WHERE Numero=@pmNumero AND IdDiferido LIKE ISNULL(@pmIdDiferido,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelNomLogVeh] @pmIdEmpleado VARCHAR(16),@pmNumReg INT
 AS 
 DELETE FROM Trn_NomLogVeh
 WHERE IdEmpleado=@pmIdEmpleado AND NumReg=@pmNumReg
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNomLogVehId] @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(NumReg) AS MXNUM
 FROM Trn_NomLogVeh
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryCnxextnaItm] 
 AS 
 SELECT MAX(IdReg) AS MXITM
 FROM Sys_Cnxextna
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomCAdmon] @pmIdEmpleado VARCHAR(16),@pmNumero INT
 AS 
 DELETE FROM Trn_NomCAdmon
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryFijosDso]
AS
  SELECT     IdCargo, Concepto,Concepto +  '  ' + IdCargo AS DsCar  FROM  Fijos WHERE Inactivo=0 ORDER BY Concepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomCAdmonId] @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(Numero) AS MXNUM
 FROM Trn_NomCAdmon
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTercCliente] @pmIdClie VARCHAR(16)
 AS 
 DELETE FROM TercCliente
 WHERE IdClie=@pmIdClie
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTercVendedor] @pmIdVend VARCHAR(16)
 AS 
 DELETE FROM TercVendedor
 WHERE IdVend=@pmIdVend
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexID] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryVehTraspasosID] @pmIdVehiculo VARCHAR(10)
 AS 
 SELECT MAX(Numero) AS MxNum 
 FROM Trn_VehTraspasos
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTercerosVen] @pmIdTercero VARCHAR(16),@pmEsVendedor BIT
 AS 
 UPDATE Terceros SET EsVendedor=@pmEsVendedor
 WHERE IdTercero=@pmIdTercero
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTercCndContrato] @pmNumContrato INT
 AS 
 DELETE FROM TercCndContrato
 WHERE NumContrato=@pmNumContrato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTercerosCli] @pmIdTercero VARCHAR(16),@pmEsCliente BIT
 AS 
 UPDATE Terceros SET EsCliente=@pmEsCliente
 WHERE IdTercero=@pmIdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelVehiculos] @pmIdVehiculo VARCHAR(10)
 AS 
 DELETE FROM Vehiculos
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTarifAnticipos] @pmNumero INT,@pmCuotaMax DECIMAL(14,4),@pmTipoCuota VARCHAR(10),@pmTipoAplica VARCHAR(10)
,@pmCdOrigen VARCHAR(8),@pmCdDestino VARCHAR(8),@pmNitCliente VARCHAR(16),@pmCdTipoVeh VARCHAR(4)
,@pmTipoAfilVeh VARCHAR(10),@pmInactivo BIT,@pmCdRuta VARCHAR(4),@pmNitPropietario VARCHAR(16)
 AS 
 UPDATE TarifAnticipos SET CuotaMax=@pmCuotaMax,TipoCuota=@pmTipoCuota,TipoAplica=@pmTipoAplica,CdOrigen=@pmCdOrigen,CdDestino=@pmCdDestino
,NitCliente=@pmNitCliente,CdTipoVeh=@pmCdTipoVeh,TipoAfilVeh=@pmTipoAfilVeh,CdRuta=@pmCdRuta,Inactivo=@pmInactivo,NitPropietario=@pmNitPropietario
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTerceros] @pmIdTercero VARCHAR(16)
 AS 
 DELETE FROM Terceros
 WHERE IdTercero=@pmIdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomConAutoId] 
 AS 
 SELECT MAX(IdReg) AS MXIDREG
 FROM Trn_NomConAuto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelVehRemolq] @pmIdRemque VARCHAR(10)
 AS 
 DELETE FROM VehRemolq
 WHERE IdRemque=@pmIdRemque
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTercerosCdt]  @pmIdTercero VARCHAR(16),@pmEsConductor BIT
 AS 
 UPDATE Terceros SET EsConductor=@pmEsConductor
 WHERE IdTercero=@pmIdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpEmp_ContratoAre] @pmIdArea VARCHAR(4),@pmIdDep VARCHAR(4)
 AS 
 UPDATE Emp_Contrato SET IdArea=@pmIdArea
 WHERE IdDep=@pmIdDep
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMttoOrden] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_MttoOrden WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelVeh_Contrato] @pmNumContrato INT
 AS 
 DELETE FROM Veh_Contrato
 WHERE NumContrato=@pmNumContrato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTercCndContrato] @pmNumContrato INT
 AS 
 SELECT NumContrato,IdConductor,IdTipCon,IdModd,FecIngreso,FecRetiro,FecVigencia,Observaciones,FecUpdate,IdUsuario
 FROM TercCndContrato
 WHERE NumContrato=@pmNumContrato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelSobretasas] @pmIdDec VARCHAR(4),@pmNumDeclara INT
 AS 
 DELETE FROM Trn_Sobretasas
 WHERE IdDec=@pmIdDec AND NumDeclara=@pmNumDeclara
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposCol] @pmIdColor VARCHAR(4)
 AS 
 DELETE FROM TiposCol
 WHERE IdColor=@pmIdColor
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelProdMcias] @pmIdProducto VARCHAR(16)
 AS 
 DELETE FROM ProdMcias
 WHERE IdProducto=@pmIdProducto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelSobDetalle] @pmIdDec VARCHAR(4),@pmNumDeclara INT
 AS 
 DELETE FROM Trn_SobDetalle
 WHERE IdDec=@pmIdDec AND NumDeclara=@pmNumDeclara
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraManifiestoAnu] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmEdoMintrans VARCHAR(5)
 AS 
 UPDATE Trn_TraManifiesto SET   EdoMintrans=@pmEdoMintrans,Anulado=@pmAnulado,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelCtasCorrientes] @pmIdCta VARCHAR(4)
 AS 
 DELETE FROM CtasCorrientes
 WHERE IdCta=@pmIdCta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelNomina] @pmIdPeriodo VARCHAR(8),@pmKeyRegistro VARCHAR(30)
 AS 
 DELETE FROM Trn_Nomina
 WHERE IdPeriodo=@pmIdPeriodo AND KeyRegistro like ISNULL(@pmKeyRegistro,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelVehPartes] @pmIdVehiculo VARCHAR(10),@pmNumParte VARCHAR(20)
 AS 
 DELETE FROM VehPartes WHERE IdVehiculo=@pmIdVehiculo AND NumParte=@pmNumParte
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomVacEmp] @pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Trn_NomVac
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpProdMciasIca] @pmIdSubgrupo VARCHAR(8),@pmCdTarIca VARCHAR(4),@pmIdNewIca VARCHAR(4)
 AS 
 UPDATE ProdMcias SET  CdTarIca=@pmIdNewIca
 WHERE IdSubgrupo=@pmIdSubgrupo AND CdTarIca=@pmCdTarIca
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraRemesaAnu] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_TraRemesa SET  Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion
,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpEmp_ContratoTlq] @pmIdNom VARCHAR(4),@pmTipoLiquida CHAR(1)
 AS 
 UPDATE Emp_Contrato SET TipoLiquida=@pmTipoLiquida
 WHERE IdNom=@pmIdNom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNomNovId] @pmIdPeriodo VARCHAR(8),@pmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(Item) AS MXITEM
 FROM Trn_NomNov
 WHERE IdPeriodo=@pmIdPeriodo AND IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTercCndtoresVeh] @pmIdConductor VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TercCndtores SET pVehiculo=@pmpVehiculo,FechaUpdate=@pmFechaUpdate
 WHERE IdConductor=@pmIdConductor
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTarifasFletes]  @pmNumero INT
 AS 
 DELETE FROM TarifasFletes  WHERE  Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMarcas] @pmIdMarca VARCHAR(4)
 AS 
 DELETE FROM Marcas
 WHERE IdMarca=@pmIdMarca
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraAnticipos] @pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraAnticipos
 WHERE TipDoc=@pmTipDoc AND Anticipo=@pmAnticipo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelFcSemanas] @pmIdVehiculo VARCHAR(10),@pmnSemana INT
 AS 
 DELETE FROM Trn_FcSemanas
 WHERE IdVehiculo=@pmIdVehiculo AND nSemana=@pmnSemana
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPeajesClase] @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM PeajesClase
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelEdsRangos] @pmTipoRango VARCHAR(3),@pmIdRango INT
 AS 
 DELETE FROM Trn_EdsRangos
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelZonas] @pmIdZona VARCHAR(4)
 AS 
 DELETE FROM Zonas
 WHERE IdZona=@pmIdZona
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelGrupos] @pmIdGrupo VARCHAR(8)
 AS 
 DELETE FROM Grupos
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_Diario] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_Diario
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraCumplido] @pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraCumplido
 WHERE TipDoc=@pmTipDoc AND Cumplido=@pmCumplido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CedContparte, NombreCparte, DirCparte, TelCparte,DanosCParte,DanosEmp, ValorDeducible, Aseguradora
,AuxilioMutuo,Reclamaciones,CdCausal,Causal,Cedtraparte,PolizaResCivil,VigencResCivil,NitEmpAseg,NombreAseg,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 JurCausales AS CS ON C.CdCausal=CS.IdCausal
  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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelJurCalifica] @pmIdCalifica VARCHAR(4)
 AS 
 DELETE FROM JurCalifica
 WHERE IdCalifica=@pmIdCalifica
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CdCausal,Cedtraparte,PolizaResCivil,VigencResCivil,NitEmpAseg,NombreAseg
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CdCausal,Cedtraparte,PolizaResCivil,VigencResCivil,NitEmpAseg,NombreAseg,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CdCausal,Cedtraparte,PolizaResCivil,VigencResCivil,NitEmpAseg,NombreAseg,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CdCausal,Causal,Cedtraparte,PolizaResCivil,VigencResCivil,NitEmpAseg,NombreAseg
,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 LEFT JOIN JurCausales AS CS ON C.CdCausal=CS.IdCausal
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryJurReclamacFmt] @pmTipDoc VARCHAR(3),@pmReclamacIni INT,@pmReclamacFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT R.TipDoc AS TipRec,TipoDoc,R.Reclamac AS NumReclam,R.IdCia AS CdCia,Compania,R.Fecha AS FechaReclam,FechaRec,TipSin,Siniestro,IdCiaSin,FechaSuc,IdReclamnte,T.RazonSocial AS NomReclamante
,R.IdTipoRec AS CdTipoRec,TipReclamante,VrReclamac,R.Observacion AS Observ,R.OrigenAdd,R.TimeSys AS FechaCreacion,R.IdCiaCrea AS CdCiaCrea,R.IdUsuario AS CdUsuario,Usuario
,C.Radicacion,C.IdVehiculo AS PlacaVeh,NumVeh,C.IdPropietario AS NitPropiterio,NP.RazonSocial AS Propietario,C.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,SinRecorrido,TipCar,Cartulina,IdCiaCar
,IdAbogado,A.RazonSocial AS Abogado,C.IdTipoPro AS IdTProc,TipoProceso,C.IdClase AS IdClaAcc,ClaseAccdte, C.IdCalifica AS IdCalf,CalificaCaso,LugarAcc,CausaAcc,Comentario,C.Observacion AS SntroObserv
,Reclamaciones,PolizaResCivil,VigencResCivil,NitEmpAseg,NombreAseg
--detalles
,Item,FechaOfrec,VrOfrecmto,OfcAceptado,FecAceptado,OfcPagado,NumSolGiro,Comentarios
 FROM Trn_JurReclamac AS R INNER JOIN Companias  AS CI ON R.IdCia=CI.IdCia INNER JOIN Terceros AS T ON R.IdReclamnte=T.IdTercero
 INNER JOIN JurTiposRec AS TR ON R.IdTipoRec=TR.IdTipoRec  INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 INNER JOIN Sys_TiposDoc AS TD ON R.TipDoc=TD.IdDoc 
 LEFT JOIN (SELECT TipDoc,CasoID,IdCia,Fecha,Radicacion,IdVehiculo,IdPropietario,IdConductor,SinRecorrido,TipCar,Cartulina,IdCiaCar,FechaSuc
,IdAbogado,IdTipoPro,IdClase,IdCalifica,LugarAcc,CausaAcc,Comentario,Observacion,Reclamaciones,PolizaResCivil,VigencResCivil,NitEmpAseg,NombreAseg,ArchivoCaso
 FROM Trn_JurCasos
 UNION ALL
 SELECT TipDoc,CasoID,IdCia,Fecha,Radicacion,'',IdEmpresa,IdAuxiliar,0,'0',0,'00',Fecha
,IdAbogado,CdTipoPro,'0','0','','','',Observacion,'','',Null,'','',ArchivoCaso FROM Trn_JurInvestiga) AS C ON R.TipSin=C.TipDoc AND R.Siniestro=C.CasoID AND R.IdCiaSin=C.IdCia
 LEFT JOIN Vehiculos AS V ON C.IdVehiculo=V.IdVehiculo LEFT JOIN Terceros AS NP ON C.IdPropietario=NP.IdTercero 
 LEFT JOIN Terceros AS NC ON C.IdConductor=NC.IdTercero LEFT JOIN Terceros AS A ON C.IdAbogado=A.IdTercero
 LEFT JOIN JurTipos AS TP ON C.IdTipoPro=TP.IdTipoPro LEFT JOIN JurClaseAcc AS CA ON C.IdClase=CA.IdClase
 LEFT JOIN JurCalifica AS CL ON C.IdCalifica=CL.IdCalifica
 LEFT JOIN Trn_JurRecOfrecmto AS RO ON R.TipDoc=RO.TipDoc AND R.Reclamac=RO.Reclamac AND R.IdCia=RO.IdCia
 WHERE R.TipDoc=@pmTipDoc AND R.Reclamac BETWEEN @pmReclamacIni AND @pmReclamacFin AND R.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
, Cedtraparte,Contraparte, DirContraparte,TelContraparte,ValDanosCParte, ValDanosEmp, ValDeducible, EmpAsegdora, AuxMutuo, ArchivoCaso, Documntos, Croquis,Reclamaciones,CdCausal,PolizaResCivil,VigencResCivil,NitEmpAseg,NombreAseg,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryJurCalifica] @pmIdCalifica VARCHAR(4)
 AS 
 SELECT IdCalifica,CalificaCaso,IdClase,Inactivo
 FROM JurCalifica
 WHERE IdCalifica=@pmIdCalifica
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,HoraInicial,HoraFinal,TipoIncLab
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomDetalleEmp] @pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Trn_NomDetalle 
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
--obsoleto , después de la versión 4.1.53 May 21/2019
CREATE PROCEDURE [dbo].[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,SubTipoCot,DenyPEN,DenyEPS,DenyARP,DenyCaj,DenyDcto,LiqEspSalud,DctoSalud,PagoSalud,D.VrBaseLiq,TN.NomDiario
 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
 INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom
 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))
 UNION ALL
 SELECT D.IdPeriodo,V.IdEmpleado,Apellidos,Nombres,V.NContrato,V.FecInicial,V.FecFinal,C.IdDep,C.IdInstala,C.IdCCosto,C.IdSubCos,C.IdNom,V.pVehiculo
,V.VrSalario,0,0,IdFonPen,E.IdFonEps,IdFonArp,IdFonCes,IdCajaCom,C.FecIngreso,C.FecRetiro,C.IdArea,C.IdClase,0,C.TipoTrabj,C.VrAuxTrans,C.VrUpc
,C.SalIntegral,C.SalMinimo,C.Inactivo,D.IdConcepto,Concepto,ClaseCon,CN.Tarifa,'NOMINA',0,D.Cantidad,D.VrDevgado,D.VrDeducido,D.VrBaseLiq,D.Tarifa
,D.Cantidad,0,D.Cantidad,E.IdFonEps,C.IdCot,C.SubTipoCot,C.DenyPEN,C.DenyEPS,C.DenyARP,C.DenyCaj,C.DenyDcto,LiqEspSalud,DctoSalud,PagoSalud,D.VrBaseLiq,TN.NomDiario
 FROM Trn_NomVac AS V INNER JOIN Trn_NomVacDet AS D ON V.IdEmpleado=D.IdEmpleado AND V.Numero=D.Numero
 INNER JOIN Empleados AS E ON V.IdEmpleado=E.IdEmpleado INNER JOIN Emp_Contrato AS C ON V.NContrato=C.NContrato
 INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto
 WHERE SUBSTRING(D.IdPeriodo,1,6)=@pmMes AND D.IdConcepto IN ('DCP','DCS','FSP','FSU') AND V.Anulado=0
 AND D.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%') AND C.IdInstala LIKE ISNULL(@pmIdInstala,'%') AND C.IdNom LIKE ISNULL(@pmIdNom,'%')
 AND C.IdArea LIKE ISNULL(@pmIdArea,'%') AND C.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 ORDER BY  N.IdEmpleado,N.NContrato,D.IdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmBasExceso,tmNitTercero,tmHoraInicial,tmHoraFinal,tmTipoInc)
 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,VrBasExceso,NitTercero,HoraInicial,HoraFinal,TipoIncLab
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrBasExceso,D.HoraInicial,D.HoraFinal,D.TipoIncLab
--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))
--Descuentos en vacaciones
UNION ALL
 SELECT V.IdEmpleado,Apellidos,Nombres,V.NContrato,V.IdEmpleado,D.IdPeriodo,V.FecInicial,V.FecFinal,V.Cantidad,0,V.VrLiquida,V.VrTotDed
,C.IdNom,TipoNomina,C.IdDep,Dependencia,C.IdCCosto,CCosto,C.IdSubCos,SubCosto,C.IdInstala,Instlacion,V.VrSalario,V.pVehiculo,V.TipCom,TipoCom,V.Comprobante,V.IdCiaCom
,V.Observacion,V.IdEstado,ED.Estado,0,V.Numero,C.TipoLiquida,V.TimeSys,V.FecUpdate,V.IdUsuario,Usuario
,D.Item,D.IdConcepto,Concepto,D.Detalle,0,CASE D.IdConcepto WHEN 'DCP' THEN 0 WHEN 'DCS' THEN 0 WHEN 'FSP' THEN 0 WHEN 'FSU' THEN 0 ELSE D.Cantidad END AS CantDed,0,D.VrDevgado,D.VrDeducido,ClaseCon,'%','NOMINA',D.VrBaseLiq,D.Tarifa
,CASE D.IdConcepto WHEN 'DCP' THEN 0 WHEN 'DCS' THEN 0 WHEN 'FSP' THEN 0 WHEN 'FSU' THEN 0 ELSE D.Cantidad END AS DiasCalc,0,'0','',D.NPrestamo,D.NCuota,'VAC',D.VrBaseLiq,0,Null,Null,''
,E.Codigo,E.IdLugarCed,Localidad,E.Direccion,E.Telefono,E.TelMovil,E.e_mail,E.IdProf,Profesion,E.IdClase,ClaseCuenta,NumCuenta,E.IdBanco,Banco,IdFonPen,FP.Fondo
,IdFonEps,FS.Fondo,IdFonArp,FR.Fondo,IdCajaCom,FC.Fondo,EsCondtor,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar,LugarLic,TallaCam,TallaPan,TallaZap,E.Inactivo
,C.IdTipCon,TipoContrato,FecIngreso,FecRetiro,Indefinido,C.IdCargo,Cargo,C.IdArea,Area,C.IdClase,ClaseRiesgo,CR.Tarifa,C.VrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina
,ATFinMes,SegFinMes,DchDotacion,DchCafeteria,C.IdEstado,ECO.Estado,C.Inactivo,C.IdJornada,Jornada,TipoTrabj,C.IdGrupo
 FROM Trn_NomVac AS V INNER JOIN Trn_NomVacDet AS D ON V.IdEmpleado=D.IdEmpleado AND V.Numero=D.Numero
 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 NomConceptos AS NC ON D.IdConcepto=NC.IdConcepto INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom INNER JOIN Dependencias AS DPD ON C.IdDep=DPD.IdDep
 INNER JOIN CentroCosto AS CC ON C.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala
 INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo
 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 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 EstadoDoc AS ED ON V.IdEstado=ED.IdEstado
 LEFT JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos LEFT JOIN TiposCom AS TCM ON V.TipCom=TCM.IdCom
 WHERE V.Anulado=0 
 AND (SUBSTRING(D.IdPeriodo,1,6)>=SUBSTRING(CONVERT(VARCHAR(30),@pmFecIni,112),1,6) AND SUBSTRING(D.IdPeriodo,1,6)<=SUBSTRING(CONVERT(VARCHAR(30),@pmFecFin,112),1,6))
 AND D.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%') AND V.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 AND C.IdNom LIKE ISNULL(@pmIdNom,'%') AND C.IdDep LIKE ISNULL(@pmIdDep,'%') AND C.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND C.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND V.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND C.IdArea LIKE ISNULL(@pmIdArea,'%')
 AND C.IdTipCon LIKE ISNULL(@pmIdTipCon,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND ClaseCon LIKE ISNULL(@pmClaseCon,'%')
 AND (V.NContrato>=ISNULL(@pmNContrato,0)  AND V.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))
 AND (D.NPrestamo>=ISNULL(@pmNPrestamo,0)  AND D.NPrestamo<=ISNULL(@pmNPrestamo,2147483647))
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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(CASE  WHEN IdConcepto IN ('DNR','SAN','STC') THEN CantDed*VrUnitario WHEN IdConcepto IN ('IGE','IRP') THEN (N.VrSalario/30)*CantDevg  ELSE VrBaseLiq END) 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 IdConcepto IN ('BAS','AXT','LIC','IGE','IRP','HHD','HHN','DSR','VAC','DNR','SAN','STC')
   AND (VrBaseLiq>0 OR IdConcepto='DNR' OR IdConcepto='SAN' OR IdConcepto='STC')
   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(CASE  WHEN IdConcepto IN ('DNR','SAN','STC') THEN CantDed*VrUnitario WHEN IdConcepto IN ('IGE','IRP') THEN (N.VrSalario/30)*CantDevg  ELSE VrBaseLiq END) 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 OR IdConcepto='DNR' OR IdConcepto='SAN' OR IdConcepto='STC')
   AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
   GROUP BY  SUBSTRING(N.IdPeriodo,1,4),SUBSTRING(N.IdPeriodo,5,2),IdConcepto
  END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 OR VrTotDevg>0)
    GROUP BY  SUBSTRING(N.IdPeriodo,1,4),SUBSTRING(N.IdPeriodo,5,2),VrATransMes
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrBasExceso,NitTercero,HoraInicial,HoraFinal,TipoIncLab
 FROM Trn_NomDetalle
 WHERE IdPeriodo=@pmIdPeriodo AND KeyRegistro=@pmKeyRegistro 
  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrBasExceso,NitTercero,HoraInicial,HoraFinal,TipoIncLab)
 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,VrBasExceso,NitTercero,HoraInicial,HoraFinal,TipoIncLab
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,N.IdDep,N.IdInstala,N.IdCCosto
,N.IdSubCos,N.IdNom,N.VrSalario,N.pVehiculo AS PlacaVeh,N.TipCom,N.Comprobante,N.IdCiaCom,EsRetiro,NLiqRet,TipoLiq,Apellidos,Nombres,EsCondtor,E.pVehiculo AS VehiculoDef
,DenyCes,DenyPri,DenyVac
--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,NitTercero
 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
 LEFT JOIN Emp_Contrato AS CN ON N.NContrato=CN.NContrato AND N.IdEmpleado=CN.IdEmpleado
 WHERE N.IdPeriodo=@pmIdPeriodo AND N.IdNom like ISNULL(@pmIdNom,'%')
 AND  (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 ORDER BY N.IdEmpleado,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,@pmVrBasExceso MONEY,@pmNitTercero VARCHAR(16)
,@pmHoraInicial DATETIME,@pmHoraFinal DATETIME,@pmTipoIncLab VARCHAR(20)
 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,VrBasExceso,NitTercero,HoraInicial,HoraFinal,TipoIncLab)
 VALUES (@pmIdPeriodo,@pmKeyRegistro,@pmIdEmpleado,@pmItem,@pmIdConcepto,@pmDetalle,@pmCantDevg,@pmCantDed,@pmVrUnitario,@pmVrTotDevg,@pmVrTotDed,@pmUnidad,@pmClaseLiq,@pmVrOrigen,@pmTarifa
,@pmDiasCalc,@pmDiasNov,@pmCodFondo,@pmNPrestamo,@pmNCuota,@pmOrigCargue,@pmVrBaseLiq,@pmVrBasExceso,@pmNitTercero,@pmHoraInicial,@pmHoraFinal,@pmTipoIncLab)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,@pmVrBasExceso MONEY,@pmNitTercero VARCHAR(16)
,@pmHoraInicial DATETIME,@pmHoraFinal DATETIME,@pmTipoIncLab VARCHAR(20)
 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,VrBasExceso=@pmVrBasExceso,NitTercero=@pmNitTercero,HoraInicial=@pmHoraInicial,HoraFinal=@pmHoraFinal,TipoIncLab=@pmTipoIncLab
 WHERE IdPeriodo=@pmIdPeriodo AND KeyRegistro=@pmKeyRegistro AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,D.TipoIncLab,D.HoraInicial,D.HoraFinal
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,SUM(VrBasExceso) AS STOTBEXC
 ,SUM(CASE WHEN IdConcepto='IGE' AND VrBasExceso>0 THEN VrBasExceso ELSE VrBaseLiq END) AS SBASEIGE 
 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','DCP','DCS','FSP','FSU','RET') 
 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,SUM(VrBasExceso) AS STOTBEXC
 ,SUM(CASE WHEN IdConcepto='IGE' AND VrBasExceso>0 THEN VrBasExceso ELSE VrBaseLiq END) AS SBASEIGE
 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 ('DCP','DCS','FSP','FSU','RET')
 AND ClaseLiq IN ('NOMINA','NOVEDAD') AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 GROUP BY IdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,TC.TipoCar AS TipoCarr,CL.NomColor AS Color,V.Modelo AS ModeloVeh,FecRep,Config,V.IdCat AS CodCatg,CarrCapac
,PesoVacio,NumMotor,SerieChasis,RQ.IdCrceria AS RmqIdCarr,MR.Marca AS MarcaRmq,CR.NomColor AS ColorRmq,NitEmpresa,NE.RazonSocial AS Empresa
,IdPoseedor,NT.RazonSocial AS Poseedor,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CdTarifa,TarjProp,RespCivil,VigRCivil,VrComcial,V.IdEstado AS CdEstado,Estado,V.Inactivo AS Inactvo,FecPriServ,FecUltServ
,RegNalCarga,FecRegNal,VigRegNal,RevTecMec,FecTecMec,VigTecMec,V.Observacion AS Observ,TipoAfil
,NumVeh,ClaseVeh,V.IdTipoVeh AS CdTipo,V.IdLinea AS CdLinea,LineaVeh,V.IdColor AS CdColor,V.IdTipoMot AS CdTipMotor,TipoMotor
,V.IdCrceria AS CdCarr,VehArtic,NumLlan,NumLlans,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,PesoMax,NumSerie,V.NitProv AS NitProveedor,NPV.RazonSocial AS Proveedor,V.FecCompra AS FechaCompra
,VrAseg,V.VrAvaludo AS VlrAvaludo,V.VidaUtil AS Vida_Util,FecSalida,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdGrupR,FecIngreso
,FecVigencia,FecRetiro,FecTProp,VigTProp,CdLugTp,LT.Localidad AS LugarTarjProp,Ulttramite,FecRCivil,KmInicial,KmActual,Km2Actual,V.Descripcion AS VehDescripcion,V.CdCenSer AS CodCentro,CentroServ
,V.CdLocal AS CodCiuUbic,LU.Localidad AS CiuUbicacion,LU.IdDep AS CodDpto,Departamento,V.Ubicacion,V.PathFoto,Regtradora,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,FecUltAcc,TieneAcc,FecPagImp
,V.IdUsuario AS CdUsuario,Usuario,V.FechaAdd AS Fec_Add,V.FechaUpdate AS Fec_Upd,EV.NColor AS NumColor,OutDemand,ValorCupo,ObligaTProd,GarantiaAcc,DocCompleta,CertMovilizacion
,FecCertMovil,VigCertMovil,CdRutaHab,RQ.IdMarca AS RmqIdMarca,RQ.IdColor AS RmqIdColor,RC.TipoCar AS RmqTipoCar,GPSoperador,GPSUsuario,GPSClave,GPSIdOper,CantFiltros
--campos libres
,TarjOper,FecTarjOper,VigTarjOper,CertGases,FecCertGas,VigCertGas,DeclaracImp,TipoIngreso,V.IdOrgTra AS CdOrgTra,NomOrgTrans
--Informacion coductor
,V.IdConductor,NC.RazonSocial AS Conductor,NC.IdLugarCed,LCCE.Localidad AS LugCed,CND.TipoSangre,CND.FactorRh,CND.FecNacmto,NC.TelMovil AS CelCnd,NC.Telefono AS TelCnd,NC.e_mail AS Mailcnd,nc.Direccion AS DirCnd
,NC.IdLocal,LCND.Localidad AS LocCnd,CND.Licencia,CND.CatLicencia,CCND.ClaseCuenta AS TipCtaCnd,CND.NumCuenta AS CtaCnd,BCND.Banco AS BanCnd,FECN.Fondo AS FonEps,FPCN.Fondo AS FonPen,FACN.Fondo AS FonArl
--INFORMACION TRAILER Y PROPIETARIO
,CdRemque,Longitud,CarrAlto,CarrAncho,CarrLargo,V.UndCapc AS UndCapacidad,Comptmtos,CapComp,PasjerosPie,PasjerosSen,VR.IdPropietario AS IdPropTra,PT.RazonSocial AS NomPropTra,LPT.Localidad AS LugCedPt
,PT.Direccion AS DirProTra,PT.TelMovil AS CelProTra,PT.Telefono AS TelProTra
--INFORMACION PROPIEATARIO DEL VEHICULO
,V.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,NP.Direccion AS DirProV,NP.TelMovil AS CelularProV,NP.Telefono AS TelProV,V.IdPpd AS CdTipProp,TipoProp,VehPropio,Adquisc
 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 VehRemolq AS RQ ON V.CdRemque=RQ.IdRemque
 LEFT JOIN Marcas AS MR ON RQ.IdMarca=MR.IdMarca LEFT JOIN TiposCol AS CR ON RQ.IdColor=CR.IdColor 
 LEFT JOIN TiposCar AS RC ON RQ.IdCrceria=RC.IdCrceria LEFT JOIN OrgTransito AS OG ON V.IdOrgTra=OG.IdOrgTra
 --CONSULTA PARA INFORMACION DEL CONDUCTOR
 LEFT JOIN TercCndtores AS CND ON V.IdConductor=CND.IdConductor LEFT JOIN Localidades AS LCCE ON NC.IdLugarCed=LCCE.IdLocal
 LEFT JOIN Localidades AS LCND ON NC.IdLocal=LCND.IdLocal LEFT JOIN ClaseCta AS CCND ON CND.IdClase=CCND.IdClase
 LEFT JOIN Bancos AS BCND ON CND.IdBanco=BCND.IdBanco LEFT JOIN Fondos AS FECN ON CND.CdFonEps=FECN.IdFondo
 LEFT JOIN Fondos AS FPCN ON CND.CdFonPen=FPCN.IdFondo LEFT JOIN Fondos AS FACN ON CND.CdFonArp=FACN.IdFondo
 --CONSULTA TRAILER
 LEFT JOIN VehRemolq AS VR ON V.CdRemque=VR.IdRemque LEFT JOIN Terceros AS PT ON VR.IdPropietario=PT.IdTercero
 LEFT JOIN Localidades AS LPT ON PT.IdLugarCed=LPT.IdLocal
 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 V.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 V.IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 AND IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')  AND V.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 (V.FecCompra>=ISNULL(@pmFecComIni,CAST('19100101' AS SMALLDATETIME)) AND V.FecCompra<=ISNULL(@pmFecComFin,CAST('20781230' AS SMALLDATETIME)))
ORDER BY  IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,DeclaracImp,TipoIngreso,V.IdOrgTra AS CdOrgTra,NomOrgTrans,GPSoperador,GPSUsuario,GPSClave,GPSIdOper
--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 LEFT JOIN OrgTransito AS OG ON V.IdOrgTra=OG.IdOrgTra
 WHERE  IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposNivs] @pmIdNiv VARCHAR(4),@pmNivelServicio VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposNivs SET NivelServicio=@pmNivelServicio,Inactivo=@pmInactivo
 WHERE IdNiv=@pmIdNiv
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,IdConcFE,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,BaseImpu,Tarifa_Iva,VrIva,Tarifa_Ret,VrRetFte,Tarifa_Ica,VrRetIca
,CodTarifIva,CodTarifRet,CodTarifIca,TipDocFac,NumDocFac,CdCiaFac
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,IndFactura, 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposNivs] @pmIdNiv VARCHAR(4),@pmNivelServicio VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposNivs (IdNiv,NivelServicio,Inactivo)
 VALUES (@pmIdNiv,@pmNivelServicio,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,BaseImpu,Tarifa_Iva,VrIva,Tarifa_Ret,VrRetFte,Tarifa_Ica,VrRetIca,CodTarifIva,CodTarifRet,CodTarifIca,TipDocFac,NumDocFac,CdCiaFac
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryVehiculosPol]
 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
,TP.VehPropio,V.TipoAfil,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,V.FecRetiro,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,TarjProp,FecTProp,VigTProp
,CdLugTp,LT.Localidad AS LugarTarjProp,Ulttramite,RespCivil,FecRCivil,VigRCivil,RegNalCarga,FecRegNal,VigRegNal,V.RevTecMec,V.FecTecMec,V.VigTecMec,CertGases,FecCertGas,VigCertGas,TarjOper,FecTarjOper,VigTarjOper
,KmInicial,KmActual,Km2Actual,Regtradora,CentInicial,CentFinal,V.VrLmtCred,V.VrSaldoAct,V.Descripcion,V.Observacion AS Observ,V.CdCenSer,CentroServ,V.CdLocal,LU.Localidad AS CiuUbicacion
,LU.IdDep AS CodDpto,Departamento,V.Ubicacion,V.PathFoto,FecPriServ,FecUltServ,FecUltAcc,TieneAcc,FecPagImp,ValorCupo,ObligaTProd,GarantiaAcc,DocCompleta,CertMovilizacion,FecCertMovil,VigCertMovil
,CdRutaHab,DeclaracImp,TipoIngreso,V.IdOrgTra AS CdOrgTra,NomOrgTrans,GPSoperador,GPSUsuario,GPSClave,CantFiltros,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
--información de polizas
,VP.IdClase AS CodClase,ClasePoliza,Numero,FecEmision,VP.FecVigencia AS FecVencePoliza,NitCompania,TCPX.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 OrgTransito AS OG ON V.IdOrgTra=OG.IdOrgTra
 LEFT JOIN VehPolizas AS VP ON V.IdVehiculo=VP.IdVehiculo LEFT JOIN ClasePol AS CLP ON VP.IdClase=CLP.IdClase
 LEFT JOIN Terceros AS TCPX ON VP.NitCompania=TCPX.IdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,N.IdConcFE, 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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.IdConcFE, 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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,TPR.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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposNivs] @pmIdNiv VARCHAR(4)
 AS 
 DELETE FROM TiposNivs
 WHERE IdNiv=@pmIdNiv
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,IndFactura,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,VF.BaseImpu,VF.Tarifa_Iva,VF.VrIva,VF.Tarifa_Ret,VF.VrRetFte,VF.Tarifa_Ica,VF.VrRetIca,VF.TipDocFac,VF.NumDocFac,VF.CdCiaFac
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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,TPR.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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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,TPR.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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryFacturasIca] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null
AS 
SELECT F.TipDoc,F.Factura,F.IdCia,Compania,F.Fecha,F.IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia,A.Agencia AS NomAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrIvaObsequio,VrRetencion,VrReteICA,VrReteCREE,VrReteIVA,VrImpCons
,VrOtros,VrCargos,VrOtrDcto,VrFletes,F.VrNeto,BaseImp,BaseRet,BaseIca,BaseRiv,BaseIvaObsq,F.TarifaIva,F.TarifaRet,F.TarifaIca,F.TarifaRiv,F.TarifaRtc,F.CodTarIca
,F.IdLocEnv AS IdLocal,LE.Localidad,LE.IdDep AS CdDpto,DE.Departamento,F.IdVend,VN.RazonSocial AS Vendedor,F.TarifaCom,F.Modalidad,F.IdConcepto,Concepto,F.NitContac,F.NomContac,F.TipPed,F.Pedido,F.IdCiaPed,F.TipRem,F.Remision,F.IdCiaRem,F.TipCom,F.Comprobante,F.IdCiaCom
,F.Anulado,'' AS TipDev,F.NumDev,F.IdCiaCrea AS IdCiaDev,F.FecDev,F.Observacion,TipoDoc
--datos terceros
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.IdLocal AS TercCdCiudad,L.Localidad AS TercNomCiudad,L.IdDep AS TercCdDpto,DP.Departamento AS TercDpto,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CL.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CL.IdGrupo AS CdGrupoCli,GrupoClie,LEV.IdDep AS ClCdDptoEnv,DEV.Departamento AS ClDptoEnvio,CL.IdLocEnv AS ClIdCiuEnv,LEV.Localidad AS ClCiuEnvio,CL.ExcIva,CL.Autoret
,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto 
FROM Trn_Facturas AS F INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia INNER JOIN Conceptos AS C ON F.IdConcepto=C.IdConcepto
INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero INNER JOIN Terceros AS VN ON F.IdVend=VN.IdTercero
INNER JOIN TercCliente AS CL ON F.IdCliente=CL.IdClie INNER JOIN Agencias AS A ON F.IdAgencia=A.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 CL.IdSzona=SZ.IdSzona INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona
INNER JOIN GruposCli AS GC ON CL.IdGrupo=GC.IdGrupo INNER JOIN Sys_TiposDoc AS TD ON F.TipDoc=TD.IdDoc
INNER JOIN Localidades AS LEV ON CL.IdLocEnv=LEV.IdLocal INNER JOIN Departamentos AS DEV ON LEV.IdDep=DEV.IdDep
INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep
INNER JOIN Localidades AS LE ON F.IdLocEnv=LE.IdLocal INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep
WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (F.IdCia=@pmIdCia OR @pmIdCia IS NULL)
AND (F.IdCliente=@pmIdCliente OR @pmIdCliente IS NULL)
UNION ALL
SELECT D.TipDev,D.Devolucion,D.IdCia,Compania,D.Fecha,D.IdCliente,T.RazonSocial,D.IdAgencia,A.Agencia,D.VrSubTotal,D.VrDescuento,D.VrImpuesto,D.VrIvaObsequio,D.VrRetencion,D.VrReteICA,D.VrReteCREE,D.VrReteIVA,D.VrImpCons
,D.VrOtros,D.VrCargos,D.VrOtrDcto,D.VrFletes,D.VrNeto,D.BaseImp,D.BaseRet,D.BaseRet,D.VrImpuesto,D.BaseIvaObsq,F.TarifaIva,F.TarifaRet,F.TarifaIca,F.TarifaRiv,D.TarifaRtc,F.CodTarIca
,D.IdLocEnv,LE.Localidad,LE.IdDep,DE.Departamento,D.IdVend,VN.RazonSocial,D.TarifaCom,D.Modalidad,D.IdConcepto,Concepto,'','','PED',D.Pedido,D.IdCiaPed,'REM',D.Remision,D.IdCiaRem,D.TipCom,D.Comprobante,D.IdCiaCom
,0,D.TipDoc,D.Factura,D.IdCiaDoc,D.FecDoc,D.Observacion,TipoDoc
--datos terceros
,T.TipoId,T.Dv,T.IdLocal,L.Localidad,L.IdDep,DP.Departamento,T.IdSector,SectorEco,T.IdRegimen,Regimen,T.TipEnte
,CL.IdSzona,Subzona,SZ.IdZona,Zona,CL.IdGrupo,GrupoClie,LEV.IdDep,DEV.Departamento,CL.IdLocEnv,LEV.Localidad,CL.ExcIva,CL.Autoret
,A.IdLocal,LA.Localidad,LA.IdDep,DA.Departamento
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 Companias AS CN ON D.IdCia=CN.IdCia INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto
INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN Terceros AS VN ON D.IdVend=VN.IdTercero
INNER JOIN TercCliente AS CL ON D.IdCliente=CL.IdClie INNER JOIN Agencias AS A ON D.IdAgencia=A.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 CL.IdSzona=SZ.IdSzona INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona
INNER JOIN GruposCli AS GC ON CL.IdGrupo=GC.IdGrupo INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
INNER JOIN Localidades AS LEV ON CL.IdLocEnv=LEV.IdLocal INNER JOIN Departamentos AS DEV ON LEV.IdDep=DEV.IdDep
INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep
INNER JOIN Localidades AS LE ON D.IdLocEnv=LE.IdLocal INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep
WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (D.IdCia=@pmIdCia OR @pmIdCia IS NULL)
AND (D.IdCliente=@pmIdCliente OR @pmIdCliente IS NULL)
UNION ALL
SELECT F.TipDoc,F.Factura,F.IdCia,Compania,F.Fecha,F.NitCliente,T.RazonSocial,F.CdAgencia,A.Agencia,VrSubTotal,VrDescuento,VrImpuesto,VrIvaObsequio,VrRetencion,VrReteICA,VrReteCREE,VrReteIVA,VrImpCons
,0,VrCargos,VrOtrDcto,VrFletes,VrNeto,BaseImp,BaseRet,BaseIca,BaseRiv,BaseIvaObsq,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,F.CodTarIca
,F.IdLocal,LE.Localidad,LE.IdDep,DE.Departamento,F.IdVend,VN.RazonSocial,F.TarifaCom,F.Modalidad,F.IdConcepto,Concepto,F.nClieCon,CT.Nombre,TipPed,Pedido,IdCiaPed,TipRem,Remision,IdCiaRem,TipCom,Comprobante,IdCiaCom
,F.Anulado,'',F.NumDev,F.IdCiaCrea,F.FecDev,F.Observacion,TipoDoc
--datos de terceros
,T.TipoId,T.Dv,T.IdLocal,L.Localidad,L.IdDep,DP.Departamento,T.IdSector,SectorEco,T.IdRegimen,Regimen,T.TipEnte
,CL.IdSzona,Subzona,SZ.IdZona,Zona,CL.IdGrupo,GrupoClie,LEV.IdDep,DEV.Departamento,CL.IdLocEnv,LEV.Localidad,CL.ExcIva,CL.Autoret
,A.IdLocal,LA.Localidad,LA.IdDep,DA.Departamento
FROM Trn_Factcon AS F INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia INNER JOIN Conceptos AS C ON F.IdConcepto=C.IdConcepto
INNER JOIN Terceros AS VN ON F.IdVend=VN.IdTercero INNER JOIN Sys_TiposDoc AS TD ON F.TipDoc=TD.IdDoc
INNER JOIN Localidades AS LE ON F.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 TercCliente AS CL ON F.NitCliente=CL.IdClie
LEFT JOIN Agencias AS A ON F.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 Subzonas AS SZ ON CL.IdSzona=SZ.IdSzona LEFT JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona
LEFT JOIN GruposCli AS GC ON CL.IdGrupo=GC.IdGrupo 
LEFT JOIN Localidades AS LEV ON CL.IdLocEnv=LEV.IdLocal LEFT JOIN Departamentos AS DEV ON LEV.IdDep=DEV.IdDep
LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep
LEFT JOIN ClieContado AS CT ON F.nClieCon=CT.IdCliente
WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (F.IdCia=@pmIdCia OR @pmIdCia IS NULL)
AND (F.NitCliente=@pmIdCliente OR @pmIdCliente IS NULL)
UNION ALL
SELECT D.TipDev,D.Devolucion,D.IdCia,Compania,D.Fecha,D.NitCliente,T.RazonSocial,D.CdAgencia,A.Agencia,D.VrSubTotal,D.VrDescuento,D.VrImpuesto,D.VrIvaObsequio,D.VrRetencion,D.VrReteICA,D.VrReteCREE,D.VrReteIVA,D.VrImpCons
,0,D.VrCargos,D.VrOtrDcto,D.VrFletes,D.VrNeto,D.BaseImp,D.BaseRet,D.BaseRet,D.VrImpuesto,D.BaseIvaObsq,F.TarifaIva,F.TarifaRet,F.TarifaIca,F.TarifaRiv,D.TarifaRtc,F.CodTarIca
,D.IdLocal,LE.Localidad,LE.IdDep,DE.Departamento,D.IdVend,VN.RazonSocial,D.TarifaCom,D.Modalidad,D.IdConcepto,Concepto,D.nClieCon,CT.Nombre,'PED',D.Pedido,D.IdCiaPed,'REM',D.Remision,D.IdCiaRem,D.TipCom,D.Comprobante,D.IdCiaCom
,0,D.TipDoc,D.Factura,D.IdCiaDoc,D.FecDoc,D.Observacion,TipoDoc
--datos de terceros
,T.TipoId,T.Dv,T.IdLocal,L.Localidad,L.IdDep,DP.Departamento,T.IdSector,SectorEco,T.IdRegimen,Regimen,T.TipEnte
,CL.IdSzona,Subzona,SZ.IdZona,Zona,CL.IdGrupo,GrupoClie,LEV.IdDep,DEV.Departamento,CL.IdLocEnv,LEV.Localidad,CL.ExcIva,CL.Autoret
,A.IdLocal,LA.Localidad,LA.IdDep,DA.Departamento
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 Companias AS CN ON D.IdCia=CN.IdCia INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto
INNER JOIN Terceros AS VN ON D.IdVend=VN.IdTercero INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
INNER JOIN Localidades AS LE ON D.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 TercCliente AS CL ON D.NitCliente=CL.IdClie
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 Subzonas AS SZ ON CL.IdSzona=SZ.IdSzona LEFT JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona
LEFT JOIN GruposCli AS GC ON CL.IdGrupo=GC.IdGrupo 
LEFT JOIN Localidades AS LEV ON CL.IdLocEnv=LEV.IdLocal LEFT JOIN Departamentos AS DEV ON LEV.IdDep=DEV.IdDep
LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep
LEFT JOIN ClieContado AS CT ON D.nClieCon=CT.IdCliente
WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (D.IdCia=@pmIdCia OR @pmIdCia IS NULL)
AND (D.NitCliente=@pmIdCliente OR @pmIdCliente IS NULL)
UNION ALL
SELECT N.TipDoc,N.NumNota,N.IdCia,Compania,N.Fecha,N.IdCliente,T.RazonSocial,N.IdAgencia,A.Agencia,VrSubTotal,0,VrImpuesto,0,VrRetencion,VrReteICA,VrReteCREE,VrReteIVA,0
,VrOtros,VrSancion,0,0,N.VrNeto,BaseImp,BaseRet,BaseIca,BaseRiv,0,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,N.CodTarIca
,N.IdLocal,LE.Localidad,LE.IdDep,DE.Departamento,N.IdVend,VN.RazonSocial,N.TarifaCom,N.Modalidad,N.IdConcepto,Concepto,N.CdConductor,N.Referencia,TipDcm,Documento,IdCiaDcm,'RMT',nRemesa,CdCiaRem,TipCom,Comprobante,IdCiaCom
,0,TipDcm,Documento,IdCiaDcm,FecDcm,N.Observacion,TipoDoc
--datos de terceros
,T.TipoId,T.Dv,T.IdLocal,L.Localidad,L.IdDep,DP.Departamento,T.IdSector,SectorEco,T.IdRegimen,Regimen,T.TipEnte
,CL.IdSzona,Subzona,SZ.IdZona,Zona,CL.IdGrupo,GrupoClie,LEV.IdDep,DEV.Departamento,CL.IdLocEnv,LEV.Localidad,CL.ExcIva,CL.Autoret
,A.IdLocal,LA.Localidad,LA.IdDep,DA.Departamento
FROM Trn_Notas AS N INNER JOIN Companias AS CN ON N.IdCia=CN.IdCia INNER JOIN Conceptos AS C ON N.IdConcepto=C.IdConcepto
INNER JOIN Terceros AS T ON N.IdCliente=T.IdTercero INNER JOIN Terceros AS VN ON N.IdVend=VN.IdTercero
INNER JOIN TercCliente AS CL ON N.IdCliente=CL.IdClie INNER JOIN Agencias AS A ON N.IdAgencia=A.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 CL.IdSzona=SZ.IdSzona INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona
INNER JOIN GruposCli AS GC ON CL.IdGrupo=GC.IdGrupo INNER JOIN Sys_TiposDoc AS TD ON N.TipDoc=TD.IdDoc
INNER JOIN Localidades AS LEV ON CL.IdLocEnv=LEV.IdLocal INNER JOIN Departamentos AS DEV ON LEV.IdDep=DEV.IdDep
INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep
INNER JOIN Localidades AS LE ON N.IdLocal=LE.IdLocal INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep
WHERE N.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (N.IdCia=@pmIdCia OR @pmIdCia IS NULL)
AND (N.IdCliente=@pmIdCliente OR @pmIdCliente IS NULL)
UNION ALL
SELECT R.TipDoc,R.Recibo,R.IdCia,Compania,R.Fecha,R.IdCliente,T.RazonSocial,R.IdAgencia,A.Agencia,VrSubTotal,VrDescuento,0,0,VrRetencion,VrReteICA,VrReteCREE,VrReteIVA,0
,VrOtros,VrPagosMas,VrOtrDcto,VrAhorro,R.VrNeto,0,BaseRet,BaseIca,BaseRiv,0,0,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,R.CodTarIca
,R.IdLocal,LE.Localidad,LE.IdDep,DE.Departamento,R.IdVend,VN.RazonSocial,R.TarifaCom,R.Modalidad,R.IdConcepto,Concepto,R.CdConductor,R.Referencia,R.TipDcm,R.Documento,R.IdCiaDcm,'',0,'',R.TipCom,R.Comprobante,R.IdCiaCom
,R.Anulado,'',R.NumDev,R.IdCiaCrea,R.FecDev,R.Observacion,TipoDoc
--datos de terceros
,T.TipoId,T.Dv,T.IdLocal,L.Localidad,L.IdDep,DP.Departamento,T.IdSector,SectorEco,T.IdRegimen,Regimen,T.TipEnte
,CL.IdSzona,Subzona,SZ.IdZona,Zona,CL.IdGrupo,GrupoClie,LEV.IdDep,DEV.Departamento,CL.IdLocEnv,LEV.Localidad,CL.ExcIva,CL.Autoret
,A.IdLocal,LA.Localidad,LA.IdDep,DA.Departamento
FROM Trn_Recibos AS R INNER JOIN Companias AS CN ON R.IdCia=CN.IdCia INNER JOIN Conceptos AS C ON R.IdConcepto=C.IdConcepto
INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero INNER JOIN Terceros AS VN ON R.IdVend=VN.IdTercero
INNER JOIN TercCliente AS CL ON R.IdCliente=CL.IdClie INNER JOIN Agencias AS A ON R.IdAgencia=A.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 CL.IdSzona=SZ.IdSzona INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona
INNER JOIN GruposCli AS GC ON CL.IdGrupo=GC.IdGrupo INNER JOIN Sys_TiposDoc AS TD ON R.TipDoc=TD.IdDoc
INNER JOIN Localidades AS LEV ON CL.IdLocEnv=LEV.IdLocal INNER JOIN Departamentos AS DEV ON LEV.IdDep=DEV.IdDep
INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep
INNER JOIN Localidades AS LE ON R.IdLocal=LE.IdLocal INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep
WHERE R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (R.IdCia=@pmIdCia OR @pmIdCia IS NULL)
AND (R.IdCliente=@pmIdCliente OR @pmIdCliente IS NULL)
UNION ALL
SELECT D.TipDev,D.Devolucion,D.IdCia,Compania,D.Fecha,D.IdCliente,T.RazonSocial,D.IdAgencia,A.Agencia,D.VrSubTotal,D.VrDescuento,0,0,D.VrRetencion,D.VrReteICA,D.VrReteCREE,D.VrReteIVA,0
,D.VrOtros,D.VrPagosMas,D.VrOtrDcto,D.VrAhorro,D.VrNeto,0,D.BaseRet,D.BaseIca,D.BaseRiv,0,0,R.TarifaRet,R.TarifaIca,R.TarifaRiv,D.TarifaRtc,R.CodTarIca
,D.IdLocal,LE.Localidad,LE.IdDep,DE.Departamento,D.IdVend,VN.RazonSocial,D.TarifaCom,D.Modalidad,D.IdConcepto,Concepto,D.CdConductor,D.Referencia,D.TipDcm,D.Documento,D.IdCiaDcm,'',0,'',D.TipCom,D.Comprobante,D.IdCiaCom
,0,D.TipDoc,D.Recibo,D.IdCiaDoc,D.FecDoc,D.Observacion,TipoDoc
--datos de terceros
,T.TipoId,T.Dv,T.IdLocal,L.Localidad,L.IdDep,DP.Departamento,T.IdSector,SectorEco,T.IdRegimen,Regimen,T.TipEnte
,CL.IdSzona,Subzona,SZ.IdZona,Zona,CL.IdGrupo,GrupoClie,LEV.IdDep,DEV.Departamento,CL.IdLocEnv,LEV.Localidad,CL.ExcIva,CL.Autoret
,A.IdLocal,LA.Localidad,LA.IdDep,DA.Departamento
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
INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto
INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN Terceros AS VN ON D.IdVend=VN.IdTercero
INNER JOIN TercCliente AS CL ON D.IdCliente=CL.IdClie INNER JOIN Agencias AS A ON D.IdAgencia=A.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 CL.IdSzona=SZ.IdSzona INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona
INNER JOIN GruposCli AS GC ON CL.IdGrupo=GC.IdGrupo INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
INNER JOIN Localidades AS LEV ON CL.IdLocEnv=LEV.IdLocal INNER JOIN Departamentos AS DEV ON LEV.IdDep=DEV.IdDep
INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep
INNER JOIN Localidades AS LE ON D.IdLocal=LE.IdLocal INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep
WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (D.IdCia=@pmIdCia OR @pmIdCia IS NULL)
AND (D.IdCliente=@pmIdCliente OR @pmIdCliente IS NULL)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenServFac] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=NULL
AS
SELECT O.TipDoc AS IdTipDoc,TipoDoc,O.NumOrden AS Num_Orden,O.IdCia AS CdCia,Compania,O.Fecha,T.TipoId AS ClieTipoId,O.IdCliente,T.Dv AS ClieDv,T.RazonSocial AS NomCliente
,O.IdVehiculo AS PlacaVeh,NumVeh,O.TipoAfiVehic,O.Modalidad,TipoOrden,O.VrTotal AS ValorTotal,O.VrCosto AS TotalTercero,O.Cantidad AS CantTotal,O.VrAnticipo
,D.Item,D.IdMercancia AS CdMercancia,D.Descripcion,D.Cantidad,D.UndMed,UM.Unidad,D.VrUnitario,D.TarifaPago,D.DocCliente
,O.IdEstacion AS CdCampo,EstServicio AS NomCampo,O.CdRuta,Ruta,O.IdOrigen AS CdOrigen,LO.Localidad AS NomOrigen,O.IdDestino AS CdDestino,LD.Localidad AS NomDestino,O.IdAgencia AS CdAgencia,Agencia
,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,NomEstacion,TipoResiduo,O.RemCliente,NomRecibido,O.FecDespacho,O.FecRecibo,O.FecIngCertif,O.FecCertificado,O.NumRemesa,O.CdCiaRem,kmtInicial,kmtFinal,Cantkms,NumAprob,IdCiaApr,FecAprob,UsuAprob,ObservAprob
,O.Anulado,O.FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,ED.Estado,O.TimeSys AS FechaCrea,O.FecUpdate AS FechaModif,O.IdCiaCrea AS CdCiaCrea,O.IdUsuario AS CdUsuario,Usuario
--cumplido
,EstCumplido,CM.Cumplido AS NumCumplido,CM.IdCia AS CiaCump,CM.Fecha AS FechaCump,CM.DescServicio AS CumpServicio,CM.Observacion AS ObservCump
,CM.IdMercancia AS CdMercanciaCump,CM.Descripcion AS DescripCump,CM.Cantidad AS CantCump,CM.UndMed AS UndMedCump,UMC.Unidad AS UnidadCump,CM.VrUnitario AS TarifCump
,CM.TarifaPago AS TarifPagoCump
--liquidacion
,O.EstOrden,O.TipLiq,O.NumLiquida,O.CdCiaLiq,L.Fecha AS FecLiquida,L.VrTotal AS LiqTotal,L.VrAnticipo AS LiqAnticipo,L.VrIva AS LiqIva,L.VrRetencion AS LiqRetencion
,L.VrReteIca,L.VrReteCREE,L.VrReteIva,L.VrOtros,L.VrDescuento,L.VrNeto,L.Cantidad AS LiqCantidad,L.TarifaPago AS LiqTarifaPago,L.TarifaImp,L.TarifaRet,L.TarifaIca,L.TarifaRiv
,L.Referencia,L.VrGastos
--Factura
,O.EstFactura,O.TipFact,O.NumFactura,O.CdCiaFact,FactFecha,FactCant,FactTarifClie,FactTotal,FactIva,FactTarifIVA,FactCosto,FactIdCli,TCF.RazonSocial AS FactNomCliente
--Inf vehiculo
,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,LC.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.TelMovil AS TelCelular,T.e_mail AS TercEmail
--Vehiculo
,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdColor AS CdColor,NomColor,Modelo,NumMotor,SerieChasis,NumSerie,NitEmpresa,NE.RazonSocial AS VehNomEmpresa
,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,V.Descripcion AS VehDescripcion
FROM Trn_TraOrdenServ AS O INNER JOIN Trn_TraOrdenDet AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.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 Terceros AS NP ON O.IdPoseedor=NP.IdTercero
INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion
INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed INNER JOIN Localidades AS LC ON T.IdLocal=LC.IdLocal
INNER JOIN Vehiculos AS V ON O.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 TiposCol AS CL ON V.IdColor=CL.IdColor 
LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia
LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
LEFT JOIN Trn_TraOrdenLiq AS L ON O.TipLiq=L.TipDoc AND O.NumLiquida=L.Liquidacion AND O.CdCiaLiq=L.IdCia
LEFT JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal LEFT JOIN Rutas AS RT ON O.CdRuta=RT.IdRuta
--Subconsulta cumplido
LEFT JOIN (SELECT C.Cumplido,C.IdCia,C.Fecha,C.TipOds,C.NumOrden,C.IdCiaOds,C.FecDespacho,C.FecRecibo,C.FecIngCertif,C.FecCertificado,C.DescServicio
,C.RemCliente,C.Funcionario,C.Observacion,D.IdMercancia,D.Descripcion,D.Item,D.Cantidad,D.UndMed,D.VrUnitario,D.TarifaPago,D.DocCliente
FROM Trn_TraOrdenCum AS C INNER JOIN Trn_TraOrdenCumDet AS D ON C.TipDoc=D.TipDoc AND C.Cumplido=D.Cumplido AND C.IdCia=D.IdCia
WHERE C.Anulado=0) AS CM ON O.TipDoc=CM.TipOds AND O.NumOrden=CM.NumOrden AND O.IdCia=CM.IdCiaOds AND D.Item=CM.Item
LEFT JOIN Sys_Um AS UMC ON CM.UndMed=UMC.UndMed
--Subconsulta factura
LEFT JOIN (SELECT F.TipDoc,F.Factura,F.IdCia,D.TipRem,D.Remesa,D.IdCiaRem,F.IdCliente AS FactIdCli,F.Fecha AS FactFecha,SUM(D.Cantidad) AS FactCant,SUM(D.VrUnitario*D.Cantidad) AS FactTotal
,SUM(D.VrCosto*D.Cantidad) AS FactCosto,SUM(D.VrImpuesto) AS FactIva,MAX(D.TarifaIva) AS FactTarifIVA,MAX(D.VrUnitario) AS FactTarifClie,MAX(D.VrCosto) AS FactTarifTerc
FROM Trn_Facturas AS F INNER JOIN Trn_TraFacRemesas AS D ON F.TipDoc=D.TipDoc AND F.Factura=D.Factura AND F.IdCia=D.IdCia
WHERE F.Anulado=0 GROUP BY F.TipDoc,F.Factura,F.IdCia,D.TipRem,D.Remesa,D.IdCiaRem,F.IdCliente,F.Fecha) AS FS ON O.TipFact=FS.TipDoc AND O.NumFactura=FS.Factura AND O.CdCiaFact=FS.IdCia
AND O.TipDoc=FS.TipRem AND O.NumOrden=FS.Remesa AND O.IdCia=FS.IdCiaRem
LEFT JOIN Terceros AS TCF ON FS.FactIdCli=TCF.IdTercero
WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.TipDoc LIKE ISNULL(@pmTipDoc,'%') AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND O.IdCliente LIKE ISNULL(@pmIdCliente,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrIvaIngProd,VrNeto,VrAplicado,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,BaseIvaIgp,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,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,F.CdRuta,Ruta,TipPed,Pedido,IdCiaPed,TipRem,Remision,IdCiaRem,TipCot,Cotizacion,IdCiaCot,FecPedido,KmtVehic,VrImpCons,VrReteCREE,F.TarifaArf,F.VrAutRetFte,F.TarifaAri,F.VrAutRetIca
,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,SZ.Subzona,SZ.IdZona AS CdZona,Z.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
,A.CdFntePago AS CdFuente,FuentePago,TipoZona,A.IdSzona AS AgeCdSubzona,SZA.Subzona AS AgeSubzona,SZA.IdZona AS AgeCdZona,ZA.Zona AS AgeZona
 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 LEFT JOIN FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente
 LEFT JOIN Subzonas AS SZA ON A.IdSzona=SZA.IdSzona LEFT JOIN Zonas AS ZA ON SZA.IdZona=ZA.IdZona 
 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.VrIvaIngProd,D.VrNeto AS TotalNeto,0,D.Cantidad AS CantDev,D.CantPuntos AS CantPuntDev,0
,D.BaseImp AS BaseIVA,D.BaseRet AS BaseRetFte,D.BaseIvaIgp,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,D.TarifaRtc,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,F.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.VrImpCons,D.VrReteCREE,D.TarifaArf,D.VrAutRetFte,D.TarifaAri,D.VrAutRetIca,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,SZ.Subzona,SZ.IdZona AS CdZona,Z.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
,A.CdFntePago AS CdFuente,FuentePago,TipoZona,A.IdSzona,SZA.Subzona,SZA.IdZona,ZA.Zona
 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 FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente 
 LEFT JOIN Terceros AS TC ON F.CdConductor=TC.IdTercero LEFT JOIN Rutas AS R ON F.CdRuta=R.IdRuta
 LEFT JOIN Subzonas AS SZA ON A.IdSzona=SZA.IdSzona LEFT JOIN Zonas AS ZA ON SZA.IdZona=ZA.IdZona 
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpCon)
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,VrImpCons
 FROM Trn_Facturas
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdCajero,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraRemesaOdp] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
AS 
SELECT R.TipDoc,R.NumOrden,R.IdCia AS CdCia,Compania,R.Fecha AS FecRemesa,D.Item,D.IdMercancia AS CdMercancia,D.DescripMcias,D.Cantidad,D.PesoNeto,D.UndMed,UMP.Unidad AS UmdPeso,D.Volumen,R.IdCliente AS NitCliente,T.RazonSocial AS NomCliente
,D.NitRemite,D.Remitente,D.DirOrigen,D.IdOrigen,LO.Localidad AS CiuOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,D.NitDestntario,D.Destinatario,D.DirDestino,D.IdDestino,LD.Localidad AS CiuDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,D.TarifClie,D.TarifPago,D.VrDeclarado,D.VrSeguro,D.TarifSeguro,D.UndTarifa,D.UndTarifPago,Remision,D.DocCliente,D.Referencia1,D.Referencia2,D.Referencia3,D.IdUnd AS CdUnd,UM.Unidad AS UndMedida
,R.IdAgencia AS CdAgencia,A.Agencia,R.IdVehiculo,R.IdConductor,CDT.RazonSocial AS NomConductor,R.nRemolque,R.TipoAfiVehic,R.Modalidad,NumPedido,IdCiaPed,R.TipDcm,R.NumDocmto,R.IdCiaDcm,R.EstCumplido,D.Cumplido AS NumCumplido,D.IdCiaCump,R.EstFactura
--datos de factura
,RF.TipoFact,ISNULL(RF.NumFactura,0) AS NumFactura,RF.IdCiaFact,F.Fecha AS FacFecha,RF.FacCantidad,RF.FacValorTotal,F.IdCliente AS FacIdCliente,NCL.RazonSocial AS FacNomCliente,F.IdAgencia AS FacIdAgencia,AF.Agencia AS FacAgencia,F.IdVend AS FacIdVend,VN.RazonSocial AS Vendedor
--Manifiesto y ordenes
,MC.TipDoc AS MucTipo,MC.Manifiesto,MC.IdCia AS MucIdCia,MC.Fecha AS MucFecha,MC.IdRuta,Ruta,MC.IdPoseedor,NP.RazonSocial AS NomPoseedor,MC.Cumplido AS MucCumplido,MC.IdCiaCump AS MucIdCiaCump,MC.EstCumplido AS MucEstCumplido,MC.EstOrden AS MucEstOrden
,OP.OdpTipo,OP.OrdPago,OP.OdpIdCia,OP.OdpVrFlete,OP.OdpDescuento,OP.OdpRetencion,OP.OdpReteIca,OP.OdpVrCREE,OP.OdpAnticipos,OP.OdpFaltantes,OP.OdpSeguros,OP.OdpFondos,OP.OdpAportes,OP.OdpOtrosDcto,OP.OdpImpuestos
,OP.OdpEstampillas,OP.OdpOtrosPagos,OP.OdpVrNeto,OP.OdpTarifaUnd,OP.OdpPesoTotal,OP.TipEgr,OP.Egreso,OP.IdCiaEgr,EG.TotEgresos
FROM Trn_TraRemesa AS R INNER JOIN Trn_TraRemMcias AS D ON R.TipDoc=D.TipDoc AND R.NumOrden=D.NumOrden AND R.IdCia=D.IdCia
INNER JOIN Companias AS CN ON R.IdCia=CN.IdCia INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero
INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo INNER JOIN Terceros AS CDT ON R.IdConductor=CDT.IdTercero 
INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd  
LEFT JOIN Agencias AS A ON R.IdAgencia=A.IdAgencia LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed
--Remeasa/Facturas
LEFT JOIN (SELECT TipRem,Remesa,IdCiaRem,ItemRem
,MAX(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN 0 ELSE Factura END) AS NumFactura
,MAX(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Factura ELSE 0 END) AS NumDevFact
,MAX(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN '0' ELSE TipDoc END) AS TipoFact
,MAX(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN '00' ELSE IdCia END) AS IdCiaFact
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Cantidad*-1 ELSE Cantidad END) AS FacCantidad
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN (Cantidad*VrUnitario)*-1 ELSE Cantidad*VrUnitario END) AS FacValorTotal
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN (CantPago*VrCosto)*-1 ELSE CantPago*VrCosto END) AS FacCostoTotal
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Unidades*-1 ELSE Unidades END) AS FacUnidades
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN PesoNeto*-1 ELSE PesoNeto END) AS FacPesoNeto
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Volumen*-1 ELSE Volumen END) AS FacVolumen
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN VrFaltante*-1 ELSE VrFaltante END) AS FacFaltantes
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN 0 ELSE TarifaIva END) AS FacTarifIva
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN 0 ELSE VrImpuesto END) AS FacImpuesto
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN 0 ELSE TarifaRet END) AS FacTarifRet
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN 0 ELSE VrRetencion END) AS FacRetencion
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN 0 ELSE TarifaIca END) AS FacTarifIca
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN 0 ELSE VrReteIca END) AS FacReteIca
FROM Trn_TraFacRemesas WHERE TipoReg=0 GROUP BY TipRem,Remesa,IdCiaRem,ItemRem) AS RF ON D.TipDoc=RF.TipRem AND D.NumOrden=RF.Remesa AND D.IdCia=RF.IdCiaRem AND D.Item=RF.ItemRem
LEFT JOIN (SELECT TipDoc,Factura,IdCia,Fecha,IdCliente,IdAgencia,IdVend,IdPlazo,BaseImp,BaseRet FROM Trn_Facturas WHERE Modalidad='TRANSPORTE' AND Anulado=0) AS F ON RF.TipoFact=F.TipDoc
AND RF.NumFactura=F.Factura AND RF.IdCiaFact=F.IdCia
--remesa/manifiesto
LEFT JOIN (SELECT MR.TipRem,MR.Remesa,MR.IdCiaRem,MR.ItemRem,MR.TipDoc,MR.Manifiesto,MR.IdCia,MR.TarifPago,MR.RemMintrans,M.Fecha,M.IdRuta,M.IdVehiculo,M.nRemolque,M.TipoAfiVehic,M.IdPoseedor,M.IdConductor
,M.VrFletes,M.VrRetencion,M.VrReteIca,M.VrAnticipo,M.VrAntAdic,M.VrCargos,M.VrDctos,M.VrFleteNeto,M.PesoTotal,M.TarifaRet,M.TarifaIca,M.Cumplido,M.IdCiaCump,M.FechaCump,M.EstCumplido,M.EstOrden
FROM Trn_TraManifRem AS MR INNER JOIN Trn_TraManifiesto AS M ON MR.TipDoc=M.TipDoc AND MR.Manifiesto=M.Manifiesto AND MR.IdCia=M.IdCia WHERE M.Anulado=0) AS MC ON D.TipDoc=MC.TipRem AND D.NumOrden=MC.Remesa AND D.IdCia=MC.IdCiaRem AND D.Item=MC.ItemRem
--Remesa/Manif/ordnenes
LEFT JOIN (SELECT OM.TipMuc,OM.Manifiesto,OM.IdCiaMuc,OM.TipDoc AS OdpTipo,OM.OrdPago,OM.IdCia AS OdpIdCia,OM.VrTotalFletes AS OdpVrFlete,OM.VrDescuento AS OdpDescuento,OM.VrRetencion AS OdpRetencion,OM.VrReteIca AS OdpReteIca
,OM.VrReteCREE AS OdpVrCREE,OM.VrAnticipos AS OdpAnticipos,OM.VrFaltantes AS OdpFaltantes,OM.VrSeguros AS OdpSeguros,OM.VrFondos AS OdpFondos,OM.VrAportes AS OdpAportes,OM.VrOtrosDctos AS OdpOtrosDcto,OM.VrImpuestos AS OdpImpuestos,OM.VrEstampilla AS OdpEstampillas
,OM.VrOtrosPagos AS OdpOtrosPagos,OM.VrNeto AS OdpVrNeto,OM.TarifaFlete AS OdpTarifaUnd,OM.PesoTotal AS OdpPesoTotal,OM.Unidades AS OdpCantidad,OM.Volumen AS OdpVolumen,OM.TipEgr,OM.Egreso,OM.IdCiaEgr
FROM Trn_TraOrdenManif AS OM INNER JOIN Trn_TraOrdenPago AS O ON OM.TipDoc=O.TipDoc AND OM.OrdPago=O.OrdPago AND OM.IdCia=O.IdCia WHERE O.Anulado=0) AS OP ON MC.TipDoc=OP.TipMuc AND MC.Manifiesto=OP.Manifiesto AND MC.IdCia=OP.IdCiaMuc
--Egresos
LEFT JOIN (SELECT EO.TipOdp,EO.OrdPago,EO.IdCiaOdp,SUM(VrAbonado) AS TotEgresos FROM Trn_TraEgrOrden AS EO INNER JOIN Trn_Comprobantes AS E ON EO.TipCom=E.TipCom AND EO.NumEgreso=E.Comprobante AND EO.IdCia=E.IdCia
WHERE E.Anulado=0 GROUP BY EO.TipOdp,EO.OrdPago,EO.IdCiaOdp) AS EG ON OP.OdpTipo=EG.TipOdp AND OP.OrdPago=EG.OrdPago AND OP.OdpIdCia=EG.IdCiaOdp
LEFT JOIN Terceros AS NCL ON F.IdCliente=NCL.IdTercero LEFT JOIN Agencias AS AF ON F.IdAgencia=AF.IdAgencia
LEFT JOIN Terceros AS VN ON F.IdVend=VN.IdTercero LEFT JOIN Terceros AS NP ON MC.IdPoseedor=NP.IdTercero
LEFT JOIN Rutas AS RT ON MC.IdRuta=RT.IdRuta
WHERE R.TipDoc='RMT' AND R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (@pmIdCia IS NULL OR R.IdCia=@pmIdCia)
AND (@pmIdCliente IS NULL OR R.IdCliente=@pmIdCliente) AND R.Anulado=0
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFacturasFmt] @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,C.Concepto AS DescConcepto,F.IdCliente,T.RazonSocial AS NomCliente
,F.IdAgencia AS IdAgenc,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,FechaVence,VrSubTotal,F.VrDescuento AS VrDcto,F.VrImpuesto AS VrIva,F.VrRetencion AS VrRetFte,F.VrReteICA AS VrRetIca,F.VrReteIVA,F.VrFletes
,F.VrOtros,F.VrCargos,F.VrOtrDcto,F.VrCostos,F.VrSobretasa,F.VrImpGlobal,F.VrFaltantes,F.VrAnticipos,F.VrNeto,VrAplicado,F.Cantidad AS CantTotal,F.PesoTotal,F.UnidTotal,F.VolTotal,CantFalt,F.VrCostoRem,F.VrCostoAfi,F.BaseImp,F.BaseRet,F.BaseIca,F.BaseRiv
,F.TarifaIva AS TarifIva,F.TarifaRet AS TarifRet,F.TarifaIca AS TarifIca,TarifaRiv,F.IdVend AS NitVend,VN.RazonSocial AS Vendedor,TarifaCom
,DirEnvio,F.IdLocEnv AS IdCiuEnvio,LE.Localidad AS CiudadEnvio,DiasEntraga,F.NitContac AS FacNitContac,F.NomContac AS FacNomContac,F.TelContac AS FacTelContac,F.emlContac AS FacEmailContac,CargoContac
,F.IdForma AS CdForma,F.DetallePago,MulPlazos,F.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,NitEmpTrans,EmpTrans,F.TipPed,F.Pedido,F.IdCiaPed,F.TipRem AS TipRemision,F.Remision AS NumRemision,TipCot,F.Cotizacion AS NumCotizacion,F.IdCiaCot AS CiaCotiza,F.FecPedido
,AutzaMora,AutzaCupo,F.Modalidad,KmtVehic,F.TipCom,TipoCom,F.Comprobante,F.IdCiaCom,F.Anulado AS FacAnulado,F.NumDev,F.FecDev,F.Observacion AS Observ,VrReteCREE,TarifaRtc,CodTarRtc,F.CodTarArf,F.TarifaArf,F.VrAutRetFte,F.CodTarAri,F.TarifaAri,F.VrAutRetIca,TipoFE,FechaFE,F.IdEstado AS CdEstado
,F.TimeSys AS Fec_Add,F.FecUpdate AS Fec_Update,F.IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario,Leyenda
--detales
,D.Item,D.TipoReg,D.FechaFact,D.TipRem AS TipRemesa,D.Remesa AS NumRemesa,D.IdCiaRem AS CdCiaRem,ItemRem,D.FecRemesa,D.Descripcion,D.Cantidad AS Cant,D.VrUnitario,D.VrCosto,D.UndTarifa,D.UndCosto,D.Unidades,D.PesoNeto,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,D.Volumen,D.UndVol,D.Cases,D.Cajas,D.Palets,D.CantPago
,D.TarifaIva AS DetTarifIva,D.VrImpuesto AS DetVrIva,TarifaDct,D.VrDescuento AS DetVrDcto,D.TarifaRet AS DetTarifRet,D.VrRetencion AS DetVrRetFte,D.TarifaIca AS DetTarifIca,D.VrReteIca AS DetVrRetIca,VrFaltante,D.Remision AS DetNumRemsion,D.DocCliente,D.Referencia1,D.Referencia2,D.Referencia3,D.CdMercancia,DescripMcia
,D.CdConcepto,CF.Concepto AS DetConcepto,D.CdCCosto,CCosto,D.CdSubCos AS CdSubCentro,SubCosto,NitTercero,NT.RazonSocial AS DetTercero,D.CdAgencia,D.pVehiculo AS PlacaVeh,D.TipoAfiVehic,D.IdOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,D.IdDestino,LD.Localidad AS CiudadDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,D.Anulado AS DetAnulado,TipDocRef,NumDocRef,IdCiaRef,FecDocRef,CantidadFalt,UnidadFalt,NumPedRem,CiaPedRem,VrDeclMcia,TarifaSeg,VrSeguroRem,NitAsegurad,R.CdRutaTarif,RT.Ruta
,RM.SedeRem,SDR.NomSede AS NomSedeRem,RM.SedeDest,SDD.NomSede AS NomSedeDest
--Información 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.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,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,ExcIva,LiqFletes,Autoret,VrCupo,VrSaldo
,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
,R.IdDestinatario AS Iddest,TDE.RazonSocial as NomDest,TDE.Direccion as DirDest,TRE.RazonSocial as NomRem,TRE.Direccion as DirRem,R.NumManif
,DM.cantimp,DM.Comentarios
,FE.EstadoFE,FE.Prefijo,FE.NumFace,FE.CUFE,FE.Resolucion,FE.RangoNum,FE.FecVigencia,FE.CUFE_QR AS QR,FE.FechaValidacion
 FROM Trn_Facturas AS F INNER JOIN Trn_TraFacRemesas AS D ON F.TipDoc=D.TipDoc  AND F.Factura=D.Factura AND F.IdCia=D.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 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 Terceros AS T ON F.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 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 Agencias AS A ON F.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 Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 LEFT JOIN  Localidades AS LE ON F.IdLocEnv=LE.IdLocal   LEFT JOIN TiposCom AS TCM ON F.TipCom=TCM.IdCom  
 LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed
 LEFT JOIN Mercancias AS MC ON D.CdMercancia=MC.IdMercancia LEFT JOIN  ConcDiversos AS CF ON D.CdConcepto=CF.IdConcepto
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS NT ON D.NitTercero=NT.IdTercero
 LEFT JOIN Trn_TraRemesa AS R ON D.TipRem=R.TipDoc AND D.Remesa=R.NumOrden AND D.IdCiaRem=R.IdCia
 LEFT JOIN Rutas AS RT ON R.CdRutaTarif=RT.IdRuta 
 LEFT JOIN Trn_DocMemo AS DM ON F.TipDoc=DM.TipDoc AND F.Factura=DM.Documento AND F.IdCia=DM.IdCia
 LEFT JOIN Trn_TraRemMcias AS RM ON D.TipRem=RM.TipDoc AND D.Remesa=RM.NumOrden AND D.IdCiaRem=RM.IdCia AND D.ItemRem=RM.Item
 LEFT JOIN TercSedes AS SDR ON RM.NitRemite=SDR.IdTercero AND RM.SedeRem=SDR.IdSede LEFT JOIN TercSedes AS SDD ON RM.NitDestntario=SDD.IdTercero AND RM.SedeDest=SDD.IdSede
 LEFT JOIN Trn_Face AS FE ON F.TipDoc=FE.TipDoc AND F.Factura=FE.Documento AND F.IdCia=FE.IdCia
 LEFT JOIN Terceros AS TDE ON R.IdDestinatario=TDE.IdTercero LEFT JOIN Terceros As TRE ON R.IdRemitente=TRE.IdTercero
 WHERE F.TipDoc=@pmTipDoc AND F.Factura BETWEEN @pmFacturaIni AND @pmFacturaFin AND F.IdCia=@pmIdCia
 ORDER BY F.Factura
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_DfcT] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,DV.TipDev,DV.Devolucion,DV.IdCia,DV.Fecha,DV.IdConcepto,DV.IdCliente,DV.IdAgencia,D.VrUnitario*D.Cantidad,D.VrDescuento,D.VrImpuesto,0,D.VrRetencion,D.VrReteIca,0,0
,0,D.VrFaltante,D.VrSeguroRem,D.VrAutRetIca,D.VrCosto*D.CantPago,(D.VrUnitario*D.Cantidad)-D.VrDescuento+D.VrImpuesto,0,0,0,0,D.Cantidad,D.CdMercancia,D.PesoNeto,0,F.IdForma,'','0'
,CASE WHEN LEN(D.Descripcion)>150 THEN SUBSTRING(D.Descripcion,1,150) ELSE D.Descripcion END ,'0',D.FecRemesa,DV.IdVend,DV.TarifaCom,'0',DV.IdUsuario,DV.IdLocEnv,D.Referencia1,D.Referencia2,'',DV.ModdDev
,D.pVehiculo,D.TipRem,D.Remesa,D.IdCiaRem,DV.TipCom,DV.Comprobante,DV.IdCiaCom,1,0,DV.Fecha,DV.Observacion,DV.TimeSys,DV.VrImpCons,F.CdRuta,D.IdOrigen,D.IdDestino,D.NitTercero,F.CdConductor,'','',DV.Factura,DV.IdCiaDoc
FROM Trn_DevFcr AS DV INNER JOIN Trn_TraFacRemesas AS D ON DV.TipDev=D.TipDoc AND DV.Devolucion=D.Factura AND DV.IdCia=D.IdCia
INNER JOIN Trn_Facturas AS F ON DV.TipDoc=F.TipDoc AND DV.Factura=F.Factura AND DV.IdCiaDoc=F.IdCia
WHERE DV.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (DV.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (DV.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_FcrT] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
 SELECT @pmtmEst,F.TipDoc,F.Factura,F.IdCia,F.Fecha,F.IdConcepto,F.IdCliente,F.IdAgencia,D.VrUnitario*D.Cantidad,D.VrDescuento,D.VrImpuesto,0,D.VrRetencion,D.VrReteIca,0,0
,0,D.VrFaltante,D.VrSeguroRem,D.VrAutRetIca,D.VrCosto*D.CantPago,(D.VrUnitario*D.Cantidad)-D.VrDescuento+D.VrImpuesto,0,0,0,0,D.Cantidad,D.CdMercancia,D.PesoNeto,0,F.IdForma,'','0'
,CASE WHEN LEN(D.Descripcion)>150 THEN SUBSTRING(D.Descripcion,1,150) ELSE D.Descripcion END ,'0',D.FecRemesa,F.IdVend,F.TarifaCom,'0',F.IdUsuario,F.IdLocEnv,D.Referencia1,D.Referencia2,'',F.Modalidad
,D.pVehiculo,D.TipRem,D.Remesa,D.IdCiaRem,F.TipCom,F.Comprobante,F.IdCiaCom,F.Anulado,F.NumDev,F.FecDev,F.Observacion,F.TimeSys,F.VrImpCons,F.CdRuta,D.IdOrigen,D.IdDestino,D.NitTercero,F.CdConductor,'','',F.Pedido,F.IdCiaPed
FROM Trn_Facturas AS F INNER JOIN Trn_TraFacRemesas AS D ON F.TipDoc=D.TipDoc AND F.Factura=D.Factura AND F.IdCia=D.IdCia
WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (F.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (F.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 F.TipDoc,F.Factura,F.IdCia AS CdCia,Compania,F.Fecha,FE.Prefijo,FE.NumFace,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,VrImpCarbono,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
,F.CdRuta,Ruta,TipPed,Pedido,IdCiaPed,TipRem,Remision,IdCiaRem,TipCot,Cotizacion,IdCiaCot,FecPedido,AutzaMora,AutzaCupo,Modalidad,PesoTotal,UnidTotal,VolTotal,CantFalt,VrCostoRem,VrCostoAfi,VrImpCons
,BaseIvaObsq,VrIvaObsequio,F.TarifaArf,F.VrAutRetFte,F.TarifaAri,F.VrAutRetIca,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,F.Observacion AS Observ,ZonaFrontera,VrReteCREE,TarifaRtc,CodTarRtc,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,CdFntePago,FuentePago
 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 
 LEFT JOIN FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente
 LEFT JOIN Trn_Face AS FE ON F.TipDoc=FE.TipDoc AND F.Factura=FE.Documento AND F.IdCia=FE.IdCia
 WHERE F.TipDoc LIKE ISNULL(@pmTipDoc,'%') AND F.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.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND F.IdVend LIKE ISNULL(@pmIdVend,'%') AND F.IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%')
 AND F.Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (F.Anulado=ISNULL(@pmAnulado,0) or F.Anulado=ISNULL(@pmAnulado,1))
 ORDER BY F.IdCia,F.Factura
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpConsumo)
 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,ISNULL(KarImpCon,0)
 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,SUM(VrImpCon) AS KarImpCon
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpCarbono,F.VrIvaIngProd,VrNeto,VrAplicado,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,BaseIca,BaseRiv,BaseIvaIgp
,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,F.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,PesoTotal,UnidTotal,VolTotal,CantFalt,VrCostoRem,VrCostoAfi,VrImpCons,BaseIvaObsq,VrIvaObsequio,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,F.Observacion AS Observ,ZonaFrontera,VrReteCREE,TarifaRtc,CodTarRtc,Parqueadero,FecCuotaParq,TipoFE,FechaFE,F.CodTarArf,F.TarifaArf,F.VrAutRetFte,F.CodTarAri,F.TarifaAri,F.VrAutRetIca,F.IdEstado AS CdEstado,Estado
,F.TimeSys AS Fec_Add,F.FecUpdate AS Fec_Update,F.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,A.CodSicom
--Detalles
,K.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,BaseIvp,TarifaIvp,IvaIngProd,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
,CdMngra,NumInicial,NumFinal,K.TarifaIco,K.VrImpCon AS DetVrImpCon,K.BaseIvaCom,k.ImpCarbono
--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,FE.EstadoFE,FE.Prefijo,FE.NumFace,FE.CUFE,FE.Resolucion,FE.RangoNum,FE.FecVigencia,FE.CUFE_QR AS QR,FE.FechaValidacion
 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
 LEFT JOIN Trn_Face AS FE ON F.TipDoc=FE.TipDoc AND F.Factura=FE.Documento AND F.IdCia=FE.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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOpedidoDetFac] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT O.TipDoc,O.Pedido,O.IdCia AS CdCia,Compania,O.Fecha,O.FechaVence,O.IdConcepto AS CdConcepto,Concepto,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia,CodAgencia,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,O.TarifaCom
,O.VrFletes,O.VrOtros,O.VrCargos,O.VrOtrDcto,O.VrNeto,O.DirEnvio,O.IdLocEnv AS IdCiuEnvio,LE.Localidad AS CiudadEnvio,LugarEnvio,O.DiasEntraga,O.NitContac AS NitContacto,O.NomContac AS NomContacto,O.TelContac AS TelContacto,O.emlContac AS EmailContacto,O.CargoContac
,O.IdForma AS CdForma,FormaPago,O.DetallePago,O.MulPlazos,O.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,O.NitEmpTrans,O.EmpTrans,O.pVehiculo AS PlacaVeh,O.CdConductor,TC.RazonSocial AS Conductor,O.CdRuta,Ruta,RefPedido,O.TipFac,O.Factura,O.IdCiaFac,O.FechaFact,O.TipRem,O.Remision,O.IdCiaRem,O.FechaRem
,O.NumCotizac,CdCiaCotizac,O.NumAutoriza,O.Modalidad,O.Vigencia,O.NumAprob,O.IdCiaApr,FecAprob,CdUsuAprob,DetalleAprob,O.Anulado,O.FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.ZonaFrontera,TipoTrans,O.TipoOrden,TipoModifica,O.TimeSys AS FechaCrea,O.IdCiaCrea,O.IdUsuario AS CdUsuario,Usuario
--detalles
,D.Item,D.IdProducto AS CdProducto,DescripProd,PM.TipoRef,D.IdBodega AS CdBodega,Bodega,D.CdTanque,D.Salidas AS CantOrden,D.IdUnd AS CdUnd,UM.Unidad,D.VrUnitario AS CostoUnd,D.VrPrecio AS VlrUnitario,D.TarifaIva,D.VrIvaSal,D.TarifaDct,D.VrDctoSal
,D.TarifaRet,D.VrReteSal,D.TarifaIca,D.VrIcaSal,D.ListaPrec,D.VrBruto,D.VrBase,D.Unidades,D.Descripcion AS DetDescripcion,D.Referencia,D.Referencia2,D.Servcios,D.NoVentas,D.EsCombo,D.EsProdBase
,D.TipOrd AS DetTipoFac,D.NumOrden AS DetNumFac,D.IdCiaOrd AS DetIdCiaFac,D.galsneto AS CantTotFact,D.CantObseq,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
--datos de item facturado
,DF.TipDoc AS FacTipo,DF.Documento AS FacNumero,DF.IdCia AS FacIdCia,DF.FecFact,DF.Salidas AS FacCantidad,DF.VrUnitario AS FacCostoUnd,DF.VrPrecio AS FacVrUnitario,DF.TarifaIva AS FacTarifIva,DF.VrIvaSal AS FacVrIva
,DF.TarifaDct AS FacTarifDcto,DF.VrDctoSal AS FacVrDcto,DF.TarifaRet AS FacTarifRet,DF.TarifaIca AS FacTarifIca,DF.ListaPrec AS FacLista,DF.VrBruto AS FacVrBruto,DF.VrBase AS FacVrBase,D.Unidades AS FacUnidades,DF.Servcios AS FacServicio
,DF.EsCombo AS FacEsCombo,DF.EsProdBase AS FacProdBase,DF.galsbruto AS FacCantTotPed,DF.VrRetencion AS FacRetencion,DF.VrReteICA AS FacReteICA,DF.VrReteIVA AS FacReteIva,DF.VrFletes AS FacFletes,DF.VrOtros AS FacVrOtros,DF.VrCargos AS FacVrCargos,DF.VrOtrDcto AS FacOtrDcto
,DF.VrNeto AS FacVrNeto,DF.BaseRet,DF.VrReteCREE AS FacReteCREE
--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
,NitRepLeg,NomRepLeg,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,ExcIva,LiqFletes,Autoret,VrCupo,VrSaldo,CdDiaEnt,DiaEntrega,CLI.IdEstrato AS CdEstrato,Estrato
 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 Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN Trn_Kardex AS D ON O.TipDoc=D.TipDoc AND O.Pedido=D.Documento AND O.IdCia=D.IdCia
 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 D.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
 LEFT JOIN Localidades AS LE ON O.IdLocEnv=LE.IdLocal LEFT JOIN Terceros AS TC ON O.CdConductor=TC.IdTercero
 LEFT JOIN Rutas AS R ON O.CdRuta=R.IdRuta LEFT JOIN TiposEnt AS EG ON CLI.CdDiaEnt=EG.IdEnt LEFT JOIN Estratos AS ETT ON CLI.IdEstrato=ETT.IdEstrato
 --facturas>
 LEFT JOIN (SELECT D.TipDoc,D.Documento,D.IdCia,D.Item,D.Fecha,D.IdProducto,D.IdBodega,Salidas,VrUnitario,VrPrecio,D.TarifaIva,D.VrIvaSal,D.TarifaDct,D.VrDctoSal,D.TarifaRet,VrReteSal
,D.TarifaIca,D.VrIcaSal,ListaPrec,D.VrBruto,VrBase,D.Unidades,D.TipOrd,D.NumOrden,D.IdCiaOrd,D.Remision,D.IdCiaRem,ItemCombo,Servcios,EsCombo,EsProdBase,galsbruto
,F.Fecha AS FecFact,F.IdConcepto AS CdConcFact,F.IdCliente,F.IdAgencia,F.VrRetencion,F.VrReteICA,F.VrReteIVA,VrFletes,F.VrOtros,VrCargos,VrOtrDcto,VrNeto,F.BaseRet,F.TarifaRet AS TarifRetFact,F.TarifaIca AS TarifIcaFact,F.TarifaRiv AS TarifRivFact
,F.TarifaRtc,F.VrReteCREE,F.Observacion FROM Trn_Kardex AS D INNER JOIN Trn_Facturas AS F ON D.TipDoc=F.TipDoc AND D.Documento=F.Factura AND D.IdCia=F.IdCia 
 WHERE D.TipDoc IN ('FCR','FC1','FC2','FC3','FC4','FC5') AND F.Anulado=0
 UNION ALL
 SELECT D.TipDoc,D.Documento,D.IdCia,D.Item,D.Fecha,D.IdProducto,D.IdBodega,Salidas,VrUnitario,VrPrecio,D.TarifaIva,D.VrIvaSal,D.TarifaDct,D.VrDctoSal,D.TarifaRet,VrReteSal
,D.TarifaIca,D.VrIcaSal,ListaPrec,D.VrBruto,VrBase,D.Unidades,D.TipOrd,D.NumOrden,D.IdCiaOrd,D.Remision,D.IdCiaRem,ItemCombo,Servcios,EsCombo,EsProdBase,galsbruto
,F.Fecha AS FecFact,F.IdConcepto AS CdConcFact,F.NitCliente,F.CdAgencia,F.VrRetencion,F.VrReteICA,F.VrReteIVA,VrFletes,0,F.VrCargos,F.VrOtrDcto,F.VrNeto,F.BaseRet,F.TarifaRet,F.TarifaIca,F.TarifaRiv,F.TarifaRtc,F.VrReteCREE,F.Observacion
 FROM Trn_Kardex AS D INNER JOIN Trn_Factcon AS F ON D.TipDoc=F.TipDoc AND D.Documento=F.Factura AND D.IdCia=F.IdCia 
 WHERE D.TipDoc IN ('FCO','FO1','FO2','FO3','FO4','FO5') AND F.Anulado=0 AND D.TipOrd='PED' AND D.NumOrden>0) AS DF ON D.TipDoc=DF.TipOrd AND D.Documento=DF.NumOrden AND D.IdCia=DF.IdCiaOrd AND D.Item=DF.ItemCombo AND D.IdProducto=DF.IdProducto
--<Facturas
 WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND (@pmIdCia IS NULL OR O.IdCia=@pmIdCia) AND (@pmIdCliente IS NULL OR O.IdCliente=@pmIdCliente)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_FactNeto] @pmtmEst CHAR(2)
 AS 
 SELECT tmTipFac,tmFactura,tmIdCia,Compania,F.Fecha,F.FechaVence,T.TipoId,F.IdCliente,T.Dv,T.RazonSocial AS NomCliente,F.IdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia
,tmEntradas,tmSalidas,tmSubTotal,tmDescuento,tmImpuesto,tmRetencion,tmReteICA,tmReteIVA,tmReteCREE,tmSobretasa,tmImpGlobal,tmOtros,tmCargos,tmOtrDcto,tmFletes,tmCostos
,tmFaltantes,tmAnticipos,tmImpCons,tmNeto AS VrNeto,F.BaseImp,F.BaseRet,F.TarifaIva,F.TarifaRet,F.TarifaIca,F.TarifaRiv,F.TarifaRtc,F.TipPed,F.Pedido,F.IdCiaPed
,F.TipRem,F.Remision,F.IdCiaRem,F.TipCot,F.Cotizacion,F.IdCiaCot,F.FecPedido,F.IdVend AS NitVend,VN.RazonSocial AS Vendedor,TarifaCom,F.pVehiculo AS PlacaVeh
,F.CdConductor,TC.RazonSocial AS Conductor,NitEmpTrans,EmpTrans,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
,F.IdCCosto AS IdCenCost,CCosto,F.IdSubCos AS IdSubCent,SubCosto,Modalidad,F.IdConcepto AS CdConcepto,Concepto,F.TipCom,F.Comprobante,F.IdCiaCom
,F.Observacion,F.NumDev,F.FecDev,F.TimeSys AS FechaCrea,F.FecUpdate AS FechaAct,F.IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario
--Información del tercero
,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.TelMovil AS TercCelular,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,CLI.TipoCliente,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
,A.CdFntePago AS CdFuente,FuentePago,TipoZona,A.CodSicom
,V.Observacion AS VehObservacion,V.Descripcion AS VehDescripcion
FROM tm_FactNeto AS TF INNER JOIN Trn_Facturas AS F ON TF.tmTipFac=F.TipDoc AND TF.tmFactura=F.Factura AND TF.tmIdCia=F.IdCia
INNER JOIN Companias AS CN ON TF.tmIdCia=CN.IdCia INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
INNER JOIN Agencias AS A ON F.IdAgencia=A.IdAgencia INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
INNER JOIN Conceptos AS C ON F.IdConcepto=C.IdConcepto INNER JOIN Plazos AS PZ ON F.IdPlazo=PZ.IdPlazo
INNER JOIN Terceros AS VN ON F.IdVend=VN.IdTercero 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 Vehiculos AS V ON F.pVehiculo=V.IdVehiculo 
LEFT JOIN SubCentros AS SC ON F.IdSubCos=SC.IdSubCos LEFT JOIN FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente
WHERE tmEst=@pmtmEst
--Detalles tmItem,tmTasaNac,tmTasaDep,tmTasaMun,tmRec_Costo,tmMgenCont,tmIvaInf
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CodAutoRet,OrigenAdd,TipCom,Comprobante,IdCiaCom
,ZonaFrontera,PesoTotal,UnidTotal,VolTotal,CantFalt,VrCostoRem,VrCostoAfi,VrImpCons,VrReteCREE,VrImpCarbono,TarifaRtc,CodTarRtc,Parqueadero,FecCuotaParq,BaseIvaObsq,VrIvaObsequio
,TipoFE,FechaFE,BaseIvaIgp,VrIvaIngProd,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Facturas
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,F.CdRuta,Ruta
,TipPed,Pedido,IdCiaPed,TipRem,F.Remision AS NumRemision,F.IdCiaRem AS CdCiaRem,TipCot,F.Cotizacion AS NumCotizacion,F.IdCiaCot AS CdCiaCotiza,FecPedido,F.TarifaArf,F.VrAutRetFte,F.TarifaAri,F.VrAutRetIca
,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,BaseIvp,TarifaIvp,IvaIngProd,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,SZ.Subzona,SZ.IdZona AS CdZona,Z.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
,A.CdFntePago AS CdFuente,FuentePago,A.TipoZona AS TipoZona,A.IdSzona AS AgeCdSubzona,SZA.Subzona AS AgeSubzona,SZA.IdZona AS AgeCdZona,ZA.Zona AS AgeZona
 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
 LEFT JOIN FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente
 LEFT JOIN Subzonas AS SZA ON A.IdSzona=SZA.IdSzona LEFT JOIN Zonas AS ZA ON SZA.IdZona=ZA.IdZona 
 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
,F.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.TarifaArf,D.VrAutRetFte,D.TarifaAri,D.VrAutRetIca,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,BaseIvp,TarifaIvp,IvaIngProd,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,SZ.Subzona,SZ.IdZona AS CdZona,Z.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
,A.CdFntePago AS  CdFuente,FuentePago,A.TipoZona,A.IdSzona,SZA.Subzona,SZA.IdZona,ZA.Zona
 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
 LEFT JOIN FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente
 LEFT JOIN Subzonas AS SZA ON A.IdSzona=SZA.IdSzona LEFT JOIN Zonas AS ZA ON SZA.IdZona=ZA.IdZona 
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryFacturasRelTraDet] @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 F.TipDoc AS TipoFact,F.Factura AS NumFactura,F.IdCia AS CdCia,Compania,F.Fecha,FE.Prefijo,FE.NumFace,F.IdConcepto AS CdConcepto,C.Concepto AS ConcDescrip,IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS IdAgenc,A.Agencia AS NomAgencia
,A.CodAgencia AS Cod_Agencia,FechaVence,VrSubTotal,F.VrDescuento AS VrDcto,F.VrImpuesto AS VrIva,F.VrRetencion AS VrRetFte,F.VrReteICA AS VrRetIca,VrReteIVA,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal
,VrFaltantes,VrAnticipos,VrNeto,VrAplicado,BaseImp,BaseRet,BaseIca,BaseRiv,F.TarifaIva AS TarifIva,F.TarifaRet AS TarifRet,F.TarifaIca AS TarifIca,TarifaRiv,PesoTotal,UnidTotal,VolTotal,CantFalt,VrCostoRem,VrCostoAfi,F.VrImpCons AS VrAutoICA,F.ZonaFrontera AS EstAutoICA
,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,CdConductor,TC.RazonSocial AS Conductor
,F.CdRuta,Ruta,TipPed,Pedido,IdCiaPed,AutzaMora,AutzaCupo,VrReteCREE,TarifaRtc,CodTarRtc,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom
,F.Anulado AS FacAnulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,TimeSys,F.FecUpdate AS Fec_Update,IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario
--detalles
,Item,TipoReg,FechaFact,D.TipRem AS TipRemesa,D.Remesa AS NumRemesa,D.IdCiaRem AS CdCiaRem,ItemRem,FecRemesa,Descripcion,D.Cantidad AS Cant,VrUnitario,VrCosto,UndTarifa,UndCosto,Unidades,PesoNeto,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,Volumen,UndVol,Cases,Cajas,Palets,CantPago
,D.TarifaIva AS DetTarifIva,D.VrImpuesto AS DetVrIva,TarifaDct,D.VrDescuento AS DetVrDcto,D.TarifaRet AS DetTarifRet,D.VrRetencion AS DetVrRetFte,D.TarifaIca AS DetTarifIca,D.VrReteIca AS DetVrRetIca,D.VrAutRetIca AS DetAutoICA,VrFaltante,D.Remision AS DetNumRemsion,DocCliente,Referencia1,Referencia2,Referencia3,CdMercancia,DescripMcia,CdConcepto,CF.Concepto AS DetConcepto
,CdCCosto,CCosto,D.CdSubCos AS CdSubCentro,SubCosto,NitTercero,NT.RazonSocial AS DetTercero,CdAgencia,D.pVehiculo AS PlacaVeh,TipoAfiVehic,IdOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,IdDestino,LD.Localidad AS CiudadDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,D.Anulado AS DetAnulado,TipDocRef,NumDocRef,IdCiaRef,FecDocRef,CantidadFalt,UnidadFalt,NumPedRem,CiaPedRem,VrDeclMcia,TarifaSeg,VrSeguroRem,NitAsegurad
--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
 FROM Trn_Facturas AS F  INNER JOIN  Trn_TraFacRemesas AS D ON F.TipDoc=D.TipDoc  AND F.Factura=D.Factura  AND F.IdCia=D.IdCia
 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 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 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 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 Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 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 Sys_Um AS UMP ON D.UndMed=UMP.UndMed
 LEFT JOIN Mercancias AS MC ON D.CdMercancia=MC.IdMercancia LEFT JOIN  ConcDiversos AS CF ON D.CdConcepto=CF.IdConcepto
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS NT ON D.NitTercero=NT.IdTercero
 LEFT JOIN Trn_Face AS FE ON F.TipDoc=FE.TipDoc AND F.Factura=FE.Documento AND F.IdCia=FE.IdCia
 WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND F.TipDoc LIKE ISNULL(@pmTipDoc,'%')
 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,F.Factura
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryFacturasRelTra] @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 F.TipDoc,F.Factura AS NumFactura,F.IdCia AS CdCia,Compania,F.Fecha,FE.Prefijo,FE.NumFace,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,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet ,TarifaIca,TarifaRiv,PesoTotal,UnidTotal,VolTotal,CantFalt,VrCostoRem,VrCostoAfi
,VrImpCons AS VrAutoICA,ZonaFrontera AS EstAutoICA,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
,F.CdRuta,Ruta,TipPed,Pedido,IdCiaPed,TipRem,Remision,IdCiaRem,AutzaMora,AutzaCupo,VrReteCREE,TarifaRtc,CodTarRtc,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom
,F.Anulado AS FacAnulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,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,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 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 
 LEFT JOIN Trn_Face AS FE ON F.TipDoc=FE.TipDoc AND F.Factura=FE.Documento AND F.IdCia=FE.IdCia
 WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 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.IdVend LIKE ISNULL(@pmIdVend,'%')
 ORDER BY F.IdCia,F.Factura
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@pmPesoTotal DECIMAL(14,4),@pmUnidTotal DECIMAL(14,4),@pmVolTotal DECIMAL(14,4),@pmCantFalt DECIMAL(14,4),@pmVrCostoRem MONEY,@pmVrCostoAfi MONEY,@pmCodAutoRet VARCHAR(4),@pmVrImpCons MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmParqueadero BIT,@pmFecCuotaParq SMALLDATETIME
,@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY,@pmVrImpCarbono MONEY,@pmTipoFE VARCHAR(3),@pmFechaFE SMALLDATETIME,@pmBaseIvaIgp MONEY,@pmVrIvaIngProd MONEY,@pmCodTarArf VARCHAR(4),@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmCodTarAri VARCHAR(4),@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY
,@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,PesoTotal,UnidTotal,VolTotal,CantFalt,VrCostoRem,VrCostoAfi,CodAutoRet,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc,Parqueadero,FecCuotaParq,BaseIvaObsq,VrIvaObsequio,VrImpCarbono,TipoFE,FechaFE,BaseIvaIgp,VrIvaIngProd,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca)
 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,@pmPesoTotal,@pmUnidTotal,@pmVolTotal,@pmCantFalt,@pmVrCostoRem,@pmVrCostoAfi,@pmCodAutoRet,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmVrImpCons,@pmVrReteCREE,@pmTarifaRtc,@pmCodTarRtc
,@pmParqueadero,@pmFecCuotaParq,@pmBaseIvaObsq,@pmVrIvaObsequio,@pmVrImpCarbono,@pmTipoFE,@pmFechaFE,@pmBaseIvaIgp,@pmVrIvaIngProd,@pmCodTarArf,@pmTarifaArf,@pmVrAutRetFte,@pmCodTarAri,@pmTarifaAri,@pmVrAutRetIca)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudContFactRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT C.NumContrato,C.IdCia AS CdCia,C.Fecha,C.FechaInicio,C.FechaFinal,C.IdCliente,C.TipOrden,C.NumOrden,C.IdCiaOrden
,FC.TipFac,FC.NumFactura,FC.IdCiaFac,FC.Fecha AS FecFact,FC.ValorTotal,FC.Anulado
,D.Item AS FacItem,D.CdConcepto,D.Descripcion,CD.Concepto,D.Cantidad,D.VrUnitario,D.VrCosto,D.PesoNeto,D.Volumen,D.TarifaIva,D.VrImpuesto,D.TarifaSeg,D.VrSeguroRem
,D.Remision,D.DocCliente,D.Referencia1,D.Referencia2,D.Referencia3,D.TipDocRef,D.NumDocRef,D.IdCiaRef,F.VrNeto,F.Observacion AS Observ,F.Anulado AS FacAnulado
 FROM Trn_MudContratos AS C INNER JOIN Trn_MudContFact AS FC ON C.TipDoc=FC.TipDoc AND C.NumContrato=FC.NumContrato AND C.IdCia=FC.IdCia
 INNER JOIN Trn_TraFacRemesas AS D ON FC.TipFac=D.TipDoc AND FC.NumFactura=D.Factura AND FC.IdCiaFac=D.IdCia
 INNER JOIN Trn_Facturas AS F ON FC.TipFac=F.TipDoc AND FC.NumFactura=F.Factura AND FC.IdCiaFac=F.IdCia
 LEFT JOIN ConcDiversos AS CD ON D.CdConcepto=CD.IdConcepto
 WHERE C.TipDoc=@pmTipDoc AND C.FechaInicio BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraRemesaFac]  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null
 AS 
SELECT FR.TipDoc AS CdTipDoc,TipoDoc,Factura,FR.IdCia AS CdCia,CN.Compania AS NomCiaFact,FechaFact,Item,TipoReg,TipRem,Remesa,IdCiaRem,CR.Compania AS NomCiaRem,ItemRem,FecRemesa,FR.Descripcion AS DescripRem
,FR.Cantidad AS CantFact,VrUnitario,UndTarifa,VrCosto,UndCosto,CantPago,TarifaDct,VrDescuento,TarifaIva,VrImpuesto,TarifaRet,VrRetencion,TarifaIca,VrReteIca,VrFaltante,CantidadFalt,UnidadFalt,Unidades,PesoNeto,FR.UndMed AS CdUmPeso,Unidad
,Volumen,UndVol,Cases,Cajas,Palets,FR.CdConcepto AS CdConcept,CDV.Concepto AS DescConcept,CdMercancia,DescripMcia,CdAgencia,Agencia,CdCCosto,CCosto,FR.CdSubCos AS CdSubCentro,SubCosto,FR.TipoAfiVehic AS TipoAfiVeh,pVehiculo
,IdOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,IdDestino,LD.Localidad AS CiudadDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,Remision,DocCliente,Referencia1,Referencia2,Referencia3,FR.Anulado AS FacAnulado,TipDocRef,NumDocRef,IdCiaRef,FecDocRef,NumPedRem,CiaPedRem
--datos de remesa
,Modalidad,IdRemitente,NomRemite,IdDestinatario,NomDestino,R.IdConductor AS RemCedCond,CDT.RazonSocial AS RemConductor,nRemolque,NumManif,IdCiaManif,R.Observacion AS RemObserv,RA.TipoRuta,RA.TipoMintrans
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo
--datos de factura encabezados
,FacCdConc,CF.Concepto AS FacConcepto,T.TipoId AS TercTipo,FacNitClie,T.Dv AS TercDv,T.RazonSocial AS NombreClie,FacNitVend,VN.RazonSocial AS Vendedor,FacCdCiudad,LF.Localidad AS FacCiudad,LF.IdDep AS FacCdDpto,DF.Departamento AS FacDpto,FacVrNeto
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
 FROM Trn_TraFacRemesas AS FR INNER JOIN Sys_TiposDoc AS TD ON FR.TipDoc=TD.IdDoc INNER JOIN Companias AS CN ON FR.IdCia=CN.IdCia 
 INNER JOIN Localidades AS LO ON FR.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS LD ON FR.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 --SUBQUERY FACTURAS
 INNER JOIN (SELECT F.TipDoc AS FacTipo,F.Factura AS FacNumero,F.IdCia AS FacCdCia,F.IdConcepto AS FacCdConc,F.IdCliente AS FacNitClie,F.IdAgencia AS FacIdAgenc
 ,F.IdVend AS FacNitVend,F.IdLocEnv AS FacCdCiudad,F.VrCargos AS FacVrCargos,F.VrOtrDcto AS FacVrOtrDctos,F.VrNeto AS FacVrNeto
 FROM Trn_Facturas AS F WHERE F.Fecha BETWEEN @pmFechaIni  AND @pmFechaFin AND  F.Modalidad='TRANSPORTE'  AND F.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND F.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 UNION ALL
 SELECT D.TipDev,D.Devolucion,D.IdCia,D.IdConcepto,D.IdCliente,D.IdAgencia,D.IdVend,D.IdLocEnv,D.VrCargos,D.VrOtrDcto,D.VrNeto
 FROM Trn_DevFcr AS D WHERE D.Fecha BETWEEN @pmFechaIni  AND @pmFechaFin AND D.Modalidad='TRANSPORTE' AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%') 
 UNION ALL
 SELECT N.TipDoc,N.NumNota,N.IdCia,N.IdConcepto,N.IdCliente,N.IdAgencia,N.IdVend,N.IdLocal,N.VrOtros,0,N.VrNeto
 FROM Trn_Notas AS N WHERE N.Fecha BETWEEN @pmFechaIni  AND @pmFechaFin AND N.Modalidad='REMESAS' AND  N.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND N.IdCliente LIKE ISNULL(@pmIdCliente,'%')  ) AS F ON FR.TipDoc=F.FacTipo AND FR.Factura=F.FacNumero AND FR.IdCia=F.FacCdCia
 INNER JOIN Terceros AS T ON F.FacNitClie=T.IdTercero INNER JOIN TercCliente AS CLI ON F.FacNitClie=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 Terceros AS VN ON F.FacNitVend=VN.IdTercero INNER JOIN Conceptos AS CF ON F.FacCdConc=CF.IdConcepto
 LEFT JOIN Localidades AS LF ON F.FacCdCiudad=LF.IdLocal LEFT JOIN Departamentos AS DF ON LF.IdDep=DF.IdDep
 LEFT JOIN Companias AS CR ON FR.IdCiaRem=CR.IdCia  LEFT JOIN Sys_Um AS UMP ON FR.UndMed=UMP.UndMed
 LEFT JOIN ConcDiversos AS CDV ON FR.CdConcepto=CDV.IdConcepto  LEFT JOIN Agencias  AS A ON FR.CdAgencia=A.IdAgencia
 LEFT JOIN CentroCosto AS CC ON FR.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON FR.CdSubCos=SC.IdSubCos
 LEFT JOIN Trn_TraRemesa AS R ON FR.TipRem=R.TipDoc AND FR.Remesa=R.NumOrden AND FR.IdCiaRem=R.IdCia
 LEFT JOIN Trn_TraRemAnexo AS RA ON FR.TipRem=RA.TipDoc AND FR.Remesa=RA.NumOrden AND FR.IdCiaRem=RA.IdCia
 LEFT JOIN Terceros AS CDT ON R.IdConductor=CDT.IdTercero
 LEFT JOIN Vehiculos AS V ON FR.pVehiculo=V.IdVehiculo LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 LEFT JOIN Mercancias AS MC ON FR.CdMercancia=MC.IdMercancia
 WHERE FR.FechaFact BETWEEN @pmFechaIni AND @pmFechaFin AND FR.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND F.FacNitClie LIKE ISNULL(@pmIdCliente,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
 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,VrImpCons
,CdRuta,IdLocEnv,'','',CdConductor,'','',Remision,IdCiaRem
 FROM Trn_Facturas
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 F.Factura,F.IdCia,FE.Prefijo,FE.NumFace,Fecha,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes
,VrOtros,VrCargos,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal,VrFaltantes,VrAnticipos,VrImpCarbono,VrIvaIngProd,VrNeto,VrAplicado,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,BaseIca
,BaseRiv,BaseIvaIgp,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,F.IdConcepto AS CdConcepto,Concepto,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,CodAutoRet
,OrigenAdd,TipCom,Comprobante,IdCiaCom,ZonaFrontera,PesoTotal,UnidTotal,VolTotal,CantFalt,VrCostoRem,VrCostoAfi,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc,BaseIvaObsq,VrIvaObsequio
,Parqueadero,FecCuotaParq,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca,TipoFE,FechaFE,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,F.IdUsuario AS IdUsuari,Usuario,F.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 
 LEFT JOIN Trn_Face AS FE ON F.TipDoc=FE.TipDoc AND F.Factura=FE.Documento AND F.IdCia=FE.IdCia
 WHERE F.TipDoc=@pmTipDoc AND F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND F.Factura BETWEEN ISNULL(@pmFacturaIni,0) AND ISNULL(@pmFacturaFin,2147483647)
 AND F.IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND F.IdVend LIKE ISNULL(@pmIdVend,'%') AND F.Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND F.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 AND (F.Anulado=ISNULL(@pmAnulado,0) OR F.Anulado=ISNULL(@pmAnulado,1))
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_FactNetoDet] @pmtmEst CHAR(2)
 AS 
 SELECT tmTipFac,tmFactura,tmIdCia,Compania,F.Fecha,F.FechaVence,T.TipoId,F.IdCliente,T.Dv,T.RazonSocial AS NomCliente,F.IdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia
,tmItem,K.IdProducto AS CdProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,CdTanque,tmEntradas,tmSalidas,tmSubTotal,tmDescuento,tmImpuesto,tmRetencion,tmReteICA,tmReteIVA,tmReteCREE,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun
,tmImpGlobal,tmOtros,tmRec_Costo,tmMgenCont,tmIvaInf,tmCargos,tmOtrDcto,tmFletes,tmCostos,tmFaltantes,tmAnticipos,tmImpCons,tmNeto AS VrNeto
,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed,K.VrPrecio,K.Unidades,K.Referencia,K.Referencia2,K.CdLocal AS KarCdLocal,LE.Localidad AS KarLocalidad,LE.IdDep AS KarCodDep,DE.Departamento AS KarDpto
,K.CdCCosto,CCosto,K.CdSubCos,SubCosto,ListaPrec,K.VrBruto,K.VrBase,K.Servcios,K.NoVentas,K.EsCombo,K.EsProdBase,K.ItemCombo,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo
,G.IdLinea AS CdLinea,Linea,PM.CodBarras,DescripLong,DescripAbrv
,F.BaseImp,F.BaseRet,F.TarifaIva,F.TarifaRet,F.TarifaIca,F.TarifaRiv,F.TarifaRtc,F.TipPed,F.Pedido,F.IdCiaPed,F.TipRem,F.Remision,F.IdCiaRem,F.TipCot,F.Cotizacion,F.IdCiaCot,F.FecPedido
,F.IdVend AS NitVend,VN.RazonSocial AS Vendedor,F.TarifaCom,F.pVehiculo AS PlacaVeh,F.CdConductor,TC.RazonSocial AS Conductor,NitEmpTrans,EmpTrans,DirEnvio,DiasEntraga,F.IdForma AS CdForma,FormaPago,DetallePago
,MulPlazos,F.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,Modalidad,F.IdConcepto AS CdConcepto,Concepto,F.TipCom,F.Comprobante,F.IdCiaCom
,F.Observacion,F.NumDev,F.FecDev,F.TimeSys AS FechaCrea,F.FecUpdate AS FechaAct,F.IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario
--Información del tercero
,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.TelMovil AS TercCelular,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,CLI.TipoCliente,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
,A.CdFntePago AS CdFuente,FuentePago,TipoZona,A.CodSicom
,V.Observacion AS VehObservacion,V.Descripcion AS VehDescripcion
FROM tm_FactNeto AS TF INNER JOIN Trn_Facturas AS F ON TF.tmTipFac=F.TipDoc AND TF.tmFactura=F.Factura AND TF.tmIdCia=F.IdCia
INNER JOIN Trn_Kardex AS K ON TF.tmTipFac=K.TipDoc AND TF.tmFactura=K.Documento AND TF.tmIdCia=K.IdCia AND TF.tmItem=K.Item
INNER JOIN Companias AS CN ON TF.tmIdCia=CN.IdCia INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
INNER JOIN Agencias AS A ON F.IdAgencia=A.IdAgencia INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
INNER JOIN Conceptos AS C ON F.IdConcepto=C.IdConcepto INNER JOIN Plazos AS PZ ON F.IdPlazo=PZ.IdPlazo
INNER JOIN Terceros AS VN ON F.IdVend=VN.IdTercero INNER JOIN Formaspago AS FP ON F.IdForma=FP.IdForma
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
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 
LEFT JOIN Terceros AS TC ON F.CdConductor=TC.IdTercero LEFT JOIN Vehiculos AS V ON F.pVehiculo=V.IdVehiculo 
LEFT JOIN FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente
LEFT JOIN CentroCosto AS CC ON K.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=SC.IdSubCos 
LEFT JOIN Localidades AS LE ON K.CdLocal=LE.IdLocal LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep
WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@pmPesoTotal DECIMAL(14,4),@pmUnidTotal DECIMAL(14,4),@pmVolTotal DECIMAL(14,4),@pmCantFalt DECIMAL(14,4),@pmVrCostoRem MONEY,@pmVrCostoAfi MONEY,@pmCodAutoRet VARCHAR(4)
,@pmVrImpCons MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmParqueadero BIT,@pmFecCuotaParq SMALLDATETIME,@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY,@pmVrImpCarbono MONEY,@pmTipoFE VARCHAR(3),@pmFechaFE SMALLDATETIME,@pmBaseIvaIgp MONEY,@pmVrIvaIngProd MONEY,@pmCodTarArf VARCHAR(4),@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmCodTarAri VARCHAR(4),@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY,@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,Parqueadero=@pmParqueadero,FecCuotaParq=@pmFecCuotaParq
,PesoTotal=@pmPesoTotal,UnidTotal=@pmUnidTotal,VolTotal=@pmVolTotal,CantFalt=@pmCantFalt,VrCostoRem=@pmVrCostoRem,VrCostoAfi=@pmVrCostoAfi,CodAutoRet=@pmCodAutoRet,VrImpCons=@pmVrImpCons,VrReteCREE=@pmVrReteCREE,TarifaRtc=@pmTarifaRtc,CodTarRtc=@pmCodTarRtc,BaseIvaObsq=@pmBaseIvaObsq,VrIvaObsequio=@pmVrIvaObsequio,VrImpCarbono=@pmVrImpCarbono,TipoFE=@pmTipoFE,FechaFE=@pmFechaFE,BaseIvaIgp=@pmBaseIvaIgp,VrIvaIngProd=@pmVrIvaIngProd
,CodTarArf=@pmCodTarArf,TarifaArf=@pmTarifaArf,VrAutRetFte=@pmVrAutRetFte,CodTarAri=@pmCodTarAri,TarifaAri=@pmTarifaAri,VrAutRetIca=@pmVrAutRetIca,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
 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,D.TipDoc,D.Factura,D.IdCiaDoc,D.TipCom,D.Comprobante,D.IdCiaCom,0,0,D.Fecha,D.Observacion,D.TimeSys,D.VrImpCons
,F.CdRuta,D.IdLocEnv,'','',F.CdConductor,'','',D.Pedido,D.IdCiaPed
 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=@pmIdCia OR D.IdCia IS NULL) AND (D.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFacturasFmtc] @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,C.Concepto AS DescConcepto,F.IdCliente,T.RazonSocial AS NomCliente
,F.IdAgencia AS IdAgenc,A.Agencia AS NomAgencia,FechaVence,VrSubTotal,F.VrDescuento AS VrDcto,F.VrImpuesto AS VrIva,F.VrRetencion AS VrRetFte,F.VrReteICA AS VrRetIca,F.VrReteIVA,F.VrFletes
,F.VrOtros,F.VrCargos,F.VrOtrDcto,F.VrCostos,F.VrSobretasa,F.VrImpGlobal,F.VrFaltantes,F.VrAnticipos,F.VrNeto,VrAplicado,F.Cantidad AS CantTotal,F.BaseImp,F.BaseRet,F.BaseIca
,F.IdVend AS NitVend,VN.RazonSocial AS Vendedor,TarifaCom,F.NitContac AS FacNitContac,F.NomContac AS FacNomContac,F.TelContac AS FacTelContac,F.emlContac AS FacEmailContac,CargoContac
,F.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,NitEmpTrans,EmpTrans,F.TipRem AS TipRemision,F.Remision AS NumRemision,F.FecPedido,F.Modalidad,F.TipCom,F.Comprobante,F.IdCiaCom
,F.Anulado AS FacAnulado,F.NumDev,F.FecDev,F.Observacion AS Observ,TipoFE,FechaFE,F.IdEstado AS CdEstado,F.TimeSys AS Fec_Add,F.FecUpdate AS Fec_Update,F.IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario,Leyenda
--detales
,D.Item,D.IdCargo,CG.Concepto AS Cargo,D.Descripcion,D.ValorCargo,D.TarifaIva,D.VrIva AS DetVrIva,D.TarifaRet,D.VrRetencion AS DetVrRetencion,D.TarifaIca,D.VrReteIca AS DetVerIca,D.CdAgencia
,D.IdVehiculo AS PlacaVeh,D.Referencia,D.CodTarIva,D.CodTarRet,D.CodTarIca,D.CdClaseCue,D.TipCausac,D.NumCausac,D.CiaCausac,D.ItemCausac
,CG.CodFEDian,CG.IdCuentaDeb,CG.IdCuentaCre,CG.IdCueDebVp,CG.IdCueCreVp,CG.CdCuentaIva,CG.CdCuentaRet,CG.CdCuentaIca,CG.IvaIncluido
--Información 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.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,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,ExcIva,LiqFletes,Autoret,VrCupo,VrSaldo
,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
,FE.EstadoFE,FE.Prefijo,FE.NumFace,FE.CUFE,FE.Resolucion,FE.RangoNum,FE.FecVigencia,FE.CUFE_QR AS QR,FE.FechaValidacion
 FROM Trn_Facturas AS F INNER JOIN  Trn_FactCausacion AS D ON F.TipDoc=D.TipDoc  AND F.Factura=D.Factura AND F.IdCia=D.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 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 Terceros AS T ON F.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 Fijos AS CG ON D.IdCargo=CG.IdCargo
 LEFT JOIN TercCliente AS CLI ON F.IdCliente=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 Agencias AS A ON F.IdAgencia=A.IdAgencia 
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Trn_Face AS FE ON F.TipDoc=FE.TipDoc AND F.Factura=FE.Documento AND F.IdCia=FE.IdCia
 WHERE F.TipDoc=@pmTipDoc AND F.Factura BETWEEN @pmFacturaIni AND @pmFacturaFin AND F.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpConsumo)
 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,ISNULL(KarImpCon,0)
 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,SUM(VrImpCon) AS KarImpCon
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraLiquidaOtrFmt] @pmTipDoc VARCHAR(3),@pmLiquidacionIni INT,@pmLiquidacionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT O.TipDoc AS TipoLiq,O.Liquidacion,O.IdCia AS CdCia,Compania,O.Item AS nItem,TipCau,O.Causacion AS NumCausac,IdCiaCau,ItemCau
,D.TipFac,O.Factura,O.ItemFac,O.IdProveedor AS NitPropietario,RazonSocial,O.pVehiculo,D.IdConcepto AS CdConcepto,D.Descripcion
,D.VrDebito,D.VrCredito,O.VrAbono,D.CdCuenta,D.Referencia,D.TipDocRef,D.DocRef,D.FecVence,D.VrBase,D.TarifBase,D.EstadoReg
,F.FecEmision,F.FecVence AS FacFecVence,F.VrFactura,F.VrAbonado AS TotalAbonado,F.Referencia AS FacReferencia,F.Detalle
 FROM Trn_TraLiquidaOtr AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN Terceros AS T ON O.IdProveedor=T.IdTercero
 LEFT JOIN Trn_TraCauDetalle AS D ON O.TipCau=D.TipDoc AND O.Causacion=D.Causacion AND O.IdCiaCau=D.IdCia AND O.ItemCau=D.Item
 LEFT JOIN Trn_ComFactura AS F ON O.TipCau=F.TipFac AND O.Factura=F.Factura AND O.IdCiaCau=F.IdCia AND O.ItemFac=F.Item AND O.IdProveedor=F.IdProveedor
 WHERE O.TipDoc=@pmTipDoc AND O.Liquidacion BETWEEN @pmLiquidacionIni AND @pmLiquidacionFin AND O.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraRemCntdorFmt] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumOrden,IdCia,Item,C.IdTipoCon AS CdTipoCon,TipoContndor,NumContdor,PesoNeto,C.IdTipoPre AS CdTipoPrec,Precinto,NumPrecinto,ColorPrecinto,Sellos
,Peligroso,Perecedero,Toxico,Inflamable,Difmanejo,Fragil,DevCont,CdLocCont,Localidad,PatioCont,C.Observacion,NumSalidaSello,CdCiaSalSello,ItemSalSello
,CdProdSalida,NumSerialSello,NumComodato,FecVenceCom
 FROM Trn_TraRemCntdor AS C INNER JOIN TiposCnd AS TC ON C.IdTipoCon=TC.IdTipoCon
 INNER JOIN TiposPcnto AS TP ON C.IdTipoPre=TP.IdTipoPre
 LEFT JOIN Localidades AS L ON C.CdLocCont=L.IdLocal
 WHERE TipDoc=@pmTipDoc AND NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND IdCia=@pmIdCia
 ORDER BY  NumOrden
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraManifPrecFmt] @pmTipDoc VARCHAR(3),@pmManifiestoIni INT,@pmManifiestoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT P.TipDoc,P.Manifiesto,P.IdCia,P.Item,P.IdTipoPre,Precinto,NumPrecinto,ColorPrecinto,Sellos
 FROM Trn_TraManifPrec AS P INNER JOIN TiposPcnto AS TP ON P.IdTipoPre=TP.IdTipoPre
 WHERE P.TipDoc=@pmTipDoc AND P.Manifiesto BETWEEN @pmManifiestoIni AND @pmManifiestoFin AND P.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposPcnto] @pmIdTipoPre VARCHAR(4),@pmPrecinto VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposPcnto (IdTipoPre,Precinto,Inactivo)
 VALUES (@pmIdTipoPre,@pmPrecinto,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposPcnto] @pmIdTipoPre VARCHAR(4)
 AS 
 DELETE FROM TiposPcnto
 WHERE IdTipoPre=@pmIdTipoPre
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraManifIntCon_Cr] @pmTipDoc VARCHAR(3),@pmManifiestoIni INT,@pmManifiestoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipDoc,C.Manifiesto,C.IdCia AS CdCia,C.Item,C.IdTipoCon,TipoContndor,C.NumCont,C.PesoCont
,C.IdTipoPre,Precinto,C.NumPrecinto,C.ColorPrecinto
FROM Trn_TraManifIntCon AS C INNER JOIN TiposCnd AS TC ON C.IdTipoCon=TC.IdTipoCon
LEFT JOIN TiposPcnto AS P ON C.IdTipoPre=P.IdTipoPre 
WHERE C.TipDoc=@pmTipDoc AND C.Manifiesto BETWEEN @pmManifiestoIni AND @pmManifiestoFin AND C.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposPcnto] @pmIdTipoPre VARCHAR(4),@pmPrecinto VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposPcnto SET Precinto=@pmPrecinto,Inactivo=@pmInactivo
 WHERE IdTipoPre=@pmIdTipoPre
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraRemesaCnt] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS 
 SELECT R.TipDoc AS CdTipDoc,R.NumOrden AS Num_Orden,R.IdCia AS CdCia,Compania,R.Fecha,FecDespacho,FecEntrega,HoraLlegada,IdCliente,NomCliente,T.RazonSocial
,R.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,IdClieFact,IdRemitente,NomRemite,IdDestinatario,NomDestino,IdLocOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,IdLocDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,IdLocFletes,CF.Localidad AS LugarFletes,R.IdVehiculo AS PlacaVeh,R.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,TipoAfiVehic
--Datos de contendor
,CN.Item,CN.IdTipoCon AS CdTipoCon,TipoContndor,NumContdor,CN.IdTipoPre AS CdTipoPrec,Precinto,NumPrecinto,ColorPrecinto,Sellos,Peligroso,Perecedero,Toxico,Inflamable,Difmanejo,Fragil
,DevCont,CdLocCont,LD.Localidad,PatioCont,CN.Observacion,NumSalidaSello,CdCiaSalSello,ItemSalSello,CN.CdProdSalida,CN.NumSerialSello,CN.NumComodato,CN.FecVenceCom
,Modalidad,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos,R.VrDeclarado AS ValDeclarado,R.VrSeguro AS ValSeguro,R.Cantidad AS CantTotal,PesoTotal
,R.IdMneda,VrTasa,NumPedido,IdCiaPed,FechaPed,TipDcm,NumDocmto,IdCiaDcm,FechaDcm,NumManif,IdCiaManif,EstCumplido,EstFactura,CdConcepto,Concepto,CdRutaTarif,TipCom,Comprobante,IdCiaCom,SerieGuia,NumGuia,CdForma,NumCausac,CdCiaCausac,EdoCausac
,Anulado,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS CdUsuario,Usuario
,RA.IdVend AS CdVend,VN.RazonSocial AS Vendedor,LugarCargue,LugarDescargue,NomContacto,TelContacto,emlContacto,ContacDestino,TelContacDest,emlContacDest,TomadorPoliza,PolizaSeguro
,NitCiaPoliza,NomCiaPoliza,FecVencePol,ManifMintrans,RemMintrans,TipSal,NumSalida,IdCiaSal,TipoRuta,TipoMintrans,CdBodega 
--Datos del cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NitRepLeg,NomRepLeg,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,A.DirAgncia AS AgeDireccion,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,A.TelAgncia AS AgeTelefono,A.Referencia AS AgeReferencia
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPoseedor AS NitPoseedor,NPO.RazonSocial AS Poseedor
,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,V.IdGrupo AS CdGrupoPro,GrupoProp
 FROM Trn_TraRemesa AS R INNER JOIN Trn_TraRemAnexo AS RA ON R.TipDoc=RA.TipDoc  AND R.NumOrden=RA.NumOrden AND R.IdCia=RA.IdCia
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 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 CO ON R.IdLocOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON R.IdLocDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Localidades AS CF ON R.IdLocFletes=CF.IdLocal  INNER JOIN Terceros AS  CDT ON R.IdConductor=CDT.IdTercero 
 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 TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN Terceros AS NPO ON V.IdPoseedor=NPO.IdTercero  INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN Terceros AS VN ON RA.IdVend=VN.IdTercero  LEFT JOIN Agencias AS A ON R.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN  Conceptos AS C ON R.CdConcepto=C.IdConcepto
 LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 LEFT JOIN Trn_TraRemCntdor AS CN ON R.TipDoc=CN.TipDoc AND R.NumOrden=CN.NumOrden AND R.IdCia=CN.IdCia
 LEFT JOIN TiposCnd AS TCN ON CN.IdTipoCon=TCN.IdTipoCon LEFT JOIN TiposPcnto AS TPC ON CN.IdTipoPre=TPC.IdTipoPre
 LEFT JOIN Localidades AS LD ON CN.CdLocCont=LD.IdLocal
 WHERE R.TipDoc=@pmTipDoc AND R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMayTiposPoliza] @pmIdTipoPol VARCHAR(4),@pmTipoPoliza VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO MayTiposPoliza (IdTipoPol,TipoPoliza,Inactivo)
 VALUES (@pmIdTipoPol,@pmTipoPoliza,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMayContratos_Cr]  @pmNContratoIni INT=Null,@pmNContratoFin INT=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT C.NContrato AS NumContrato,C.IdCia AS CdCia,Compania,NumInterno,Fecha,FechaInicio,ActFechaIni,FechaFirma,IdCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS CdAgencia,Agencia
,ClaseContrato,C.IdTipo AS CdTipo,TipContrato,DescTipoCont,C.IdBonif AS CdBonif,TipoBonif,ConvBonif,CuotaFija,CondPago,C.DiasGracia AS Dias_Gracia
,ModPlazo,C.Plazo AS PlazoCont,FechaVence,VrCuota,ModCuota,CuotaMin,CuotaBase,GraciaBonif,C.IdBandera AS CdBandera,TipoBandera,CdZonaCont,TZN.TipoZona AS TipoZonaCont
,C.IdVend AS CdVend,V.RazonSocial AS Vendedor,AprobJunta,FechaAprob,ObservAprob,CdAprobNeg,TA.TipoAprob AS Tipo_Aprob,ArchivoAprob,C.Observacion AS Observ
,GarantHipoteca,GarantPrenda,GarantPoliza,GarantBanco,GarantPagare,GarantOtro,CdTipoBien,TipoBien,VrAvaludo,VrAvaComerc,NroEscritura,FecRegEscritura,NotariaEsc,DescHipotec
,CdTipoPoliza,TipoPoliza,DescPoliza,VigencPoliza,FecVencPoliza,ValorPoliza,CdTipPrenda,TipoPrenda,VrAvaPrenda,PolizaPrenda,FecVigPrenda,VrAsegPrenda,EdoGarantias,DescGarantOtro
,VolContrac,C.VolContMes AS VlmContMes,C.VolContTotal AS VlmContTotal,C.VolViabMes AS VlmViabMes,C.VolViabTotal AS VlmViabTotal,InverContab,InverContrac,C.InvCtbObra AS InvCntbObra,C.InvCtbCapital AS InvCntbCapital
,C.InvCtbEfectivo AS InvCntbEfe,C.InvCtbImagen AS InvCntbImagen,C.InvCtbMntmto AS InvCntbMnto,C.InvContObra AS InvConObra,C.InvContCapital AS InvConCapital,C.InvContEfectivo AS InvConEfe
,C.InvContImagen AS InvConImagen,C.FecDesemb AS FechaDesemb,C.CrtlAprovInv AS CntrolAprobInv,C.CrtlAprovImg AS CntrolAprobImagen,RentabInicial,ModRecursos,VrInversion,VrInventario,InvCombus
,ContVigente,ContOperando,C.IdEstado AS CdEstado,Estado,NColor,CdTipoBloq,TBO.TipoBloqueo AS ConTipoBloq,EdoDocmtos,C.DescEdoDoc AS ConDescEstdo,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS CdUsuario,Usuario
--datos de bonificaciones
,Item,TipoProd,VrInicial,VrFinal,Unidad,Tarifa,TipoTarifa,Periodo,CdProducto,DescripProd,CdSubgrupo,Subgrupo,TipoMargen,IntvExcluido,IntvGlobal
--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
,CLI.FecUpCupo AS FechaActCupo,Cmntario1,Cmntario2,Cmntario3,CLI.IdPlazo AS CliCdPlazo,PZ.Plazo AS PlazoCli,CLI.CdPlazoComb AS CdPlazoComb,PZC.Plazo AS PlazoComb,CdTipBloq,TBC.TipoBloqueo AS CliTipoBloq
,CLI.DescEdoDoc AS CliDescEdoDoc,EdoRadicaDoc,FecPlazoDoc
--agencias
,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.IdPlazo AS AgeCdPlazo,PZA.Plazo AS AgePlazo,A.CdPlazoComb AS AgeCdPlazoComb,PZAC.Plazo AS AgePlazoComb,VrCupoCre,A.FecUpCupo AS AgeFecActCupo,CodSicom
,A.FecIngreso AS AgeFecIngreso,Comentarios,A.TipoZona AS AgeTipoZona,CdTipAgenc,TipoAgencia,CdSubAgenc,SubTipoAgenc,CdGrupAgenc,GrupoAgenc
,CdSubGrupAgenc,SubGrupoAgenc,CdFntePago,FuentePago,PlazoSobtasa,DocFinanc,FecVigCond,TasaIntArranq
 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 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 T ON C.IdCliente=T.IdTercero INNER JOIN TercCliente AS CLI ON C.IdCliente=CLI.IdClie
 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  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 Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Plazos AS PZ ON CLI.IdPlazo=PZ.IdPlazo
 INNER JOIN Trn_MayBonif AS MB ON C.NContrato=MB.NContrato AND C.IdCia=MB.IdCia
 LEFT JOIN  TiposBloq AS TBO ON C.CdTipoBloq=TBO.IdTipBloq LEFT JOIN MayTiposAprob AS TA ON C.CdAprobNeg=TA.IdAprob
 LEFT JOIN MayTiposBienes AS TBN ON C.CdTipoBien=TBN.IdTipBien LEFT JOIN MayTiposPoliza AS TPZ ON C.CdTipoPoliza=TPZ.IdTipoPol
 LEFT JOIN TiposPrend AS TPD ON C.CdTipPrenda=TPD.IdPrend LEFT JOIN TiposZona AS TZN ON C.CdZonaCont=TZN.IdTipZona
 LEFT JOIN Plazos AS PZC ON CLI.CdPlazoComb=PZC.IdPlazo LEFT JOIN TiposBloq AS TBC ON CLI.CdTipBloq=TBC.IdTipBloq
 LEFT JOIN  Agencias   AS A ON C.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Plazos AS PZA ON A.IdPlazo=PZA.IdPlazo LEFT JOIN Plazos AS PZAC ON A.CdPlazoComb=PZAC.IdPlazo
 LEFT JOIN TiposAgenc AS TAG ON A.CdTipAgenc=TAG.IdTipAgenc LEFT JOIN TiposComAgenc AS STA ON A.CdSubAgenc=STA.IdSubAgenc
 LEFT JOIN GruposAgenc AS GA ON A.CdGrupAgenc=GA.IdGrupo LEFT JOIN SubGruposAgenc AS SGA ON A.CdSubGrupAgenc=SGA.IdSubAgenc
 LEFT JOIN FuentesPago AS FP ON A.CdFntePago=FP.IdFuente 
 LEFT JOIN SubGrupos AS SG ON MB.CdSubgrupo=SG.IdSubgrupo  LEFT JOIN ProdMcias AS PM ON MB.CdProducto=PM.IdProducto
 WHERE   C.NContrato BETWEEN ISNULL(@pmNContratoIni,0) AND ISNULL(@pmNContratoFin,2147483647)
 AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY C.NContrato
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMayTiposPoliza] @pmIdTipoPol VARCHAR(4)
 AS 
 DELETE FROM MayTiposPoliza
 WHERE IdTipoPol=@pmIdTipoPol
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMayTiposPoliza] @pmIdTipoPol VARCHAR(4),@pmTipoPoliza VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE MayTiposPoliza SET TipoPoliza=@pmTipoPoliza,Inactivo=@pmInactivo
 WHERE IdTipoPol=@pmIdTipoPol
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraOstCum] @pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
AS 
 INSERT INTO tm_TraOst (tmNumero,tmItem,tmIdMercancia,tmDescripcion,tmCantidad,tmUndMed,tmVrUnitario,tmTarifPago,tmCantCump,tmTarifaCump,tmPagoCump,tmDocCliente)
 SELECT @pmtmNumero,Item,IdMercancia,Descripcion,Cantidad,UndMed,VrUnitario,TarifaPago,Cantidad,VrUnitario,TarifaPago,DocCliente
 FROM Trn_TraOrdenCumDet  
 WHERE TipDoc=@pmTipDoc AND Cumplido=@pmCumplido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraOstSel] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
AS 
 INSERT INTO tm_TraOst (tmNumero,tmItem,tmIdMercancia,tmDescripcion,tmCantidad,tmUndMed,tmVrUnitario,tmTarifPago,tmCantCump,tmTarifaCump,tmPagoCump,tmDocCliente)
 SELECT @pmtmNumero,Item,IdMercancia,Descripcion,Cantidad,UndMed,VrUnitario,TarifaPago,CantCump,TarifaCump,PagoCump,DocCliente
 FROM  Trn_TraOrdenDet
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraOst] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdMercancia VARCHAR(16),@pmtmDescripcion VARCHAR(250),@pmtmCantidad DECIMAL(14,4)
,@pmtmUndMed VARCHAR(10),@pmtmVrUnitario MONEY,@pmtmCantCump DECIMAL(14,4),@pmtmTarifaCump MONEY,@pmtmDocCliente VARCHAR(30),@pmtmTarifPago MONEY,@pmtmPagoCump MONEY
 AS 
 INSERT INTO tm_TraOst (tmNumero,tmItem,tmIdMercancia,tmDescripcion,tmCantidad,tmUndMed,tmVrUnitario,tmTarifPago,tmCantCump,tmTarifaCump,tmPagoCump,tmDocCliente)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdMercancia,@pmtmDescripcion,@pmtmCantidad,@pmtmUndMed,@pmtmVrUnitario,@pmtmTarifPago,@pmtmCantCump,@pmtmTarifaCump,@pmtmPagoCump,@pmtmDocCliente)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmConcTrib,tmTarifTrib,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut,tmCuentaNiif)
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,tmConcTrib,tmTarifTrib,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut,''
FROM  tm_Compau 
WHERE  tmNumero=@pmtmNumero AND tmTipCom=@pmtmTipCom AND tmComprobante=@pmtmComprobante  AND tmIdCia=@pmtmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_CompID] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITEM
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,tmConcTrib,tmTarifTrib,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut,tmCuentaNiif)
 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,CdConcTrib,CdTarifTrib,NumEstablec,PtoEmision,Num_Autoriza,FechAutoriza,CdCuentaNiif
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmConcTrib,tmTarifTrib,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut,tmCuentaNiif)
SELECT  @pmtmNumero,Item,D.IdCuenta,Detalle,VrDebito,VrCredito,NitTercero,pVehiculo,CdCCosto,CdSubCos,VrBase,TarifaBase,TipDoc,Recibo,IdCia,IdConcepto,TipFac,Factura,IdCiaFac,CASE WHEN LEN(ISNULL(D.TipoImp,''))>0 AND ISNULL(D.TipoImp,'')<>'0' THEN D.ItemBase ELSE D.ItemFac END,FecVence
,CodCta,NumCheque,1,TipoAplica,ISNULL(D.DocSop,0),IdConcepto,NitOtros,CodAgencia,0,Referncia,0,SUBSTRING(NomCuenta,1,100),SUBSTRING(CCosto,1,100),SUBSTRING(RazonSocial,1,150),CASE WHEN ISNULL(D.DocSop,0)=1 THEN D.TipSop ELSE D.TipDocRef END
,CASE WHEN ISNULL(D.DocSop,0)=1 THEN D.NumSoporte ELSE D.DocRef END,CASE WHEN ISNULL(D.DocSop,0)=1 THEN D.IdCiaSop ELSE D.IdCiaRef END,D.TipoImp,D.CodTarImp,'','','',Null,P.IdCueNiif
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmConsolida,tmTipDocRef,tmDocRef,tmIdCiaRef,tmConcTrib,tmTarifTrib
,tmNombre,tmNitOtros,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmConcTrib,tmTarifTrib,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut,tmCuentaNiif
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,tmConcTrib,tmTarifTrib,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut)
 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
,tmConcTrib,tmTarifTrib,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_CompLta] @pmtmNumero VARCHAR(5) 
 AS 
 SELECT tmItem,tmIdCuenta,NomCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmTercero,tmVrBase,tmTarifaBase
,tmIdCCosto,CCosto,tmIdSubCos,SubCosto,tmIdVehiculo,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence
,tmTipDoc,tmDocumento,tmIdCiaDoc,tmCodConce,tmReferncia,tmNitOtros,RazonSocial,tmCuentaNiif
 FROM tm_Comp AS D LEFT JOIN Puc AS P ON D.tmIdCuenta=P.IdCuenta
 LEFT JOIN Terceros AS T ON D.tmNitOtros=T.IdTercero
 LEFT JOIN CentroCosto AS O ON D.tmIdCCosto=O.IdCCosto LEFT JOIN SubCentros AS SC ON D.tmIdSubCos=SC.IdSubCos
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmConcTrib,tmTarifTrib,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut,tmCuentaNiif
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Comp_Niif] @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
,tmConcTrib,tmTarifTrib,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut,tmCuentaNiif)
 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,CdConcTrib,CdTarifTrib,NumEstablec,PtoEmision,Num_Autoriza,FechAutoriza,D.IdCuenta
 FROM Trn_NiifDetalle AS D INNER JOIN PucNiif 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@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),@pmtmConcTrib VARCHAR(4),@pmtmTarifTrib VARCHAR(4)
,@pmtmEstablec VARCHAR(20),@pmtmPtoEmision VARCHAR(20),@pmtmAutoriza VARCHAR(50),@pmtmFechAut SMALLDATETIME,@pmtmCuentaNiif 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,tmConcTrib,tmTarifTrib,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut,tmCuentaNiif)
 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,@pmtmConcTrib,@pmtmTarifTrib,@pmtmEstablec,@pmtmPtoEmision,@pmtmAutoriza,@pmtmFechAut,@pmtmCuentaNiif)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_EstClientes] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_EstClientes WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelBodUbic] @pmIdUbic VARCHAR(10)
 AS 
 DELETE FROM BodUbic
 WHERE IdUbic=@pmIdUbic
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryBodUbicCan] @pmIdBodega VARCHAR(4)
 AS 
 SELECT COUNT(IdUbic) AS SCANT 
 FROM BodUbic
 WHERE  Inactivo=0 AND IdBodega=@pmIdBodega AND IdUbic<>'0'
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelMayTipos] @pmIdTipo VARCHAR(4)
 AS 
 DELETE FROM MayTipos
 WHERE IdTipo=@pmIdTipo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMayLiqTotalesLta] @pmNLiquida INT,@pmIdCia CHAR(2)
 AS 
 SELECT T.NContrato AS NumContrato,IdCiaCon,T.IdCliente AS NitCliente,TC.RazonSocial AS NombreCliente,T.IdAgencia AS Id_Agencia,Agencia
,Unidades,VrBonif,BaseIvaBonif,VrIvaBonif,VrBonifNeto,VrAbono,T.DiasGracia AS Dias_Gracia,L.Fecha AS FechaLiq,Item,LogLiquida
--datos del contrato
,CN.Fecha AS FechaContrato,FechaInicio,FechaVence,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsMayTipos] @pmIdTipo VARCHAR(4),@pmTipContrato VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO MayTipos (IdTipo,TipContrato,Inactivo)
 VALUES (@pmIdTipo,@pmTipContrato,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMayContratosLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdTipo VARCHAR(4)=Null
 AS 
 SELECT C.NContrato AS NumContrato,C.IdCia AS CdCia,Compania,Fecha,FechaInicio,IdCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS CdAgencia,Agencia,ClaseContrato,C.IdTipo AS CdTipo,TipContrato
,DescTipoCont,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,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,ActFechaIni,CdZonaCont,ContVigente,VolContrac,DescHipotec,VrAvaComerc,FecRegEscritura
,NotariaEsc,CdTipPrenda,VrAvaPrenda,PolizaPrenda,FecVigPrenda,VrAsegPrenda,ContOperando,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 IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND C.IdTipo LIKE ISNULL(@pmIdTipo,'%') 
 ORDER BY C.NContrato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMayInventarioLta] @pmNContratoIni INT,@pmNContratoFin 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 BETWEEN @pmNContratoIni AND @pmNContratoFin AND IdCia=@pmIdCia  
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpMayTipos] @pmIdTipo VARCHAR(4),@pmTipContrato VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE MayTipos SET TipContrato=@pmTipContrato,Inactivo=@pmInactivo
 WHERE IdTipo=@pmIdTipo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmImpConEnt,tmImpConSal,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.NContrato AS ClieNumContrato,CiaContMay
,CLI.CdBandera AS CliCdBandera,TB.TipoBandera AS CliBandera,A.CdBandera AS AgeCdBandera,TBA.TipoBandera AS AgeBandera
,VrCupo,DiasPago,A.NContrato AS AgeNContrato,A.CiaCont AS AgeCiaCont,NumInterno,CdFntePago,FuentePago
 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 LEFT JOIN FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente
 LEFT JOIN Trn_MayContratos AS MC ON A.NContrato=MC.NContrato AND A.CiaCont=MC.IdCia
 WHERE V.tmEst=@pmtmEst
 ORDER BY DescripProd,tmIdCia,T.RazonSocial
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmImpConsumo,tmImpGlobal,tmSoldicom,tmVolumen,tmUnidades2,tmVentaNeto2,tmCostoNeto2,tmImpuestos2,tmImpConsumo2
,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.NContrato AS ClieNumContrato,CiaContMay
,CLI.CdBandera AS CliCdBandera,TB.TipoBandera AS CliBandera,A.CdBandera AS AgeCdBandera,TBA.TipoBandera AS AgeBandera,A.NContrato AS AgeNContrato,A.CiaCont AS AgeCiaCont,NumInterno
,CdFntePago,FuentePago
 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 
 LEFT JOIN FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente
 LEFT JOIN Trn_MayContratos AS MC ON A.NContrato=MC.NContrato AND A.CiaCont=MC.IdCia
 WHERE V.tmEst=@pmtmEst
 ORDER BY DescripProd,tmIdCia,T.RazonSocial
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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.NContrato AS ClieNumContrato,CiaContMay
,CLI.CdBandera AS CliCdBandera,TB.TipoBandera AS CliBandera,A.CdBandera AS AgeCdBandera,TBA.TipoBandera AS AgeBandera,A.IdLocal AS AgeCdCiu,LA.Localidad AS AgeNomCiudad
,A.NContrato AS AgeNContrato,A.CiaCont AS AgeCiaCont,NumInterno,CdFntePago,FuentePago
 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 
 LEFT JOIN FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente
 LEFT JOIN Trn_MayContratos AS MC ON A.NContrato=MC.NContrato AND A.CiaCont=MC.IdCia
 WHERE V.tmEst=@pmtmEst
 ORDER BY DescripProd,tmIdCia,T.RazonSocial
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDeveloper]
AS
 SELECT NitCia,Nombre,Direccion,Telefono,Fax,Ciudad,Internet,Email,RepLegal
 FROM Sys_Developer
 ORDER BY NitCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraMovLiquidaOdp] @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdPoseedor VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
 AS 
 INSERT INTO tm_TraMovLiquida (tmEst,tmTipo,tmNumero,tmIdCia,tmItem,tmFecha,tmFecVence,tmIdPoseedor,tmIdVehiculo,tmIdConcepto,tmDescripcion
,tmVrDebito,tmVrCredito,tmVrAbonado,tmTipoVehic,tmVrBase,tmTarifa,tmReferencia,tmTipDocRef,tmDocRef,tmEstado,tmTipoLiq,tmLiquida,tmCiaLiq,tmSaldoAnt,tmSaldoCia,tmSaldoAntVeh,tmSaldoVehCia)
 SELECT @pmtmEst,O.TipDoc,O.OrdPago,O.IdCia,M.Item,O.Fecha,O.Fecha,M.IdPoseedor,M.IdVehiculo,'0','ORDENES DE PAGO'
,0,M.VrNeto,ISNULL(TotAbonado,0),'TERCEROS',M.VrTotalFletes-M.VrDescuento,0,M.Referencia,M.TipMuc,M.Manifiesto,M.EdoLiqCausac,'0',0,'00',0,0,0,0
 FROM Trn_TraOrdenPago AS O INNER JOIN Trn_TraOrdenManif AS M ON O.TipDoc=M.TipDoc AND O.OrdPago=M.OrdPago AND O.IdCia=M.IdCia
 LEFT JOIN (SELECT TipDoc,Documento,IdCiaDoc,SUM(VrAbonado) AS TotAbonado FROM Trn_ComFactura WHERE TipFac='ODP' AND TipDoc='ODP' GROUP BY TipDoc,Documento,IdCiaDoc)
 AS ABO ON O.TipDoc=ABO.TipDoc AND O.OrdPago=ABO.Documento AND O.IdCia=ABO.IdCiaDoc
 WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND M.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%') AND M.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenPago] @pmTipDoc VARCHAR(3),@pmOrdPago INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,OrdPago,IdCia,Fecha,IdConcepto,Modalidad,LiqRemesas,CxPagar,MulPlazos,IdPlazo,FechaVence,TipCom,Comprobante
,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraOrdenPago
 WHERE TipDoc=@pmTipDoc AND OrdPago=@pmOrdPago AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenPagoLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null
 AS 
 SELECT O.OrdPago AS NumOrden,O.IdCia AS CdCia,Compania,Fecha,TipMuc,Manifiesto,IdCiaMuc,M.IdVehiculo AS PlacaVeh,NumVeh,M.IdPropietario AS NitPropietario,T.RazonSocial AS Propietario,M.IdPoseedor AS NitPoseedor,NP.RazonSocial AS Poseedor
,M.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,LiqRemesas,TipoLiq,VrTotalFletes,VrDescuento,VrRetencion,VrReteIca,VrAnticipos,VrFaltantes,VrSeguros,VrFondos
,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,VrNeto,TarifaTabla,TarifaFlete,UnidTarifa,PesoTotal,Unidades,Volumen,PesoOrigen,UnidOrigen,VolOrigen
,PesoDestino,UnidDestino,VolDestino,BaseRet,BaseIca,BaseSeg,BaseImp,TarifaRet,TarifaIca,TarifaSeg,TarifaImp,CxPagar,MulPlazos,O.IdPlazo AS CdPlazo,Plazo,NVmto,FechaVence
,VrConcPagos,VrConcDctos,VrConcFondo,VrConcSeguro,VrConcAporte,VrConcImpuesto,VrRemesas,VrSeguroRc,VrRecCaja,VrReteCREE,TarifaRtc,CodTarRtc,CodTarifAvta,TarifaAvta,VrImpAvTa,VrDctoPago,CodTarifDcpp,CdForma,CdCenCosto,CdSubCenCos
,O.IdConcepto AS CdConcepto,Concepto,Modalidad,TipEgr,Egreso,IdCiaEgr,FechaEgr,NumCheque,Referencia,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,O.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraOrdenPago AS O INNER JOIN Trn_TraOrdenManif AS M ON O.TipDoc=M.TipDoc AND O.OrdPago=M.OrdPago AND O.IdCia=M.IdCia
 INNER JOIN Companias AS CI ON O.IdCia=CI.IdCia INNER JOIN Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo   INNER JOIN Terceros AS T ON M.IdPropietario=T.IdTercero
 INNER JOIN Terceros AS NC ON M.IdConductor=NC.IdTercero  INNER JOIN Terceros AS NP ON M.IdPoseedor=NP.IdTercero 
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 LEFT JOIN Plazos AS PZ ON O.IdPlazo=PZ.IdPlazo
 WHERE O.TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND M.IdVehiculo  LIKE ISNULL(@pmIdVehiculo ,'%') AND M.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 AND  M.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 ORDER BY O.IdCia,O.OrdPago
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraLiquidaFmt] @pmTipDoc VARCHAR(3),@pmLiquidacionIni INT,@pmLiquidacionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT LQ.TipDoc AS TipLiq,TipoDoc,LQ.Liquidacion AS NumLiquida,LQ.IdCia AS CdCia,CN.Compania AS NomCompania,LQ.Fecha AS FechaLiq,LQ.IdConcepto AS CdConcepto,Concepto
,LQ.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,VrTotal,LQ.CxPagar AS CPagar,LQ.TipCom AS CdTipCom,TipoCom,LQ.Comprobante AS NumComp,LQ.IdCiaCom AS CdCiaComp
,LQ.Anulado AS Anuldo,LQ.NumDev AS NDevolucion,LQ.FecDev AS FechDev,LQ.Observacion AS Observ,LQ.IdEstado AS CdEstado,Estado,FecIniCau,FecFinCau,FecIniOdp,FecFinOdp,ModLiquida
,LQ.TimeSys AS FechaAdd,LQ.FecUpdate AS FechaAct,LQ.IdCiaCrea AS CdCiaCrea,LQ.IdUsuario AS CdUsuario,Usuario,Leyenda
--datos de ordenes de pago
,LP.TipOdp AS TipOrden,LP.OrdPago AS NumOrden,LP.IdCiaOdp AS CdCiaOdp,COP.Compania AS NomCiaOrden,OP.Fecha AS FechaOdp,OP.TipMuc AS TipManif,OP.Manifiesto AS NumManif,OP.IdCiaMuc AS CdCiaManif
,OP.IdVehiculo AS PlacaVeh,OP.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,OP.VrTotalFletes,OP.VrDescuento AS Vr_Dcto,OP.VrRetencion AS Vr_RetFte,OP.VrReteIca AS Vr_RetIca,OP.VrAnticipos,OP.VrFaltantes,OP.VrSeguros
,OP.VrFondos,OP.VrAportes,OP.VrOtrosDctos,OP.VrImpuestos,OP.VrEstampilla,OP.VrOtrosPagos,OP.VrNeto AS Vr_Neto,VrAbonado,TotAbonado,OP.TarifaTabla,OP.TarifaFlete AS Tarifa_Flete,OP.UnidTarifa,OP.PesoTotal AS Peso_Total
,OP.Unidades,OP.Volumen AS VolmTotal,OP.PesoOrigen,OP.UnidOrigen,OP.VolOrigen,OP.PesoDestino,OP.UnidDestino,OP.VolDestino,OP.BaseRet,OP.BaseIca,OP.BaseSeg,OP.BaseImp,OP.TarifaRet,OP.TarifaIca,OP.TarifaSeg,OP.TarifaImp,OP.NumCheque,OP.Referencia AS Referncia,OP.EdoLiqCausac
,OP.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,OP.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,OP.IdRuta AS CdRuta,R.Ruta AS DescRuta,OP.nRemolque
--datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,OP.FecManif,VrApos,VrFam,VrFsc,VrFgi,VrTarApos ,VrTarFam ,VrTarFGi,VrTarFsc,VrBaseApos ,VrBaseFam,VrBaseFgi,VrBaseFsc,TPV.NumCuenta AS NumCtaProveedor,CTA.ClaseCuenta
,TPV.NitContac,TPV.NomContac,BA.Banco 
 FROM Trn_TraLiquida AS LQ INNER JOIN Companias AS CN ON LQ.IdCia=CN.IdCia INNER JOIN Sys_TiposDoc AS TD ON LQ.TipDoc=TD.IdDoc
 INNER JOIN EstadoDoc AS ED ON LQ.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON LQ.IdUsuario=U.IdUsuario 
 INNER JOIN Conceptos AS C ON LQ.IdConcepto=C.IdConcepto INNER JOIN Terceros AS T ON LQ.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Trn_TraLiquidaOdp AS LP ON LQ.TipDoc=LP.TipDoc AND LQ.Liquidacion=LP.Liquidacion AND LQ.IdCia=LP.IdCia
--* SubConsulta: Ordenes de pago/ Liquidaciones
 INNER JOIN (SELECT OP.TipDoc,OP.OrdPago,OP.IdCia,OP.Fecha,OP.IdConcepto,OP.Modalidad,OP.Anulado,OP.Observacion,O.TipMuc,O.Manifiesto,O.IdCiaMuc
,O.IdVehiculo,O.IdConductor,O.IdPropietario,O.IdPoseedor,O.VrTotalFletes,O.VrDescuento,O.VrRetencion,O.VrReteIca,O.VrAnticipos,O.VrFaltantes,O.VrSeguros,O.VrFondos,O.VrAportes,O.VrReteCREE
,O.VrOtrosDctos,O.VrImpuestos,O.VrEstampilla,O.VrOtrosPagos,O.VrNeto,O.TarifaTabla,O.TarifaFlete,O.UnidTarifa,O.PesoTotal,O.Unidades,O.Volumen,O.PesoOrigen,O.UnidOrigen,O.VolOrigen,O.PesoDestino,O.UnidDestino,O.VolDestino
,O.BaseRet,O.BaseIca,O.BaseSeg,O.BaseImp,O.TarifaRet,O.TarifaIca,O.TarifaSeg,O.TarifaImp,O.VrConcPagos,O.VrConcDctos,O.VrConcFondo,O.VrConcSeguro,O.VrConcAporte,O.VrConcImpuesto,O.VrImpAvTa
,O.TipoLiq,O.TipEgr,O.Egreso,O.IdCiaEgr,O.FechaEgr,O.NumCheque,O.Referencia,O.CdCenCosto,O.CdSubCenCos,O.EdoLiqCausac
,M.Fecha AS FecManif,M.FecDespacho,M.IdOrigen,M.IdDestino,M.IdRuta,M.nRemolque,M.TipoAfiVehic,M.Remesa,M.IdCiaRem,M.Cumplido,M.IdCiaCump,M.FechaCump,M.EstCumplido
 FROM Trn_TraOrdenPago AS OP INNER JOIN Trn_TraOrdenManif AS O ON OP.TipDoc=O.TipDoc AND OP.OrdPago=O.OrdPago AND OP.IdCia=O.IdCia
 INNER JOIN Trn_TraManifiesto AS M ON O.TipMuc=M.TipDoc AND O.Manifiesto=M.Manifiesto AND O.IdCiaMuc=M.IdCia
 --Datos de Liq. ordenes de servico
 UNION ALL SELECT L.TipDoc,L.Liquidacion,L.IdCia,L.Fecha,L.IdConcepto,O.Modalidad,L.Anulado,L.Observacion,L.TipOds,L.NumOrden,L.IdCiaOds
,L.IdVehiculo,L.IdConductor,L.IdPoseedor,L.IdPoseedor,L.VrTotal,0,L.VrRetencion,L.VrReteIca,L.VrAnticipo,0,0,0,0,L.VrReteCREE,L.VrDescuento,L.VrIva,0,L.VrOtros,L.VrNeto,L.TarifaPago,L.TarifaPago,'UNIDAD',0,L.Cantidad,0,0,0,0,0,0,0,L.BaseRet,L.BaseIca,0,L.BaseImp
,L.TarifaRet,L.TarifaIca,0,L.TarifaImp,L.VrConcCargos,L.VrConcDed,0,0,0,0,0,0,L.TipEgr,L.Egreso,L.IdCiaEgr,Null,L.NumCheque,L.Referencia,L.CdCCosto,L.CdSubCos,0,O.Fecha,O.FecDespacho,O.IdOrigen,O.IdDestino,O.CdRuta,O.nRemolque,O.TipoAfiVehic,O.NumRemesa
,O.CdCiaRem,O.NumCump,O.CdCiaCum,Null,O.EstCumplido
 FROM Trn_TraOrdenLiq AS L INNER JOIN Trn_TraOrdenServ AS O ON L.TipOds=O.TipDoc AND L.NumOrden=O.NumOrden AND L.IdCiaOds=O.IdCia WHERE L.TipDoc='OPT') AS OP ON LP.TipOdp=OP.TipDoc AND LP.OrdPago=OP.OrdPago AND LP.IdCiaOdp=OP.IdCia
--* Fin consulta liquidaciones
 INNER JOIN Vehiculos AS V ON OP.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 INNER JOIN Terceros AS CDT ON OP.IdConductor=CDT.IdTercero
 INNER JOIN Companias AS COP ON LP.IdCiaOdp=COP.IdCia
 INNER JOIN Localidades AS CO ON OP.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON OP.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON OP.IdRuta=R.IdRuta
 LEFT JOIN Terceros AS NE ON V.NitEmpresa=NE.IdTercero LEFT JOIN TiposCom AS TCM ON LQ.TipCom=TCM.IdCom 
 LEFT JOIN (SELECT TipDoc,Documento,IdCiaDoc,SUM(VrAbonado) AS TotAbonado FROM Trn_ComFactura WHERE (TipFac='ODP' OR TipFac='OPT') AND (TipDoc='ODP' OR TipDoc='OPT') GROUP BY TipDoc,Documento,IdCiaDoc)
 AS ABO ON LP.TipOdp=ABO.TipDoc AND LP.OrdPago=ABO.Documento AND LP.IdCiaOdp=ABO.IdCiaDoc
 --fondos-conceptos
 LEFT JOIN (SELECT TipDoc,Documento,IdCia,SUM(CASE WHEN Concepto='FLE' AND Factura='FGI' THEN Valor ELSE 0 END ) AS VrFgi,MAX(CASE WHEN Concepto='FLE' AND Factura='FGI' THEN Vrtarifa ELSE 0 END ) as VrTarFGi,MAX(CASE WHEN Concepto='FLE' AND Factura='FGI' THEN VrBase ELSE 0 END ) as VrBaseFgi
 ,SUM(CASE WHEN Concepto='FLE' AND Factura='FAM' THEN Valor ELSE 0 END ) AS VrFam,MAX(CASE WHEN Concepto='FLE' AND Factura='FAM' THEN Vrtarifa ELSE 0 END ) as VrTarFam,MAX(CASE WHEN Concepto='FLE' AND Factura='FAM' THEN VrBase ELSE 0 END ) as VrBaseFam,
 SUM(CASE WHEN Concepto='FLE' AND Factura='FSC' THEN Valor ELSE 0 END ) AS VrFsc, MAX(CASE WHEN Concepto='FLE' AND Factura='FSC' THEN Vrtarifa ELSE 0 END ) as VrTarFsc,MAX(CASE WHEN Concepto='FLE' AND Factura='FSC' THEN VrBase ELSE 0 END ) as VrBaseFsc
,SUM(CASE WHEN Concepto='OTR' AND Factura='APOS' THEN Valor ELSE 0 END) AS VrApos,MAX(CASE WHEN Concepto='OTR' AND Factura='APOS' THEN Vrtarifa ELSE 0 END ) as VrTarApos,MAX(CASE WHEN Concepto='OTR' AND Factura='APOS' THEN VrBase ELSE 0 END ) as VrBaseApos
 FROM Trn_DetCuentas WHERE (TipDoc='ODP' OR TipDoc='OPT') GROUP BY TipDoc,Documento,IdCia) AS FCN ON LP.TipOdp=FCN.TipDoc AND LP.OrdPago=FCN.Documento AND LP.IdCiaOdp=FCN.IdCia
 LEFT JOIN TercProvee TPV ON LQ.IdPoseedor=TPV.IdProv  LEFT JOIN ClaseCta CTA ON TPV.IdClase=CTA.IdClase 
 LEFT JOIN Bancos BA ON TPV.IdBanco=BA.IdBanco 
 WHERE LQ.TipDoc=@pmTipDoc AND LQ.Liquidacion BETWEEN @pmLiquidacionIni AND @pmLiquidacionFin AND LQ.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraOrdenPago]  @pmTipDoc VARCHAR(3),@pmOrdPago INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmModalidad VARCHAR(10),@pmLiqRemesas BIT,@pmCxPagar BIT,@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmFechaVence SMALLDATETIME
,@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_TraOrdenPago (TipDoc,OrdPago,IdCia,Fecha,IdConcepto,Modalidad,LiqRemesas,CxPagar,MulPlazos,IdPlazo,FechaVence,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmOrdPago,@pmIdCia,@pmFecha,@pmIdConcepto,@pmModalidad,@pmLiqRemesas,@pmCxPagar,@pmMulPlazos,@pmIdPlazo,@pmFechaVence,@pmTipCom,@pmComprobante,@pmIdCiaCom
,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenPagoFmt] @pmTipDoc VARCHAR(3),@pmOrdPagoIni INT,@pmOrdPagoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT OP.TipDoc AS TipoOdp,TipoDoc,OP.OrdPago AS NumOdp,OP.IdCia AS CdCia,Compania,OP.Fecha AS FechaOdp,OP.IdConcepto AS CdConcepto,Concepto,Modalidad,LiqRemesas,CxPagar,MulPlazos,OP.IdPlazo AS CdPlazo,Plazo,DiasPago,FechaVence
,OP.TipCom AS TipComp,TipoCom,OP.Comprobante AS NumComp,OP.IdCiaCom AS CdCiaComp,OP.Anulado AS Anuldo,NumDev,OP.FecDev AS FechaDev,OP.Observacion AS Observ,OP.IdEstado AS CdEstado,Estado,OP.TimeSys AS FechaCrea,OP.FecUpdate AS FechaAct,OP.IdCiaCrea AS CdCiaCrea,OP.IdUsuario AS CdUsuario,Usuario,Leyenda
,O.TipMuc AS TipManif,O.Manifiesto AS NumManif,O.IdCiaMuc AS CdCiaManif,O.IdVehiculo AS PlacaVeh,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,O.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,O.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,VrTotalFletes,O.VrDescuento AS Vr_Dcto,O.VrRetencion AS Vr_RetFte,O.VrReteIca AS Vr_RetIca,VrAnticipos,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,O.VrNeto AS Vr_Neto
,TarifaTabla,O.TarifaFlete AS Tarifa_Flete,UnidTarifa,O.PesoTotal AS Peso_Total,Unidades,O.Volumen AS VolmTotal,PesoOrigen,UnidOrigen,VolOrigen,PesoDestino,UnidDestino,VolDestino,O.BaseRet,O.BaseIca,BaseSeg,BaseImp,O.TarifaRet,O.TarifaIca,TarifaSeg,TarifaImp
,TipoLiq,TipEgr,Egreso,IdCiaEgr,FechaEgr,NumCheque,Referencia,CantFaltante,UnidadFalt,TolFaltNeto,VrConcPagos,VrConcDctos,VrConcFondo,VrConcSeguro,VrConcAporte,VrConcImpuesto,EdoLiqCausac,VrRemesas,VrSeguroRc,VrRecCaja,VrReteCREE,TarifaRtc,CodTarRtc,CdForma
,M.Fecha AS FecManif,FecDespacho,FecEntrega,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,nRemolque,TipoAfiVehic
,IdLocFletes,CF.Localidad AS LugarFletes,M.Observacion AS MucObserv,CdCenCosto,CdSubCenCos,CodTarifAvta,TarifaAvta,VrImpAvTa,O.VrDctoPago,O.CodTarifDcpp
,TipRem,ORM.Remesa AS NumRemesa,ORM.IdCiaRem AS CdCiaRem,ItemRem,ORM.Cantidad AS Cant,PesoNeto,ORM.UndMed AS CdUMed,UMP.Unidad AS UmPeso,ORM.Volumen AS Volmen,UndVol,Cases,Cajas,Palets,TarifPago,TarifTabla,UndTarifa,Detalle
--Datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,NumLiquida
 FROM Trn_TraOrdenPago AS OP INNER JOIN Trn_TraOrdenManif AS O ON OP.TipDoc=O.TipDoc AND OP.OrdPago=O.OrdPago AND OP.IdCia=O.IdCia
 INNER JOIN Trn_TraManifiesto AS M ON O.TipMuc=M.TipDoc AND O.Manifiesto=M.Manifiesto AND O.IdCiaMuc=M.IdCia
 INNER JOIN Companias AS CN ON OP.IdCia=CN.IdCia INNER JOIN Sys_TiposDoc AS TD ON OP.TipDoc=TD.IdDoc
 INNER JOIN EstadoDoc AS ED ON OP.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON OP.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON OP.IdConcepto=C.IdConcepto INNER JOIN Plazos AS PZ ON OP.IdPlazo=PZ.IdPlazo
 INNER JOIN Terceros AS  CDT ON O.IdConductor=CDT.IdTercero INNER JOIN Terceros AS T ON O.IdPoseedor=T.IdTercero
 INNER JOIN Terceros AS NP ON O.IdPropietario=NP.IdTercero  INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta    INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN Trn_TraOrdenRemesas AS ORM ON O.TipDoc=ORM.TipDoc AND   O.OrdPago=ORM.OrdPago AND  O.IdCia=ORM.IdCia 
 AND  O.TipMuc=ORM.TipMuc AND  O.Manifiesto=ORM.Manifiesto AND  O.IdCiaMuc=ORM.IdCiaMuc
 LEFT JOIN Sys_Um AS UMP ON ORM.UndMed=UMP.UndMed  LEFT JOIN TiposCom AS TCM ON OP.TipCom=TCM.IdCom 
 LEFT JOIN (SELECT TipOdp,OrdPago,IdCiaOdp,MAX(Liquidacion) AS NumLiquida,SUM(VrAbonado) AS TotalAbonos FROM Trn_TraLiquidaOdp GROUP BY TipOdp,OrdPago,IdCiaOdp)
 AS LQ ON OP.TipDoc=LQ.TipOdp AND OP.OrdPago=LQ.OrdPago AND OP.IdCia=LQ.IdCiaOdp
 WHERE OP.TipDoc=@pmTipDoc AND OP.OrdPago BETWEEN @pmOrdPagoIni AND @pmOrdPagoFin AND OP.IdCia=@pmIdCia
 ORDER BY OP.OrdPago
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenPagoNet] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
SELECT OP.TipDoc AS CdTipoDoc,TipoDoc,OP.OrdPago AS NumOrden,OP.IdCia AS CdCia,Compania,OP.Fecha AS FecOrden,OP.IdConcepto AS CdConcepto,Concepto,O.TipMuc AS TipManif,O.Manifiesto AS NumManif,O.IdCiaMuc AS CdCiaManif
,O.IdVehiculo AS PlacaVeh,NumVeh,O.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,O.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,VrTotalFletes,O.VrDescuento AS Vr_Dcto,O.VrRetencion AS Vr_RetFte,O.VrReteIca AS Vr_RetIca,VrReteCREE,VrAnticipos,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,O.VrNeto AS Vr_Neto
,TarifaTabla,O.TarifaFlete AS Tarifa_Flete,UnidTarifa,O.PesoTotal AS Peso_Total,Unidades,O.Volumen AS VolmTotal,PesoOrigen,UnidOrigen,VolOrigen,PesoDestino,UnidDestino,VolDestino,O.BaseRet,O.BaseIca,BaseSeg,BaseImp,O.TarifaRet,O.TarifaIca,TarifaSeg,TarifaImp,TarifaRtc
,CxPagar,MulPlazos,OP.IdPlazo AS CdPlazo,Plazo,DiasPago,FechaVence,OP.TipCom AS TipComp,TipoCom,OP.Comprobante AS NumComp,OP.IdCiaCom AS CdCiaCom,OP.Anulado AS Anuldo,NumDev,OP.FecDev AS FechaDev,OP.Observacion AS Observ,OP.IdCiaCrea AS CdCiaCrea,OP.IdUsuario AS CdUsuario,Usuario
,TipoLiq,TipEgr,Egreso,IdCiaEgr,NumCheque,Referencia,CantFaltante,UnidadFalt,TolFaltNeto,VrConcPagos,VrConcDctos,VrConcFondo,VrConcSeguro,VrConcAporte,VrConcImpuesto,EdoLiqCausac,VrRemesas,VrSeguroRc,VrRecCaja,TarifaAvta,VrImpAvTa
,M.Fecha AS FecManif,FecDespacho,FecEntrega,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,nRemolque,TipoAfiVehic,IdLocFletes,CF.Localidad AS LugarFletes,M.Observacion AS MucObserv
,ISNULL(EGR.Vrabono,0) AS VrEgreso,ISNULL(FechaEgr,EGR.EGRFEC) AS FechaEgr,ISNULL(VC.VrTotalAbo,0) AS CxpAbonado,ISNULL(VC.VrSaldo,0) AS CxpSaldo
--Datos del vehiculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Direccion AS DirPoseedor,T.Telefono AS TelPoseedor,T.TelMovil AS TelMovilPosee,T.e_mail AS emailPoseedor
,TIC.CdLocdad AS IdIcaOdp,LIC.Localidad AS CiudadICaOdp,CDTR.IdLugar AS IdLugarLic,LLC.Localidad AS LugarLicencia
,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,Modelo,Config,TipoAfil,V.Descripcion AS VehDescripcion
 FROM Trn_TraOrdenPago AS OP INNER JOIN Trn_TraOrdenManif AS O ON OP.TipDoc=O.TipDoc AND OP.OrdPago=O.OrdPago AND OP.IdCia=O.IdCia
 INNER JOIN Trn_TraManifiesto AS M ON O.TipMuc=M.TipDoc AND O.Manifiesto=M.Manifiesto AND O.IdCiaMuc=M.IdCia
 INNER JOIN Companias AS CN ON OP.IdCia=CN.IdCia  INNER JOIN adm_Usuarios AS U ON OP.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON OP.IdConcepto=C.IdConcepto INNER JOIN Sys_TiposDoc AS TD ON OP.TipDoc=TD.IdDoc
 INNER JOIN Plazos AS PZ ON OP.IdPlazo=PZ.IdPlazo
 INNER JOIN Terceros AS  CDT ON O.IdConductor=CDT.IdTercero INNER JOIN Terceros AS T ON O.IdPoseedor=T.IdTercero
 INNER JOIN Terceros AS NP ON O.IdPropietario=NP.IdTercero  INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca  LEFT JOIN TiposCom AS TCM ON OP.TipCom=TCM.IdCom 
 LEFT JOIN TercCndtores AS CDTR ON O.IdConductor =CDTR.IdConductor LEFT JOIN Localidades AS LLC ON CDTR.IdLugar=LLC.IdLocal 
 LEFT JOIN Tablapor AS TIC ON O.CodTarIca=TIC.IdTarifa  LEFT JOIN Localidades LIC ON TIC.CdLocdad=LIC.IdLocal 
 LEFT JOIN ( SELECT TipOdp,OrdPago,IdCiaOdp,SUM(VrAbonado) AS Vrabono,MAX(Trn_Comprobantes.Fecha) AS EGRFEC FROM Trn_TraEgrOrden 
 LEFT JOIN Trn_Comprobantes ON Trn_TraEgrOrden.TipCom =Trn_Comprobantes.TipCom AND Trn_TraEgrOrden.NumEgreso =Trn_Comprobantes.Comprobante
 AND Trn_TraEgrOrden.IdCia =Trn_Comprobantes.IdCia  GROUP BY TipOdp,OrdPago,IdCiaOdp) AS EGR
 ON OP.TipDoc =EGR.TipOdp AND OP.OrdPago =EGR.OrdPago AND OP.IdCia =EGR.IdCiaOdp 
 --subconsulta abonos cuentas por pagar
 LEFT JOIN (SELECT TipFac,Factura,IdCia,IdProveedor,Documento,SUM(VrFactura) AS VrTotalFac,SUM(VrAbonado) AS VrTotalAbo,SUM(VrFactura-VrAbonado) AS VrSaldo FROM Trn_ComFactura
 WHERE TipFac='ODP' AND TipDoc='ODP' AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProveedor LIKE ISNULL(@pmIdPoseedor,'%')
 GROUP BY TipFac,Factura,IdCia,IdProveedor,Documento) AS VC ON OP.TipDoc=VC.TipFac AND OP.OrdPago=VC.Documento AND OP.IdCia=VC.IdCia
 WHERE OP.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND OP.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')  AND O.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
UNION ALL
 SELECT TipDev,TipoDoc,Devolucion,D.IdCia,Compania,D.Fecha,D.IdConcepto,Concepto,O.TipMuc,O.Manifiesto,O.IdCiaMuc,D.IdVehiculo,NumVeh
,O.IdPropietario,NP.RazonSocial,D.IdPoseedor,T.RazonSocial,D.IdConductor,CDT.RazonSocial,VrTotalFletes,O.VrDescuento,O.VrRetencion,O.VrReteIca,VrReteCREE,VrAnticipos
,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,O.VrNeto
,TarifaTabla,O.TarifaFlete,UnidTarifa,O.PesoTotal,Unidades,O.Volumen,PesoOrigen,UnidOrigen,VolOrigen,PesoDestino,UnidDestino,VolDestino,O.BaseRet,O.BaseIca,BaseSeg,BaseImp
,O.TarifaRet,O.TarifaIca,TarifaSeg,TarifaImp,TarifaRtc,D.CxPagar,0,'','','',FecDoc,D.TipCom,TipoCom,D.Comprobante,D.IdCiaCom,CAST(0 AS BIT),0,Null,D.Observacion,D.IdCiaCrea,D.IdUsuario,Usuario
,TipoLiq,D.TipDoc,D.OrdPago,D.IdCiaDoc,'',Referencia,CantFaltante,UnidadFalt,TolFaltNeto,VrConcPagos,VrConcDctos,VrConcFondo,VrConcSeguro,VrConcAporte,VrConcImpuesto,EdoLiqCausac,VrRemesas,VrSeguroRc,VrRecCaja,TarifaAvta,VrImpAvTa
,M.Fecha,FecDespacho,FecEntrega,M.IdOrigen,CO.Localidad,CO.IdDep,DPO.Departamento,M.IdDestino,CD.Localidad,CD.IdDep,DPD.Departamento,M.IdRuta,R.Ruta,nRemolque,TipoAfiVehic,IdLocFletes
,CF.Localidad,M.Observacion,ISNULL(EGR.Vrabono,0),ISNULL(FechaEgr,EGR.EGRFEC),O.VrNeto,0
--Datos del vehiculo
,T.TipoId,T.Dv,T.IdLocal,L.Localidad,T.Direccion,T.Telefono,T.TelMovil,T.e_mail,TIC.CdLocdad,LIC.Localidad,CDTR.IdLugar,LLC.Localidad
,V.IdTipoVeh,TipoVehiculo,V.IdMarca,MV.Marca,Modelo,Config,TipoAfil,V.Descripcion
 FROM Trn_TraDevOdp AS D INNER JOIN Trn_TraOrdenManif AS O ON D.TipDoc=O.TipDoc AND D.OrdPago=O.OrdPago AND D.IdCiaDoc=O.IdCia
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN Terceros AS  CDT ON D.IdConductor=CDT.IdTercero
 INNER JOIN Terceros AS T ON D.IdPoseedor=T.IdTercero INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Terceros AS NP ON O.IdPropietario=NP.IdTercero
 INNER JOIN Trn_TraManifiesto AS M ON O.TipMuc=M.TipDoc AND O.Manifiesto=M.Manifiesto AND O.IdCiaMuc=M.IdCia
 INNER JOIN Localidades AS CO ON M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca  LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom 
 LEFT JOIN TercCndtores AS CDTR ON D.IdConductor=CDTR.IdConductor LEFT JOIN Localidades AS LLC ON CDTR.IdLugar=LLC.IdLocal 
 LEFT JOIN Tablapor AS TIC ON O.CodTarIca=TIC.IdTarifa  LEFT JOIN Localidades LIC ON TIC.CdLocdad=LIC.IdLocal 
 LEFT JOIN ( SELECT TipOdp,OrdPago,IdCiaOdp,SUM(VrAbonado) AS Vrabono,MAX(Trn_Comprobantes.Fecha) AS EGRFEC FROM Trn_TraEgrOrden 
 LEFT JOIN Trn_Comprobantes ON Trn_TraEgrOrden.TipCom =Trn_Comprobantes.TipCom AND Trn_TraEgrOrden.NumEgreso =Trn_Comprobantes.Comprobante
 AND Trn_TraEgrOrden.IdCia=Trn_Comprobantes.IdCia GROUP BY TipOdp,OrdPago,IdCiaOdp) AS EGR
 ON D.TipDoc=EGR.TipOdp AND D.OrdPago=EGR.OrdPago AND D.IdCiaDoc=EGR.IdCiaOdp 
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')  AND D.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraLiquidaRelo] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null
AS 
 SELECT LQ.TipDoc AS TipLiq,LQ.Liquidacion AS NumLiquida,LQ.IdCia AS CdCia,CN.Compania AS NomCompania,LQ.Fecha AS FechaLiq,LQ.IdConcepto AS CdConcepto,Concepto,LQ.IdVehiculo AS PlacaVeh
,LQ.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,LQ.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,VrTotal,LQ.CxPagar AS CuePagar,LQ.TipCom AS CdTipCom,TipoCom,LQ.Comprobante AS NumComprob,LQ.IdCiaCom AS CdCiaCom
,LQ.Anulado AS Anuldo,LQ.NumDev AS NumDevoluc,LQ.FecDev AS FechaDev,LQ.Observacion AS Observ,LQ.IdEstado AS CdEstado,Estado,FecIniCau,FecFinCau,FecIniOdp,FecFinOdp,ModLiquida,LQ.TimeSys AS FechaCrea,LQ.FecUpdate AS FechaAct,LQ.IdCiaCrea AS CdCiaCrea,LQ.IdUsuario AS CdUsuario,Usuario
--datos de la orden
,LP.TipOdp AS TipOrden,LP.OrdPago AS NumOrden,LP.IdCiaOdp AS CdCiaOdp,COP.Compania AS NomCiaOrden,OP.Fecha AS FechaOdp,O.TipMuc AS TipManif,O.Manifiesto AS NumManif,O.IdCiaMuc AS CdCiaManif,VrTotalFletes,O.VrDescuento AS Vr_Dcto
,O.VrRetencion AS Vr_RetFte,O.VrReteIca AS Vr_RetIca,VrAnticipos,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,O.VrNeto AS Vr_Neto,VrAbonado,TotAbonado,TarifaTabla
,O.TarifaFlete AS Tarifa_Flete,UnidTarifa,O.PesoTotal AS Peso_Total,Unidades,O.Volumen AS VolmTotal,PesoOrigen,UnidOrigen,VolOrigen,PesoDestino,UnidDestino,VolDestino,O.BaseRet,O.BaseIca,BaseSeg,BaseImp,O.TarifaRet,O.TarifaIca,TarifaSeg,TarifaImp
,NumCheque,O.Referencia AS Referncia,EdoLiqCausac,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,nRemolque
--datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraLiquida AS LQ INNER JOIN Companias AS CN ON LQ.IdCia=CN.IdCia 
 INNER JOIN EstadoDoc AS ED ON LQ.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON LQ.IdUsuario=U.IdUsuario 
 INNER JOIN Conceptos AS C ON LQ.IdConcepto=C.IdConcepto
 INNER JOIN Terceros AS  CDT ON LQ.IdConductor=CDT.IdTercero INNER JOIN Terceros AS T ON LQ.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON LQ.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 INNER JOIN Trn_TraLiquidaOdp AS LP ON LQ.TipDoc=LP.TipDoc AND LQ.Liquidacion=LP.Liquidacion AND LQ.IdCia=LP.IdCia
 INNER JOIN Trn_TraOrdenPago AS OP ON LP.TipOdp=OP.TipDoc AND LP.OrdPago=OP.OrdPago AND LP.IdCiaOdp=OP.IdCia
 INNER JOIN Trn_TraOrdenManif AS O ON LP.TipOdp=O.TipDoc AND LP.OrdPago=O.OrdPago AND LP.IdCiaOdp=O.IdCia
 INNER JOIN Trn_TraManifiesto AS M ON O.TipMuc=M.TipDoc AND O.Manifiesto=M.Manifiesto AND O.IdCiaMuc=M.IdCia
 INNER JOIN Companias AS COP ON LP.IdCiaOdp=COP.IdCia
 INNER JOIN Localidades AS CO ON M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN TiposCom AS TCM ON LQ.TipCom=TCM.IdCom 
 LEFT JOIN (SELECT TipDoc,Documento,IdCiaDoc,SUM(VrAbonado) AS TotAbonado FROM Trn_ComFactura WHERE TipFac='ODP' AND TipDoc='ODP' GROUP BY TipDoc,Documento,IdCiaDoc)
 AS ABO ON LP.TipOdp=ABO.TipDoc AND LP.OrdPago=ABO.Documento AND LP.IdCiaOdp=ABO.IdCiaDoc
 WHERE LQ.TipDoc=@pmTipDoc AND LQ.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND LQ.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND LQ.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND LQ.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND LQ.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 AND LQ.IdConductor LIKE ISNULL(@pmIdConductor,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Dop] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,D.TipDev,D.Devolucion,D.IdCia,D.Fecha,D.IdConcepto,OM.IdPropietario,'0',OM.VrTotalFletes,OM.VrDescuento,OM.VrImpuestos,OM.VrEstampilla,OM.VrRetencion,OM.VrReteIca,0,OM.VrFondos
,OM.VrOtrosPagos,OM.VrOtrosDctos,OM.VrSeguros,OM.VrFaltantes,OM.VrTotalFletes-OM.VrDescuento,OM.VrNeto,0,0,OM.VrAnticipos,0,OM.Unidades,'',OM.PesoTotal,O.LiqRemesas,OM.CdForma,OM.NumCheque,''
,'','',O.FechaVence,'0',0,'0',D.IdUsuario,M.IdLocFletes,OM.Referencia,M.TipoAfiVehic,'',D.ModdDev,D.IdVehiculo,OM.TipMuc,OM.Manifiesto,OM.IdCiaMuc,D.TipCom,D.Comprobante,D.IdCiaCom,1,0,D.Fecha,D.Observacion,D.TimeSys
,OM.VrReteCREE,M.IdRuta,M.IdOrigen,M.IdDestino,D.IdPoseedor,D.IdConductor,'','',D.OrdPago,D.IdCiaDoc
FROM Trn_TraDevOdp AS D INNER JOIN Trn_TraOrdenPago AS O ON D.TipDoc=O.TipDoc AND D.OrdPago=O.OrdPago AND D.IdCiaDoc=O.IdCia
INNER JOIN Trn_TraOrdenManif AS OM ON D.TipDoc=OM.TipDoc AND D.OrdPago=OM.OrdPago AND D.IdCiaDoc=OM.IdCia
INNER JOIN Trn_TraManifiesto AS M ON OM.TipMuc=M.TipDoc AND OM.Manifiesto=M.Manifiesto AND OM.IdCiaMuc=M.IdCia
WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (D.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (D.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraOrdenPagoAnu] @pmTipDoc VARCHAR(3),@pmOrdPago INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_TraOrdenPago SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND OrdPago=@pmOrdPago AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenPagoRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
,@pmOrdPagoIni INT=Null,@pmOrdPagoFin INT=Null
 AS 
 SELECT OP.TipDoc AS TipoOdp,OP.OrdPago AS NumOdp,OP.IdCia AS CdCia,Compania,OP.Fecha AS FechaOdp,OP.IdConcepto AS CdConcepto,Concepto,Modalidad,LiqRemesas,CxPagar,MulPlazos,OP.IdPlazo AS CdPlazo,Plazo,DiasPago,FechaVence
,OP.TipCom AS TipComp,TipoCom,OP.Comprobante AS NumComp,OP.IdCiaCom AS CdCiaComp,OP.Anulado AS Anuldo,NumDev,OP.FecDev AS FechaDev,OP.Observacion AS Observ,OP.IdEstado AS CdEstado,OP.TimeSys AS FechaCrea,OP.FecUpdate AS FechaAct,OP.IdCiaCrea AS CdCiaCrea,OP.IdUsuario AS CdUsuario,Usuario
,O.TipMuc AS TipManif,O.Manifiesto AS NumManif,O.IdCiaMuc AS CdCiaManif,O.IdVehiculo AS PlacaVeh,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,O.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,O.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,VrTotalFletes,O.VrDescuento AS Vr_Dcto,O.VrRetencion AS Vr_RetFte,O.VrReteIca AS Vr_RetIca,VrAnticipos,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,O.VrNeto AS Vr_Neto
,TarifaTabla,O.TarifaFlete AS Tarifa_Flete,UnidTarifa,O.PesoTotal AS Peso_Total,Unidades,O.Volumen AS VolmTotal,PesoOrigen,UnidOrigen,VolOrigen,PesoDestino,UnidDestino,VolDestino,O.BaseRet,O.BaseIca,BaseSeg,BaseImp,O.TarifaRet,O.TarifaIca,TarifaSeg,TarifaImp
,TipoLiq,TipEgr,Egreso,IdCiaEgr,FechaEgr,NumCheque,Referencia,CantFaltante,UnidadFalt,TolFaltNeto,VrConcPagos,VrConcDctos,VrConcFondo,VrConcSeguro,VrConcAporte,VrConcImpuesto,VrRemesas,VrSeguroRc,VrRecCaja,VrReteCREE,TarifaRtc,CodTarRtc,VrImpAvTa,CodTarifAvta,TarifaAvta,CdForma,EdoLiqCausac
,M.Fecha AS FecManif,FecDespacho,FecEntrega,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,nRemolque,TipoAfiVehic
,IdLocFletes,CF.Localidad AS LugarFletes,M.Observacion AS MucObserv
--Datos del vehiculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,V.IdGrupo AS CdGrupoPro,GrupoProp
,ISNULL(VC.VrTotalAbo,0) AS CxpAbonado,ISNULL(VC.VrSaldo,0) AS CxpSaldo
 FROM Trn_TraOrdenPago AS OP INNER JOIN Trn_TraOrdenManif AS O ON OP.TipDoc=O.TipDoc AND OP.OrdPago=O.OrdPago AND OP.IdCia=O.IdCia
 INNER JOIN Trn_TraManifiesto AS M ON O.TipMuc=M.TipDoc AND O.Manifiesto=M.Manifiesto AND O.IdCiaMuc=M.IdCia
 INNER JOIN Companias AS CN ON OP.IdCia=CN.IdCia  INNER JOIN adm_Usuarios AS U ON OP.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON OP.IdConcepto=C.IdConcepto INNER JOIN Plazos AS PZ ON OP.IdPlazo=PZ.IdPlazo
 INNER JOIN Terceros AS  CDT ON O.IdConductor=CDT.IdTercero INNER JOIN Terceros AS T ON O.IdPoseedor=T.IdTercero
 INNER JOIN Terceros AS NP ON O.IdPropietario=NP.IdTercero  INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta    INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN TiposCom AS TCM ON OP.TipCom=TCM.IdCom  LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 --subconsulta abonos cuentas por pagar
 LEFT JOIN (SELECT TipFac,Factura,IdCia,IdProveedor,Documento,SUM(VrFactura) AS VrTotalFac,SUM(VrAbonado) AS VrTotalAbo,SUM(VrFactura-VrAbonado) AS VrSaldo FROM Trn_ComFactura
 WHERE TipFac=@pmTipDoc AND TipDoc=@pmTipDoc AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProveedor LIKE ISNULL(@pmIdPoseedor,'%')
 GROUP BY TipFac,Factura,IdCia,IdProveedor,Documento) AS VC ON O.TipDoc=VC.TipFac AND O.OrdPago=VC.Documento AND O.IdCia=VC.IdCia AND O.IdPoseedor=VC.IdProveedor
 WHERE OP.TipDoc=@pmTipDoc AND OP.OrdPago BETWEEN ISNULL(@pmOrdPagoIni,0) AND ISNULL(@pmOrdPagoFin,2147483647)
 AND OP.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 --Mayo 10/2018 Parametros Obsoletos
 --AND OP.IdCia LIKE ISNULL(@pmIdCia,'%%')
 --AND OP.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND O.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 --AND O.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 --2147483647
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevOdpLta] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipOdp,D.OrdPago AS NumOrden,IdCiaDoc,FecDoc,TipMuc,Manifiesto,IdCiaMuc,D.IdVehiculo AS PlacaVeh,NumVeh
,D.IdPoseedor AS NitPoseedor,NP.RazonSocial AS Poseedor,D.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,D.CxPagar AS EsCxpagar,FechaVence,LiqRemesas,TipoLiq,VrTotalFletes,VrDescuento
,VrRetencion,VrReteIca,VrAnticipos,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,M.VrImpAvTa,VrNeto,TarifaTabla,TarifaFlete
,UnidTarifa,PesoTotal,Unidades,Volumen,VrRemesas,VrSeguroRc,VrRecCaja,CdForma,NumCheque,Referencia,ModdDev,D.TipCom AS Tip_Comp,D.Comprobante AS NumComp,D.IdCiaCom AS CdCiaCom,D.Observacion AS Observ
,D.IdEstado AS CdEstado,Estado,D.TimeSys AS FechaCrea,D.FecUpdate AS FechaAct,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraDevOdp AS D INNER JOIN  Trn_TraOrdenPago AS O ON D.TipDoc=O.TipDoc  AND D.OrdPago=O.OrdPago AND D.IdCiaDoc=O.IdCia
 INNER JOIN Trn_TraOrdenManif AS M ON O.TipDoc=M.TipDoc AND O.OrdPago=M.OrdPago AND O.IdCia=M.IdCia
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN Terceros AS NP ON D.IdPoseedor=NP.IdTercero 
 INNER JOIN Terceros AS NC ON D.IdConductor=NC.IdTercero
 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 D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdVehiculo  LIKE ISNULL(@pmIdVehiculo ,'%') AND D.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 ORDER BY  D.IdCia,D.Devolucion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenServVeh] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT O.TipDoc,TipoDoc,O.NumOrden,O.IdCia AS CdCia,Compania,O.Fecha,O.FecDespacho,O.IdTipoServ AS CdTipoServ,TipoServicio,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia
,O.IdOrigen,LO.Localidad AS CiuOrigen,LO.IdDep AS CdDepOrig,DPO.Departamento AS DptoOrigen,O.IdDestino,LD.Localidad AS CiuDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino
,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest,O.VrServicio,O.VrEmpaque,O.VrBodega,O.VrOtros,O.VrDescuento,O.VrImpuesto,O.VrNeto,O.VrDeclarado,O.VrSeguro
,O.IdVend,VN.RazonSocial AS NomVendedor,O.NomContacto,O.TelsContacto,O.emlContacto,O.ContactoDest,O.emlContDest,O.NitRemitente,O.Remitente,O.NitDestinatario,O.Destinatario,O.Servicios,O.FormaPago,O.Referencia,O.TipInv,O.NumInvent,O.IdCiaInv
,O.EstFactura,O.TipCausac,O.Causacion,O.CdCiaCau,O.EstCumplido,O.FechaCump,O.TipoRuta,O.Volumen,EstadoBod,O.CdBodega,B.Bodega AS NomBodega,FechaIngBod,FechaRetBod,O.Anulado,O.FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado
--Detalles de vehículos
,VH.Item,VH.IdVehiculo,VH.TipoAfiVehic,VH.NitEmpresa,NomEmpresa,VH.nRemolque,VH.IdConductor,NC.RazonSocial AS Conductor,IdCondAux,NA.RazonSocial AS CondAuxiliar,VH.TipDocRef AS TipRem,VH.NumDocRef AS NumRemesa,VH.CdCiaRef AS CdCiaRem
,VH.CdTipoOper,TipoOperacion,VH.TipoIngreso,VH.TarifaIng,VH.VrIngreso,V.NumVeh,V.IdMarca AS CdMarca,MV.Marca,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.Modelo,V.IdColor AS CdColor,NomColor,V.Config,V.IdPoseedor AS CdPoseedor,NP.RazonSocial AS Poseedor
,V.NumSoat,VigSoat,V.NitEmpSoat,EST.RazonSocial AS NomEmpSoat,V.SerieChasis,V.CarrCapac,V.UndCapc
,CDT.Licencia AS NumLicencia,CDT.CatLicencia,CDT.VigLicencia,CDA.Licencia AS AuxNumLicencia,CDA.CatLicencia AS AuxCategLic,CDA.VigLicencia AS AuxVigLicencia
--datos de remesas
,ISNULL(RM.RemVrTotal,0) AS RemVrTotal,ISNULL(RM.RemVrPago,0) AS RemVrPago,ISNULL(RM.MucNumero,0) AS MucNumero,ISNULL(RM.MucVrPago,0) AS MucVrPago
,ISNULL(OP.OdpNumero,0) AS NumOrdPago,ISNULL(OP.OdpVrPago,0) AS OdpVrPago,ISNULL(OP.OdpTarifa,0) AS OdpTarifa,ISNULL(OP.OdpVrTotal,0) AS OdpTotal
--datos del tercero
,T.Codigo AS TercCod,T.TipoId,T.Dv,T.Direccion,T.IdLocal AS TercCdLocal,L.Localidad AS TercLocalidad,T.Telefono AS TercTelefono,T.TelMovil AS TercTerCelular,T.e_mail AS TercEmail
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupo,GrupoClie,CLI.IdPlazo AS CdPlazo,Plazo
,TS.Transporte,TS.Bodega,TS.Empaque,TS.Personal,O.OrigenAdd,O.TimeSys AS FechaCrea,O.FecUpdate,O.IdCiaCrea,O.IdUsuario AS CdUsuario,U.Usuario
 FROM Trn_MudOrdenServ AS O INNER JOIN Trn_MudOrdenVeh AS VH ON O.TipDoc=VH.TipDoc AND O.NumOrden=VH.NumOrden AND O.IdCia=VH.IdCia
 INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie
 INNER JOIN Terceros AS VN ON O.IdVend=VN.IdTercero INNER JOIN MudServicios AS TS ON O.IdTipoServ=TS.IdTipoServ
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal INNER JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona 
 INNER JOIN Vehiculos AS V ON VH.IdVehiculo=V.IdVehiculo INNER JOIN Terceros AS NC ON VH.IdConductor=NC.IdTercero  
 INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca
 INNER JOIN TiposCol AS CLR ON V.IdColor=CLR.IdColor INNER JOIN Terceros AS NP ON V.IdPoseedor=NP.IdTercero
 LEFT JOIN TercCndtores AS CDT ON VH.IdConductor=CDT.IdConductor
 LEFT JOIN Terceros AS NA ON VH.IdCondAux=NA.IdTercero LEFT JOIN TercCndtores AS CDA ON VH.IdCondAux=CDA.IdConductor
 LEFT JOIN Terceros AS EST ON V.NitEmpSoat=EST.IdTercero LEFT JOIN TiposOperac AS TP ON VH.CdTipoOper=TP.IdTipoOper
 LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo LEFT JOIN Plazos AS PZ ON CLI.IdPlazo=PZ.IdPlazo
 LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia LEFT JOIN AlmBodegas AS B ON O.CdBodega=B.IdBodega
 LEFT JOIN (SELECT D.TipDoc AS RemTipo,D.NumOrden AS RemNumero,D.IdCia AS RemIdCia
 ,SUM(CASE D.UndTarifa WHEN 'PESO' THEN D.PesoNeto*D.TarifClie WHEN 'UNIDADES' THEN D.Cantidad*D.TarifClie WHEN 'VOLUMEN' THEN D.Volumen*D.TarifClie ELSE D.PesoNeto*D.TarifClie END) AS RemVrTotal
 ,SUM(CASE D.UndTarifPago WHEN 'PESO' THEN D.PesoNeto*D.TarifPago WHEN 'UNIDADES' THEN D.Cantidad*D.TarifPago WHEN 'VOLUMEN' THEN D.Volumen*D.TarifPago ELSE D.PesoNeto*D.TarifPago END) AS RemVrPago
 ,SUM(CASE D.UndTarifPago WHEN 'PESO' THEN D.PesoNeto*MR.TarifPago WHEN 'UNIDADES' THEN D.Cantidad*MR.TarifPago WHEN 'VOLUMEN' THEN D.Volumen*MR.TarifPago ELSE D.PesoNeto*MR.TarifPago END) AS MucVrPago
 ,MAX(MR.Manifiesto) AS MucNumero
  FROM Trn_TraRemMcias AS D INNER JOIN Trn_TraRemesa AS R ON D.TipDoc=R.TipDoc AND D.NumOrden=R.NumOrden AND D.IdCia=R.IdCia
  LEFT JOIN Trn_TraManifRem AS MR ON D.TipDoc=MR.TipRem AND D.NumOrden=MR.Remesa AND D.IdCia=MR.IdCiaRem AND D.Item=MR.ItemRem
  LEFT JOIN Trn_TraManifiesto AS M ON MR.TipDoc=M.TipDoc AND MR.Manifiesto=M.Manifiesto AND MR.IdCia=M.IdCia
  WHERE D.TipDoc='RMT' AND R.Anulado=0 AND ISNULL(M.Anulado,0)=0 GROUP BY D.TipDoc,D.NumOrden,D.IdCia) AS RM ON VH.TipDocRef=RM.RemTipo AND VH.NumDocRef=RM.RemNumero AND VH.CdCiaRef=RM.RemIdCia
 LEFT JOIN (SELECT OD.TipRem,OD.Remesa,OD.IdCiaRem,MAX(OD.OrdPago) AS OdpNumero,MAX(OD.Manifiesto) AS OdpNumManif
 ,SUM(CASE OD.UndTarifa WHEN 'PESO' THEN OD.PesoNeto*OD.TarifPago WHEN 'UNIDADES' THEN OD.Cantidad*OD.TarifPago WHEN 'VOLUMEN' THEN OD.Volumen*OD.TarifPago ELSE OD.PesoNeto*OD.TarifPago END) AS OdpVrPago
 ,MAX(OM.TarifaFlete) AS OdpTarifa,SUM(OM.VrTotalFletes-OM.VrDescuento) AS OdpVrTotal
 FROM Trn_TraOrdenRemesas AS OD INNER JOIN Trn_TraOrdenManif AS OM ON OD.TipDoc=OM.TipDoc AND OD.OrdPago=OM.OrdPago AND OD.IdCia=OM.IdCia
 INNER JOIN Trn_TraOrdenPago AS O ON OD.TipDoc=O.TipDoc AND OD.OrdPago=O.OrdPago AND OD.IdCia=O.IdCia
 WHERE OD.TipRem='RMT' AND O.Anulado=0 GROUP BY OD.TipRem,OD.Remesa,OD.IdCiaRem) AS OP ON VH.TipDocRef=OP.TipRem AND VH.NumDocRef=OP.Remesa AND VH.CdCiaRef=OP.IdCiaRem
 WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraManifiestoRen]  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
SELECT RM.TipDoc AS TipMuc,RM.Manifiesto AS NumManif,RM.IdCia AS CdCia,Compania,M.Fecha AS MucFecha,M.FecDespacho AS MucFecDespacho,RM.TipRem AS TipRem,RM.Remesa AS NumRemesa,RM.IdCiaRem AS CdCiaRem
,RM.ItemRem AS ItemRemsa,R.Fecha AS RemFecha,R.FecDespacho AS RemFecDespacho,IdMercancia,DescripMcias,D.Cantidad AS RemCantidad,D.PesoNeto AS RemPesoNeto,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,D.Volumen AS RemVolumen,UndVol,Cases,Cajas,Palets
,D.TarifClie AS RemTarifClie,UndTarifa,D.VrDeclarado AS RemVrDeclara,D.VrSeguro AS RemVrSeguro,D.TarifSeguro AS RemTarifSeguro,RM.TarifPago AS MucTarifPago,RM.TarifTabla AS MucTarifTabla,UndTarifPago,D.Cumplido AS NumCumplido,D.IdCiaCump AS CdCiaCump
,D.FechaCump AS FecCumplido,CantidadCump,PesoCump,VolCump,CasesCump,CajasCump,PaletsCump,TarifCump,UndTarCump,PagoCump,UndTarPagoCump,DetalleCump,D.TarifClieFac AS RemTarifFact,D.TarifOdp AS RemTarifOdp,D.IdUnd AS CdUnd,UM.Unidad AS UndMedida,Remision,DocCliente,Referencia1,Referencia2,Referencia3
,NitRemite,Remitente,NitDestntario,Destinatario,D.IdOrigen AS CdCiuOrigen,LO.Localidad AS CiuOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,D.IdDestino AS CdCiuDestino,LD.Localidad AS CiuDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,IdCliente,T.RazonSocial AS NomCliente,R.IdAgencia AS CdAgencia,Agencia,R.Modalidad AS TipoRemesa,M.IdVehiculo AS PlacaVeh,M.TipoAfiVehic AS TipoAfiVeh,M.IdConductor AS CedCondutor,CDT.RazonSocial AS NomConductor,M.nRemolque AS NumRemolque
,M.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,M.VrFletes AS MucVrFletes,M.VrRetencion AS MucVrRetencion,M.VrReteIca AS MucVrReteIca,M.VrAnticipo+VrAntAdic AS MucAnticipos,M.TarifaRet AS MucTarifRet,M.TarifaIca AS MucTarifIca,M.IdRuta AS CdRuta
,M.TipOdp AS CdTipOdp,M.OrdPago AS MucNumOdp,M.IdCiaOdp AS MucCiaOdp,M.FechaOdp AS FecOrdenPago,EstOrden,M.Observacion AS MucObserv
--datos de orden pago
,OP.PesoTotal AS OdpPesoNeto,OP.Unidades AS OdpCantidad,OP.Volumen AS OdpVolumen,OP.TarifaTabla AS OdpTarifTabla,OP.TarifaFlete AS OdpTarifFlete,OP.UnidTarifa AS OdpUndTarifa,OP.VrTotalFletes AS OdpTotalFletes,OP.VrDescuento AS OdpVrDcto
,OP.VrRetencion AS OdpVrRetFte,OP.VrReteIca AS OdpReteIca,OP.VrAnticipos AS OdpAnticipos,OP.VrFaltantes AS OdpFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla
,VrOtrosPagos,OP.VrNeto AS OdpVrNetoPagar,VrRemesas,VrRecCaja,OP.TarifaRet AS OdpTarifRet,OP.TarifaIca AS OdpTarifIca
,OP.IdPoseedor AS OdpNitPoseedor,ODP.Anulado AS OdpAnulado,ODP.Observacion AS OdpObserv,TipEgr,Egreso,IdCiaEgr,NumCheque,OP.Referencia AS OdpReferencia
,R.NumPedido AS nPedido,IdCiaPed,FechaPed,R.TipDcm AS TipOdc,R.NumDocmto AS nOCargue,R.IdCiaDcm AS CdCiaOdc,R.FechaDcm AS FecOCargue
,R.EstCumplido AS RemEdoCumplido,R.EstFactura AS RemEdoFactura,SerieGuia,NumGuia,R.Observacion AS RemObserv
--columnas de detalles ocultas,TipFac,Factura,IdCiaFac,FechaFac,TipOdp,NumeroOdp,IdCiaOdp,PesoCont
--datos de factura
,D.TipFac AS CdTipFact,Factura,NumFactura,D.IdCiaFac AS CdCiaFact,FechaFac,FacCantidad,FacUnidades,FacPesoNeto,FacVolumen,FacValorTotal,FacCostoTotal,FacFaltantes
,NumDevFactura,DfaCantidad,DfaUnidades,DfaPesoNeto,DfaVolumen,DfaValorTotal,DfaCostoTotal,DfaFaltantes
,NumNotaDb,NumNotaCr,NotCantidad,NotPesoNeto,NotVolumen,NotValorTotal,NotCostoTotal,EGR.FecEgr
,ISNULL(NM.Cargos,0) AS MucNovCargos,ISNULL(NM.Deducciones,0) AS MucNovDeducc,ISNULL(NR.Cargos,0) AS RemNovCargos,ISNULL(NR.Deducciones,0) AS RemNovDeducc
 FROM Trn_TraManifRem AS RM INNER JOIN Trn_TraManifiesto AS M ON RM.TipDoc=M.TipDoc AND RM.Manifiesto=M.Manifiesto AND RM.IdCia=M.IdCia
 INNER JOIN Trn_TraRemMcias AS D ON RM.TipRem=D.TipDoc AND RM.Remesa=D.NumOrden AND RM.IdCiaRem=D.IdCia AND RM.ItemRem=D.Item
 INNER JOIN Trn_TraRemesa AS R ON RM.TipRem=R.TipDoc AND RM.Remesa=R.NumOrden AND RM.IdCiaRem=R.IdCia
 INNER JOIN Companias AS CN ON RM.IdCia=CN.IdCia 
 INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero
 INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo INNER JOIN Terceros AS CDT ON M.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS NP ON M.IdPoseedor=NP.IdTercero 
 LEFT JOIN Trn_TraOrdenPago AS ODP ON M.TipOdp=ODP.TipDoc AND M.OrdPago=ODP.OrdPago AND M.IdCiaOdp=ODP.IdCia
 LEFT JOIN Trn_TraOrdenManif AS OP ON M.TipOdp=OP.TipDoc AND M.OrdPago=OP.OrdPago AND M.IdCiaOdp=OP.IdCia
 LEFT JOIN Agencias AS A ON R.IdAgencia=A.IdAgencia LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed 
--subconsulta de facturas 
LEFT JOIN (SELECT TipRem,Remesa,IdCiaRem,ItemRem,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='FC' THEN Cantidad ELSE 0 END) AS FacCantidad
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='FC' THEN Cantidad*VrUnitario ELSE 0 END) AS FacValorTotal
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='FC' THEN CantPago*VrCosto ELSE 0 END) AS FacCostoTotal
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='FC' THEN VrFaltante ELSE 0 END) AS FacFaltantes
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='FC' THEN Unidades ELSE 0 END) AS FacUnidades
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='FC' THEN PesoNeto ELSE 0 END) AS FacPesoNeto
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='FC' THEN Volumen ELSE 0 END) AS FacVolumen
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='FC' THEN Cases ELSE 0 END) AS FacCases
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='FC' THEN Cajas ELSE 0 END) AS FacCajas
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='FC' THEN Palets ELSE 0 END) AS FacPalets
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Cantidad ELSE 0 END) AS DfaCantidad
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Cantidad*VrUnitario ELSE 0 END) AS DfaValorTotal
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN CantPago*VrCosto ELSE 0 END) AS DfaCostoTotal
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN VrFaltante ELSE 0 END) AS DfaFaltantes
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Unidades ELSE 0 END) AS DfaUnidades
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN PesoNeto ELSE 0 END) AS DfaPesoNeto
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Volumen ELSE 0 END) AS DfaVolumen
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Cases ELSE 0 END) AS DfaCases
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Cajas ELSE 0 END) AS DfaCajas
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Palets ELSE 0 END) AS DfaPalets
,SUM(CASE TipDoc WHEN 'NDB' THEN Cantidad WHEN 'NCR' THEN Cantidad*-1 ELSE 0 END) AS NotCantidad
,SUM(CASE TipDoc WHEN 'NDB' THEN Cantidad*VrUnitario WHEN 'NCR' THEN (Cantidad*VrUnitario)*-1 ELSE 0 END) AS NotValorTotal
,SUM(CASE TipDoc WHEN 'NDB' THEN Cantidad*VrCosto WHEN 'NCR' THEN (Cantidad*VrCosto)*-1 ELSE 0 END) AS NotCostoTotal
,SUM(CASE TipDoc WHEN 'NDB' THEN PesoNeto WHEN 'NCR' THEN PesoNeto*-1 ELSE 0 END) AS NotPesoNeto
,SUM(CASE TipDoc WHEN 'NDB' THEN Volumen WHEN 'NCR' THEN Volumen*-1 ELSE 0 END) AS NotVolumen
,MAX(CASE WHEN SUBSTRING(TipDoc,1,2)='FC' THEN Factura ELSE 0 END) AS NumFactura
,MAX(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Factura ELSE 0 END) AS NumDevFactura
,MAX(CASE TipDoc WHEN 'NDB' THEN Factura ELSE 0 END) AS NumNotaDb
,MAX(CASE TipDoc WHEN 'NCR' THEN Factura ELSE 0 END) AS NumNotaCr
 FROM Trn_TraFacRemesas GROUP BY TipRem,Remesa,IdCiaRem,ItemRem) AS RF ON RM.TipRem=RF.TipRem AND RM.Remesa=RF.Remesa AND RM.IdCiaRem=RF.IdCiaRem AND RM.ItemRem=RF.ItemRem
LEFT JOIN(select OE.TipOdp ,OE.OrdPago,OE.IdCiaOdp,MAX(E.Fecha)AS FecEgr from Trn_TraEgrOrden  OE inner join Trn_Comprobantes E
ON OE.TipCom =E.TipCom AND OE.NumEgreso =E.Comprobante  AND OE.IdCia=E.IdCia 
group by OE.TipOdp ,OE.OrdPago,OE.IdCiaOdp) AS EGR ON ODP.TipDoc =EGR.TipOdp AND ODP.OrdPago=EGR.OrdPago AND ODP.IdCia =EGR.IdCiaOdp 
--Novedades
LEFT JOIN (SELECT TipDoc,Documento,IdCia,SUM(CASE WHEN (TipoConc='PAGO' OR TipoConc='GASTO') THEN (CASE TipoTarif WHEN '%' THEN (VrBase*Tarifa)/100 ELSE Tarifa END) ELSE 0 END) AS Cargos
,SUM(CASE TipoConc WHEN 'DEDUCCION' THEN (CASE TipoTarif WHEN '%' THEN (VrBase*Tarifa)/100 ELSE Tarifa END) ELSE  0 END) AS Deducciones
FROM Trn_TraConceptos WHERE TipDoc='MUC' GROUP BY TipDoc,Documento,IdCia) AS NM ON M.TipDoc=NM.TipDoc AND M.Manifiesto=NM.Documento AND M.IdCia=NM.IdCia
LEFT JOIN (SELECT TipDoc,Documento,IdCia,SUM(CASE TipoConc WHEN 'CARGO' THEN (CASE TipoTarif WHEN '%' THEN (VrBase*Tarifa)/100 ELSE Tarifa END) ELSE 0 END) AS Cargos
,SUM(CASE TipoConc WHEN 'DEDUCCION' THEN (CASE TipoTarif WHEN '%' THEN (VrBase*Tarifa)/100 ELSE Tarifa END) ELSE  0 END) AS Deducciones
FROM Trn_TraConceptos WHERE TipDoc='RMT' GROUP BY TipDoc,Documento,IdCia) AS NR ON RM.TipRem=NR.TipDoc AND RM.Remesa=NR.Documento AND RM.IdCiaRem=NR.IdCia
--main query condition
WHERE  RM.TipDoc='MUC' AND M.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND M.Anulado=0 AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
AND  M.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND M.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenVehRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT O.TipDoc,O.NumOrden,O.IdCia,O.FecDespacho,O.IdCliente,VH.Item,VH.IdVehiculo,VH.TipoAfiVehic,VH.NitEmpresa,NomEmpresa,VH.nRemolque
,VH.IdConductor,NC.RazonSocial AS Conductor,IdCondAux,NA.RazonSocial AS CondAuxiliar,VH.TipDocRef AS TipRem,VH.NumDocRef AS NumRemesa,VH.CdCiaRef AS CdCiaRem
,VH.CdTipoOper,TipoOperacion,VH.TipoIngreso,VH.TarifaIng,VH.VrIngreso
,V.NumVeh,V.IdMarca AS CdMarca,MV.Marca,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.Modelo,V.IdColor AS CdColor,NomColor,V.Config
,V.IdPoseedor AS CdPoseedor,T.RazonSocial AS Poseedor,V.NumSoat,VigSoat,V.NitEmpSoat,EST.RazonSocial AS NomEmpSoat,V.SerieChasis,V.CarrCapac,V.UndCapc
,CDT.Licencia AS NumLicencia,CDT.CatLicencia,CDT.VigLicencia
,CDA.Licencia AS AuxNumLicencia,CDA.CatLicencia AS AuxCategLic,CDA.VigLicencia AS AuxVigLicencia
--datos de remesas
,ISNULL(RM.RemVrTotal,0) AS RemVrTotal,ISNULL(RM.RemVrPago,0) AS RemVrPago,ISNULL(RM.MucNumero,0) AS MucNumero,ISNULL(RM.MucVrPago,0) AS MucVrPago
,ISNULL(OP.OdpNumero,0) AS NumOrdPago,ISNULL(OP.OdpVrPago,0) AS OdpVrPago,ISNULL(OP.OdpTarifa,0) AS OdpTarifa,ISNULL(OP.OdpVrTotal,0) AS OdpTotal
 FROM Trn_MudOrdenServ AS O INNER JOIN Trn_MudOrdenVeh AS VH ON O.TipDoc=VH.TipDoc AND O.NumOrden=VH.NumOrden AND O.IdCia=VH.IdCia
 INNER JOIN Vehiculos AS V ON VH.IdVehiculo=V.IdVehiculo
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN TiposCol AS C ON V.IdColor=C.IdColor
 INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Terceros AS T ON V.IdPoseedor=T.IdTercero
 INNER JOIN Terceros AS NC ON VH.IdConductor=NC.IdTercero 
 LEFT JOIN Terceros AS NA ON VH.IdCondAux=NA.IdTercero LEFT JOIN Terceros AS EST ON V.NitEmpSoat=EST.IdTercero
 LEFT JOIN TercCndtores AS CDT ON VH.IdConductor=CDT.IdConductor LEFT JOIN TercCndtores AS CDA ON VH.IdCondAux=CDA.IdConductor
 LEFT JOIN TiposOperac AS TP ON VH.CdTipoOper=TP.IdTipoOper
 LEFT JOIN (SELECT D.TipDoc AS RemTipo,D.NumOrden AS RemNumero,D.IdCia AS RemIdCia
 ,SUM(CASE D.UndTarifa WHEN 'PESO' THEN D.PesoNeto*D.TarifClie WHEN 'UNIDADES' THEN D.Cantidad*D.TarifClie WHEN 'VOLUMEN' THEN D.Volumen*D.TarifClie ELSE D.PesoNeto*D.TarifClie END) AS RemVrTotal
 ,SUM(CASE D.UndTarifPago WHEN 'PESO' THEN D.PesoNeto*D.TarifPago WHEN 'UNIDADES' THEN D.Cantidad*D.TarifPago WHEN 'VOLUMEN' THEN D.Volumen*D.TarifPago ELSE D.PesoNeto*D.TarifPago END) AS RemVrPago
 ,SUM(CASE D.UndTarifPago WHEN 'PESO' THEN D.PesoNeto*MR.TarifPago WHEN 'UNIDADES' THEN D.Cantidad*MR.TarifPago WHEN 'VOLUMEN' THEN D.Volumen*MR.TarifPago ELSE D.PesoNeto*MR.TarifPago END) AS MucVrPago
 ,MAX(MR.Manifiesto) AS MucNumero
  FROM Trn_TraRemMcias AS D INNER JOIN Trn_TraRemesa AS R ON D.TipDoc=R.TipDoc AND D.NumOrden=R.NumOrden AND D.IdCia=R.IdCia
  LEFT JOIN Trn_TraManifRem AS MR ON D.TipDoc=MR.TipRem AND D.NumOrden=MR.Remesa AND D.IdCia=MR.IdCiaRem AND D.Item=MR.ItemRem
  LEFT JOIN Trn_TraManifiesto AS M ON MR.TipDoc=M.TipDoc AND MR.Manifiesto=M.Manifiesto AND MR.IdCia=M.IdCia
  WHERE D.TipDoc='RMT' AND R.Anulado=0 AND ISNULL(M.Anulado,0)=0 GROUP BY D.TipDoc,D.NumOrden,D.IdCia) AS RM ON VH.TipDocRef=RM.RemTipo AND VH.NumDocRef=RM.RemNumero AND VH.CdCiaRef=RM.RemIdCia
 LEFT JOIN (SELECT OD.TipRem,OD.Remesa,OD.IdCiaRem,MAX(OD.OrdPago) AS OdpNumero,MAX(OD.Manifiesto) AS OdpNumManif
 ,SUM(CASE OD.UndTarifa WHEN 'PESO' THEN OD.PesoNeto*OD.TarifPago WHEN 'UNIDADES' THEN OD.Cantidad*OD.TarifPago WHEN 'VOLUMEN' THEN OD.Volumen*OD.TarifPago ELSE OD.PesoNeto*OD.TarifPago END) AS OdpVrPago
 ,MAX(OM.TarifaFlete) AS OdpTarifa,SUM(OM.VrTotalFletes-OM.VrDescuento) AS OdpVrTotal
 FROM Trn_TraOrdenRemesas AS OD INNER JOIN Trn_TraOrdenManif AS OM ON OD.TipDoc=OM.TipDoc AND OD.OrdPago=OM.OrdPago AND OD.IdCia=OM.IdCia
 INNER JOIN Trn_TraOrdenPago AS O ON OD.TipDoc=O.TipDoc AND OD.OrdPago=O.OrdPago AND OD.IdCia=O.IdCia
 WHERE OD.TipRem='RMT' AND O.Anulado=0 GROUP BY OD.TipRem,OD.Remesa,OD.IdCiaRem) AS OP ON VH.TipDocRef=OP.TipRem AND VH.NumDocRef=OP.Remesa AND VH.CdCiaRef=OP.IdCiaRem
WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Odp] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,O.TipDoc,O.OrdPago,O.IdCia,O.Fecha,O.IdConcepto,OM.IdPropietario,'0',OM.VrTotalFletes,OM.VrDescuento,OM.VrImpuestos,OM.VrEstampilla,OM.VrRetencion,OM.VrReteIca,0,OM.VrFondos
,OM.VrOtrosPagos,OM.VrOtrosDctos,OM.VrSeguros,OM.VrFaltantes,OM.VrTotalFletes-OM.VrDescuento,OM.VrNeto,0,0,OM.VrAnticipos,0,OM.Unidades,'',OM.PesoTotal,O.LiqRemesas,OM.CdForma,OM.NumCheque,''
,'','',O.FechaVence,'0',0,'0',O.IdUsuario,M.IdLocFletes,OM.Referencia,M.TipoAfiVehic,'',O.Modalidad,OM.IdVehiculo,OM.TipMuc,OM.Manifiesto,OM.IdCiaMuc,O.TipCom,O.Comprobante,O.IdCiaCom,O.Anulado,O.NumDev,O.FecDev,O.Observacion,O.TimeSys
,OM.VrReteCREE,M.IdRuta,M.IdOrigen,M.IdDestino,OM.IdPoseedor,OM.IdConductor,'','',OM.Egreso,OM.IdCiaEgr
FROM Trn_TraOrdenPago AS O INNER JOIN Trn_TraOrdenManif AS OM ON O.TipDoc=OM.TipDoc AND O.OrdPago=OM.OrdPago AND O.IdCia=OM.IdCia
INNER JOIN Trn_TraManifiesto AS M ON OM.TipMuc=M.TipDoc AND OM.Manifiesto=M.Manifiesto AND OM.IdCiaMuc=M.IdCia
WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (O.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (O.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraOrdenPago] @pmTipDoc VARCHAR(3),@pmOrdPago INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraOrdenPago
 WHERE TipDoc=@pmTipDoc AND OrdPago=@pmOrdPago AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenPagoRelDet] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
,@pmOrdPagoIni INT=Null,@pmOrdPagoFin INT=Null
 AS 
 SELECT OP.TipDoc AS TipoOdp,OP.OrdPago AS NumOdp,OP.IdCia AS CdCia,Compania,OP.Fecha AS FechaOdp,OP.IdConcepto AS CdConcepto,Concepto,OP.Modalidad,LiqRemesas,CxPagar,MulPlazos,OP.IdPlazo AS CdPlazo,Plazo,DiasPago,FechaVence
,OP.TipCom AS TipComp,TipoCom,OP.Comprobante AS NumComp,OP.IdCiaCom AS CdCiaComp,OP.Anulado AS Anuldo,NumDev,OP.FecDev AS FechaDev,OP.Observacion AS Observ,OP.IdEstado AS CdEstado,OP.TimeSys AS FechaCrea,OP.FecUpdate AS FechaAct,OP.IdCiaCrea AS CdCiaCrea,OP.IdUsuario AS CdUsuario,Usuario
,O.TipMuc AS TipManif,O.Manifiesto AS NumManif,O.IdCiaMuc AS CdCiaManif,O.IdVehiculo AS PlacaVeh,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,O.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,O.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,VrTotalFletes,O.VrDescuento AS Vr_Dcto,O.VrRetencion AS Vr_RetFte,O.VrReteIca AS Vr_RetIca,VrAnticipos,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,O.VrNeto AS Vr_Neto
,TarifaTabla,O.TarifaFlete AS Tarifa_Flete,UnidTarifa,O.PesoTotal AS Peso_Total,Unidades,O.Volumen AS VolmTotal,PesoOrigen,UnidOrigen,VolOrigen,PesoDestino,UnidDestino,VolDestino,O.BaseRet,O.BaseIca,BaseSeg,BaseImp,O.TarifaRet,O.TarifaIca,TarifaSeg,TarifaImp
,TipoLiq,TipEgr,Egreso,IdCiaEgr,FechaEgr,O.NumCheque,O.Referencia,CantFaltante,UnidadFalt,TolFaltNeto,VrConcPagos,VrConcDctos,VrConcFondo,VrConcSeguro,VrConcAporte,VrConcImpuesto,VrRemesas,VrSeguroRc,VrRecCaja,VrReteCREE,TarifaRtc,CodTarRtc,VrImpAvTa,CodTarifAvta,TarifaAvta,EdoLiqCausac
,M.Fecha AS FecManif,M.FecDespacho,M.FecEntrega,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,M.nRemolque,M.TipoAfiVehic
,M.IdLocFletes,CF.Localidad AS LugarFletes,M.Observacion AS MucObserv
--detalles
,TipRem,ORM.Remesa AS NumRemesa,ORM.IdCiaRem AS CdCiaRem,ItemRem,ORM.Cantidad AS Cant,PesoNeto,ORM.UndMed AS CdUMed,UMP.Unidad AS UmPeso,ORM.Volumen AS Volmen,UndVol,Cases,Cajas,Palets,TarifPago,TarifTabla,UndTarifa,Detalle
,RM.IdCliente AS CdCliente,NC.RazonSocial AS NomCliente,RM.IdAgencia AS CdAgencia,A.Agencia
--Datos del vehiculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,V.IdGrupo AS CdGrupoPro,GrupoProp
,ISNULL(VC.VrTotalAbo,0) AS CxpAbonado,ISNULL(VC.VrSaldo,0) AS CxpSaldo
 FROM Trn_TraOrdenPago AS OP INNER JOIN Trn_TraOrdenManif AS O ON OP.TipDoc=O.TipDoc AND OP.OrdPago=O.OrdPago AND OP.IdCia=O.IdCia
 INNER JOIN Trn_TraManifiesto AS M ON O.TipMuc=M.TipDoc AND O.Manifiesto=M.Manifiesto AND O.IdCiaMuc=M.IdCia
 INNER JOIN Companias AS CN ON OP.IdCia=CN.IdCia  INNER JOIN adm_Usuarios AS U ON OP.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON OP.IdConcepto=C.IdConcepto INNER JOIN Plazos AS PZ ON OP.IdPlazo=PZ.IdPlazo
 INNER JOIN Terceros AS  CDT ON O.IdConductor=CDT.IdTercero INNER JOIN Terceros AS T ON O.IdPoseedor=T.IdTercero
 INNER JOIN Terceros AS NP ON O.IdPropietario=NP.IdTercero  INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta    INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS NE ON V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN TiposCom AS TCM ON OP.TipCom=TCM.IdCom 
 LEFT JOIN Trn_TraOrdenRemesas AS ORM ON O.TipDoc=ORM.TipDoc AND O.OrdPago=ORM.OrdPago AND O.IdCia=ORM.IdCia 
 AND O.TipMuc=ORM.TipMuc AND O.Manifiesto=ORM.Manifiesto AND O.IdCiaMuc=ORM.IdCiaMuc
 LEFT JOIN Sys_Um AS UMP ON ORM.UndMed=UMP.UndMed LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 LEFT JOIN Trn_TraRemesa AS RM ON ORM.TipRem=RM.TipDoc AND ORM.Remesa=RM.NumOrden AND ORM.IdCiaRem=RM.IdCia
 LEFT JOIN Terceros AS NC ON RM.IdCliente=NC.IdTercero LEFT JOIN Agencias AS A ON RM.IdAgencia=A.IdAgencia
  --subconsulta abonos cuentas por pagar
 LEFT JOIN (SELECT TipFac,Factura,IdCia,IdProveedor,Documento,SUM(VrFactura) AS VrTotalFac,SUM(VrAbonado) AS VrTotalAbo,SUM(VrFactura-VrAbonado) AS VrSaldo FROM Trn_ComFactura
 WHERE TipFac=@pmTipDoc AND TipDoc=@pmTipDoc AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProveedor LIKE ISNULL(@pmIdPoseedor,'%')
 GROUP BY TipFac,Factura,IdCia,IdProveedor,Documento) AS VC ON O.TipDoc=VC.TipFac AND O.OrdPago=VC.Documento AND O.IdCia=VC.IdCia AND O.IdPoseedor=VC.IdProveedor
 WHERE OP.TipDoc=@pmTipDoc AND OP.OrdPago BETWEEN ISNULL(@pmOrdPagoIni,0) AND ISNULL(@pmOrdPagoFin,2147483647)
 AND OP.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 --Mayo 10/2018 Parametros Obsoletos
 --AND OP.IdCia LIKE ISNULL(@pmIdCia,'%%')
 --AND OP.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND O.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 --AND O.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraOrdenPago] @pmTipDoc VARCHAR(3),@pmOrdPago INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmModalidad VARCHAR(10),@pmLiqRemesas BIT,@pmCxPagar BIT,@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmFechaVence SMALLDATETIME
,@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_TraOrdenPago SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,Modalidad=@pmModalidad,LiqRemesas=@pmLiqRemesas,CxPagar=@pmCxPagar,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,FechaVence=@pmFechaVence,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom
,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND OrdPago=@pmOrdPago AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryEdsUnion_Cr] @pmTipDoc VARCHAR(3),@pmNumNotaIni INT,@pmNumNotaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT N.TipDoc AS Tip_Doc,TipoDoc,N.NumNota,N.IdCia AS CdCia,Compania,N.Fecha,N.CdTanque AS TanqNuevo,T.IdProducto,T.Descripcion,N.Capacidad,N.Cantidad,N.NivAgua
,N.TipoTrasl,N.NumTraslado,N.Anulado,N.FecDev,N.Observacion AS Observ,N.TimeSys AS Fec_Add,N.FecUpdate AS Fec_Update,N.IdCiaCrea,N.IdUsuario AS CdUsuario,U.Usuario AS NomUsuario,N.OrigenAdd
,D.Item,D.IdTanque AS CdTanque,TQ.IdProducto AS CdProducto,TQ.Descripcion AS TanDescripcion,D.Capacidad AS TanCapacidad,D.NivelAgua AS TanNivelAgua,D.Cantidad AS SaldoActual
 FROM Trn_EdsUnion AS N INNER JOIN Trn_EdsUnionItems AS D ON N.TipDoc=D.TipDoc AND N.NumNota=D.NumNota AND N.IdCia=D.IdCia
 INNER JOIN Companias AS CN ON N.IdCia=CN.IdCia INNER JOIN Sys_TiposDoc AS TD ON N.TipDoc=TD.IdDoc
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario INNER JOIN Tanques AS TQ ON D.IdTanque=TQ.IdTanque
 LEFT JOIN Tanques AS T ON N.CdTanque=T.IdTanque
 WHERE N.TipDoc=@pmTipDoc AND N.NumNota BETWEEN @pmNumNotaIni AND @pmNumNotaFin AND N.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[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,tmTarifSeguro,tmVrSeguro)
 SELECT @pmtmNumero,Item,Numero,Fecha,FecPago,VrCuota,CdBanco,CtaCheque,Detalle,TarifSeguro,TotalSeguro
 FROM Trn_FinPagos
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuPrestamos_Uni] @pmIdCliente VARCHAR(16),@pmIdClienteDos VARCHAR(16),@pmNewCliente VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCliente FROM AcuPrestamos WHERE IdCliente=@pmIdCliente OR IdCliente=@pmIdClienteDos)
  INSERT INTO AcuPrestamos (nAnno,nMes,IdCia,IdCliente,IdAgencia,SaldoAnt,Prestamos,DevPrestamos,Recibos,DevRecibos,NotasDeb,NotasCre,Facturas,DevFacturas)
 SELECT nAnno,nMes,IdCia,@pmNewCliente,IdAgencia,SUM(SaldoAnt),SUM(Prestamos),SUM(DevPrestamos),SUM(Recibos),SUM(DevRecibos),SUM(NotasDeb)
,SUM(NotasCre),SUM(Facturas),SUM(DevFacturas)
 FROM AcuPrestamos WHERE IdCliente=@pmIdCliente OR IdCliente=@pmIdClienteDos
  GROUP BY  nAnno,nMes,IdCia,IdAgencia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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(CASE WHEN C.TipDoc='PR1' AND C.Concepto='CAPITAL' AND ISNULL(P.CauAnticipado,0)=1 THEN P.FecPrestamo ELSE C.Fecha END)
,MONTH(CASE WHEN C.TipDoc='PR1' AND C.Concepto='CAPITAL' AND ISNULL(P.CauAnticipado,0)=1 THEN P.FecPrestamo ELSE C.Fecha END)
,C.IdCia,C.IdCliente,C.IdAgencia,0,SUM(CASE C.TipDoc WHEN 'PR1' THEN C.VrTotal ELSE 0 END),0,0,0
 ,SUM(CASE C.TipDoc WHEN 'ND2' THEN C.VrTotal ELSE 0 END),0,SUM(CASE C.TipDoc WHEN 'FIF' THEN C.VrTotal ELSE 0 END),0
 FROM Trn_FinCuotas AS C LEFT JOIN Trn_FinPrestamo AS P ON C.TipDoc=P.TipDoc  AND C.IdPrestamo=P.IdPrestamo AND C.IdCia=P.IdCia
 WHERE C.TipDoc IN ('PR1','ND2','FIF')  AND YEAR(CASE WHEN C.TipDoc='PR1' AND C.Concepto='CAPITAL' AND ISNULL(P.CauAnticipado,0)=1 THEN P.FecPrestamo ELSE C.Fecha END)=@pmnAnno
 AND MONTH(CASE WHEN C.TipDoc='PR1' AND C.Concepto='CAPITAL' AND ISNULL(P.CauAnticipado,0)=1 THEN P.FecPrestamo ELSE C.Fecha END)=@pmnMes 
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 GROUP BY YEAR(CASE WHEN C.TipDoc='PR1' AND C.Concepto='CAPITAL' AND ISNULL(P.CauAnticipado,0)=1 THEN P.FecPrestamo ELSE C.Fecha END)
 ,MONTH(CASE WHEN C.TipDoc='PR1' AND C.Concepto='CAPITAL' AND ISNULL(P.CauAnticipado,0)=1 THEN P.FecPrestamo ELSE C.Fecha END)
 ,C.IdCia,C.IdCliente,C.IdAgencia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelVeh_Cupos] @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4)
 AS 
 DELETE FROM Veh_Cupos
 WHERE IdVehiculo=@pmIdVehiculo AND IdClase like ISNULL(@pmIdClase,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsVeh_CuposUni] @pmIdVehiculo VARCHAR(10),@pmIdVehiculoDos VARCHAR(10),@pmNewVehiculo VARCHAR(10)
AS 
 IF EXISTS (SELECT * FROM Veh_Cupos WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos)
  INSERT INTO Veh_Cupos (IdVehiculo,IdClase,ValorCupo,ValorSaldo,FecUpdate,IdUsuario)
  SELECT @pmNewVehiculo,IdClase,SUM(ValorCupo),SUM(ValorSaldo),MAX(FecUpdate),'ADMIN'
  FROM Veh_Cupos WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos
  GROUP BY IdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsDocSoporte] @pmTipDoc VARCHAR(3),@pmNumDoc INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdTercero VARCHAR(16),@pmIdAdquiriente VARCHAR(16),@pmIdConcepto VARCHAR(4),@pmIdMedPago VARCHAR(4),@pmMetPago INT,@pmModalidad VARCHAR(20),@pmNumFactura VARCHAR(15),@pmFechaFac SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmSubTotal DECIMAL(16,6),@pmDescuento DECIMAL(16,6),@pmRetencion DECIMAL(16,6),@pmReteIca DECIMAL(16,6),@pmBomberil DECIMAL(16,6),@pmImpAviTab DECIMAL(16,6),@pmOtrosCargos DECIMAL(16,6),@pmOtrosDctos DECIMAL(16,6),@pmValorTotal DECIMAL(16,6)
,@pmBaseRet DECIMAL(16,6),@pmBaseBom DECIMAL(16,6),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaBom DECIMAL(14,4),@pmTarifaAvta DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarifBom VARCHAR(4),@pmCodTarifAvta VARCHAR(4),@pmDescServicio VARCHAR(500),@pmCodRes VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmTipDocRef VARCHAR(3),@pmNumDocRef INT,@pmIdCiaRef CHAR(2),@pmAnulado BIT,@pmTipDocDev VARCHAR(3),@pmNumDev INT,@pmCdCiaDev CHAR(2),@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(500),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DocSoporte (TipDoc,NumDoc,IdCia,Fecha,IdTercero,IdAdquiriente,IdConcepto,IdMedPago,MetPago,Modalidad,NumFactura,FechaFac,FechaVence,SubTotal,Descuento,Retencion,ReteIca,Bomberil,ImpAviTab,OtrosCargos,OtrosDctos,ValorTotal,BaseRet,BaseBom,TarifaRet,TarifaIca,TarifaBom,TarifaAvta,CodTarRet,CodTarIca,CodTarifBom,CodTarifAvta,DescServicio,CodRes,CdCCosto,CdSubCos,TipCom,Comprobante,IdCiaCom,TipDocRef,NumDocRef,IdCiaRef,Anulado,TipDocDev,NumDev,CdCiaDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmNumDoc,@pmIdCia,@pmFecha,@pmIdTercero,@pmIdAdquiriente,@pmIdConcepto,@pmIdMedPago,@pmMetPago,@pmModalidad,@pmNumFactura,@pmFechaFac,@pmFechaVence,@pmSubTotal,@pmDescuento,@pmRetencion,@pmReteIca,@pmBomberil,@pmImpAviTab,@pmOtrosCargos,@pmOtrosDctos,@pmValorTotal,@pmBaseRet,@pmBaseBom,@pmTarifaRet,@pmTarifaIca,@pmTarifaBom,@pmTarifaAvta,@pmCodTarRet,@pmCodTarIca,@pmCodTarifBom,@pmCodTarifAvta,@pmDescServicio,@pmCodRes,@pmCdCCosto,@pmCdSubCos,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmTipDocRef,@pmNumDocRef,@pmIdCiaRef,@pmAnulado,@pmTipDocDev,@pmNumDev,@pmCdCiaDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryDocSoporte] @pmTipDoc VARCHAR(3),@pmNumDoc INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumDoc,IdCia,Fecha,IdTercero,IdAdquiriente,IdConcepto,IdMedPago,MetPago,Modalidad,NumFactura,FechaFac,FechaVence,SubTotal,Descuento,Retencion,ReteIca,Bomberil
,ImpAviTab,OtrosCargos,OtrosDctos,ValorTotal,BaseRet,BaseBom,TarifaRet,TarifaIca,TarifaBom,TarifaAvta,CodTarRet,CodTarIca,CodTarifBom,CodTarifAvta,DescServicio,CodRes,CdCCosto,CdSubCos,TipCom,Comprobante,IdCiaCom
,TipDocRef,NumDocRef,IdCiaRef,Anulado,TipDocDev,NumDev,CdCiaDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DocSoporte
 WHERE TipDoc=@pmTipDoc AND NumDoc=@pmNumDoc AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDocSoporteRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmnIdTercero VARCHAR(16)=Null
AS 
 SELECT D.TipDoc,TipoDoc,D.NumDoc,D.IdCia AS CdCia,CI.Compania,D.Fecha,D.IdTercero AS NitTercero,T.RazonSocial,D.IdAdquiriente,N.RazonSocial AS Adquiriente
 ,D.Modalidad,D.IdConcepto,Concepto,D.IdMedPago,MedioPago,D.MetPago,D.NumFactura,D.FechaFac,D.FechaVence,D.SubTotal,D.Descuento,D.Retencion,D.ReteIca,D.Bomberil,D.ImpAviTab,D.OtrosCargos,D.OtrosDctos,D.ValorTotal
 ,D.BaseRet,D.BaseBom,D.TarifaRet,D.TarifaIca,D.TarifaBom,D.TarifaAvta,D.CodTarRet,D.CodTarIca,D.CodTarifBom,D.CodTarifAvta,D.DescServicio
 ,D.CodRes,R.Resolucion,R.Prefijo,R.NumInicial,R.NumFinal,R.FechaExpRes,R.FechaVigencia,R.Establecimiento,D.CdCCosto,CCosto,D.CdSubCos,SubCosto,D.TipCom,TipoCom,D.Comprobante,D.IdCiaCom,D.TipDocRef,D.NumDocRef,D.IdCiaRef
,D.Anulado,D.TipDocDev,D.NumDev,D.CdCiaDev,D.FecDev,D.Observacion,D.IdEstado AS CdEstado,ED.Estado,D.TimeSys AS FechaCrea,D.OrigenAdd,D.FecUpdate,D.IdCiaCrea,D.IdUsuario,Usuario
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,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.e_mail AS TercEmail,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco
,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
FROM Trn_DocSoporte 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 Terceros AS T ON D.IdTercero=T.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
LEFT JOIN TiposComRes AS R ON D.CodRes=R.IdRes LEFT JOIN Terceros AS N ON D.IdAdquiriente=N.IdTercero
LEFT JOIN Conceptos AS CN ON D.IdConcepto=CN.IdConcepto LEFT JOIN NomMediosPago AS MP ON D.IdMedPago=MP.IdMedPago
LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto
LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos
WHERE D.TipDoc=@pmTipDoc AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
AND (D.IdCia=@pmIdCia OR @pmIdCia IS NULL)
AND D.IdTercero LIKE ISNULL(@pmnIdTercero,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDocSoporteFmt] @pmTipDoc VARCHAR(3),@pmNumDocIni INT,@pmNumDocFin INT,@pmIdCia CHAR(2)
AS 
 SELECT D.TipDoc,TipoDoc,D.NumDoc,D.IdCia AS CdCia,CI.Compania,D.Fecha,D.IdTercero AS NitTercero,T.RazonSocial,D.IdAdquiriente,N.RazonSocial AS Adquiriente
 ,D.Modalidad,D.IdConcepto,Concepto,D.IdMedPago,MedioPago,D.MetPago,D.NumFactura,D.FechaFac,D.FechaVence,D.SubTotal,D.Descuento,D.Retencion,D.ReteIca,D.Bomberil,D.ImpAviTab,D.OtrosCargos,D.OtrosDctos,D.ValorTotal
 ,D.BaseRet,D.BaseBom,D.TarifaRet,D.TarifaIca,D.TarifaBom,D.TarifaAvta,D.CodTarRet,D.CodTarIca,D.CodTarifBom,D.CodTarifAvta,D.DescServicio
,D.CdCCosto,CCosto,D.CdSubCos,SubCosto,D.TipCom,TipoCom,D.Comprobante,D.IdCiaCom,D.TipDocRef,D.NumDocRef,D.IdCiaRef,D.Anulado,D.TipDocDev,D.NumDev,D.CdCiaDev,D.FecDev
,D.Observacion,D.IdEstado AS CdEstado,ED.Estado,D.TimeSys AS FechaCrea,D.OrigenAdd,D.FecUpdate,D.IdCiaCrea,D.IdUsuario,Usuario
--detalles
,DT.Item,DT.IdProducto,P.DescripProd,DT.Descripcion,DT.Cantidad,DT.VrUnitario,DT.ValorOper,DT.VrDcto,DT.VrRetencion,DT.VrRetIca,DT.VrBomberil,DT.VrAviTab,DT.TarifaDct AS PorcDcto
,DT.TarifaRet AS PorcRet,DT.TarifaIca AS PorcICA,DT.TarifaBom AS PorcBom,DT.TarifaAvta AS PorcAvta,DT.CodTarDct AS DetCodDcto,DT.CodTarRet AS DetCodRet
,DT.CodTarIca AS DetCodIca,DT.CodTarifBom AS DetCodBom,DT.CodTarifAvta AS DetCodAvta,DT.ItemCom,DT.CdCuenta,PC.NomCuenta,DT.CdCueCre
--Datos resolución
,D.CodRes,R.Resolucion,R.Prefijo,R.NumInicial,R.NumFinal,R.FechaExpRes,R.FechaVigencia,R.Establecimiento
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,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.e_mail AS TercEmail,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco
,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
FROM Trn_DocSoporte 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 Terceros AS T ON D.IdTercero=T.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 Trn_DocSoporteDet AS DT ON D.TipDoc=DT.TipDoc AND D.NumDoc=DT.NumDoc AND D.IdCia=DT.IdCia
LEFT JOIN ProdMcias AS P ON DT.IdProducto=P.IdProducto
LEFT JOIN Conceptos AS CN ON D.IdConcepto=CN.IdConcepto LEFT JOIN NomMediosPago AS MP ON D.IdMedPago=MP.IdMedPago
LEFT JOIN TiposComRes AS R ON D.CodRes=R.IdRes LEFT JOIN Terceros AS N ON D.IdAdquiriente=N.IdTercero
LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto
LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos LEFT JOIN Puc AS PC ON DT.CdCuenta=PC.IdCuenta
WHERE D.TipDoc=@pmTipDoc AND D.NumDoc BETWEEN @pmNumDocIni AND @pmNumDocFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpDocSoporte] @pmTipDoc VARCHAR(3),@pmNumDoc INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdTercero VARCHAR(16),@pmIdAdquiriente VARCHAR(16),@pmIdConcepto VARCHAR(4),@pmIdMedPago VARCHAR(4),@pmMetPago INT,@pmModalidad VARCHAR(20),@pmNumFactura VARCHAR(15),@pmFechaFac SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmSubTotal DECIMAL(16,6),@pmDescuento DECIMAL(16,6),@pmRetencion DECIMAL(16,6),@pmReteIca DECIMAL(16,6),@pmBomberil DECIMAL(16,6),@pmImpAviTab DECIMAL(16,6),@pmOtrosCargos DECIMAL(16,6),@pmOtrosDctos DECIMAL(16,6),@pmValorTotal DECIMAL(16,6),@pmBaseRet DECIMAL(16,6),@pmBaseBom DECIMAL(16,6),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaBom DECIMAL(14,4),@pmTarifaAvta DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarifBom VARCHAR(4),@pmCodTarifAvta VARCHAR(4),@pmDescServicio VARCHAR(500),@pmCodRes VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmTipDocRef VARCHAR(3),@pmNumDocRef INT,@pmIdCiaRef CHAR(2),@pmAnulado BIT,@pmTipDocDev VARCHAR(3),@pmNumDev INT,@pmCdCiaDev CHAR(2),@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(500),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_DocSoporte SET Fecha=@pmFecha,IdTercero=@pmIdTercero,IdAdquiriente=@pmIdAdquiriente,IdConcepto=@pmIdConcepto,IdMedPago=@pmIdMedPago,MetPago=@pmMetPago,Modalidad=@pmModalidad,NumFactura=@pmNumFactura,FechaFac=@pmFechaFac,FechaVence=@pmFechaVence,SubTotal=@pmSubTotal,Descuento=@pmDescuento,Retencion=@pmRetencion,ReteIca=@pmReteIca,Bomberil=@pmBomberil,ImpAviTab=@pmImpAviTab,OtrosCargos=@pmOtrosCargos,OtrosDctos=@pmOtrosDctos,ValorTotal=@pmValorTotal,BaseRet=@pmBaseRet,BaseBom=@pmBaseBom,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaBom=@pmTarifaBom,TarifaAvta=@pmTarifaAvta,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarifBom=@pmCodTarifBom,CodTarifAvta=@pmCodTarifAvta,DescServicio=@pmDescServicio,CodRes=@pmCodRes,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,TipDocRef=@pmTipDocRef,NumDocRef=@pmNumDocRef,IdCiaRef=@pmIdCiaRef,Anulado=@pmAnulado,TipDocDev=@pmTipDocDev,NumDev=@pmNumDev,CdCiaDev=@pmCdCiaDev
,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumDoc=@pmNumDoc AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,E.FechaFac,E.FechaVence
,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrImpCarbono,VrIvaIngProd,VrNeto
,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,CodTarRetFle,CodTarIcaFle,FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos,E.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,CxPagar,DocEquiv,E.BaseImp,E.BaseRet,E.BaseIca,BaseRiv,BaseIvaIgp
,E.TarifaIva AS TarifIva,E.TarifaRet AS TarifRet,E.TarifaIca AS TarifIca,TarifaRiv,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,E.CodTarifBom,E.VrBomberil,E.TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpuCons,VrSobtasaCons,E.ImpAviTab,E.TarifaAvta,E.CodTarifAvta
,E.CdCCosto AS CodCenCosto,CC.CCosto AS CentCosto,E.CdSubCos AS CodSubCent,SC.SubCosto AS SubcCosto,E.TipEgr,E.Egreso,E.IdCiaEgr,E.Modalidad,TipoSA,NumSalAlm,CdCiaSA,TipSop,NumDocSop,CdCiaSop
,E.TipCom,TipoCom,E.Comprobante,E.IdCiaCom,E.Anulado,E.NumDev,E.FecDev,E.Observacion AS Observ,E.IdEstado AS CdEstado,Estado,E.OrigenAdd,E.TimeSys AS Fec_add,E.FecUpdate AS Fec_Update,E.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,K.pVehiculo,K.Referencia AS KarReferencia,Descripcion,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,Remision,IdCiaRem,K.Factura AS KarFactura,K.TipDocDev,K.NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal
,OtroImpto,Unidades,ItemCombo,Servcios,EsCombo,EsProdBase,K.CdMoneda,K.VrTasaCamb,K.VrDivisa1,K.VrDivisa2,K.VrDivisa3,Referencia2,FecOrden,CodTarIco,TarifaIco,VrImpCon,TarifaStc,SobtasaCons,VrImvCosto,K.BaseIvp,K.TarifaIvp,K.IvaIngProd
--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
--datos memo
,Comentarios,CantImp
--datos de resolucion doc. equivalente
,DS.CodRes,DS.DescServicio,RS.Resolucion,RS.Prefijo,RS.NumInicial,RS.NumFinal,RS.FechaExpRes,RS.FechaVigencia,RS.Establecimiento
 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
 LEFT JOIN Trn_DocMemo AS MM ON E.TipDoc=MM.TipDoc AND E.Entrada=MM.Documento AND E.IdCia=MM.IdCia
 --datos de resolucion
 LEFT JOIN Trn_DocSoporte DS ON E.TipSop=DS.TipDoc AND E.NumDocSop=DS.NumDoc AND E.CdCiaSop=DS.IdCia
 LEFT JOIN TiposComRes AS RS ON DS.CodRes=RS.IdRes
 WHERE E.TipDoc=@pmTipDoc AND Entrada BETWEEN @pmEntradaIni AND @pmEntradaFin  AND E.IdCia=@pmIdCia
 ORDER BY Entrada,Item
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrTarifa,TipoTarif,CodConc,CodTarif
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmVrTarifa DECIMAL(14,4)
,@pmTipoTarif CHAR(1),@pmCodConc VARCHAR(4),@pmCodTarif VARCHAR(4)
 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,VrTarifa,TipoTarif,CodConc,CodTarif)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmConcepto,@pmItem,@pmIdCuenta,@pmValor,@pmDetalle,@pmIdTercero,@pmCodAgncia,@pmCdCCosto,@pmCdSubCos
,@pmpVehiculo,@pmVrBase,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmItemFac,@pmFecVence,@pmCheque,@pmCiuOrigen,@pmReferencia,@pmVrTarifa,@pmTipoTarif,@pmCodConc,@pmCodTarif)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmTarifa,tmTipoTarif,tmCodConc,tmCodTarif)
 SELECT @pmtmNumero,Item,Concepto,IdCuenta,Valor,Detalle,IdTercero,CodAgncia,CdCCosto,CdSubCos,pVehiculo,VrBase,TipFac,Factura,IdCiaFac,ItemFac
,FecVence,Cheque,CiuOrigen,Referencia,VrTarifa,TipoTarif,CodConc,CodTarif
 FROM Trn_DetCuentas
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND Concepto LIKE ISNULL(@pmConcepto,'%')
 ORDER BY  Item
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrTarifa,TipoTarif,CodConc,CodTarif
 FROM Trn_DetCuentas
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND (Concepto=@pmConcepto OR @pmConcepto IS NULL)
 AND (Item>=ISNULL(@pmItem,0) AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY  Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDetCuentasRci] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null
AS 
SELECT D.TipDoc,D.Documento,D.IdCia AS CdCia,D.Concepto,D.IdCuenta,NomCuenta,D.VrBase,D.VrTarifa AS Tarifa,D.TipoTarif
,D.Valor,D.CodTarif,T.CdLocdad,L.Localidad,L.IdDep,DP.Departamento
FROM Trn_DetCuentas AS D INNER JOIN (SELECT TipDoc,Recibo,IdCia,Fecha,IdCliente,VrNeto FROM Trn_Recibos WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin
UNION ALL SELECT TipDev,Devolucion,IdCia,Fecha,IdCliente,VrNeto FROM Trn_DevRec WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin) AS R ON D.TipDoc=R.TipDoc AND D.Documento=R.Recibo AND D.IdCia=R.IdCia
LEFT JOIN Tablapor AS T ON D.CodTarif=T.IdTarifa LEFT JOIN Localidades AS L ON T.CdLocdad=L.IdLocal LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
LEFT JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
WHERE D.TipDoc IN ('REC','ODB') AND D.Concepto='ICA' AND R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
AND (D.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (R.IdCliente=@pmIdCliente OR @pmIdCliente IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMttoTiposOdt] @pmIdTipoOdt VARCHAR(4),@pmTipoOrden VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE MttoTiposOdt SET TipoOrden=@pmTipoOrden,Inactivo=@pmInactivo
 WHERE IdTipoOdt=@pmIdTipoOdt
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoOrdenLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null
 AS 
 SELECT NumOrden,O.IdCia AS CdCia,Compania,Fecha,FechaOrden,FechaVence,O.IdVehiculo,kmVehiculo,nRemolque,kmRemolque,O.IdTipoOdt AS CdTipoOdt,TipoOrden,O.IdCenSer AS CdCenSer,CentroServ
,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,O.IdOperario AS NitOperario,T.RazonSocial AS NomOperario,IdSupervisor,NS.RazonSocial AS Supervisor,O.Modalidad
,VrServicios,VrRepuestos,VrManoObra,VrOtros,FechaCierre,NitCenSer,O.CdEstadoVeh,EV.Estado AS EstadoVeh,O.IdEstado AS CdEstado,ED.Estado AS EstadoDoc
,O.Anulado,NumDev,FecDev,O.Observacion AS Observ,O.TipFact,O.NumFactura,O.IdCiaFact,O.TimeSys AS FechaCrea,O.FecUpdate AS FecActualiza,O.IdCiaCrea AS CdCIaCrea,O.OrigenAdd,O.IdUsuario AS CdUsuario,Usuario
 FROM Trn_MttoOrden AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN MttoTiposOdt AS TOT ON O.IdTipoOdt=TOT.IdTipoOdt INNER JOIN CentrosServ AS CS ON O.IdCenSer=CS.IdCenSer
 INNER JOIN Terceros AS T ON O.IdOperario=T.IdTercero INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN Terceros AS NS ON O.IdSupervisor=NS.IdTercero
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 LEFT JOIN EstadoVeh AS EV ON O.CdEstadoVeh=EV.IdEstado
 WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoOrdenRelDet] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null
AS 
 SELECT O.TipDoc AS IdTipDoc,TipoDoc,O.NumOrden,O.IdCia AS CdCia,Compania,Fecha,FechaOrden,FechaVence,O.IdVehiculo,NumVeh,kmVehiculo,nRemolque,kmRemolque,O.IdTipoOdt AS CdTipoOdt,TipoOrden
,O.IdCenSer AS CdCenSer,CentroServ,NitCenSer,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,IdSupervisor,NSU.RazonSocial AS Supervisor
--<campos de detalles
,D.Item,D.TipoItem,D.IdConc AS CdConc,D.Descripcion,D.Cantidad,D.IdUnd,D.UndMed,D.VrUnitario,D.TipoServ,D.IdOperario AS NitOperario,T.RazonSocial AS Operario,D.IdEstado AS CdEstadoServ,ESV.Estado AS EstadoServ,ESV.NColor AS ES_NumColor
,D.CdMtto,TipoMtto,C.IdSistema AS CdSistema,Sistema,Seccion,Concepto,D.IdProducto,DescripProd,D.FechaServ,D.NumParte,D.TipReq,D.Requisicion,D.IdCiaReq,TipSal,NumSalida,IdCiaSal,CantSalida,D.EstadoReq,C.TarifaClie,Req_Rep,Req_MO
,CASE ISNULL(D.TipoReg,0) WHEN 0 THEN 'SERVICIO' ELSE 'REQ' END AS Tipo_Reg
--campos de detalles>
,O.Modalidad,VrServicios,VrRepuestos,VrManoObra,VrOtros,O.IdEstado AS CdEstado,ED.Estado AS EstadoDoc,ED.NColor AS EdNumColor,FechaCierre,O.Anulado,NumDev,FecDev
,O.Observacion AS Observ,O.TipFact,O.NumFactura,O.IdCiaFact,O.TimeSys AS FechaCrea,O.FecUpdate AS FecActualiza,O.IdCiaCrea AS CdCIaCrea,O.OrigenAdd,O.IdUsuario AS CdUsuario,Usuario,Leyenda
--campos de vehículo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad
,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config,V.IdGrupo AS CdGrupo,GrupoProp
,PesoVacio,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPoseedor AS NitPoseedor,NPO.RazonSocial AS Poseedor
,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,V.Descripcion AS VehDescripcion,KmInicial,KmActual,Km2Actual AS kmRuta,CentFinal AS KmOtro,V.Ubicacion
,V.IdEstado AS VehCdEstado,EV.Estado AS VehEstado,EV.NColor AS VehEstColor,EV.OutDemand AS VehNoDisponible,V.Inactivo AS VehInactivo
,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
,ISNULL(RS.ReqAprob,0) AS Req_Aprob,ISNULL(RS.ReqNoAprob,0) AS Req_NoAprob
 FROM Trn_MttoOrden AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN MttoTiposOdt AS TOT ON O.IdTipoOdt=TOT.IdTipoOdt INNER JOIN CentrosServ AS CS ON O.IdCenSer=CS.IdCenSer
 INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN Terceros AS NSU ON O.IdSupervisor=NSU.IdTercero
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
  --<subconsulta detalles
 INNER JOIN (SELECT DS.TipOdt,DS.NumOrden,DS.IdCia,DS.Item,DS.FechaServ,DS.IdConc,DS.Descripcion,DS.Cantidad,DS.VrUnitario,'0' AS IdUnd,DS.UndMed,DS.CdMtto,DS.IdEstado,DS.IdOperario
 ,DS.NumVehic,DS.NumParte,DS.TipoServ,DS.FechaCrea,DS.IdUsuario,'0' AS TipReq,0 AS Requisicion,'00' AS IdCiaReq
 ,CASE DS.TipoServ WHEN 'EXTERNA' THEN 'MO' ELSE 'REPUESTO' END AS TipoItem,'0' AS IdProducto,0 AS EstadoReq,'0' AS TipSal,0 AS NumSalida,'00' AS IdCiaSal,0 AS CantSalida,0 AS TipoReg
,DS.Req_Rep,DS.Req_MO
 FROM Trn_MttoOrdenDet AS DS INNER JOIN Trn_MttoOrden AS O ON DS.TipOdt=O.TipDoc AND DS.NumOrden=O.NumOrden AND DS.IdCia=O.IdCia
 WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND DS.IdCia LIKE ISNULL(@pmIdCia,'%%') AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 UNION ALL
 SELECT R.TipOdt,R.NumOrden,R.IdCiaOdt,R.Item,R.FechaCrea,R.CdConcServ,R.Descripcion,R.Cantidad,R.VrUnitario,R.IdUnd,Unidad,C.IdMtto
 ,CASE R.EstadoReq WHEN -1 THEN '9999' WHEN 0 THEN '0001' WHEN 1 THEN '9998' ELSE '0001' END,R.IdOperario
 ,R.NumVehic,R.NumParte,'INTERNA',R.FechaCrea,R.IdUsuario,R.TipReq,R.Requisicion,R.IdCia,R.TipoItem,R.IdProducto,R.EstadoReq
 ,R.TipSal,R.NumSalida,R.IdCiaSal,R.CantSalida,1 AS TipoReg,0,0
 FROM Trn_MttoOrdenReq AS R INNER JOIN Trn_MttoOrden AS O ON R.TipOdt=O.TipDoc AND R.NumOrden=O.NumOrden AND R.IdCiaOdt=O.IdCia
 INNER JOIN Trn_Requisicion AS RQ ON R.TipReq=RQ.TipDoc AND R.Requisicion=RQ.Requisicion AND R.IdCia=RQ.IdCia 
 LEFT JOIN MttoConceptos AS C ON R.CdConcServ=C.IdConc LEFT JOIN UndMed AS UM ON R.IdUnd=UM.IdUnd
 WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND R.IdCiaOdt LIKE ISNULL(@pmIdCia,'%%') AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') ) AS D ON O.TipDoc=D.TipOdt AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
 --subconsulta detalles>
 INNER JOIN Terceros AS T ON D.IdOperario=T.IdTercero INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 LEFT JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 LEFT JOIN Marcas AS M ON V.IdMarca=M.IdMarca LEFT JOIN TiposCol AS CL ON V.IdColor=CL.IdColor 
 LEFT JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria LEFT JOIN Terceros AS NPO ON V.IdPoseedor=NPO.IdTercero
 LEFT JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 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 O.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 LEFT JOIN MttoConceptos AS C ON D.IdConc=C.IdConc LEFT JOIN MttoSistemas AS S ON C.IdSistema=S.IdSistema
 LEFT JOIN MttoTipos AS TM ON D.CdMtto=TM.IdMtto LEFT JOIN MttoEstados AS ESV ON D.IdEstado=ESV.IdEstado
 LEFT JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
--<consulta requisiciones aprobadas
 LEFT JOIN (SELECT R.TipOdt,R.NumOrden,R.IdCiaOdt,SUM(CASE WHEN (RQ.IdEstado='0002' OR RQ.NumAprob<=0) THEN 1 ELSE 0 END) AS ReqNoAprob
 ,SUM(CASE WHEN RQ.IdEstado<>'0002' AND RQ.NumAprob>0 THEN 1 ELSE 0 END) AS ReqAprob
  FROM Trn_MttoOrdenReq AS R INNER JOIN Trn_Requisicion AS RQ ON R.TipReq=RQ.TipDoc AND R.Requisicion=RQ.Requisicion AND R.IdCia=RQ.IdCia
  WHERE RQ.Anulado=0 GROUP BY R.TipOdt,R.NumOrden,R.IdCiaOdt) AS RS ON O.TipDoc=RS.TipOdt AND O.NumOrden=RS.NumOrden AND O.IdCia=RS.IdCiaOdt
--fin consulta requisiciones aprobadas>
 WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoDiagnFmt] @pmTipOdt VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2) 
AS 
 SELECT DG.TipOdt,DG.NumOrden,DG.IdCia AS CdCia,Compania,DG.NumDiagn,FechaDiagn,TipoUnidad,NumVehic,DG.IdOperario,T.RazonSocial AS NomOperario
,DG.FechaCrea,DG.IdUsuario AS CdUsuario,Usuario,FechaOrden,O.IdTipoOdt AS CdTipoOdt,TipoOrden,O.IdCenSer AS CdCenSer,CentroServ
,O.Modalidad,O.kmVehiculo,O.kmRemolque,O.IdEstado AS CdEstado,Estado
,Item,D.IdInspec AS CdInspec,Inspeccion,ValorInspec,UndMedida,NumColor,D.Req_Mtto,D.Intervalo,D.Observacion AS Observ
,I.IdSistema AS CdSistema,Sistema,I.IdMtto AS CdMtto,TipoMtto,TipoDato
FROM Trn_MttoDiagn AS DG INNER JOIN Trn_MttoDiagnDet AS D ON DG.TipOdt=D.TipOdt AND DG.NumOrden=D.NumOrden AND DG.IdCia=D.IdCia AND DG.NumDiagn=D.NumDiagn
INNER JOIN Companias AS CN ON DG.IdCia=CN.IdCia INNER JOIN Terceros AS T ON DG.IdOperario=T.IdTercero
INNER JOIN adm_Usuarios AS U ON DG.IdUsuario=U.IdUsuario 
INNER JOIN MttoInspecciones AS I ON D.IdInspec=I.IdInspec INNER JOIN MttoSistemas AS S ON I.IdSistema=S.IdSistema
INNER JOIN MttoTipos AS TM ON I.IdMtto=TM.IdMtto
INNER JOIN Trn_MttoOrden AS O ON DG.TipOdt=O.TipDoc AND DG.NumOrden=O.NumOrden AND DG.IdCia=O.IdCia
INNER JOIN MttoTiposOdt AS TOT ON O.IdTipoOdt=TOT.IdTipoOdt INNER JOIN CentrosServ AS CS ON O.IdCenSer=CS.IdCenSer
INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado 
WHERE DG.TipOdt=@pmTipOdt AND DG.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND DG.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoOrdenRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null
AS 
 SELECT O.TipDoc AS IdTipDoc,TipoDoc,O.NumOrden,O.IdCia AS CdCia,Compania,Fecha,FechaOrden,FechaVence,O.IdVehiculo,NumVeh,kmVehiculo,nRemolque,kmRemolque,O.IdTipoOdt AS CdTipoOdt,TipoOrden,O.IdCenSer AS CdCenSer,CentroServ
,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,O.IdOperario AS NitOperario,T.RazonSocial AS NomOperario,IdSupervisor,NSU.RazonSocial AS Supervisor,O.Modalidad
,VrServicios,VrRepuestos,VrManoObra,VrOtros,FechaCierre,NitCenSer,O.CdEstadoVeh,EV.Estado AS EstadoVeh,EV.NColor AS EdvNColor,EV.OutDemand,O.IdEstado AS CdEstado,ED.Estado AS EstadoDoc,ED.NColor AS EdNumColor
,O.Anulado,NumDev,FecDev,O.Observacion AS Observ,O.TipFact,O.NumFactura,O.IdCiaFact,O.TimeSys AS FechaCrea,O.FecUpdate AS FecActualiza,O.IdCiaCrea AS CdCIaCrea,O.OrigenAdd,O.IdUsuario AS CdUsuario,Usuario,Leyenda
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad
,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPoseedor AS NitPoseedor,NPO.RazonSocial AS Poseedor
,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,V.Descripcion AS VehDescripcion,KmInicial,KmActual,Km2Actual,V.Ubicacion
,V.IdEstado AS VehCdEstado,EVH.Estado AS VehEstado,EVH.NColor AS VehEstColor,EVH.OutDemand AS VehNoDisponible,V.Inactivo AS VehInactivo,V.IdGrupo AS CdGrupo,GrupoProp
,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
,ISNULL(RS.ReqAprob,0) AS Req_Aprob,ISNULL(RS.ReqNoAprob,0) AS Req_NoAprob
 FROM Trn_MttoOrden AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN MttoTiposOdt AS TOT ON O.IdTipoOdt=TOT.IdTipoOdt INNER JOIN CentrosServ AS CS ON O.IdCenSer=CS.IdCenSer
 INNER JOIN Terceros AS T ON O.IdOperario=T.IdTercero INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN Terceros AS NSU ON O.IdSupervisor=NSU.IdTercero
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 LEFT JOIN EstadoVeh AS EV ON O.CdEstadoVeh=EV.IdEstado 
 LEFT JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 LEFT JOIN Marcas AS M ON V.IdMarca=M.IdMarca LEFT JOIN TiposCol AS CL ON V.IdColor=CL.IdColor 
 LEFT JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria LEFT JOIN Terceros AS NPO ON V.IdPoseedor=NPO.IdTercero
 LEFT JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 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 O.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN EstadoVeh AS EVH ON V.IdEstado=EVH.IdEstado LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
--consulta requisiciones aprobadas
 LEFT JOIN (SELECT R.TipOdt,R.NumOrden,R.IdCiaOdt,SUM(CASE WHEN (RQ.IdEstado='0002' OR RQ.NumAprob<=0) THEN 1 ELSE 0 END) AS ReqNoAprob
 ,SUM(CASE WHEN RQ.IdEstado<>'0002' AND RQ.NumAprob>0 THEN 1 ELSE 0 END) AS ReqAprob
  FROM Trn_MttoOrdenReq AS R INNER JOIN Trn_Requisicion AS RQ ON R.TipReq=RQ.TipDoc AND R.Requisicion=RQ.Requisicion AND R.IdCia=RQ.IdCia
  WHERE RQ.Anulado=0 GROUP BY R.TipOdt,R.NumOrden,R.IdCiaOdt) AS RS ON O.TipDoc=RS.TipOdt AND O.NumOrden=RS.NumOrden AND O.IdCia=RS.IdCiaOdt
--fin consulta requisiciones aprobadas
 WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMttoTiposOdt] @pmIdTipoOdt VARCHAR(4),@pmTipoOrden VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO MttoTiposOdt (IdTipoOdt,TipoOrden,Inactivo)
 VALUES (@pmIdTipoOdt,@pmTipoOrden,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMttoTiposOdt] @pmIdTipoOdt VARCHAR(4)
 AS 
 DELETE FROM MttoTiposOdt WHERE IdTipoOdt=@pmIdTipoOdt
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoTiposOdt] @pmIdTipoOdt VARCHAR(4)
 AS 
 IF @pmIdTipoOdt IS NULL
  BEGIN
   SELECT IdTipoOdt,TipoOrden FROM MttoTiposOdt WHERE Inactivo=0
  END
 ELSE
  BEGIN
   SELECT IdTipoOdt,TipoOrden,Inactivo FROM MttoTiposOdt WHERE IdTipoOdt=@pmIdTipoOdt
  END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoOrdenFmt] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2) 
AS 
 SELECT O.TipDoc AS IdTipDoc,TipoDoc,O.NumOrden,O.IdCia AS CdCia,Compania,Fecha,FechaOrden,FechaVence,O.IdVehiculo,NumVeh,kmVehiculo,nRemolque,kmRemolque,O.IdTipoOdt AS CdTipoOdt,TipoOrden,O.IdCenSer AS CdCenSer,CentroServ
,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,O.IdOperario AS NitOperario,T.RazonSocial AS NomOperario,IdSupervisor,NSU.RazonSocial AS Supervisor,O.Modalidad
,VrServicios,VrRepuestos,VrManoObra,VrOtros,FechaCierre,NitCenSer,O.CdEstadoVeh,EV.Estado AS EstadoVeh,EV.NColor AS EdvNColor,EV.OutDemand,O.IdEstado AS CdEstado,ED.Estado AS EstadoDoc,ED.NColor AS EdNumColor
,O.Anulado,NumDev,FecDev,O.Observacion AS Observ,O.TipFact,O.NumFactura,O.IdCiaFact,O.TimeSys AS FechaCrea,O.FecUpdate AS FecActualiza,O.IdCiaCrea AS CdCIaCrea,O.OrigenAdd,O.IdUsuario AS CdUsuario,Usuario
,D.Item,FechaServ,D.IdConc,D.Descripcion,Concepto,D.Cantidad,D.VrUnitario,UndMed,D.CdMtto,TipoMtto,D.IdEstado AS DetIdEstado,ESV.Estado AS DetEstado,ESV.NColor AS DetNumColor
,D.IdOperario AS DetNitOperario,NOD.RazonSocial AS DetOperario,NumVehic,NumParte,Req_Rep,Req_MO,CO.TarifaClie,D.FechaCrea AS DetFecCrea,D.IdUsuario AS DetCdUsuario,TipoServ,CO.IdSistema AS CdSistema,Sistema 
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad
,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPoseedor AS NitPoseedor,NPO.RazonSocial AS Poseedor
,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,V.Descripcion AS VehDescripcion,KmInicial,KmActual,Km2Actual,V.Ubicacion
,V.IdEstado AS VehCdEstado,EVH.Estado AS VehEstado,EVH.NColor AS VehEstColor,EVH.OutDemand AS VehNoDisponible,V.Inactivo AS VehInactivo
,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
 FROM Trn_MttoOrden AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN MttoTiposOdt AS TOT ON O.IdTipoOdt=TOT.IdTipoOdt INNER JOIN CentrosServ AS CS ON O.IdCenSer=CS.IdCenSer
 INNER JOIN Terceros AS T ON O.IdOperario=T.IdTercero INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN Terceros AS NSU ON O.IdSupervisor=NSU.IdTercero
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Trn_MttoOrdenDet AS D ON O.TipDoc=D.TipOdt AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
 INNER JOIN MttoConceptos AS CO ON D.IdConc=CO.IdConc INNER JOIN MttoSistemas AS S ON CO.IdSistema=S.IdSistema
 LEFT JOIN Terceros AS NOD ON D.IdOperario=NOD.IdTercero LEFT JOIN MttoTipos AS TM ON D.CdMtto=TM.IdMtto
 LEFT JOIN MttoEstados AS ESV ON D.IdEstado=ESV.IdEstado
 LEFT JOIN EstadoVeh AS EV ON O.CdEstadoVeh=EV.IdEstado 
 LEFT JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 LEFT JOIN Marcas AS M ON V.IdMarca=M.IdMarca LEFT JOIN TiposCol AS CL ON V.IdColor=CL.IdColor 
 LEFT JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria LEFT JOIN Terceros AS NPO ON V.IdPoseedor=NPO.IdTercero
 LEFT JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 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 O.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN EstadoVeh AS EVH ON V.IdEstado=EVH.IdEstado
 WHERE O.TipDoc=@pmTipDoc AND O.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND O.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_RmtD] @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null,@pmTipDoc VARCHAR(3)=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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,R.TipDoc,R.NumOrden,R.IdCia,R.Fecha,CdConcepto,R.IdCliente,R.IdAgencia,D.PesoNeto*D.TarifClie,0,0,0,0,0,0,D.VrSeguro,D.PesoNeto*D.TarifPago,0,0,0,D.PesoNeto*D.TarifTabla,D.PesoNeto*D.TarifClie
,0,0,D.VrDeclarado,0,D.Cantidad,D.IdMercancia,D.PesoNeto,0,R.CdForma,RA.TipoRuta,'',CASE WHEN LEN(D.DescripMcias)>150 THEN SUBSTRING(D.DescripMcias,1,150) ELSE D.DescripMcias END,'',R.FecDespacho,RA.IdVend,0,'0',R.IdUsuario
,R.IdLocFletes,D.Referencia1,D.Referencia2,CASE WHEN LEN(D.Remitente)>150 THEN SUBSTRING(D.Remitente,1,150) ELSE D.Remitente END,R.Modalidad,R.IdVehiculo,R.TipDcm,R.NumDocmto,R.IdCiaDcm,R.TipCom,R.Comprobante,R.IdCiaCom,R.Anulado
,0,R.FecDev,R.Observacion,R.TimeSys,R.VrCargos,R.CdRutaTarif,D.IdOrigen,D.IdDestino,'0',R.IdConductor,D.NitRemite,D.NitDestntario,R.NumPedido,R.IdCiaPed
FROM Trn_TraRemesa AS R INNER JOIN Trn_TraRemAnexo AS RA ON R.TipDoc=RA.TipDoc AND R.NumOrden=RA.NumOrden AND R.IdCia=RA.IdCia
INNER JOIN Trn_TraRemMcias AS D ON R.TipDoc=D.TipDoc AND R.NumOrden=D.NumOrden AND R.IdCia=D.IdCia
WHERE R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (R.TipDoc=@pmTipDoc OR @pmTipDoc IS NULL)
AND (R.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (R.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Muc] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,M.TipDoc,M.Manifiesto,M.IdCia,M.Fecha,'0',M.IdPropietario,'0',M.VrFletes,M.VrDescuento,0,0,M.VrRetencion,M.VrReteIca,0,M.VrPagos,M.VrCargos,M.VrDctos,0,0,M.VrFletes,M.VrNeto,0,0,M.VrAnticipo,M.VrAntAdic
,M.Cantidad,'0',M.PesoTotal,0,'',MA.TipoRuta,'','','',M.FecDespacho,M.CdCondRelev,0,'0',M.IdUsuario,M.IdLocFletes,M.nRemolque,M.TipoAfiVehic,MA.NomRemite,MA.TipoMintrans,M.IdVehiculo
,'RMT',M.Remesa,M.IdCiaRem,M.TipCom,M.Comprobante,M.IdCiaCom,M.Anulado,0,M.FecDev,M.Observacion,M.TimeSys,0,M.IdRuta,M.IdOrigen,M.IdDestino,M.IdPoseedor,M.IdConductor,'','',M.Cumplido,M.IdCiaCump
FROM Trn_TraManifiesto AS M INNER JOIN Trn_TraManifAnexo AS MA ON M.TipDoc=MA.TipDoc AND M.Manifiesto=MA.Manifiesto AND M.IdCia=MA.IdCia
WHERE M.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (M.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (M.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_MucD] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,M.TipDoc,M.Manifiesto,M.IdCia,M.Fecha,R.CdConcepto,R.IdCliente,R.IdAgencia,D.PesoNeto*MR.TarifPago,0,0,0,0,0,0,D.PesoNeto*MR.TarifTabla,0,0,0,0,D.PesoNeto*MR.TarifPago,D.PesoNeto*MR.TarifPago,0,0,M.VrAnticipo,M.VrAntAdic
,D.Cantidad,D.IdMercancia,D.PesoNeto,0,'',MA.TipoRuta,'',CASE WHEN LEN(D.DescripMcias)>150 THEN SUBSTRING(D.DescripMcias,1,150) ELSE D.DescripMcias END,'',M.FecDespacho,M.CdCondRelev,0,'0',M.IdUsuario
,M.IdLocFletes,D.Referencia1,D.Referencia2,CASE WHEN LEN(D.Remitente)>150 THEN SUBSTRING(D.Remitente,1,150) ELSE D.Remitente END,MA.TipoMintrans,M.IdVehiculo,MR.TipRem,MR.Remesa,MR.IdCiaRem,M.TipCom,M.Comprobante,M.IdCiaCom
,M.Anulado,0,M.FecDev,M.Observacion,M.TimeSys,0,M.IdRuta,D.IdOrigen,D.IdDestino,M.IdPoseedor,M.IdConductor,D.NitRemite,D.NitDestntario,R.NumPedido,R.IdCiaPed
FROM Trn_TraManifiesto AS M INNER JOIN Trn_TraManifAnexo AS MA ON M.TipDoc=MA.TipDoc AND M.Manifiesto=MA.Manifiesto AND M.IdCia=MA.IdCia
INNER JOIN Trn_TraManifRem AS MR ON M.TipDoc=MR.TipDoc AND M.Manifiesto=MR.Manifiesto AND M.IdCia=MR.IdCia
INNER JOIN Trn_TraRemMcias AS D ON MR.TipRem=D.TipDoc AND MR.Remesa=D.NumOrden AND MR.IdCiaRem=D.IdCia AND MR.ItemRem=D.Item
INNER JOIN Trn_TraRemesa AS R ON MR.TipRem=R.TipDoc AND MR.Remesa=R.NumOrden AND MR.IdCiaRem=R.IdCia
WHERE M.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (M.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (M.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Dcu] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,D.TipDev,D.Devolucion,D.IdCia,D.Fecha,ISNULL(D.IdConcepto,''),M.IdPropietario,'0',CR.TotalCobro,0,0,0,0,0,0,C.VrAdicFlete,C.VrAdicCargue,C.VrAdicDescargue,0,0,CR.TotalPagos,CR.TotalPagos,0,0,0,C.VrAdicAnticipo
,CR.Cantidad,'0',CR.PesoTotal,0,'','','','','',C.FecPago,'0',0,'0',C.IdUsuario,C.CdOrigen,M.nRemolque,M.TipoAfiVehic,'',D.ModdDev,C.IdVehiculo
,C.TipMuc,C.Manifiesto,C.IdCiaMuc,D.TipCom,D.Comprobante,D.IdCiaCom,1,0,D.Fecha,D.Observacion,D.TimeSys,0,C.CdRuta,C.CdOrigen,C.CdDestino,M.IdPoseedor,M.IdConductor,'','',D.Cumplido,D.IdCiaDoc
FROM Trn_TraDevCum AS D INNER JOIN Trn_TraCumplido AS C ON D.TipDoc=C.TipDoc AND D.Cumplido=C.Cumplido AND D.IdCiaDoc=C.IdCia
INNER JOIN Trn_TraManifiesto AS M ON C.TipMuc=M.TipDoc AND C.Manifiesto=M.Manifiesto AND C.IdCiaMuc=M.IdCia
LEFT JOIN (SELECT TipDoc,Cumplido,IdCia,SUM(Cantidad) AS Cantidad,SUM(PesoNeto) AS PesoTotal,SUM(PesoNeto*TarifClie) AS TotalCobro,SUM(PesoNeto*TarifPago) AS TotalPagos
FROM Trn_TraCumRemesas GROUP BY TipDoc,Cumplido,IdCia) AS CR ON D.TipDoc=CR.TipDoc AND D.Cumplido=CR.Cumplido AND D.IdCiaDoc=CR.IdCia
WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (D.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (D.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpConsumo,VrNeto
,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,D.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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpConsumo,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 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 Terceros AS V ON  D.IdVend=V.IdTercero 
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Dlg] @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null,@pmTipDev VARCHAR(3)=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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,D.TipDev,D.Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.IdPropietario,'0',D.VrGastos,0,0,0,0,0,0,0,D.VrOtrosCred,0,0,0,D.VrCombustible,D.VrAnticipos,0,0,D.VrDiferencia,0
,D.galsComb,'0',D.PesoTotal,L.LiqMixto,'','','',L.DescMcias,'',D.FecDoc,D.NitEmpresa,0,'0',D.IdUsuario,L.IdLocOrigen,L.Referencia,D.ModdDev,'',D.Modalidad,D.IdVehiculo
,D.TipMuc,D.Manifiesto,D.IdCiaMuc,D.TipCom,D.Comprobante,D.IdCiaCom,1,0,D.Fecha,D.Observacion,D.TimeSys,0,L.IdRuta,L.IdLocOrigen,L.IdLocDestino,D.IdPoseedor,D.IdConductor,'','',D.Liquidacion,D.IdCiaDoc
FROM Trn_TraDevLeg AS D INNER JOIN Trn_TraLiqViajes AS L ON D.TipDoc=L.TipDoc AND D.Liquidacion=L.Liquidacion AND D.IdCiaDoc=L.IdCia
WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (D.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (D.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
AND (D.TipDev=@pmTipDev OR @pmTipDev IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Dao] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,D.TipDev,D.Devolucion,D.IdCia,D.Fecha,D.IdConcepto,O.IdCliente,O.IdAgencia,O.VrCosto,0,0,0,0,0,0,A.VrAnticipo,0,0,0,0,0,D.VrDevolucion,0,A.VrAbonado,0,0
,0,'0',O.Cantidad,CASE WHEN A.TipoPago='EFECTIVO' THEN 1 ELSE 0 END,SUBSTRING(A.TipoPago,1,3),A.NumCheque,'',A.TipoPago,D.CdCta,A.FecCheque,A.CedBenef,0,'0',D.IdUsuario,O.IdOrigen,O.nRemolque,O.TipoAfiVehic,A.Beneficiario,D.ModdDev,O.IdVehiculo
,A.TipOds,A.NumOrden,A.IdCiaOds,D.TipCom,D.Comprobante,D.IdCiaCom,1,0,D.Fecha,D.Observacion,D.TimeSys,0,O.CdRuta,O.IdOrigen,O.IdDestino,O.IdPoseedor,O.IdConductor,'','',D.Anticipo,D.IdCiaDoc
FROM Trn_TraDevAos AS D INNER JOIN Trn_TraOrdenAnt AS A ON D.TipDoc=A.TipDoc AND D.Anticipo=A.Anticipo AND D.IdCiaDoc=A.IdCia
INNER JOIN Trn_TraOrdenServ AS O ON A.TipOds=O.TipDoc AND A.NumOrden=O.NumOrden AND A.IdCiaOds=O.IdCia
WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (D.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (D.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Opt] @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null,@pmTipDoc VARCHAR(3)=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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,L.TipDoc,L.Liquidacion,L.IdCia,L.Fecha,L.IdConcepto,O.IdCliente,O.IdAgencia,L.VrTotal,L.VrDescuento,L.VrIva,0,L.VrRetencion,L.VrReteIca,L.VrReteIva,L.VrOtros
,L.VrGastos,0,0,0,L.VrCosto,L.VrNeto,0,0,L.VrAnticipo,0,0,'0',L.Cantidad,L.CxPagar,'',L.NumCheque,'',O.TipoResiduo,'',L.FechaVence,L.IdConductor,0,'0',L.IdUsuario,O.IdOrigen,L.Referencia,L.TipoAfiVehic,O.NomRecibido,O.Modalidad,L.IdVehiculo
,L.TipOds,L.NumOrden,L.IdCiaOds,L.TipCom,L.Comprobante,L.IdCiaCom,L.Anulado,L.NumDev,L.FecDev,L.Observacion,L.TimeSys,L.VrReteCREE,O.CdRuta,O.IdOrigen,O.IdDestino,L.IdPoseedor,L.IdConductor,'','',L.Egreso,L.IdCiaEgr
FROM Trn_TraOrdenLiq AS L INNER JOIN Trn_TraOrdenServ AS O ON L.TipOds=O.TipDoc AND L.NumOrden=O.NumOrden AND L.IdCiaOds=O.IdCia
WHERE L.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (L.TipDoc=@pmTipDoc OR @pmTipDoc IS NULL)
AND (L.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (L.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Aos] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,A.TipDoc,A.Anticipo,A.IdCia,A.Fecha,A.IdConcepto,O.IdCliente,O.IdAgencia,O.VrCosto,0,0,0,0,0,0,O.VrAnticipo,0,0,0,0,0,A.VrAnticipo,0,A.VrAbonado,0,0
,0,'0',O.Cantidad,CASE WHEN A.TipoPago='EFECTIVO' THEN 1 ELSE 0 END,SUBSTRING(A.TipoPago,1,3),A.NumCheque,'',A.TipoPago,A.IdCta,A.FecCheque,A.CedBenef,0,'0',A.IdUsuario,O.IdOrigen,O.nRemolque,O.TipoAfiVehic,A.Beneficiario,A.TipoAncpo,O.IdVehiculo
,A.TipOds,A.NumOrden,A.IdCiaOds,A.TipCom,A.Comprobante,A.IdCiaCom,A.Anulado,A.NumDev,A.FecDev,A.Observacion,A.TimeSys,0,O.CdRuta,O.IdOrigen,O.IdDestino,O.IdPoseedor,O.IdConductor,'','',O.NumRemesa,O.CdCiaRem
FROM Trn_TraOrdenAnt AS A INNER JOIN Trn_TraOrdenServ AS O ON A.TipOds=O.TipDoc AND A.NumOrden=O.NumOrden AND A.IdCiaOds=O.IdCia
WHERE A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (A.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (A.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_PdtD] @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null,@pmTipDoc VARCHAR(3)=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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,P.TipDoc,P.Pedido,P.IdCia,P.Fecha,P.TipDoc,P.IdCliente,P.IdAgencia,D.TarifClie*D.PesoNeto,0,0,0,0,0,0,D.VrSeguro,0,0,0,0,D.TarifPago*D.PesoNeto,D.TarifClie*D.PesoNeto
,0,0,D.VrDeclarado,0,D.Cantidad,D.IdMercancia,D.PesoNeto,0,'',PA.TipoRuta,'',CASE WHEN LEN(D.DescripMcias)>150 THEN SUBSTRING(D.DescripMcias,1,150) ELSE D.DescripMcias END,'',P.FecDespacho,P.IdVend,0,'0',P.IdUsuario,D.IdOrigen
,D.Referencia1,D.Referencia2,CASE WHEN LEN(D.Remitente)>150 THEN SUBSTRING(D.Remitente,1,150) ELSE D.Remitente END,PA.TipoServicio,D.CdTipoVehic,P.TipRem,P.Remesa,P.IdCiaRem,'0',0,''
,P.Anulado,0,P.FecDev,P.Observacion,P.TimeSys,0,PA.CdRutaTarif,D.IdOrigen,D.IdDestino,'','',D.NitRemite,D.NitDestntario,P.Factura,P.IdCiaFac
FROM Trn_TraPedido AS P INNER JOIN Trn_TraPedAnexo AS PA ON P.TipDoc=PA.TipDoc AND P.Pedido=PA.Pedido AND P.IdCia=PA.IdCia
INNER JOIN Trn_TraPedMcias AS D ON P.TipDoc=D.TipDoc AND P.Pedido=D.Pedido AND P.IdCia=D.IdCia
WHERE P.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (P.TipDoc=@pmTipDoc OR @pmTipDoc IS NULL)
AND (P.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (P.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Ant] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,A.TipDoc,A.Anticipo,A.IdCia,A.Fecha,A.IdConcepto,M.IdPropietario,'0',M.VrFletes,M.VrDescuento,0,0,0,0,0,M.VrAnticipo+M.VrAntAdic,0,A.VrDeduccion,0,0,0,A.VrAnticipo,0,A.VrAbonado,0,0
,M.Cantidad,'0',M.PesoTotal,CASE WHEN A.TipoPago='EFECTIVO' THEN 1 ELSE 0 END,SUBSTRING(A.TipoPago,1,3),A.NumCheque,'',A.TipoPago,A.IdCta,A.FecCheque,A.CedBenef,0,'0',A.IdUsuario,M.IdOrigen,M.nRemolque,M.TipoAfiVehic,A.Beneficiario,A.Modalidad,A.IdVehiculo
,A.TipMuc,A.Manifiesto,A.IdCiaMuc,A.TipCom,A.Comprobante,A.IdCiaCom,A.Anulado,A.NumDev,A.FecDev,A.Observacion,A.TimeSys,0,M.IdRuta,M.IdOrigen,M.IdDestino,A.IdPoseedor,A.IdConductor,'','',A.NumCargue,A.CiaCargue
FROM Trn_TraAnticipos AS A INNER JOIN Trn_TraManifiesto AS M ON A.TipMuc=M.TipDoc AND A.Manifiesto=M.Manifiesto AND A.IdCiaMuc=M.IdCia
WHERE A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (A.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (A.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Ost] @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null,@pmTipDoc VARCHAR(3)=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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,O.TipDoc,O.NumOrden,O.IdCia,O.Fecha,O.TipDoc,O.IdCliente,O.IdAgencia,O.VrTotal,0,0,0,0,0,0,0,0,0,0,0,O.VrCosto,O.VrTotal
,0,0,0,0,0,'0',O.Cantidad,0,'',O.RemCliente,'',O.TipoResiduo,'',O.FecDespacho,O.IdConductor,0,'0',O.IdUsuario,O.IdOrigen,O.nRemolque,O.TipoAfiVehic,O.NomRecibido,O.Modalidad,O.IdVehiculo
,O.TipFact,O.NumFactura,O.CdCiaFact,'0',0,'00',O.Anulado,0,O.FecDev,O.Observacion,O.TimeSys,0,O.CdRuta,O.IdOrigen,O.IdDestino,O.IdPoseedor,O.IdConductor,'','',O.NumRemesa,O.CdCiaRem 
FROM Trn_TraOrdenServ AS O
WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (O.TipDoc=@pmTipDoc OR @pmTipDoc IS NULL)
AND (O.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (O.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpConsumo)
 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,VrImpCons
 FROM Trn_Factcon
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Dls] @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null,@pmTipDev VARCHAR(3)=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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,D.TipDev,D.Devolucion,D.IdCia,D.Fecha,D.IdConcepto,O.IdCliente,O.IdAgencia,D.VrTotal,D.VrDescuento,D.VrIva,0,D.VrRetencion,D.VrReteIca,D.VrReteIva,D.VrOtros
,D.VrGastos,0,0,0,D.VrCosto,D.VrNeto,0,0,D.VrAnticipo,0,0,'0',D.Cantidad,D.CxPagar,'',L.NumCheque,'',O.TipoResiduo,'',L.FechaVence,L.IdConductor,0,'0',D.IdUsuario,O.IdOrigen,L.Referencia,L.TipoAfiVehic,O.NomRecibido,D.ModdDev,L.IdVehiculo
,L.TipOds,L.NumOrden,L.IdCiaOds,D.TipCom,D.Comprobante,D.IdCiaCom,1,0,D.Fecha,D.Observacion,D.TimeSys,D.VrReteCREE,O.CdRuta,O.IdOrigen,O.IdDestino,L.IdPoseedor,L.IdConductor,'','',D.Liquidacion,D.IdCiaDoc
FROM Trn_TraDevLgo AS D INNER JOIN Trn_TraOrdenLiq AS L ON D.TipDoc=L.TipDoc AND D.Liquidacion=L.Liquidacion AND D.IdCiaDoc=L.IdCia
INNER JOIN Trn_TraOrdenServ AS O ON L.TipOds=O.TipDoc AND L.NumOrden=O.NumOrden AND L.IdCiaOds=O.IdCia
WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (D.TipDev=@pmTipDev OR @pmTipDev IS NULL)
AND (D.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (D.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpConsumo)
 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,ISNULL(KarImpCon,0)
 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,SUM(VrImpCon) AS KarImpCon
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpConsumo)
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,ISNULL(KarImpCon,0)
 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,SUM(VrImpCon) AS KarImpCon
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Lga] @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null,@pmTipDoc VARCHAR(3)=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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,L.TipDoc,L.Liquidacion,L.IdCia,L.Fecha,L.IdConcepto,L.IdPropietario,'0',L.VrGastos,0,0,0,0,0,0,0,L.VrOtrosCred,0,0,0,L.VrCombustible,L.VrAnticipos,0,0,L.VrDiferencia,0
,L.galsComb,'0',L.PesoTotal,L.LiqMixto,'','','',L.DescMcias,'',L.FechaMuc,L.NitEmpresa,0,'0',L.IdUsuario,L.IdLocOrigen,L.Referencia,L.Modalidad,'',L.Modalidad,L.IdVehiculo
,L.TipMuc,L.Manifiesto,L.IdCiaMuc,L.TipCom,L.Comprobante,L.IdCiaCom,L.Anulado,L.NumDev,L.FecDev,L.Observacion,L.TimeSys,0,L.IdRuta,L.IdLocOrigen,L.IdLocDestino,L.IdPoseedor,L.IdConductor,'','',L.NumDocRef,L.IdCiaRef
FROM Trn_TraLiqViajes AS L
WHERE L.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (L.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (L.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
AND (L.TipDoc=@pmTipDoc OR @pmTipDoc IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Cos] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,C.TipDoc,C.Cumplido,C.IdCia,C.Fecha,C.IdConcepto,O.IdCliente,O.IdAgencia,C.VrTotal,0,0,0,0,0,0,0,0,0,0,0,C.VrCosto,C.VrTotal
,0,0,0,0,0,'0',C.Cantidad,0,'',C.RemCliente,'',C.DescServicio,'',C.FecDespacho,O.IdConductor,0,'0',C.IdUsuario,O.IdOrigen,O.nRemolque,C.TipoAfiVehic,C.Funcionario,O.Modalidad,O.IdVehiculo
,C.TipOds,C.NumOrden,C.IdCiaOds,C.TipCom,C.Comprobante,C.IdCiaCom,C.Anulado,C.NumDev,C.FecDev,C.Observacion,C.TimeSys,0,O.CdRuta,O.IdOrigen,O.IdDestino,O.IdPoseedor,O.IdConductor,'','',O.NumRemesa,O.CdCiaRem 
FROM Trn_TraOrdenCum AS C INNER JOIN Trn_TraOrdenServ AS O ON C.TipOds=O.TipDoc AND C.NumOrden=O.NumOrden AND C.IdCiaOds=O.IdCia
WHERE C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (C.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (C.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Odt] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,O.TipDoc,O.NumOrden,O.IdCia,O.Fecha,O.TipDoc,O.IdConductor,'0',O.VrServicios,0,0,0,0,0,0,O.VrOtros,O.VrManoObra,0,0,0,O.VrRepuestos,O.VrServicios
,0,0,0,0,O.kmRemolque,'0',O.kmVehiculo,0,'',O.nRemolque,'',O.IdCenSer,'',O.FechaOrden,O.IdOperario,0,O.IdSupervisor,O.IdUsuario,'0',O.nRemolque,O.IdCenSer,'',O.Modalidad,O.IdVehiculo
,O.TipFact,O.NumFactura,O.IdCiaFact,'0',0,'00',O.Anulado,O.NumDev,O.FecDev,O.Observacion,O.TimeSys,0,'','','',O.IdConductor,O.IdConductor,'','',0,''
FROM Trn_MttoOrden AS O
WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (O.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (O.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Pdt] @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null,@pmTipDoc VARCHAR(3)=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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,P.TipDoc,P.Pedido,P.IdCia,P.Fecha,P.TipDoc,P.IdCliente,P.IdAgencia,P.VrCobro,0,0,0,0,0,0,P.VrSeguro,P.VrPagos,P.VrDctos,0,0,P.VrFletes,P.VrCobro
,0,0,P.VrDeclarado,0,P.CantDesp,'0',P.Cantidad,0,'',PA.TipoRuta,'',PA.EmbAdicional,'',P.FecDespacho,P.IdVend,0,'0',P.IdUsuario,P.IdLocOrigen,PA.TipoTrans,PA.CdTipoVeh,PA.NomCliente,PA.TipoServicio,''
,P.TipRem,P.Remesa,P.IdCiaRem,'0',0,'',P.Anulado,0,P.FecDev,P.Observacion,P.TimeSys,0,PA.CdRutaTarif,P.IdLocOrigen,P.IdLocDestino,'','',P.IdRemitente,P.IdDestinatario,P.Factura,P.IdCiaFac
FROM Trn_TraPedido AS P INNER JOIN Trn_TraPedAnexo AS PA ON P.TipDoc=PA.TipDoc AND P.Pedido=PA.Pedido AND P.IdCia=PA.IdCia
WHERE P.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (P.TipDoc=@pmTipDoc OR @pmTipDoc IS NULL)
AND (P.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (P.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Dco] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,D.TipDev,D.Devolucion,D.IdCia,D.Fecha,D.IdConcepto,O.IdCliente,O.IdAgencia,C.VrTotal,0,0,0,0,0,0,0,0,0,0,0,C.VrCosto,C.VrTotal
,0,0,0,0,0,'0',C.Cantidad,0,'',C.RemCliente,'',C.DescServicio,'',C.FecDespacho,O.IdConductor,0,'0',D.IdUsuario,O.IdOrigen,O.nRemolque,C.TipoAfiVehic,C.Funcionario,D.ModdDev,O.IdVehiculo
,C.TipOds,C.NumOrden,C.IdCiaOds,D.TipCom,D.Comprobante,D.IdCiaCom,1,0,D.Fecha,D.Observacion,D.TimeSys,0,O.CdRuta,O.IdOrigen,O.IdDestino,O.IdPoseedor,O.IdConductor,'','',D.Cumplido,D.IdCiaDoc 
FROM Trn_TraDevCos AS D INNER JOIN Trn_TraOrdenCum AS C ON D.TipDoc=C.TipDoc AND D.Cumplido=C.Cumplido AND D.IdCiaDoc=C.IdCia
INNER JOIN Trn_TraOrdenServ AS O ON C.TipOds=O.TipDoc AND C.NumOrden=O.NumOrden AND C.IdCiaOds=O.IdCia
WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (D.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (D.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpConsumo)
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,D.VrImpCons
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Dva] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,D.TipDev,D.Devolucion,D.IdCia,D.Fecha,D.IdConcepto,M.IdPropietario,'0',M.VrFletes,M.VrDescuento,0,0,0,0,0,M.VrAnticipo+M.VrAntAdic,0,A.VrDeduccion,0,0,0,D.VrDevolucion,A.VrAnticipo,A.VrAbonado,0,0
,M.Cantidad,'0',M.PesoTotal,CASE WHEN A.TipoPago='EFECTIVO' THEN 1 ELSE 0 END,SUBSTRING(A.TipoPago,1,3),A.NumCheque,'',A.TipoPago,A.IdCta,A.FecCheque,A.CedBenef,0,'0',D.IdUsuario,M.IdOrigen,M.nRemolque,M.TipoAfiVehic,A.Beneficiario,D.ModdDev,D.IdVehiculo
,A.TipMuc,A.Manifiesto,A.IdCiaMuc,D.TipCom,D.Comprobante,D.IdCiaCom,1,D.Devolucion,D.Fecha,D.Observacion,D.TimeSys,0,M.IdRuta,M.IdOrigen,M.IdDestino,D.IdPoseedor,D.IdConductor,'','',D.Anticipo,D.IdCiaDoc
FROM Trn_TraDevAnt AS D INNER JOIN Trn_TraAnticipos AS A ON D.TipDoc=A.TipDoc AND D.Anticipo=A.Anticipo AND D.IdCiaDoc=A.IdCia
INNER JOIN Trn_TraManifiesto AS M ON A.TipMuc=M.TipDoc AND A.Manifiesto=M.Manifiesto AND A.IdCiaMuc=M.IdCia
WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (D.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (D.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Rmt] @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null,@pmTipDoc VARCHAR(3)=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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,R.TipDoc,R.NumOrden,R.IdCia,R.Fecha,CdConcepto,R.IdCliente,R.IdAgencia,R.VrCobro,0,0,0,0,0,0,R.VrSeguro,R.VrPagos,R.VrDctos,0,0,R.VrFletes,R.VrCobro
,0,0,R.VrDeclarado,0,0,'0',R.PesoTotal,0,R.CdForma,RA.TipoRuta,'','','',R.FecDespacho,RA.IdVend,0,'0',R.IdUsuario,R.IdLocFletes,R.nRemolque,R.TipoAfiVehic,RA.NomCliente,R.Modalidad,R.IdVehiculo
,R.TipDcm,R.NumDocmto,R.IdCiaDcm,R.TipCom,R.Comprobante,R.IdCiaCom,R.Anulado,0,R.FecDev,R.Observacion,R.TimeSys,R.VrCargos,R.CdRutaTarif,R.IdLocOrigen,R.IdLocDestino,'0',R.IdConductor,R.IdRemitente,R.IdDestinatario,R.NumPedido,R.IdCiaPed
FROM Trn_TraRemesa AS R INNER JOIN Trn_TraRemAnexo AS RA ON R.TipDoc=RA.TipDoc AND R.NumOrden=RA.NumOrden AND R.IdCia=RA.IdCia
WHERE R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (R.TipDoc=@pmTipDoc OR @pmTipDoc IS NULL)
AND (R.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (R.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Diario_Cum] @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
,VrImpConsumo,IdRuta,IdLocOrigen,IdLocDestino,IdPoseedor,IdConductor,IdRemitente,IdDestinatario,NumPedido,IdCiaPed)
SELECT @pmtmEst,C.TipDoc,C.Cumplido,C.IdCia,C.Fecha,ISNULL(C.CodConcepto,''),M.IdPropietario,'0',CR.TotalCobro,0,0,0,0,0,0,C.VrAdicFlete,C.VrAdicCargue,C.VrAdicDescargue,0,0,CR.TotalPagos,CR.TotalPagos,0,0,0,C.VrAdicAnticipo
,CR.Cantidad,'0',CR.PesoTotal,0,'','','','','',C.FecPago,'0',0,'0',C.IdUsuario,C.CdOrigen,M.nRemolque,M.TipoAfiVehic,'',C.Modalidad,C.IdVehiculo
,C.TipMuc,C.Manifiesto,C.IdCiaMuc,C.TipoComp,C.NumComp,C.IdCia,C.Anulado,0,C.FecDev,C.Observacion,C.TimeSys,0,C.CdRuta,C.CdOrigen,C.CdDestino,M.IdPoseedor,M.IdConductor,'','',M.Remesa,M.IdCiaRem
FROM Trn_TraCumplido AS C INNER JOIN Trn_TraManifiesto AS M ON C.TipMuc=M.TipDoc AND C.Manifiesto=M.Manifiesto AND C.IdCiaMuc=M.IdCia
LEFT JOIN (SELECT TipDoc,Cumplido,IdCia,SUM(Cantidad) AS Cantidad,SUM(PesoNeto) AS PesoTotal,SUM(PesoNeto*TarifClie) AS TotalCobro,SUM(PesoNeto*TarifPago) AS TotalPagos
FROM Trn_TraCumRemesas GROUP BY TipDoc,Cumplido,IdCia) AS CR ON C.TipDoc=CR.TipDoc AND C.Cumplido=CR.Cumplido AND C.IdCia=CR.IdCia
WHERE C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (C.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (C.IdUsuario=@pmIdUsuario OR @pmIdUsuario IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNomAbonosId] @pmNPrestamo INT
 AS 
 SELECT MAX(Item) AS MXITEM
 FROM Trn_NomAbonos
 WHERE NPrestamo=@pmNPrestamo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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','VCC')
 AND TipoRef<>'SERVICIO' AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY IdCia,K.IdProducto,K.IdBodega
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,MgenCont,BaseIvaCom,ImpCarbono,BaseIvp,TarifaIvp,IvaIngProd,ListaPrec,VrBruto,VrBase,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo
,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,VrImvCosto,TarifaIco,VrImpCon,CodTarIco,TarifaStc,SobtasaCons,CantObseq,VrIvaObseq,IngBaseCom,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  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.TipDoc LIKE ISNULL(@pmTipDoc,'%') 
 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 ,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenServRelDet] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=NULL
 AS 
 SELECT O.TipDoc AS IdTipDoc,TipoDoc,O.NumOrden AS Num_Orden,O.IdCia AS CdCia,Compania,O.Fecha,FecDespacho,FecRecibo,O.IdCliente,T.RazonSocial AS NomCliente
,O.IdAgencia AS CdAgencia,Agencia,O.IdVehiculo AS PlacaVeh,NumVeh,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,nRemolque,O.TipoAfiVehic,O.Modalidad,O.VrTotal,O.VrCosto,O.Cantidad AS Cant_Total,VrAnticipo,O.CdRuta,Ruta,O.IdOrigen,LO.Localidad AS Origen,O.IdDestino AS CdDestino,LD.Localidad AS NomDestino,TipoOrden,O.IdEstacion AS CdCampo,EstServicio AS NomCampo
,NomEstacion,TipoResiduo,RemCliente,NomRecibido,FecIngCertif,FecCertificado,NumRemesa,CdCiaRem,EstCumplido,NumCump,CdCiaCum,EstFactura,TipFact,NumFactura,CdCiaFact
,kmtInicial,kmtFinal,Cantkms,EstOrden,TipLiq,NumLiquida,CdCiaLiq,NumAprob,IdCiaApr,FecAprob,UsuAprob,ObservAprob,O.Anulado,FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado
,O.TimeSys AS FechaCrea,O.FecUpdate AS FechaModif,O.IdCiaCrea AS CdCiaCrea,OrigenAdd,O.IdUsuario AS CdUsuario,Usuario
--Cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad
,T.Telefono AS TercTelefono,T.TelMovil AS TelCelular,T.e_mail AS TercEmail
--Vehiculo
,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPoseedor AS NitPoseedor,NPO.RazonSocial AS Poseedor
,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
--detalles
,Item,D.IdMercancia AS CdMercancia,DescripMcia,D.Descripcion,D.Cantidad,D.UndMed,Unidad,D.VrUnitario,D.TarifaPago
,CantCump,TarifaCump,PagoCump,DocCliente
 FROM Trn_TraOrdenServ AS O 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 Terceros AS NP ON O.IdPoseedor=NP.IdTercero
 INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Vehiculos AS V ON O.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 TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
 INNER JOIN Terceros AS NPO ON V.IdPoseedor=NPO.IdTercero  INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 INNER JOIN Trn_TraOrdenDet AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
 INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed 
 LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 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 O.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia  LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 LEFT JOIN Mercancias AS MC ON D.IdMercancia=MC.IdMercancia
 LEFT JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal LEFT JOIN Rutas AS RT ON O.CdRuta=RT.IdRuta
 WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND O.IdCliente LIKE ISNULL(@pmIdCliente,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenServNov] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=NULL
 AS 
 SELECT O.TipDoc AS IdTipDoc,O.NumOrden AS Num_Orden,O.IdCia AS CdCia,Compania,O.Fecha,FecDespacho,FecRecibo,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,A.Agencia
,O.IdVehiculo AS PlacaVeh,NumVeh,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,nRemolque,O.TipoAfiVehic,O.Modalidad,O.VrTotal,O.VrCosto,O.Cantidad AS Cant_Total,VrAnticipo,O.CdRuta,Ruta,O.IdOrigen,LO.Localidad AS Origen,O.IdDestino AS CdDestino,LD.Localidad AS NomDestino
,TipoOrden,O.IdEstacion AS CdCampo,EstServicio AS NomCampo,NomEstacion,TipoResiduo,RemCliente,NomRecibido,FecIngCertif,FecCertificado,NumRemesa,CdCiaRem,EstCumplido,NumCump,CdCiaCum,EstFactura,TipFact,NumFactura,CdCiaFact
,kmtInicial,kmtFinal,Cantkms,EstOrden,TipLiq,NumLiquida,CdCiaLiq,NumAprob,IdCiaApr,FecAprob,UsuAprob,ObservAprob,O.Anulado,FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.TimeSys AS FechaCrea
--novedades
,Item,C.Descripcion AS Descrpcion,Tarifa,TipoConc,RubroConcep,C.Cantidad AS CantConc,VrUnitario,TarifIva,C.IdConcepto AS CdConcepto,Concepto,CdCuenta,NomCuenta
,NitTercero,TCN.RazonSocial AS NomTercero,FechaNov,VrBase,TipoTarif,RefConc,Referencia2,Referencia3,Fijos,IncBaseRet
--Cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad
,T.Telefono AS TercTelefono,T.TelMovil AS TelCelular,T.e_mail AS TercEmail
--Vehiculo
,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPoseedor AS NitPoseedor,NPO.RazonSocial AS Poseedor
,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
 FROM Trn_TraOrdenServ AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero
 INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero
 INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Vehiculos AS V ON O.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 TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
 INNER JOIN Terceros AS NPO ON V.IdPoseedor=NPO.IdTercero  INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 INNER JOIN Trn_TraConceptos AS C ON O.TipDoc=C.TipDoc AND O.NumOrden=C.Documento AND O.IdCia=C.IdCia
 LEFT JOIN ConcDiversos AS CDV ON C.IdConcepto=CDV.IdConcepto LEFT JOIN Terceros AS TCN ON C.NitTercero=TCN.IdTercero
 LEFT JOIN Puc AS P ON C.CdCuenta=P.IdCuenta
 LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 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 O.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia  LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 LEFT JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal LEFT JOIN Rutas AS RT ON O.CdRuta=RT.IdRuta
 WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND O.IdCliente LIKE ISNULL(@pmIdCliente,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraRemesaRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null
 AS 
 SELECT R.TipDoc AS CdTipDoc,R.NumOrden AS Num_Orden,R.IdCia AS CdCia,Compania,Fecha,FecDespacho,FecEntrega,HoraLlegada,IdCliente,NomCliente,T.RazonSocial AS RazonSocialCli
,R.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,IdClieFact,IdRemitente,NomRemite,IdDestinatario,NomDestino,IdLocOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,IdLocDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,IdLocFletes,CF.Localidad AS LugarFletes,R.IdVehiculo AS PlacaVeh,R.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,nRemolque,TipoAfiVehic,Modalidad,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos,R.VrDeclarado AS ValDeclarado,R.VrSeguro AS ValSeguro,R.Cantidad AS CantTotal,PesoTotal,Items
,IdMneda,VrTasa,NumPedido,IdCiaPed,FechaPed,TipDcm,NumDocmto,IdCiaDcm,FechaDcm,NumManif,IdCiaManif,EstCumplido,EstFactura,CdConcepto,Concepto,CdRutaTarif,TipCom,Comprobante,IdCiaCom,SerieGuia,NumGuia,CdForma,NumCausac,CdCiaCausac,EdoCausac
,Anulado,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS CdUsuario,Usuario
,RA.IdVend AS CdVend,VN.RazonSocial AS Vendedor,LugarCargue,LugarDescargue,NomContacto,TelContacto,emlContacto,ContacDestino,TelContacDest,emlContacDest,TomadorPoliza,PolizaSeguro
,NitCiaPoliza,NomCiaPoliza,FecVencePol,ManifMintrans,RemMintrans,TipSal,NumSalida,IdCiaSal,TipOrdMud,NumInvent,IdCiaInv,TipoRuta,TipoMintrans,CdBodega,Bodega,RA.TipoDevRemesa,RA.CodCiuTransb,LT.Localidad AS CiudadTransb,RA.MvoTransbordo
--Datos del cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NitRepLeg,NomRepLeg,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,A.DirAgncia AS AgeDireccion,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,A.TelAgncia AS AgeTelefono,A.Referencia AS AgeReferencia
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPoseedor AS NitPoseedor,NPO.RazonSocial AS Poseedor
,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,V.IdGrupo AS CdGrupoPro,GrupoProp
 FROM Trn_TraRemesa AS R  INNER JOIN Trn_TraRemAnexo AS RA ON R.TipDoc=RA.TipDoc  AND R.NumOrden=RA.NumOrden AND R.IdCia=RA.IdCia
 INNER JOIN Companias AS CN ON R.IdCia=CN.IdCia  INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 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 CO ON R.IdLocOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON R.IdLocDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Localidades AS CF ON R.IdLocFletes=CF.IdLocal  INNER JOIN Terceros AS  CDT ON R.IdConductor=CDT.IdTercero 
 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 TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN Terceros AS NPO ON V.IdPoseedor=NPO.IdTercero  INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN Terceros AS VN ON RA.IdVend=VN.IdTercero  LEFT JOIN Agencias AS A ON R.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN  Conceptos AS C ON R.CdConcepto=C.IdConcepto 
 LEFT JOIN AlmBodegas AS BG ON R.CdBodega=BG.IdBodega LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 LEFT JOIN Localidades AS LT ON RA.CodCiuTransb=LT.IdLocal
 WHERE R.TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND R.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND R.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 ORDER BY R.IdCia,R.NumOrden
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraCausacionRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null
 AS 
 SELECT C.TipDoc AS TipCau,C.Causacion AS NumCausac,C.IdCia AS CdCia,Compania,Fecha,C.IdConcepto AS CdConcepto,CO.Concepto AS DescConcepto
,IdProv,T.RazonSocial AS Proveedor,C.Factura AS NumFactura,VrTotal,VrOtros,VrNeto,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado
,TimeSys,C.FecUpdate AS FechaAct,IdCiaCrea,C.IdUsuario AS CdUsuario,Usuario
,D.IdConcepto AS CdConcCausac,CCA.Concepto AS ConcCausac,D.Descripcion AS DetDescripcion,D.TipoConc AS TipConcept,VrDebito,VrCredito,CdCuenta,NomCuenta,pVehiculo,NitTercero,NP.RazonSocial AS Razon_Social
,CdCCosto,CCosto,CdSubCos,SubCosto,TipoAfiVehic,VrBase,TarifBase,Referencia,TipDocRef,DocRef,TipFac,D.Factura AS DetFactura,IdCiaFac,ItemFac,FecVence,EstadoReg,NumLiquida,CiaLiquida,VrAbonado
--datos del proveedor
,T.TipoId AS TercTipId,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 CdDpto,DP.Departamento AS DescDpto
,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraCausacion AS C   INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia 
 INNER JOIN Conceptos AS CO ON C.IdConcepto=CO.IdConcepto
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario 
 INNER JOIN Terceros AS T ON C.IdProv=T.IdTercero 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 INNER JOIN Trn_TraCauDetalle AS D ON C.TipDoc=D.TipDoc AND C.Causacion=D.Causacion AND C.IdCia=D.IdCia
 INNER JOIN TraConcCausac AS CCA ON D.IdConcepto=CCA.IdConcepto 
 LEFT JOIN  Vehiculos AS V ON D.pVehiculo=V.IdVehiculo   LEFT JOIN  TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 LEFT JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 LEFT JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  LEFT JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 LEFT JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd 
 LEFT JOIN Terceros AS NP ON D.NitTercero=NP.IdTercero  LEFT JOIN Puc AS P ON D.CdCuenta=P.IdCuenta 
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos 
 LEFT JOIN TiposCom AS TCM ON C.TipCom=TCM.IdCom 
  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.IdProv LIKE ISNULL(@pmIdProv,'%')
 ORDER BY C.IdCia,C.Causacion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevLegRel] @pmTipDev VARCHAR(3),@pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipLiq,D.Liquidacion AS NumLiquida,IdCiaDoc,FecDoc,D.Modalidad AS Modaldad
,D.TipMuc AS TipManif,D.Manifiesto AS NumManif,D.IdCiaMuc AS CdCiaManif,D.IdVehiculo AS PlacaVeh,D.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,D.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario
,D.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,D.NitEmpresa AS Nit_Empresa,ETR.RazonSocial AS NomEmpresa,D.VrAnticipos AS TotalAntcpos,D.VrGastos AS TotalGastos,D.VrCombustible AS TotalComb,D.VrDiferencia AS ValorDif
,D.VrOtrosCred AS TotalOtrosDed,D.galsComb AS CantGals,D.PesoTotal AS Peso_Total,D.Cantidad AS CantTotal,D.Volumen AS VolTotal,ModdDev,D.OrigenAdd AS OrigenCrea,D.TipCom AS CdTipComp,TipoCom,D.Comprobante AS NumComp,D.IdCiaCom AS CdCiaComp
,D.Observacion AS Observ,D.IdEstado AS CdEstado,D.TimeSys AS FechaCrea,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
,LQ.IdRuta AS CdRuta,Ruta,IdLocOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,IdLocDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
--datos del vehículo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,V.NitEmpresa AS VehNitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,V.IdGrupo AS CdGrupo,GrupoProp
 FROM Trn_TraDevLeg AS D  INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto INNER JOIN Terceros AS T ON D.IdPoseedor=T.IdTercero
 INNER JOIN Terceros AS  CDT ON D.IdConductor=CDT.IdTercero   INNER JOIN Terceros AS NP ON D.IdPropietario=NP.IdTercero   
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 INNER JOIN Trn_TraLiqViajes AS LQ ON  D.TipDoc=LQ.TipDoc AND D.Liquidacion=LQ.Liquidacion AND D.IdCiaDoc=LQ.IdCia
 INNER JOIN Localidades AS CO ON  LQ.IdLocOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON LQ.IdLocDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON LQ.IdRuta=R.IdRuta  
 LEFT JOIN Terceros AS ETR ON D.NitEmpresa=ETR.IdTercero LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 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.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 AND D.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 ORDER BY D.IdCia,D.Devolucion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenCumRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS 
 SELECT C.TipDoc AS IdTipDoc,TipoDoc,C.Cumplido AS NumCumplido,C.IdCia AS CdCia,Compania,C.Fecha AS FechaCump,C.IdConcepto AS CdConcepto,Concepto,TipOds,C.NumOrden AS Num_Orden,IdCiaOds
,O.Fecha AS FecOrden,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia,O.IdVehiculo AS PlacaVeh,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor
,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,O.Modalidad,O.TipoOrden,O.VrAnticipo AS Vr_Anticipo,O.IdEstacion AS CdCampo,EstServicio AS NomCampo
,C.TipoAfiVehic,C.FecDespacho,C.FecRecibo,C.FecIngCertif,C.FecCertificado,DescServicio,C.Cantidad AS CumCantidad,C.VrTotal AS CumValor,C.VrCosto AS CumValTercero,C.RemCliente
,Funcionario,O.NumRemesa,O.CdCiaRem,O.kmtInicial,O.kmtFinal,O.IdDestino,LD.Localidad,C.TipCom,C.Comprobante AS NumComp,C.IdCiaCom,C.Anulado AS CumAnulado,C.NumDev,C.FecDev,C.Observacion AS Observ
,C.IdEstado AS CdEstado,Estado,C.OrigenAdd,C.TimeSys AS FechaCrea,C.FecUpdate AS FechaModif,C.IdCiaCrea AS CdCiaCrea,C.IdUsuario AS CdUsuario,Usuario
--detalles
,Item,D.IdMercancia,D.Descripcion,D.Cantidad,D.UndMed AS Und_Med,Unidad,D.VrUnitario,D.TarifaPago,D.DocCliente
--Datos del Cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
FROM Trn_TraOrdenCum AS C INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario INNER JOIN Conceptos AS CT ON C.IdConcepto=CT.IdConcepto
INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
INNER JOIN Trn_TraOrdenServ AS O ON C.TipOds=O.TipDoc AND C.NumOrden=O.NumOrden  AND C.IdCiaOds=O.IdCia
INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero
INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion  INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero
INNER JOIN Trn_TraOrdenCumDet AS D ON C.TipDoc=D.TipDoc AND C.Cumplido=D.Cumplido AND C.IdCia=D.IdCia
INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca 
INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
WHERE C.TipDoc=@pmTipDoc AND C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenCumFmt] @pmTipDoc VARCHAR(3),@pmCumplidoIni INT,@pmCumplidoFin INT,@pmIdCia CHAR(2)
AS 
 SELECT C.TipDoc AS IdTipDoc,TipoDoc,C.Cumplido AS NumCumplido,C.IdCia AS CdCia,Compania,C.Fecha AS FechaCump,C.IdConcepto AS CdConcepto,Concepto,TipOds,C.NumOrden AS Num_Orden,IdCiaOds
,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia,O.IdVehiculo AS PlacaVeh,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor
,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,O.Modalidad,O.TipoOrden,O.VrAnticipo AS Vr_Anticipo,O.IdEstacion AS CdCampo,EstServicio AS NomCampo
,C.TipoAfiVehic,C.FecDespacho,C.FecRecibo,C.FecIngCertif,C.FecCertificado,DescServicio,C.Cantidad AS CumCantidad,C.VrTotal AS CumValor,C.VrCosto AS CumValTercero,C.RemCliente
,Funcionario,O.NumRemesa,O.CdCiaRem,O.kmtInicial,O.kmtFinal,IdDestino,LD.Localidad,C.TipCom,C.Comprobante AS NumComp,C.IdCiaCom,C.Anulado AS CumAnulado,C.NumDev,C.FecDev,C.Observacion AS Observ
,C.IdEstado AS CdEstado,Estado,C.OrigenAdd,C.TimeSys AS FechaCrea,C.FecUpdate AS FechaModif,C.IdCiaCrea AS CdCiaCrea,C.IdUsuario AS CdUsuario,Usuario,Leyenda
--detalles
,Item,D.IdMercancia,D.Descripcion,D.Cantidad,D.UndMed AS Und_Med,Unidad,D.VrUnitario,D.TarifaPago,D.DocCliente
--Datos del Cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
FROM Trn_TraOrdenCum AS C INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario INNER JOIN Conceptos AS CT ON C.IdConcepto=CT.IdConcepto
INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
INNER JOIN Trn_TraOrdenServ AS O ON C.TipOds=O.TipDoc AND C.NumOrden=O.NumOrden  AND C.IdCiaOds=O.IdCia
INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero
INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion  INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero
INNER JOIN Trn_TraOrdenCumDet AS D ON C.TipDoc=D.TipDoc AND C.Cumplido=D.Cumplido AND C.IdCia=D.IdCia
INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca 
INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
WHERE C.TipDoc=@pmTipDoc AND C.Cumplido BETWEEN @pmCumplidoIni AND @pmCumplidoFin AND C.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryVehiculosUbi] @pmIdEst CHAR(2),@pmTipoRep INT,@pmFecInicio SMALLDATETIME,@pmIdTipoVeh VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
SELECT V.IdVehiculo AS PlacaVeh,V.NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca,V.IdLinea AS CdLinea,V.IdColor AS CdColor,CL.NomColor AS ColorVeh,V.IdCrceria AS CdCrceria,TipoCar,V.Modelo
,V.Config,V.PesoVacio,V.NumMotor,V.SerieChasis,V.CdRemque,V.CarrCapac,V.UndCapc,NitEmpresa,NE.RazonSocial AS Empresa,V.IdPropietario,NP.RazonSocial AS Propietario,V.IdPoseedor,NT.RazonSocial AS Poseedor
,V.IdConductor,NC.RazonSocial AS Conductor,V.TipoAfil,V.IdPpd,TipoProp,V.IdGrupo AS CdGrupo,GrupoProp,V.IdEstado AS CdEstado,EV.Estado,EV.NColor AS EstNColor,NumSoat,FecSoat,VigSoat,KmActual,V.Descripcion,V.Observacion,FecPriServ,FecUltServ
,V.Ubicacion,V.CdLocal AS CodCiuUbic,LU.Localidad AS CiuUbicacion,LU.IdDep AS CodDpto,Departamento,V.FechaAdd AS FechaCrea
--datos de ordenes
,TM.TipoOrd,TM.NumOrden,TM.IdCiaOrd,TM.Remesa,TM.IdCiaRem,R.Fecha AS FecOrden,R.IdCliente,C.RazonSocial AS NomCliente,IdLocOrigen,CO.Localidad AS CiuOrigen,IdLocDestino,CD.Localidad AS CiuDestino,TM.nRemolque
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 TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
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 GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
LEFT JOIN Terceros AS NE ON V.NitEmpresa=NE.IdTercero 
LEFT JOIN Localidades AS LU ON V.CdLocal=LU.IdLocal LEFT JOIN Departamentos AS DU ON LU.IdDep=DU.IdDep
LEFT JOIN (SELECT IdOperario,IdVehiculo,nRemolque,TipoOrd,NumOrden,IdCiaOrd,Remesa,IdCiaRem FROM tm_TraInfOper WHERE IdEst=@pmIdEst AND TipoRep=@pmTipoRep) AS TM ON V.IdVehiculo=TM.IdVehiculo
LEFT JOIN (SELECT TipDoc,NumOrden,IdCia,Fecha,FecDespacho,IdCliente,IdAgencia,IdLocOrigen,IdLocDestino,IdVehiculo,IdConductor,nRemolque
FROM Trn_TraRemesa WHERE TipDoc='OCT' AND Fecha>=@pmFecInicio
UNION ALL SELECT TipDoc,NumOrden,IdCia,Fecha,FecDespacho,IdCliente,IdAgencia,IdDestino,IdDestino AS CiuDstno,IdVehiculo,IdConductor,nRemolque
FROM Trn_TraOrdenServ WHERE TipDoc='OSA' AND Fecha>=@pmFecInicio) AS R ON TM.TipoOrd=R.TipDoc AND TM.NumOrden=R.NumOrden AND TM.IdCiaOrd=R.IdCia
LEFT JOIN Terceros AS C ON R.IdCliente=C.IdTercero LEFT JOIN Localidades AS CO ON R.IdLocOrigen=CO.IdLocal LEFT JOIN Localidades AS CD ON R.IdLocDestino=CD.IdLocal
WHERE V.Inactivo=0 AND (V.IdTipoVeh=@pmIdTipoVeh OR @pmIdTipoVeh IS NULL)
AND (V.IdEstado=@pmIdEstado OR @pmIdEstado IS NULL)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevCosRel] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT D.TipDev AS IdTipDev,TipoDoc,D.Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc,D.Cumplido AS NumCump,D.IdCiaDoc,D.FecDoc
,TipOds,C.NumOrden AS Num_Orden,IdCiaOds,O.Fecha AS FecOrden,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia,O.IdVehiculo AS PlacaVeh,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor
,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,O.Modalidad,O.TipoOrden,O.VrAnticipo AS Vr_Anticipo,O.IdEstacion AS CdCampo,EstServicio AS NomCampo
,C.TipoAfiVehic,C.FecDespacho,C.FecRecibo,C.FecIngCertif,C.FecCertificado,DescServicio,C.Cantidad AS CumCantidad,C.VrTotal AS CumValor,C.VrCosto AS CumValTercero,C.RemCliente
,Funcionario,O.NumRemesa,O.CdCiaRem,O.kmtInicial,O.kmtFinal,O.IdDestino,LD.Localidad,D.ModdDev,D.TipCom,D.Comprobante AS NumComp,D.IdCiaCom,D.Observacion AS Observ
,D.OrigenAdd,D.TimeSys AS FechaCrea,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
--detalles
,Item,DC.IdMercancia,DC.Descripcion,DC.Cantidad,DC.UndMed AS Und_Med,Unidad,DC.VrUnitario,DC.TarifaPago,DC.DocCliente
--Datos del Cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
FROM Trn_TraDevCos AS D INNER JOIN Trn_TraOrdenCum AS C ON D.TipDoc=C.TipDoc AND D.Cumplido=C.Cumplido AND D.IdCiaDoc=C.IdCia
INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
INNER JOIN Conceptos AS CT ON D.IdConcepto=CT.IdConcepto INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
INNER JOIN Trn_TraOrdenServ AS O ON C.TipOds=O.TipDoc AND C.NumOrden=O.NumOrden  AND C.IdCiaOds=O.IdCia
INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero
INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion  INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero
INNER JOIN Trn_TraOrdenCumDet AS DC ON C.TipDoc=DC.TipDoc AND C.Cumplido=DC.Cumplido AND C.IdCia=DC.IdCia
INNER JOIN Sys_Um AS UM ON DC.UndMed=UM.UndMed INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca 
INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
WHERE D.TipDev=@pmTipDev AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraCausacionFmt] @pmTipDoc VARCHAR(3),@pmCausacionIni INT,@pmCausacionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipDoc AS TipCau,TipoDoc,C.Causacion AS NumCausac,C.IdCia AS CdCia,Compania,Fecha,C.IdConcepto AS CdConcepto,CO.Concepto AS DescConcepto
,IdProv,T.RazonSocial AS Proveedor,C.Factura AS NumFactura,VrTotal,VrOtros,VrNeto,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado
,TimeSys,C.FecUpdate AS FechaAct,IdCiaCrea,C.IdUsuario AS CdUsuario,Usuario,Leyenda
,D.IdConcepto AS CdConcCausac,CCA.Concepto AS ConcCausac,D.Descripcion AS DetDescripcion,D.TipoConc AS TipConcept,VrDebito,VrCredito,CdCuenta,NomCuenta,pVehiculo,NitTercero,NP.RazonSocial AS Razon_Social
,CdCCosto,CCosto,CdSubCos,SubCosto,TipoAfiVehic,VrBase,TarifBase,Referencia,TipDocRef,DocRef,TipFac,D.Factura AS DetFactura,IdCiaFac,ItemFac,FecVence,NumLiquida,CiaLiquida,VrAbonado
--datos del proveedor
,T.TipoId AS TercTipId,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 CdDpto,DP.Departamento AS DescDpto
,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraCausacion AS C   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 CO ON C.IdConcepto=CO.IdConcepto
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario 
 INNER JOIN Terceros AS T ON C.IdProv=T.IdTercero 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 INNER JOIN Trn_TraCauDetalle AS D ON C.TipDoc=D.TipDoc AND C.Causacion=D.Causacion AND C.IdCia=D.IdCia
 INNER JOIN TraConcCausac AS CCA ON D.IdConcepto=CCA.IdConcepto 
 LEFT JOIN  Vehiculos AS V ON D.pVehiculo=V.IdVehiculo   LEFT JOIN  TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 LEFT JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 LEFT JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  LEFT JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 LEFT JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd 
 LEFT JOIN Terceros AS NP ON D.NitTercero=NP.IdTercero  LEFT JOIN Puc AS P ON D.CdCuenta=P.IdCuenta 
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos 
 LEFT JOIN TiposCom AS TCM ON C.TipCom=TCM.IdCom 
  WHERE C.TipDoc=@pmTipDoc AND C.Causacion  BETWEEN @pmCausacionIni AND @pmCausacionFin AND C.IdCia=@pmIdCia
 ORDER BY C.Causacion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenServRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=NULL
 AS 
 SELECT O.TipDoc AS IdTipDoc,TipoDoc,O.NumOrden AS Num_Orden,O.IdCia AS CdCia,Compania,O.Fecha,FecDespacho,FecRecibo,O.IdCliente,T.RazonSocial AS NomCliente
,O.IdAgencia AS CdAgencia,Agencia,O.IdVehiculo AS PlacaVeh,NumVeh,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,nRemolque,O.TipoAfiVehic,O.Modalidad,O.VrTotal,O.VrCosto,O.Cantidad AS Cant_Total,VrAnticipo,O.CdRuta,Ruta,O.IdOrigen,LO.Localidad AS Origen,O.IdDestino AS CdDestino,LD.Localidad AS NomDestino,TipoOrden,O.IdEstacion AS CdCampo,EstServicio AS NomCampo
,NomEstacion,TipoResiduo,RemCliente,NomRecibido,FecIngCertif,FecCertificado,NumRemesa,CdCiaRem,EstCumplido,NumCump,CdCiaCum,EstFactura,TipFact,NumFactura,CdCiaFact
,kmtInicial,kmtFinal,Cantkms,EstOrden,TipLiq,NumLiquida,CdCiaLiq,NumAprob,IdCiaApr,FecAprob,UsuAprob,ObservAprob,O.Anulado,FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado
,O.TimeSys AS FechaCrea,O.FecUpdate AS FechaModif,O.IdCiaCrea AS CdCiaCrea,OrigenAdd,O.IdUsuario AS CdUsuario,Usuario
--Cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad
,T.Telefono AS TercTelefono,T.TelMovil AS TelCelular,T.e_mail AS TercEmail
--Vehiculo
,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPoseedor AS NitPoseedor,NPO.RazonSocial AS Poseedor
,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
 FROM Trn_TraOrdenServ AS O 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 Terceros AS NP ON O.IdPoseedor=NP.IdTercero
 INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Vehiculos AS V ON O.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 TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
 INNER JOIN Terceros AS NPO ON V.IdPoseedor=NPO.IdTercero  INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 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 O.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia  LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 LEFT JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal LEFT JOIN Rutas AS RT ON O.CdRuta=RT.IdRuta
 WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND O.IdCliente LIKE ISNULL(@pmIdCliente,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevLgoRel] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT D.TipDev AS IdTipDev,TipoDoc,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc,D.Liquidacion AS NumLiquida,D.IdCiaDoc,FecDoc
,TipOds,L.NumOrden,IdCiaOds,O.Fecha AS FecOrden,L.IdVehiculo AS PlacaVeh,L.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,L.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,L.TipoAfiVehic
,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia,O.IdDestino,LD.Localidad AS NomDestino,O.TipoOrden,O.IdEstacion AS CdCampo,EstServicio AS NomCampo
,O.FecDespacho,O.FecRecibo,O.FecIngCertif,O.FecCertificado,TipoResiduo,O.RemCliente,O.NumRemesa,O.CdCiaRem
,D.VrTotal,D.VrCosto,D.VrAnticipo,D.VrGastos,D.VrIva,D.VrRetencion,D.VrReteIca,D.VrReteCREE,D.VrReteIva,D.VrOtros,D.VrDescuento,L.VrConcCargos,L.VrConcDed,D.VrNeto,D.Cantidad
,TarifaPago,BaseImp,BaseRet,BaseIca,TarifaImp,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,L.Referencia,CdPlazo,Plazo,DiasPago,FechaVence,L.UndMed,Unidad
,D.CxPagar,D.TipCom,D.Comprobante,D.IdCiaCom,D.ModdDev,D.Observacion AS Observ,D.IdEstado AS CdEstado,D.OrigenAdd,D.TimeSys AS FechaCrea
,D.FecUpdate AS FechaModif,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
--vehiuculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,LT.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraDevLgo AS D INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia INNER JOIN Conceptos AS CT ON D.IdConcepto=CT.IdConcepto
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN Trn_TraOrdenLiq AS L ON D.TipDoc=L.TipDoc AND D.Liquidacion=L.Liquidacion AND D.IdCiaDoc=L.IdCia
 INNER JOIN Terceros AS NP ON L.IdPoseedor=NP.IdTercero INNER JOIN Terceros AS CDT ON L.IdConductor=CDT.IdTercero
 INNER JOIN Trn_TraOrdenServ AS O ON L.TipOds=O.TipDoc AND L.NumOrden=O.NumOrden AND L.IdCiaOds=O.IdCia
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion 
 INNER JOIN Localidades AS LT ON T.IdLocal=LT.IdLocal
 INNER JOIN Vehiculos AS V ON L.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 LEFT JOIN Plazos AS PZ ON L.CdPlazo=PZ.IdPlazo  LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal LEFT JOIN Sys_Um AS UM ON L.UndMed=UM.UndMed
 WHERE D.TipDev=@pmTipDev AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraAnticiposFmt] @pmTipDoc VARCHAR(3),@pmAnticipoIni INT,@pmAnticipoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT A.TipDoc AS TipoAnt,TipoDoc,Anticipo,A.IdCia AS CdCia,Compania,A.Fecha AS FechaAnt,A.IdConcepto AS CdConcepto,Concepto,TipMuc,A.Manifiesto AS NumManif,IdCiaMuc,A.IdVehiculo AS PlacaVeh
,A.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,A.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,A.VrAnticipo AS ValAnticipo,VrAbonado,VrDeduccion,Modalidad,TipoPago,A.NumCheque AS NroCheque,FecCheque
,A.IdCta AS CdCta,NumeroCta,CTA.IdBanco,Banco,A.IdCuenta AS CdCuenta,P.NomCuenta AS DescCuenta,CedBenef,Beneficiario,A.Cantidad AS CantGals,FechaVence,NumPresAnt,PreAnticipo,NumCargue,CiaCargue,A.TipCom AS CdTipCom,TipoCom,A.Comprobante AS NumComp,A.IdCiaCom AS CdCiaComp
,A.Anulado AS Anuldo,NumDev,A.FecDev AS FechaDev,A.Observacion AS Observ,A.IdEstado AS CdEstado,Estado,A.TimeSys AS FechaCrea,A.FecUpdate AS FechaAct,A.IdCiaCrea AS CdCiaCrea,A.IdUsuario AS CdUsuario,Usuario,Leyenda
--datos de manifiesto
,FecDespacho,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,VrFletes,TarifaFlete,PesoTotal
--datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TPR.TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
--comprobante
,Item,DC.IdCuenta AS CodCuenta,PC.NomCuenta AS Nom_Cuenta,Detalle,VrDebito,VrCredito,DC.IdTercero AS NitTercero,DT.RazonSocial AS NomTercero,DC.IdCCosto AS DetCodCentCost,DCC.CCosto AS DetCentCost
,DC.IdSubCos AS DetCodSubCentro,DS.SubCosto  AS DetSubCentro,VrBase,TarifaBase,TipFac,Factura,IdCiaFac,ItemFac,FecVence,DC.NumCheque AS DetNumCheque,TipoAplica,NitOtros,Referncia
 FROM Trn_TraAnticipos AS A  INNER JOIN Companias AS CN ON A.IdCia=CN.IdCia INNER JOIN Sys_TiposDoc AS TD ON A.TipDoc=TD.IdDoc
 INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario 
 INNER JOIN Conceptos AS C ON A.IdConcepto=C.IdConcepto
 INNER JOIN Terceros AS  CDT ON A.IdConductor=CDT.IdTercero  INNER JOIN Terceros AS T ON A.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON A.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Trn_TraManifiesto AS M ON A.TipMuc=M.TipDoc AND  A.Manifiesto=M.Manifiesto AND  A.IdCiaMuc=M.IdCia
 LEFT JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal LEFT JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 LEFT JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal LEFT JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 LEFT JOIN Rutas AS R ON M.IdRuta=R.IdRuta 
 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 A.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN CtasCorrientes AS CTA ON A.IdCta=CTA.IdCta LEFT JOIN Bancos AS B ON CTA.IdBanco=B.IdBanco
 LEFT JOIN Puc AS P ON A.IdCuenta=P.IdCuenta LEFT JOIN TiposCom AS TCM ON A.TipCom=TCM.IdCom 
 LEFT JOIN  Trn_ComDetalle AS DC ON A.TipCom=DC.TipCom AND A.Comprobante=DC.Comprobante AND A.IdCiaCom=DC.IdCia
 LEFT JOIN Puc AS PC ON DC.IdCuenta=PC.IdCuenta LEFT JOIN Terceros AS DT ON DC.IdTercero=DT.IdTercero
 LEFT JOIN CentroCosto AS DCC ON DC.IdCCosto=DCC.IdCCosto  LEFT JOIN SubCentros AS DS ON DC.IdSubCos=DS.IdSubCos
 WHERE A.TipDoc=@pmTipDoc AND A.Anticipo BETWEEN @pmAnticipoIni AND @pmAnticipoFin AND A.IdCia=@pmIdCia
 ORDER BY A.Anticipo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraAnticiposRelAbo] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT A.TipDoc AS TipoAnt,A.Anticipo AS NumAnticipo,A.IdCia AS CdCia,Compania,A.Fecha AS FechaAnt,A.IdConcepto AS CdConcepto,Concepto,TipMuc,A.Manifiesto AS NumManif,IdCiaMuc,A.IdVehiculo AS PlacaVeh
,A.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,A.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,A.VrAnticipo AS ValAnticipo,VrAbonado,A.VrAnticipo-VrAbonado AS VrSaldo
,AP.TipDoc AS TipDocAbo,AP.Documento AS NumDocAbo,AP.IdCia AS CdCiaAbo,Item,AP.Fecha AS FechaAbo,VrAbono,TipoAplica,Detalle
,Modalidad,TipoPago,NumCheque,FecCheque,A.IdCta AS CdCta,NumeroCta,CTA.IdBanco,Banco,A.IdCuenta AS CdCuenta,NomCuenta,CedBenef,Beneficiario,A.Cantidad AS CantGals,FechaVence,NumPresAnt,PreAnticipo,A.TipCom,TipoCom,A.Comprobante,A.IdCiaCom
,A.Anulado AS Anuldo,NumDev,A.FecDev AS FechaDev,A.Observacion AS Observ,A.IdEstado AS CdEstado,A.TimeSys AS FechaCrea,A.FecUpdate AS FechaAct,A.IdCiaCrea AS CdCiaCrea,A.IdUsuario AS CdUsuario,Usuario
--datos de manifiesto
,FecDespacho,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,VrFletes,TarifaFlete,PesoTotal,EstCumplido,Cumplido,IdCiaCump,FechaCump
--Datos del vehiculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TPR.TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,V.IdGrupo AS CdGrupo,GrupoProp
 FROM Trn_TraAnticipos AS A  INNER JOIN Companias AS CN ON A.IdCia=CN.IdCia  INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario 
 INNER JOIN Conceptos AS C ON A.IdConcepto=C.IdConcepto
 INNER JOIN Terceros AS  CDT ON A.IdConductor=CDT.IdTercero  INNER JOIN Terceros AS T ON A.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON A.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Trn_TraManifiesto AS M ON A.TipMuc=M.TipDoc AND  A.Manifiesto=M.Manifiesto AND  A.IdCiaMuc=M.IdCia
 LEFT JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal LEFT JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 LEFT JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal LEFT JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 LEFT JOIN Rutas AS R ON M.IdRuta=R.IdRuta 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN CtasCorrientes AS CTA ON A.IdCta=CTA.IdCta LEFT JOIN Bancos AS B ON CTA.IdBanco=B.IdBanco
 LEFT JOIN Puc AS P ON A.IdCuenta=P.IdCuenta LEFT JOIN TiposCom AS TCM ON A.TipCom=TCM.IdCom 
 LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 LEFT JOIN Trn_TraAntAbonos AS AP ON A.TipDoc=AP.TipAnt AND A.Anticipo=AP.Anticipo AND A.IdCia=AP.IdCiaAnt
 WHERE A.TipDoc=@pmTipDoc AND A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND A.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND A.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 AND A.IdConductor LIKE ISNULL(@pmIdConductor,'%') AND A.Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY A.IdCia,A.Anticipo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraAnticiposRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
,@pmtmEst CHAR(2)=Null
 AS 
 SELECT A.TipDoc AS TipoAnt,A.Anticipo AS NumAnticipo,A.IdCia AS CdCia,Compania,A.Fecha AS FechaAnt,A.IdConcepto AS CdConcepto,Concepto,TipMuc,A.Manifiesto AS NumManif,IdCiaMuc,A.IdVehiculo AS PlacaVeh
,A.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,A.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,A.VrAnticipo AS ValAnticipo,VrAbonado,A.VrAnticipo-VrAbonado AS VrSaldo,Modalidad,TipoPago,NumCheque,FecCheque
,A.IdCta AS CdCta,NumeroCta,CTA.IdBanco,Banco,A.IdCuenta AS CdCuenta,NomCuenta,CedBenef,Beneficiario,A.Cantidad AS CantGals,FechaVence,NumPresAnt,PreAnticipo,NumCargue,CiaCargue,A.VrDeduccion,A.TipCom,TipoCom,A.Comprobante,A.IdCiaCom
,A.Anulado AS Anuldo,NumDev,A.FecDev AS FechaDev,A.Observacion AS Observ,A.IdEstado AS CdEstado,A.TimeSys AS FechaCrea,A.FecUpdate AS FechaAct,A.IdCiaCrea AS CdCiaCrea,A.IdUsuario AS CdUsuario,Usuario
--datos de manifiesto
,FecDespacho,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,VrFletes,TarifaFlete,PesoTotal,EstCumplido,Cumplido,IdCiaCump,FechaCump
--Datos del vehiculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TPR.TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,V.IdGrupo AS CdGrupo,GrupoProp
--,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
--Apr 10/2018 Deducciones de anticipos ,tmConcepto1,tmValor1,tmConcepto2,tmValor2,tmConcepto3,tmValor3,tmConcepto4,tmValor4,tmConcepto5,tmValor5
 FROM Trn_TraAnticipos AS A  INNER JOIN Companias AS CN ON A.IdCia=CN.IdCia  INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario 
 INNER JOIN Conceptos AS C ON A.IdConcepto=C.IdConcepto
 INNER JOIN Terceros AS  CDT ON A.IdConductor=CDT.IdTercero  INNER JOIN Terceros AS T ON A.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON A.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Trn_TraManifiesto AS M ON A.TipMuc=M.TipDoc AND A.Manifiesto=M.Manifiesto AND  A.IdCiaMuc=M.IdCia
 LEFT JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal LEFT JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 LEFT JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal LEFT JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 LEFT JOIN Rutas AS R ON M.IdRuta=R.IdRuta 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN CtasCorrientes AS CTA ON A.IdCta=CTA.IdCta LEFT JOIN Bancos AS B ON CTA.IdBanco=B.IdBanco
 LEFT JOIN Puc AS P ON A.IdCuenta=P.IdCuenta LEFT JOIN TiposCom AS TCM ON A.TipCom=TCM.IdCom 
 LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
-- LEFT JOIN TercCndtores AS CT ON A.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
--Apr 10/2018 Deducciones de anticipos LEFT JOIN tm_TraDedAnt AS DA ON A.TipDoc=DA.tmTipAnt AND A.Anticipo=DA.tmAnticipo AND A.IdCia=DA.tmIdCia 
 WHERE A.TipDoc=@pmTipDoc AND A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND A.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND A.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 AND A.IdConductor LIKE ISNULL(@pmIdConductor,'%') AND A.Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY A.IdCia,A.Anticipo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraManifiestoRelDet] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdRuta VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
 SELECT M.TipDoc AS CdTipDoc,M.Manifiesto AS NumManif,M.IdCia AS CdCia,Compania,M.Fecha,M.FecDespacho,FecEntrega,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,IdRutaFle,RF.Ruta AS RutaFletes
,M.IdVehiculo AS PlacaVeh,M.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,TipoAfiVehic,M.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,M.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,M.VrFletes,M.VrRetencion,M.VrReteIca,M.VrDescuento,M.VrAnticipo,M.VrAntAdic,M.VrNeto,M.VrPagos,M.VrCargos,M.VrDctos,M.TarifaFlete,M.Cantidad AS CantTotal,M.PesoTotal,M.BaseRet,M.TarifaRet,M.TarifaIca
,M.IdLocFletes,CF.Localidad AS LugarFletes,M.FechaPago,M.PagoCargue,M.PagoDescargue,NumMintrans,EdoMintrans,M.Cumplido AS NumCumplido,M.IdCiaCump AS CdCiaCump,M.FechaCump AS FecCumplido,EstCumplido
,M.TipOdp AS TipoOdp,M.OrdPago AS NumOrdPago,M.IdCiaOdp AS CdCiaOdp,M.FechaOdp,M.EstOrden,OP.TipEgr,OP.Egreso,OP.IdCiaEgr,OP.NumCheque AS EgrNumCheque,ISNULL(TotalEgresos,0) AS TotalEgresos
,M.VrFleteNeto,TipInfVia,NumViaje,CiaInfViaje,CdCatPeaje,M.Anulado,M.FecDev,M.Observacion AS Observ,M.IdEstado AS CdEstado,Estado,M.TipCom,M.Comprobante,M.IdCiaCom,M.TimeSys,M.FecUpdate,M.IdCiaCrea
,M.IdUsuario AS CdUsuario,Usuario,kmsTotal,VrFleteTon,TipoRuta,CdLocTrao,CdLocTrad,KmsTraOri,KmsTraDes,IdMneda,VrTasa,NomRemite,NomDestino,LugarFletes,NumAnticipo,MA.NumCheque,TipoMintrans,MucMintrans,ContIntegral,PuntosRuta,VolumenCarga
,CdCondRelev,CRV.RazonSocial AS SegConductor,ISNULL(Num_Radica,0) AS NumRadicac,Transbordo,NumMucAnu,CiaMucAnu
--detalles
,MR.Item AS ItemMuc,TipRem,MR.Remesa AS NumRemesa,MR.IdCiaRem AS CdCiaRem,ItemRem,D.IdMercancia AS CdMercancia,DescripMcias,CodigoMcia,D.Cantidad AS Cant,PesoNeto,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,dmsAlto,dmsAncho,dmsLargo,D.Volumen,UndVol
,D.Cases,D.Cajas,D.Palets,D.NitRemite,Remitente,DirOrigen,D.IdOrigen AS CodCiuOrigen,LO.Localidad AS CiudadOrigen,NitDestntario,Destinatario,DirDestino,D.IdDestino AS CodCiuDestino,LD.Localidad AS CiudadDestino,TarifClie,MR.TarifTabla AS TarifaTabla,MR.TarifPago AS TarifaPago
,D.VrDeclarado AS DetVrDeclarado,D.VrSeguro AS DetVrSeguro,TarifSeguro,UndTarifa,UndTarifPago,Remision,DocCliente,Referencia1,Referencia2,Referencia3,Contenedor1,Contenedor2,D.CdAgencia AS DetCdAgencia,D.Cumplido AS NumCumplidoRem,D.IdCiaCump AS CdCiaCumpRem
,D.FechaCump AS FecCumRem,DetalleCump,CantidadCump,PesoCump,VolCump,CasesCump,CajasCump,PaletsCump,TarifCump,PagoCump,UndTarCump,UndTarPagoCump
,TipFac,Factura,IdCiaFac,FechaFac,TarifClieFac,D.TipOdp AS DetTipOdp,D.NumeroOdp AS NumOdp,D.IdCiaOdp AS CdCiaOdp,TarifOdp,PesoCont,RemMintrans,EdoRemMin
,D.IdUnd AS CdUnd,UM.Unidad AS UndMedida,D.IdEmp AS CdEmp,Empaque,D.IdNat AS CdNat,Natlzaprod,D.IdMnjo AS CdMnjo,ManejoMcia,D.IdTmcia AS CdTmcia,TipoMcia,CdRango,DescripRango
--Datos del vehiculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia,V.IdGrupo AS CdGrupoPro,GrupoProp
 FROM Trn_TraManifiesto AS M INNER JOIN Trn_TraManifAnexo AS MA ON M.TipDoc=MA.TipDoc AND  M.Manifiesto=MA.Manifiesto AND  M.IdCia=MA.IdCia
 INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia  INNER JOIN EstadoDoc AS ED ON M.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta  INNER JOIN Terceros AS  CDT ON M.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS NP ON M.IdPropietario=NP.IdTercero   INNER JOIN Terceros AS T ON M.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 INNER JOIN Trn_TraManifRem AS MR ON M.TipDoc=MR.TipDoc AND  M.Manifiesto=MR.Manifiesto AND  M.IdCia=MR.IdCia
 INNER JOIN Trn_TraRemMcias AS D ON MR.TipRem=D.TipDoc  AND MR.Remesa=D.NumOrden AND MR.IdCiaRem=D.IdCia AND MR.ItemRem=D.Item
 INNER JOIN Mercancias AS MCA ON D.IdMercancia=MCA.IdMercancia
 INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal   INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN Empaques AS EMP ON D.IdEmp=EMP.IdEmp
 INNER JOIN TiposNat AS NZ ON D.IdNat=NZ.IdNat INNER JOIN TiposMnjo AS MJ ON D.IdMnjo=MJ.IdMnjo
 INNER JOIN TiposMcia AS TM ON D.IdTmcia=TM.IdTmcia 
 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 M.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN Rutas AS  RF ON M.IdRutaFle=RF.IdRuta  LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed 
 LEFT JOIN RangosPeso AS RP ON D.CdRango=RP.IdRango  LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 LEFT JOIN Trn_TraOrdenManif AS OP ON M.TipOdp=OP.TipDoc AND M.OrdPago=OP.OrdPago AND M.IdCiaOdp=OP.IdCia
 LEFT JOIN Terceros AS CRV ON M.CdCondRelev=CRV.IdTercero
 LEFT JOIN (SELECT EO.TipOdp AS TipOrden,EO.OrdPago AS NumOPago,EO.IdCiaOdp AS CdCiaOpago,SUM(VrAbonado) AS TotalEgresos
 FROM Trn_TraEgrOrden AS EO INNER JOIN Trn_Comprobantes AS E ON EO.TipCom=E.TipCom AND EO.NumEgreso=E.Comprobante AND EO.IdCia=E.IdCia
 WHERE E.Anulado=0 GROUP BY EO.TipOdp,EO.OrdPago,EO.IdCiaOdp) AS EG ON M.TipOdp=EG.TipOrden AND M.OrdPago=EG.NumOPago AND M.IdCiaOdp=EG.CdCiaOpago
 --radicacion de cumplidos
 LEFT JOIN (SELECT TipMuc,Manifiesto,IdCiaMuc,MAX(NumRadica) AS Num_Radica FROM Trn_TraCumRadica GROUP BY TipMuc,Manifiesto,IdCiaMuc) AS RC ON M.TipDoc=RC.TipMuc AND M.Manifiesto=RC.Manifiesto AND M.IdCia=RC.IdCiaMuc
--Condiciones consulta general
 WHERE M.TipDoc=@pmTipDoc AND  M.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND M.IdRuta LIKE ISNULL(@pmIdRuta,'%') AND M.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND M.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 AND M.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 ORDER BY M.IdCia,M.Manifiesto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevLegRelDet] @pmTipDev VARCHAR(3),@pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,C.Concepto AS DescConcepto,D.TipDoc AS TipLiq,D.Liquidacion AS NumLiquida,IdCiaDoc,FecDoc,D.Modalidad AS Modaldad
,D.TipMuc AS TipManif,D.Manifiesto AS NumManif,D.IdCiaMuc AS CdCiaManif,D.IdVehiculo AS PlacaVeh,D.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,D.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario
,D.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,D.NitEmpresa AS Nit_Empresa,ETR.RazonSocial AS NomEmpresa,D.VrAnticipos AS TotalAntcpos,D.VrGastos AS TotalGastos,D.VrCombustible AS TotalComb,D.VrDiferencia AS ValorDif
,D.VrOtrosCred AS TotalOtrosDed,D.galsComb AS CantGals,D.PesoTotal AS Peso_Total,D.Cantidad AS CantTotal,D.Volumen AS VolTotal,ModdDev,D.OrigenAdd AS OrigenCrea,D.TipCom AS CdTipComp,TipoCom,D.Comprobante AS NumComp,D.IdCiaCom AS CdCiaComp
,D.Observacion AS Observ,D.IdEstado AS CdEstado,D.TimeSys AS FechaCrea,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
,LQ.IdRuta AS CdRuta,Ruta,IdLocOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,IdLocDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
--detales
,Item,DT.IdConcepto AS DetCodConc,CG.Concepto AS DetNomConc,DescConcepto,DT.IdCuenta AS CdCuenta,NomCuenta,VrDebito,VrCredito,DT.IdTercero AS NitTercero,N.RazonSocial AS NomTercero
,CdCCosto,CCosto,CdSubCos,SubCosto,DT.RubroConc,VrBase,TarifBase,CantComb,TipFac,Factura,IdCiaFac,ItemFac,FecVence,RefConcep
--datos del vehículo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,V.NitEmpresa AS VehNitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,V.IdGrupo AS CdGrupo,GrupoProp
 FROM Trn_TraDevLeg AS D  INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto INNER JOIN Terceros AS T ON D.IdPoseedor=T.IdTercero
 INNER JOIN Terceros AS  CDT ON D.IdConductor=CDT.IdTercero   INNER JOIN Terceros AS NP ON D.IdPropietario=NP.IdTercero   
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 INNER JOIN Trn_TraLiqViajes AS LQ ON  D.TipDoc=LQ.TipDoc AND D.Liquidacion=LQ.Liquidacion AND D.IdCiaDoc=LQ.IdCia
 INNER JOIN Localidades AS CO ON  LQ.IdLocOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON LQ.IdLocDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON LQ.IdRuta=R.IdRuta  
 LEFT JOIN Terceros AS ETR ON D.NitEmpresa=ETR.IdTercero LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero
 LEFT JOIN Trn_TraLiqDetalle AS DT ON D.TipDoc=DT.TipDoc AND D.Liquidacion=DT.Liquidacion AND  D.IdCiaDoc=DT.IdCia
 LEFT JOIN ConcDiversos AS CG ON DT.IdConcepto=CG.IdConcepto LEFT JOIN Puc AS P ON DT.IdCuenta=P.IdCuenta
 LEFT JOIN Terceros AS N ON  DT.IdTercero=N.IdTercero LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 LEFT JOIN CentroCosto AS CC ON DT.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON DT.CdSubCos=SC.IdSubCos 
 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.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 AND D.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 ORDER BY D.IdCia,D.Devolucion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraManifModLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT MV.Manifiesto AS NumManif,MV.IdCia AS CdCia,Compania,M.Fecha AS FecManif,FecDespacho,FecEntrega,M.IdVehiculo AS PlacaVeh
,MV.Fecha AS FechaMod,Item,MV.VrFleteTotal AS FleteTotal,MV.VrRetencion AS Retencion,MV.VrReteIca AS ReteIca,MV.VrAnticipo AS Anticipo,MV.PesoTotal AS Peso_Total
,MV.NumCump AS MmcCumplido,MV.CdCiaCump AS MmcCiaCump,IdLugarPago,LP.Localidad AS LugarPago,MV.FechaPago AS FecPagoSaldo,MV.TimeSys AS FechaCrea,MV.IdUsuario AS MmcIdUsuario,UM.Usuario AS MmcUsuario
,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,IdRutaFle,RF.Ruta AS RutaFletes,M.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,TipoAfiVehic,M.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario
,M.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,M.VrFletes AS MucFleteTotal,M.VrRetencion AS MucRetencion,M.VrReteIca AS MucReteIca,VrDescuento,M.VrAnticipo AS MucAnticipo,VrAntAdic,VrNeto,VrPagos,VrCargos,VrDctos
,TarifaFlete,M.Cantidad AS MucCantidad,M.PesoTotal AS MucPesoTotal,BaseRet,TarifaRet,TarifaIca,M.IdLocFletes AS MucIdLugar,CF.Localidad AS MucLugarPago,M.FechaPago AS MucFecPago,PagoCargue,PagoDescargue,NumMintrans,EdoMintrans,M.Cumplido AS NumCumplido,M.IdCiaCump AS CdCiaCump
,M.FechaCump AS FecCumplido,EstCumplido,M.TipOdp AS TipoOdp,M.OrdPago AS NumOrdPago,M.IdCiaOdp AS CdCiaOdp,FechaOdp,EstOrden,Anulado,FecDev,M.Observacion AS Observ,M.IdEstado AS CdEstado,Estado
,TipCom,Comprobante,IdCiaCom,M.TimeSys AS FechaCrea,FecUpdate,IdCiaCrea,M.IdUsuario AS CdUsuario,U.Usuario AS MucUsuario,M.TipDoc AS CdTipDoc
,kmsTotal,VrFleteTon,TipoRuta,CdLocTrao,CdLocTrad,KmsTraOri,KmsTraDes,IdMneda,VrTasa,NomRemite,NomDestino,LugarFletes,NumAnticipo,NumCheque,TipoMintrans,MucMintrans,ContIntegral,VolumenCarga
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MC.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
 FROM Trn_TraManifMod AS MV INNER JOIN Trn_TraManifiesto AS M ON MV.TipDoc=M.TipDoc AND MV.Manifiesto=M.Manifiesto AND MV.IdCia=M.IdCia
 INNER JOIN Trn_TraManifAnexo AS MA ON M.TipDoc=MA.TipDoc AND  M.Manifiesto=MA.Manifiesto AND  M.IdCia=MA.IdCia
 INNER JOIN Companias AS CN ON MV.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON M.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS CO ON M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta INNER JOIN Terceros AS  CDT ON M.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS NP ON M.IdPropietario=NP.IdTercero INNER JOIN Terceros AS T ON M.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MC ON V.IdMarca=MC.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd INNER JOIN adm_Usuarios AS UM ON MV.IdUsuario=UM.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 M.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN Rutas AS RF ON M.IdRutaFle=RF.IdRuta  LEFT JOIN Localidades AS LP ON MV.IdLugarPago=LP.IdLocal 
 WHERE MV.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND MV.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevAosFmt] @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
AS 
 SELECT D.TipDev AS IdTipDev,TipoDoc,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc,D.Anticipo AS NumAnticipo,IdCiaDoc,FecDoc
,VrDevolucion,A.VrAnticipo,A.VrAbonado,A.VrAnticipo-A.VrAbonado AS SaldoActual,TipoPago,NumCheque,FecCheque,CdCta,NumeroCta,CT.IdBanco,Banco,CdCuenta,NomCuenta,A.Cantidad AS CantGalones
,TipOds,A.NumOrden,IdCiaOds,O.Fecha AS FecOrden,O.IdVehiculo AS PlacaVeh,nRemolque,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor
,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,O.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,O.VrTotal,O.VrCosto,O.Cantidad
,CedBenef,Beneficiario,FechaVence,D.ModdDev,D.OrigenAdd,D.TipCom,D.Comprobante AS NumComp,D.IdCiaCom,D.Observacion AS Observ
,D.IdEstado AS CdEstado,Estado,D.TimeSys AS FechaCrea,D.FecUpdate AS FechaModif,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario,Leyenda
--datos orden
,O.Modalidad,O.TipoOrden,O.IdEstacion AS CdCampo,EstServicio AS NomCampo,NomEstacion,TipoResiduo,RemCliente,NomRecibido,IdDestino,LD.Localidad AS NomDestino
,FecDespacho,FecRecibo,FecIngCertif,FecCertificado,NumRemesa,CdCiaRem,EstFactura,TipFact,NumFactura,CdCiaFact,EstOrden,TipLiq,NumLiquida,CdCiaLiq,O.Observacion AS OdsObserv
--Cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TPR.TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraDevAos AS D INNER JOIN Trn_TraOrdenAnt AS A ON D.TipDoc=A.TipDoc AND D.Anticipo=A.Anticipo AND D.IdCiaDoc=A.IdCia
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN Trn_TraOrdenServ AS O ON A.TipOds=O.TipDoc AND A.NumOrden=O.NumOrden AND A.IdCiaOds=O.IdCia
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero
 INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN CtasCorrientes AS CT ON D.CdCta=CT.IdCta LEFT JOIN Bancos AS B ON CT.IdBanco=B.IdBanco
 LEFT JOIN Puc AS PU ON D.CdCuenta=PU.IdCuenta LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 WHERE D.TipDev=@pmTipDev AND D.Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraManifiestoRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdRuta VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
 SELECT M.TipDoc AS CdTipDoc,M.Manifiesto AS NumManif,M.IdCia AS CdCia,Compania,M.Fecha,M.FecDespacho,FecEntrega,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,IdRutaFle,RF.Ruta AS RutaFletes
,M.IdVehiculo AS PlacaVeh,M.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,TipoAfiVehic,M.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,M.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,M.VrFletes,M.VrRetencion,M.VrReteIca,M.VrDescuento,M.VrAnticipo,M.VrAntAdic,M.VrNeto,M.VrPagos,M.VrCargos,M.VrDctos,M.TarifaFlete,M.Cantidad AS CantTotal,M.PesoTotal,M.BaseRet,M.TarifaRet,M.TarifaIca
,M.IdLocFletes,CF.Localidad AS LugarFletes,M.FechaPago,M.PagoCargue,M.PagoDescargue,NumMintrans,EdoMintrans,M.Remesa AS NumRemesa,M.IdCiaRem,M.Cumplido AS NumCumplido,M.IdCiaCump AS CdCiaCump,M.FechaCump AS FecCumplido,EstCumplido
,M.TipOdp AS TipoOdp,M.OrdPago AS NumOrdPago,M.IdCiaOdp AS CdCiaOdp,FechaOdp,EstOrden,OP.TipEgr,OP.Egreso,OP.IdCiaEgr,OP.NumCheque AS EgrNumCheque,ISNULL(TotalEgresos,0) AS TotalEgresos,M.VrFleteNeto,TipInfVia,NumViaje,CiaInfViaje,CdCatPeaje,M.Anulado,M.FecDev
,M.Observacion AS Observ,M.IdEstado AS CdEstado,Estado,M.TipCom,M.Comprobante,M.IdCiaCom,TimeSys,FecUpdate,IdCiaCrea,M.IdUsuario AS CdUsuario,Usuario
,kmsTotal,VrFleteTon,TipoRuta,CdLocTrao,CdLocTrad,KmsTraOri,KmsTraDes,IdMneda,VrTasa,NomRemite,NomDestino,LugarFletes,NumAnticipo,MA.NumCheque,TipoMintrans,MucMintrans,ContIntegral,PuntosRuta,MA.CantViajes,VolumenCarga,ISNULL(Num_Radica,0) AS NumRadicac
,Transbordo,NumMucAnu,CiaMucAnu
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia,V.IdGrupo AS CdGrupoPro,GrupoProp,CdCondRelev,CRV.RazonSocial AS SegConductor
 FROM Trn_TraManifiesto AS M INNER JOIN Trn_TraManifAnexo AS MA ON M.TipDoc=MA.TipDoc AND  M.Manifiesto=MA.Manifiesto AND M.IdCia=MA.IdCia
 INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia  INNER JOIN EstadoDoc AS ED ON M.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta  INNER JOIN Terceros AS  CDT ON M.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS NP ON M.IdPropietario=NP.IdTercero   INNER JOIN Terceros AS T ON M.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 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 M.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN Rutas AS  RF ON M.IdRutaFle=RF.IdRuta LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 LEFT JOIN Terceros AS CRV ON M.CdCondRelev=CRV.IdTercero
 LEFT JOIN Trn_TraOrdenManif AS OP ON M.TipOdp=OP.TipDoc AND M.OrdPago=OP.OrdPago AND M.IdCiaOdp=OP.IdCia
 LEFT JOIN (SELECT EO.TipOdp AS TipOrden,EO.OrdPago AS NumOPago,EO.IdCiaOdp AS CdCiaOpago,SUM(VrAbonado) AS TotalEgresos
 FROM Trn_TraEgrOrden AS EO INNER JOIN Trn_Comprobantes AS E ON EO.TipCom=E.TipCom AND EO.NumEgreso=E.Comprobante AND EO.IdCia=E.IdCia
 WHERE E.Anulado=0 GROUP BY EO.TipOdp,EO.OrdPago,EO.IdCiaOdp) AS EG ON M.TipOdp=EG.TipOrden AND M.OrdPago=EG.NumOPago AND M.IdCiaOdp=EG.CdCiaOpago
 --radicacion de cumplidos
 LEFT JOIN (SELECT TipMuc,Manifiesto,IdCiaMuc,MAX(NumRadica) AS Num_Radica FROM Trn_TraCumRadica GROUP BY TipMuc,Manifiesto,IdCiaMuc) AS RC ON M.TipDoc=RC.TipMuc AND M.Manifiesto=RC.Manifiesto AND M.IdCia=RC.IdCiaMuc
--Condiciones consulta general
 WHERE M.TipDoc=@pmTipDoc AND  M.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND M.IdRuta LIKE ISNULL(@pmIdRuta,'%') AND M.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND M.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 AND M.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenLiqRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT L.TipDoc AS IdTipDoc,TipoDoc,L.Liquidacion,L.IdCia AS CdCia,Compania,L.Fecha AS FechaLiq,L.IdConcepto AS CdConcepto,Concepto,TipOds,L.NumOrden,IdCiaOds,O.Fecha AS FecOrden
,L.IdVehiculo AS PlacaVeh,L.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,L.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,L.TipoAfiVehic
,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia,O.IdDestino,LD.Localidad AS NomDestino,O.Modalidad,O.TipoOrden,O.IdEstacion AS CdCampo,EstServicio AS NomCampo
,O.FecDespacho,O.FecRecibo,O.FecIngCertif,O.FecCertificado,TipoResiduo,O.Cantidad AS CantOrden,O.RemCliente,O.NumRemesa,O.CdCiaRem,L.VrTotal,L.VrCosto,L.VrAnticipo,VrGastos,VrIva,VrRetencion,VrReteIca
,VrReteCREE,VrReteIva,VrOtros,VrDescuento,VrConcCargos,VrConcDed,VrNeto,L.Cantidad,TarifaPago,BaseImp,BaseRet,BaseIca,TarifaImp,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,CodTarRtc
,L.Referencia,L.CxPagar,CdPlazo,Plazo,DiasPago,FechaVence,L.UndMed,Unidad,L.TipCom,L.Comprobante,L.IdCiaCom,L.Anulado,L.NumDev,L.FecDev,L.Observacion AS Observ,L.IdEstado AS CdEstado,Estado
,L.TipEgr,L.Egreso,L.IdCiaEgr,L.NumCheque,ISNULL(VC.VrTotalAbo,0) AS CxpAbonado,ISNULL(VC.VrSaldo,0) AS CxpSaldo
,L.OrigenAdd,L.TimeSys AS FechaCrea,L.FecUpdate AS FechaModif,L.IdCiaCrea AS CdCiaCrea,L.IdUsuario AS CdUsuario,Usuario
--vehiuculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,LT.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,L.CdCCosto,CCosto,L.CdSubCos,SubCosto
 FROM Trn_TraOrdenLiq AS L INNER JOIN Companias AS CN ON L.IdCia=CN.IdCia INNER JOIN Conceptos AS CT ON L.IdConcepto=CT.IdConcepto
 INNER JOIN adm_Usuarios AS U ON L.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS ED ON L.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS NP ON L.IdPoseedor=NP.IdTercero INNER JOIN Terceros AS CDT ON L.IdConductor=CDT.IdTercero
 INNER JOIN Trn_TraOrdenServ AS O ON L.TipOds=O.TipDoc AND L.NumOrden=O.NumOrden AND L.IdCiaOds=O.IdCia
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion 
 INNER JOIN Sys_TiposDoc AS TD ON L.TipDoc=TD.IdDoc INNER JOIN Localidades AS LT ON T.IdLocal=LT.IdLocal
 INNER JOIN Vehiculos AS V ON L.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal LEFT JOIN Sys_Um AS UM ON L.UndMed=UM.UndMed
 LEFT JOIN Plazos AS PZ ON L.CdPlazo=PZ.IdPlazo
 LEFT JOIN CentroCosto AS CC ON L.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON L.CdSubCos=SC.IdSubCos
 --Subconsulta egresos-pagos
 LEFT JOIN (SELECT TipFac,Factura,IdCia,IdProveedor,Documento,SUM(VrFactura) AS VrTotalFac,SUM(VrAbonado) AS VrTotalAbo,SUM(VrFactura-VrAbonado) AS VrSaldo FROM Trn_ComFactura
 WHERE TipFac=@pmTipDoc AND TipDoc=@pmTipDoc AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY TipFac,Factura,IdCia,IdProveedor,Documento) AS VC ON L.TipDoc=VC.TipFac AND L.Liquidacion=VC.Documento AND L.IdCia=VC.IdCia
 -- FIN Subconsulta
 WHERE L.TipDoc=@pmTipDoc AND L.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND L.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposPpt] @pmIdPpd VARCHAR(4)
 AS 
 DELETE FROM TiposPpt
 WHERE IdPpd=@pmIdPpd
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevLgoFmt] @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT D.TipDev AS IdTipDev,TipoDoc,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc,D.Liquidacion AS NumLiquida,D.IdCiaDoc,FecDoc
,TipOds,L.NumOrden,IdCiaOds,L.IdVehiculo AS PlacaVeh,L.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,L.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,L.TipoAfiVehic
,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia,O.IdDestino,LD.Localidad AS NomDestino,O.TipoOrden,O.IdEstacion AS CdCampo,EstServicio AS NomCampo
,O.FecDespacho,O.FecRecibo,O.FecIngCertif,O.FecCertificado,TipoResiduo,O.RemCliente,O.NumRemesa,O.CdCiaRem
,D.VrTotal,D.VrCosto,D.VrAnticipo,D.VrGastos,D.VrIva,D.VrRetencion,D.VrReteIca,D.VrReteCREE,D.VrReteIva,D.VrOtros,D.VrDescuento,L.VrConcCargos,L.VrConcDed,D.VrNeto,D.Cantidad
,TarifaPago,BaseImp,BaseRet,BaseIca,TarifaImp,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,L.Referencia,CdPlazo,Plazo,DiasPago,FechaVence,L.UndMed,Unidad
,D.CxPagar,D.TipCom,D.Comprobante,D.IdCiaCom,D.ModdDev,D.Observacion AS Observ,D.IdEstado AS CdEstado,D.OrigenAdd,D.TimeSys AS FechaCrea
,D.FecUpdate AS FechaModif,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario,Leyenda
--vehiuculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,LT.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraDevLgo AS D INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia INNER JOIN Conceptos AS CT ON D.IdConcepto=CT.IdConcepto
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN Trn_TraOrdenLiq AS L ON D.TipDoc=L.TipDoc AND D.Liquidacion=L.Liquidacion AND D.IdCiaDoc=L.IdCia
 INNER JOIN Terceros AS NP ON L.IdPoseedor=NP.IdTercero INNER JOIN Terceros AS CDT ON L.IdConductor=CDT.IdTercero
 INNER JOIN Trn_TraOrdenServ AS O ON L.TipOds=O.TipDoc AND L.NumOrden=O.NumOrden AND L.IdCiaOds=O.IdCia
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion 
 INNER JOIN Localidades AS LT ON T.IdLocal=LT.IdLocal
 INNER JOIN Vehiculos AS V ON L.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 LEFT JOIN Plazos AS PZ ON L.CdPlazo=PZ.IdPlazo  LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal LEFT JOIN Sys_Um AS UM ON L.UndMed=UM.UndMed
 WHERE D.TipDev=@pmTipDev AND D.Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevLiqFmt] @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,Concepto,DV.TipDoc AS TipoLiq,DV.Liquidacion AS NumLiquida,IdCiaDoc,FecDoc
,DV.IdVehiculo AS PlacaVeh,DV.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,DV.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,VrTotal,ModdDev
,TipCom,TipoCom,Comprobante,IdCiaCom,DV.Observacion AS Observ,TimeSys,IdCiaCrea,DV.IdUsuario AS CdUsuario,Usuario,Leyenda
,Item,TipCau,Causacion,IdCiaCau,ItemCau,FechaCau,D.IdConcepto AS CdConcCausac,D.Descripcion AS DetDescripcion,VrAbono,NitPoseedor,N.RazonSocial AS NomPoseedor,pVehiculo,EsVence
,TipOdp,OrdenPago,IdCiaOdp,TipMuc,Manifiesto,IdCiaMuc,VrSaldoOrden,CdCuenta,CdCuePagar,NumFactura,CdCCosto,CCosto,CdSubCos,SubCosto
--datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraDevLiq AS DV   INNER JOIN Companias AS CN ON DV.IdCia=CN.IdCia INNER JOIN Sys_TiposDoc AS TD ON DV.TipDev=TD.IdDoc
 INNER JOIN adm_Usuarios AS U ON DV.IdUsuario=U.IdUsuario   INNER JOIN Conceptos AS C ON DV.IdConcepto=C.IdConcepto
 INNER JOIN Terceros AS  CDT ON DV.IdConductor=CDT.IdTercero  INNER JOIN Terceros AS T ON DV.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON DV.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 INNER JOIN Trn_TraLiquidaDed AS D ON DV.TipDoc=D.TipDoc  AND DV.Liquidacion=D.Liquidacion AND DV.IdCiaDoc=D.IdCia
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos 
 LEFT JOIN Terceros AS N ON  D.NitPoseedor=N.IdTercero  LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero 
 LEFT JOIN TiposCom AS TCM ON DV.TipCom=TCM.IdCom 
 WHERE DV.TipDev=@pmTipDev AND DV.Devolucion BETWEEN @pmDevolucionIni AND  @pmDevolucionFin AND DV.IdCia=@pmIdCia
 ORDER BY  DV.Devolucion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenAntRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=NULL
 AS 
 SELECT A.TipDoc AS TipoAnt,TipoDoc,Anticipo,A.IdCia AS CdCia,Compania,A.Fecha AS FecAnt,A.IdConcepto AS CdConcepto,Concepto,TipOds,A.NumOrden,IdCiaOds,O.Fecha AS FecOrden
,O.IdVehiculo AS PlacaVeh,nRemolque,O.TipoAfiVehic,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,O.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,AG.Agencia,A.VrAnticipo,A.VrAbonado,A.VrAnticipo-A.VrAbonado AS VrSaldo,TipoPago,A.NumCheque,FecCheque
,A.IdCta AS CdCta,NumeroCta,CTA.IdBanco,Banco,A.IdCuenta AS CdCuenta,PU.NomCuenta,CedBenef,Beneficiario,FechaVence,TipoAncpo,NumPresAnt,A.Cantidad AS CantGalones,A.TipCom,A.Comprobante,A.IdCiaCom,A.Anulado AS Anuldo,A.NumDev
,A.FecDev,A.Observacion AS Observ,A.IdEstado AS CdEstado,Estado,A.OrigenAdd,A.TimeSys AS FechaCrea,A.FecUpdate AS FechaModif,A.IdCiaCrea AS CdCiaCrea,A.IdUsuario AS CdUsuario,Usuario
--datos de la orden
,O.Modalidad,O.TipoOrden,O.VrTotal,O.VrCosto,O.Cantidad,O.IdEstacion AS CdCampo,EstServicio AS NomCampo,NomEstacion,TipoResiduo,RemCliente,NomRecibido
,O.CdRuta,Ruta,O.IdOrigen,LO.Localidad AS Origen,O.IdDestino,LD.Localidad AS NomDestino,FecDespacho,FecRecibo,FecIngCertif,FecCertificado,NumRemesa,CdCiaRem,EstCumplido
,EstFactura,TipFact,NumFactura,CdCiaFact,EstOrden,TipLiq,NumLiquida,CdCiaLiq,O.Observacion AS OdsObserv
--Cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TPR.TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
 FROM Trn_TraOrdenAnt AS A INNER JOIN Trn_TraOrdenServ AS O ON A.TipOds=O.TipDoc AND A.NumOrden=O.NumOrden AND A.IdCiaOds=O.IdCia
 INNER JOIN Companias AS CN ON A.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON A.IdConcepto=C.IdConcepto INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero
 INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Sys_TiposDoc AS TD ON A.TipDoc=TD.IdDoc
 INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 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 O.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN Agencias AS AG ON O.IdAgencia=AG.IdAgencia  LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 LEFT JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal LEFT JOIN Rutas AS RT ON O.CdRuta=RT.IdRuta
 LEFT JOIN CtasCorrientes AS CTA ON A.IdCta=CTA.IdCta LEFT JOIN Bancos AS B ON CTA.IdBanco=B.IdBanco
 LEFT JOIN Puc AS PU ON A.IdCuenta=PU.IdCuenta
 WHERE A.TipDoc=@pmTipDoc AND A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevAosRel] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS 
 SELECT D.TipDev AS IdTipDev,TipoDoc,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc,D.Anticipo AS NumAnticipo,IdCiaDoc,FecDoc
,VrDevolucion,A.VrAnticipo,A.VrAbonado,A.VrAnticipo-A.VrAbonado AS SaldoActual,TipoPago,NumCheque,FecCheque,CdCta,NumeroCta,CT.IdBanco,Banco,CdCuenta,NomCuenta,A.Cantidad AS CantGalones
,TipOds,A.NumOrden,IdCiaOds,O.Fecha AS FecOrden,O.IdVehiculo AS PlacaVeh,nRemolque,O.TipoAfiVehic,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor
,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,O.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,O.VrTotal,O.VrCosto,O.Cantidad
,CedBenef,Beneficiario,FechaVence,D.ModdDev,D.OrigenAdd,D.TipCom,D.Comprobante AS NumComp,D.IdCiaCom,D.Observacion AS Observ
,D.IdEstado AS CdEstado,Estado,D.TimeSys AS FechaCrea,D.FecUpdate AS FechaModif,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario,Leyenda
--datos orden
,O.Modalidad,O.TipoOrden,O.IdEstacion AS CdCampo,EstServicio AS NomCampo,NomEstacion,TipoResiduo,RemCliente,NomRecibido,O.CdRuta,Ruta,O.IdOrigen,LO.Localidad AS Origen,O.IdDestino,LD.Localidad AS NomDestino
,FecDespacho,FecRecibo,FecIngCertif,FecCertificado,NumRemesa,CdCiaRem,EstFactura,TipFact,NumFactura,CdCiaFact,EstOrden,TipLiq,NumLiquida,CdCiaLiq,O.Observacion AS OdsObserv
--Cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TPR.TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraDevAos AS D INNER JOIN Trn_TraOrdenAnt AS A ON D.TipDoc=A.TipDoc AND D.Anticipo=A.Anticipo AND D.IdCiaDoc=A.IdCia
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN Trn_TraOrdenServ AS O ON A.TipOds=O.TipDoc AND A.NumOrden=O.NumOrden AND A.IdCiaOds=O.IdCia
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero
 INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN CtasCorrientes AS CT ON D.CdCta=CT.IdCta LEFT JOIN Bancos AS B ON CT.IdBanco=B.IdBanco
 LEFT JOIN Puc AS PU ON D.CdCuenta=PU.IdCuenta LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 LEFT JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal LEFT JOIN Rutas AS RT ON O.CdRuta=RT.IdRuta
 WHERE D.TipDev=@pmTipDev AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraCumplidoRelDet] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME, @pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null
 AS 
 SELECT CU.TipDoc AS TipCum,CU.Cumplido AS NumCumplido,CU.IdCia AS CdCia,Compania,CU.Fecha AS FechaCum,TipMuc,CU.Manifiesto AS NumManif,IdCiaMuc,CU.IdVehiculo AS PlacaVeh,Modalidad,DiasPlazo,FecPago
,TipoMargen,MargenFalt,UndCalcFalt,TarifFaltPago,TarifFaltCobro,NRadicaDoc,IdCiaRadic,CdCiaOfic,FecRadic,CU.Anulado AS Anuldo,CU.FecDev AS FechaDev,TipoComp,NumComp,NumRadicaMT,CU.Observacion AS Observ,CU.IdEstado AS CdEstado,Estado
,CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,CdRuta,R.Ruta AS DescRuta
,CU.TimeSys AS FechaCrea,CU.FecUpdate AS FechaAct,CU.IdCiaCrea AS CdCiaCrea,CU.IdUsuario AS CdUsuario,Usuario
,M.Fecha AS FecManif,FecDespacho,M.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,TipoAfiVehic,M.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,M.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,VrFletes,VrRetencion,VrReteIca,VrDescuento,VrAnticipo,VrAntAdic,VrNeto,VrPagos,VrCargos,VrDctos,TarifaFlete,M.Cantidad AS CantTotal,PesoTotal
,IdLocFletes,CF.Localidad AS LugarFletes,FechaPago,PagoCargue,PagoDescargue,M.TipOdp AS TipoOdp,M.OrdPago AS NumOrdPago,M.IdCiaOdp AS CdCiaOdp,FechaOdp,EstOrden,M.Observacion AS MucObserv
,MA.TipoRuta,MA.kmsTotal,MA.NomRemite,MA.NomDestino,MA.LugarFletes,MA.NumAnticipo AS NumAnticipo,MA.NumCheque AS Num_Cheque,MA.TipoMintrans,MA.WsSeguro,MA.NumRadSeguro,CU.TipoCumpMT,CU.MotivoSusp,CU.ConsecSusp
--detalle de cumplidos
,D.Item AS DetItem,TipRem,D.Remesa AS NumRemesa,D.IdCiaRem AS CdCiaRem,ItemRem,D.Cantidad AS Cant,D.PesoNeto AS PesoCump,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,D.Volumen AS VolCump,D.UndVol AS Und_Vol
,D.Cases AS CasesCump,D.Cajas AS CajasCump,D.Palets AS PaletsCump,D.TarifClie AS Tarif_Clie,D.TarifPago AS Tarif_Pago,TarifFlete,UndTarifClie,D.UndTarifPago AS UndTarifPag,CantCargue,PesoCargue,VolCargue,CasesCargue,CajasCargue,PaletsCargue
,EstadoCump,D.Remision AS NumRemision,D.DocCliente AS CumDocClie,D.Referencia1 AS CumRef1,D.Referencia2 AS CumRef2,D.Referencia3 AS CumRef3,D.Detalle AS CumDetalle
,IdMercancia,DescripMcias,RM.Cantidad AS RemCant,RM.PesoNeto AS RemPeso,NitRemite,Remitente,NitDestntario,Destinatario,D.HoraLlegaCargue,D.HoraEntraCargue,D.HoraSaleCargue,D.HoraLlegaDescargue,D.HoraEntraDescargue,D.HoraSaleDescargue
,D.CodCCosto,CCosto,D.CodSubCos,SubCosto
--Datos del vehiculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,V.IdGrupo AS CdGrupoPro,GrupoProp
 FROM Trn_TraCumplido AS CU INNER JOIN Companias AS CN ON CU.IdCia=CN.IdCia
 INNER JOIN EstadoDoc AS ED ON CU.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON CU.IdUsuario=U.IdUsuario
 INNER JOIN Trn_TraManifiesto AS M ON CU.TipMuc=M.TipDoc AND CU.Manifiesto=M.Manifiesto AND CU.IdCiaMuc=M.IdCia
 INNER JOIN Trn_TraManifAnexo AS MA ON CU.TipMuc=MA.TipDoc AND CU.Manifiesto=MA.Manifiesto AND CU.IdCiaMuc=MA.IdCia
 INNER JOIN Terceros AS  CDT ON M.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS NP ON M.IdPropietario=NP.IdTercero   INNER JOIN Terceros AS T ON M.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON CU.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 INNER JOIN Trn_TraCumRemesas AS D ON CU.TipDoc=D.TipDoc  AND CU.Cumplido=D.Cumplido AND CU.IdCia=D.IdCia
 LEFT JOIN Localidades AS CO ON CU.CdOrigen=CO.IdLocal LEFT JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 LEFT JOIN Localidades AS CD ON CU.CdDestino=CD.IdLocal LEFT JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 LEFT JOIN Rutas AS R ON CU.CdRuta=R.IdRuta LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed 
 LEFT JOIN Trn_TraRemMcias AS RM ON D.TipRem=RM.TipDoc  AND D.Remesa=RM.NumOrden AND D.IdCiaRem=RM.IdCia  AND D.ItemRem=RM.Item
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 LEFT JOIN CentroCosto AS CC ON D.CodCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CodSubCos=SC.IdSubCos
 WHERE CU.TipDoc=@pmTipDoc AND CU.Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND  CU.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND  CU.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND M.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%') AND M.IdConductor LIKE ISNULL(@pmIdConductor,'%')
  ORDER BY CU.IdCia,CU.Cumplido
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraLiqViajesRelDet] @pmTipDoc VARCHAR(3),@pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
 SELECT LQ.TipDoc AS TipoLiq,LQ.Liquidacion AS NumLiquida,LQ.IdCia AS CdCia,Compania,Fecha,LQ.IdConcepto AS CdConcepto,C.Concepto AS DescConcepto,Modalidad,TipMuc,Manifiesto,IdCiaMuc,FechaMuc
,LQ.IdVehiculo AS PlacaVeh,LQ.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,LQ.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,LQ.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,LQ.NitEmpresa AS Nit_Empresa,ETR.RazonSocial AS NomEmpresa,VrAnticipos,VrGastos,VrCombustible,VrOtrosCred,VrDiferencia,galsComb,PesoTotal,Cantidad,Volumen
,LQ.IdRuta AS CdRuta,Ruta,IdLocOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,IdLocDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,Referencia,DescMcias,LiqMixto,kmtInicial,kmtFinal,EstFactura,TipDocRef,NumDocRef,IdCiaRef,FecDocRef,Num_Ppto,TipCom,TipoCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,LQ.Observacion AS Observ,LQ.IdEstado AS CdEstado,TimeSys,LQ.FecUpdate AS FechaAct,IdCiaCrea,LQ.IdUsuario AS CdUsuario,Usuario
--detalles
,Item,D.IdConcepto AS DetCodConc,CG.Concepto AS DetNomConc,DescConcepto,D.IdCuenta AS CdCuenta,NomCuenta,VrDebito,VrCredito,D.IdTercero AS NitTercero,N.RazonSocial AS NomTercero
,CdCCosto,CCosto,CdSubCos,SubCosto,D.RubroConc,VrBase,TarifBase,CantComb,TipFac,Factura,IdCiaFac,ItemFac,FecVence,RefConcep
--datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,V.NitEmpresa AS VehNitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,V.IdGrupo AS CdGrupo,GrupoProp
 FROM Trn_TraLiqViajes AS LQ INNER JOIN Companias AS CN ON LQ.IdCia=CN.IdCia  INNER JOIN adm_Usuarios AS U ON LQ.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON LQ.IdConcepto=C.IdConcepto
 INNER JOIN Localidades AS CO ON  LQ.IdLocOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON LQ.IdLocDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON LQ.IdRuta=R.IdRuta  INNER JOIN Terceros AS  CDT ON LQ.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS NP ON LQ.IdPropietario=NP.IdTercero   INNER JOIN Terceros AS T ON LQ.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON LQ.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS ETR ON LQ.NitEmpresa=ETR.IdTercero LEFT JOIN TiposCom AS TCM ON LQ.TipCom=TCM.IdCom 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero
 LEFT JOIN Trn_TraLiqDetalle AS D ON LQ.TipDoc=D.TipDoc AND LQ.Liquidacion=D.Liquidacion AND LQ.IdCia=D.IdCia
 LEFT JOIN ConcDiversos AS CG ON D.IdConcepto=CG.IdConcepto LEFT JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 LEFT JOIN Terceros AS N ON  D.IdTercero=N.IdTercero LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos 
 WHERE LQ.TipDoc=@pmTipDoc  AND LQ.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND LQ.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND LQ.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND LQ.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND LQ.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 AND LQ.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 ORDER BY LQ.IdCia,LQ.Liquidacion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenLiqDet] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT L.TipDoc AS IdTipDoc,TipoDoc,L.Liquidacion,L.IdCia AS CdCia,Compania,L.Fecha AS FechaLiq,L.IdConcepto AS CdConcepto,CT.Concepto,TipOds,L.NumOrden,IdCiaOds,O.Fecha AS FecOrden
,L.IdVehiculo AS PlacaVeh,L.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,L.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,L.TipoAfiVehic
,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,A.Agencia,O.IdDestino,LD.Localidad AS NomDestino,O.Modalidad,O.TipoOrden,O.IdEstacion AS CdCampo,EstServicio AS NomCampo
,O.FecDespacho,O.FecRecibo,O.FecIngCertif,O.FecCertificado,TipoResiduo,O.Cantidad AS CantOrden,O.RemCliente,O.NumRemesa,O.CdCiaRem,L.VrTotal,L.VrCosto,L.VrAnticipo,VrGastos,VrIva,VrRetencion,VrReteIca
,VrReteCREE,VrReteIva,VrOtros,VrDescuento,VrNeto,L.Cantidad,TarifaPago,BaseImp,BaseRet,BaseIca,TarifaImp,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,CodTarRtc
,L.Referencia,L.CxPagar,CdPlazo,Plazo,DiasPago,FechaVence,L.UndMed,Unidad,L.TipCom,L.Comprobante,L.IdCiaCom,L.Anulado,L.NumDev,L.FecDev,L.Observacion AS Observ
,L.IdEstado AS CdEstado,Estado,L.OrigenAdd,L.TimeSys AS FechaCrea,L.FecUpdate AS FechaModif,L.IdCiaCrea AS CdCiaCrea,L.IdUsuario AS CdUsuario,Usuario
,DC.Item,DC.IdConcepto AS DetCodConc,DescConcepto,DC.IdCuenta AS CdCuenta,NomCuenta,VrDebito,VrCredito
,DC.IdTercero AS NitTercero,TDT.RazonSocial AS NomTercero,DC.CodAgncia,DC.CdCCosto,CCosto,DC.CdSubCos,SubCosto,pVehiculo
,DC.RubroConc,DC.VrBase,DC.TarifBase,DC.TipFac,DC.Factura,DC.IdCiaFac,ItemFac,DC.FecVence,RefConcep,CD.Concepto AS DetConcep
--vehiuculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,LT.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraOrdenLiq AS L INNER JOIN Companias AS CN ON L.IdCia=CN.IdCia INNER JOIN Conceptos AS CT ON L.IdConcepto=CT.IdConcepto
 INNER JOIN adm_Usuarios AS U ON L.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS ED ON L.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS NP ON L.IdPoseedor=NP.IdTercero INNER JOIN Terceros AS CDT ON L.IdConductor=CDT.IdTercero
 INNER JOIN Trn_TraOrdenServ AS O ON L.TipOds=O.TipDoc AND L.NumOrden=O.NumOrden AND L.IdCiaOds=O.IdCia
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion 
 INNER JOIN Sys_TiposDoc AS TD ON L.TipDoc=TD.IdDoc INNER JOIN Localidades AS LT ON T.IdLocal=LT.IdLocal
 INNER JOIN Vehiculos AS V ON L.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN Trn_TraLiqDetalle AS DC ON L.TipDoc=DC.TipDoc AND L.Liquidacion=DC.Liquidacion AND L.IdCia=DC.IdCia
 INNER JOIN ConcDiversos AS CD ON DC.IdConcepto=CD.IdConcepto 
 LEFT JOIN Puc AS PC ON DC.IdCuenta=PC.IdCuenta LEFT JOIN Terceros AS TDT ON DC.IdTercero=TDT.IdTercero
 LEFT JOIN CentroCosto AS CC ON DC.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON DC.CdSubCos=SC.IdSubCos 
 LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal LEFT JOIN Sys_Um AS UM ON L.UndMed=UM.UndMed
 LEFT JOIN Plazos AS PZ ON L.CdPlazo=PZ.IdPlazo
 WHERE L.TipDoc=@pmTipDoc AND L.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND L.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevAntFmt] @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Anticipo,IdCiaDoc,FecDoc
,D.IdVehiculo AS PlacaVeh,D.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,D.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,VrDevolucion
,CdCta,NumeroCta,CTA.IdBanco,Banco,CdCuenta,NomCuenta,ModdDev,TipCom,TipoCom,Comprobante,IdCiaCom,D.Observacion AS Observ
,TimeSys,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario,Leyenda
--Datos poseedor y vehiculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TPR.TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraDevAnt AS D  INNER JOIN Companias AS CN ON D.IdCia=CN.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 Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN Terceros AS  CDT ON D.IdConductor=CDT.IdTercero  INNER JOIN Terceros AS T ON D.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN CtasCorrientes AS CTA ON D.CdCta=CTA.IdCta LEFT JOIN Bancos AS B ON CTA.IdBanco=B.IdBanco
 LEFT JOIN Puc AS P ON D.CdCuenta=P.IdCuenta LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero
 WHERE D.TipDev=@pmTipDev AND D.Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
  ORDER BY D.Devolucion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenAntFmt] @pmTipDoc VARCHAR(3),@pmAnticipoIni INT,@pmAnticipoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT A.TipDoc AS TipoAnt,TipoDoc,Anticipo,A.IdCia AS CdCia,Compania,A.Fecha AS FecAnt,A.IdConcepto AS CdConcepto,Concepto,TipOds,A.NumOrden,IdCiaOds,O.Fecha AS FecOrden
,O.IdVehiculo AS PlacaVeh,nRemolque,O.TipoAfiVehic,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,O.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,AG.Agencia,A.VrAnticipo,A.VrAbonado,TipoPago,A.NumCheque,FecCheque
,A.IdCta AS CdCta,NumeroCta,CTA.IdBanco,Banco,A.IdCuenta AS CdCuenta,PU.NomCuenta,CedBenef,Beneficiario,FechaVence,A.TipoAncpo,A.NumPresAnt,A.Cantidad AS CantGalones,A.TipCom,A.Comprobante,A.IdCiaCom,A.Anulado AS Anuldo,A.NumDev
,A.FecDev,A.Observacion AS Observ,A.IdEstado AS CdEstado,Estado,A.OrigenAdd,A.TimeSys AS FechaCrea,A.FecUpdate AS FechaModif,A.IdCiaCrea AS CdCiaCrea,A.IdUsuario AS CdUsuario,Usuario,Leyenda
--datos de la orden
,O.Modalidad,O.TipoOrden,O.VrTotal,O.VrCosto,O.Cantidad,O.IdEstacion AS CdCampo,EstServicio AS NomCampo,NomEstacion,TipoResiduo,RemCliente,NomRecibido,IdDestino,LD.Localidad AS NomDestino
,FecDespacho,FecRecibo,FecIngCertif,FecCertificado,NumRemesa,CdCiaRem,EstFactura,TipFact,NumFactura,CdCiaFact,EstOrden,TipLiq,NumLiquida,CdCiaLiq,O.Observacion AS OdsObserv
--Cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TPR.TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
--detalles
,Item,DC.IdCuenta AS Cd_Cuenta,PC.NomCuenta AS Nom_Cuenta,Detalle,VrDebito,VrCredito,DC.IdCCosto AS DetCodCentCost,DCC.CCosto AS DetCentCost
,DC.IdSubCos AS DetCodSubCentro,DS.SubCosto AS DetSubCentro,VrBase,TarifaBase,Referncia
 FROM Trn_TraOrdenAnt AS A INNER JOIN Trn_TraOrdenServ AS O ON A.TipOds=O.TipDoc AND A.NumOrden=O.NumOrden AND A.IdCiaOds=O.IdCia
 INNER JOIN Companias AS CN ON A.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON A.IdConcepto=C.IdConcepto INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero
 INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Sys_TiposDoc AS TD ON A.TipDoc=TD.IdDoc
 INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 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 O.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN Agencias AS AG ON O.IdAgencia=AG.IdAgencia  LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 LEFT JOIN CtasCorrientes AS CTA ON A.IdCta=CTA.IdCta LEFT JOIN Bancos AS B ON CTA.IdBanco=B.IdBanco
 LEFT JOIN Puc AS PU ON A.IdCuenta=PU.IdCuenta
 LEFT JOIN Trn_ComDetalle AS DC ON A.TipCom=DC.TipCom AND A.Comprobante=DC.Comprobante AND A.IdCiaCom=DC.IdCia
 LEFT JOIN Puc AS PC ON DC.IdCuenta=PC.IdCuenta LEFT JOIN CentroCosto AS DCC ON DC.IdCCosto=DCC.IdCCosto
 LEFT JOIN SubCentros AS DS ON DC.IdSubCos=DS.IdSubCos
 WHERE A.TipDoc=@pmTipDoc AND A.Anticipo BETWEEN @pmAnticipoIni AND @pmAnticipoFin AND A.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevLiqRel] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null
AS
 SELECT TipDev,Devolucion,DV.IdCia AS CdCia,Compania,Fecha,DV.IdConcepto AS CdConcepto,Concepto,DV.TipDoc AS TipoLiq,DV.Liquidacion AS NumLiquida,IdCiaDoc,FecDoc
,DV.IdVehiculo AS PlacaVeh,DV.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,DV.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,VrTotal,ModdDev
,TipCom,TipoCom,Comprobante,IdCiaCom,DV.Observacion AS Observ,TimeSys,IdCiaCrea,DV.IdUsuario AS CdUsuario,Usuario
,Item,TipCau,Causacion,IdCiaCau,ItemCau,FechaCau,D.IdConcepto AS CdConcCausac,D.Descripcion AS DetDescripcion,VrAbono,NitPoseedor,N.RazonSocial AS NomPoseedor,pVehiculo,EsVence
,TipOdp,OrdenPago,IdCiaOdp,TipMuc,Manifiesto,IdCiaMuc,VrSaldoOrden,CdCuenta,CdCuePagar,NumFactura,CdCCosto,CCosto,CdSubCos,SubCosto
--datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraDevLiq AS DV   INNER JOIN Companias AS CN ON DV.IdCia=CN.IdCia 
 INNER JOIN adm_Usuarios AS U ON DV.IdUsuario=U.IdUsuario   INNER JOIN Conceptos AS C ON DV.IdConcepto=C.IdConcepto
 INNER JOIN Terceros AS  CDT ON DV.IdConductor=CDT.IdTercero  INNER JOIN Terceros AS T ON DV.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON DV.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 INNER JOIN Trn_TraLiquidaDed AS D ON DV.TipDoc=D.TipDoc  AND DV.Liquidacion=D.Liquidacion AND DV.IdCiaDoc=D.IdCia
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos 
 LEFT JOIN Terceros AS N ON  D.NitPoseedor=N.IdTercero  LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero 
 LEFT JOIN TiposCom AS TCM ON DV.TipCom=TCM.IdCom 
 WHERE DV.TipDev=@pmTipDev  AND DV.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND DV.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND DV.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND DV.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND DV.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 AND DV.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 ORDER BY DV.IdCia,DV.Devolucion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevCosFmt] @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT D.TipDev AS IdTipDev,TipoDoc,D.Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc,D.Cumplido AS NumCump,D.IdCiaDoc,D.FecDoc
,TipOds,C.NumOrden AS Num_Orden,IdCiaOds,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia,O.IdVehiculo AS PlacaVeh,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor
,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,O.Modalidad,O.TipoOrden,O.VrAnticipo AS Vr_Anticipo,O.IdEstacion AS CdCampo,EstServicio AS NomCampo
,C.TipoAfiVehic,C.FecDespacho,C.FecRecibo,C.FecIngCertif,C.FecCertificado,DescServicio,C.Cantidad AS CumCantidad,C.VrTotal AS CumValor,C.VrCosto AS CumValTercero,C.RemCliente
,Funcionario,O.NumRemesa,O.CdCiaRem,O.kmtInicial,O.kmtFinal,IdDestino,LD.Localidad,D.ModdDev,D.TipCom,D.Comprobante AS NumComp,D.IdCiaCom,D.Observacion AS Observ
,D.OrigenAdd,D.TimeSys AS FechaCrea,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario,Leyenda
--detalles
,Item,DC.IdMercancia,DC.Descripcion,DC.Cantidad,DC.UndMed AS Und_Med,Unidad,DC.VrUnitario,DC.TarifaPago,DC.DocCliente
--Datos del Cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
FROM Trn_TraDevCos AS D INNER JOIN Trn_TraOrdenCum AS C ON D.TipDoc=C.TipDoc AND D.Cumplido=C.Cumplido AND D.IdCiaDoc=C.IdCia
INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
INNER JOIN Conceptos AS CT ON D.IdConcepto=CT.IdConcepto INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
INNER JOIN Trn_TraOrdenServ AS O ON C.TipOds=O.TipDoc AND C.NumOrden=O.NumOrden  AND C.IdCiaOds=O.IdCia
INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero
INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion  INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero
INNER JOIN Trn_TraOrdenCumDet AS DC ON C.TipDoc=DC.TipDoc AND C.Cumplido=DC.Cumplido AND C.IdCia=DC.IdCia
INNER JOIN Sys_Um AS UM ON DC.UndMed=UM.UndMed INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca 
INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
WHERE D.TipDev=@pmTipDev AND D.Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevCauFmt] @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,CO.Concepto AS DescConcepto,DV.TipDoc AS TipoCau,DV.Causacion AS NumCausac,IdCiaDoc,FecDoc
,IdProv,T.RazonSocial AS Proveedor,DV.Factura AS NumFactura,VrTotal,VrOtros,VrNeto,ModdDev,TipCom,TipoCom,Comprobante,IdCiaCom
,DV.Observacion AS Observ,TimeSys,IdCiaCrea,DV.IdUsuario AS CdUsuario,Usuario,Leyenda
,D.IdConcepto AS CdConcCausac,CCA.Concepto AS ConcCausac,D.Descripcion AS DetDescripcion,D.TipoConc AS TipConcept,VrDebito,VrCredito,CdCuenta,NomCuenta,pVehiculo,NitTercero,NP.RazonSocial AS Razon_Social
,CdCCosto,CCosto,CdSubCos,SubCosto,TipoAfiVehic,VrBase,TarifBase,Referencia,TipDocRef,DocRef,TipFac,D.Factura AS DetFactura,IdCiaFac,ItemFac,FecVence,NumLiquida,CiaLiquida,VrAbonado
--datos del proveedor
,T.TipoId AS TercTipId,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 CdDpto,DP.Departamento AS DescDpto
,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraDevCau AS DV  INNER JOIN Companias AS CN ON DV.IdCia=CN.IdCia INNER JOIN Sys_TiposDoc AS TD ON DV.TipDev=TD.IdDoc
 INNER JOIN Conceptos AS CO ON DV.IdConcepto=CO.IdConcepto INNER JOIN adm_Usuarios AS U ON DV.IdUsuario=U.IdUsuario 
 INNER JOIN Terceros AS T ON DV.IdProv=T.IdTercero 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 INNER JOIN Trn_TraCauDetalle AS D ON DV.TipDoc=D.TipDoc AND DV.Causacion=D.Causacion AND DV.IdCiaDoc=D.IdCia
 INNER JOIN TraConcCausac AS CCA ON D.IdConcepto=CCA.IdConcepto 
 LEFT JOIN  Vehiculos AS V ON D.pVehiculo=V.IdVehiculo   LEFT JOIN  TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 LEFT JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 LEFT JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  LEFT JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 LEFT JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd 
 LEFT JOIN Terceros AS NP ON D.NitTercero=NP.IdTercero  LEFT JOIN Puc AS P ON D.CdCuenta=P.IdCuenta 
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos 
 LEFT JOIN TiposCom AS TCM ON DV.TipCom=TCM.IdCom 
 WHERE DV.TipDev=@pmTipDev AND DV.Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND DV.IdCia=@pmIdCia
 ORDER BY  DV.Devolucion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenAntAbo] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=NULL
 AS 
 SELECT A.TipDoc AS TipoAnt,TD.TipoDoc,A.Anticipo,A.IdCia AS CdCia,CN.Compania,A.Fecha AS FecAnt,A.IdConcepto AS CdConcepto,Concepto,TipOds,A.NumOrden,IdCiaOds,O.Fecha AS FecOrden
,O.IdVehiculo AS PlacaVeh,nRemolque,O.TipoAfiVehic,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,O.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,AG.Agencia,A.VrAnticipo,A.VrAbonado,TipoPago,A.NumCheque,FecCheque
,A.IdCta AS CdCta,NumeroCta,CTA.IdBanco,Banco,A.IdCuenta AS CdCuenta,PU.NomCuenta,CedBenef,Beneficiario,FechaVence,A.TipCom,A.Comprobante,A.IdCiaCom,A.Anulado AS Anuldo,A.NumDev
,A.FecDev,A.Observacion AS Observ,A.IdEstado AS CdEstado,Estado,A.OrigenAdd,A.TimeSys AS FechaCrea,A.FecUpdate AS FechaModif,A.IdCiaCrea AS CdCiaCrea,A.IdUsuario AS CdUsuario,Usuario
--datos de la orden
,O.Modalidad,O.TipoOrden,O.VrTotal,O.VrCosto,O.Cantidad,O.IdEstacion AS CdCampo,EstServicio AS NomCampo,NomEstacion,TipoResiduo,RemCliente,NomRecibido,IdDestino,LD.Localidad AS NomDestino
,FecDespacho,FecRecibo,FecIngCertif,FecCertificado,NumRemesa,CdCiaRem,EstFactura,TipFact,NumFactura,CdCiaFact,EstOrden,TipLiq,NumLiquida,CdCiaLiq,O.Observacion AS OdsObserv
,AP.TipDoc AS CdTipoAbo,TDA.TipoDoc AS TipDocAbo,Documento,AP.IdCia AS CdCiaAbo,CA.Compania AS CompaniaAbo,Item,AP.Fecha AS FechaAbo,VrAbono,TipoAplica,AP.Observacion AS ObservAbono,CodCuenta
--Cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TPR.TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
 FROM Trn_TraOrdenAnt AS A INNER JOIN Trn_TraOrdenServ AS O ON A.TipOds=O.TipDoc AND A.NumOrden=O.NumOrden AND A.IdCiaOds=O.IdCia
 INNER JOIN Companias AS CN ON A.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON A.IdConcepto=C.IdConcepto INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero
 INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Sys_TiposDoc AS TD ON A.TipDoc=TD.IdDoc
 INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 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 O.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN Agencias AS AG ON O.IdAgencia=AG.IdAgencia  LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 LEFT JOIN CtasCorrientes AS CTA ON A.IdCta=CTA.IdCta LEFT JOIN Bancos AS B ON CTA.IdBanco=B.IdBanco
 LEFT JOIN Puc AS PU ON A.IdCuenta=PU.IdCuenta
 LEFT JOIN Trn_TraOrdenAbo AS AP ON A.TipDoc=AP.TipAnt AND A.Anticipo=AP.Anticipo AND A.IdCia=AP.IdCiaAnt
 LEFT JOIN Companias AS CA ON AP.IdCia=CA.IdCia LEFT JOIN Sys_TiposDoc AS TDA ON AP.TipDoc=TDA.IdDoc
 WHERE A.TipDoc=@pmTipDoc AND A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenLiqFmt] @pmTipDoc VARCHAR(3),@pmLiquidacionIni INT,@pmLiquidacionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT L.TipDoc AS IdTipDoc,TipoDoc,L.Liquidacion,L.IdCia AS CdCia,Compania,L.Fecha AS FechaLiq,L.IdConcepto AS CdConcepto,Concepto,TipOds,L.NumOrden,IdCiaOds,O.Fecha AS FecOrden
,L.IdVehiculo AS PlacaVeh,L.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,L.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,L.TipoAfiVehic
,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia,O.IdDestino,LD.Localidad AS NomDestino,O.TipoOrden,O.IdEstacion AS CdCampo,EstServicio AS NomCampo
,O.FecDespacho,O.FecRecibo,O.FecIngCertif,O.FecCertificado,TipoResiduo,O.RemCliente,O.NumRemesa,O.CdCiaRem,L.VrTotal,L.VrCosto,L.VrAnticipo,VrGastos,VrIva,VrRetencion,VrReteIca
,VrReteCREE,VrReteIva,VrOtros,VrDescuento,VrNeto,L.Cantidad,TarifaPago,BaseImp,BaseRet,BaseIca,TarifaImp,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,CodTarRtc
,L.Referencia,L.CxPagar,CdPlazo,Plazo,DiasPago,FechaVence,L.UndMed,Unidad,L.TipCom,L.Comprobante,L.IdCiaCom,L.Anulado,L.NumDev,L.FecDev,L.Observacion AS Observ
,L.IdEstado AS CdEstado,Estado,TipEgr,Egreso,IdCiaEgr,NumCheque,VrConcCargos,VrConcDed,L.OrigenAdd,L.TimeSys AS FechaCrea,L.FecUpdate AS FechaModif,L.IdCiaCrea AS CdCiaCrea,L.IdUsuario AS CdUsuario,Usuario,Leyenda
--vehiuculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,LT.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,L.CdCCosto,CCosto,L.CdSubCos,SubCosto
 FROM Trn_TraOrdenLiq AS L INNER JOIN Companias AS CN ON L.IdCia=CN.IdCia INNER JOIN Conceptos AS CT ON L.IdConcepto=CT.IdConcepto
 INNER JOIN adm_Usuarios AS U ON L.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS ED ON L.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS NP ON L.IdPoseedor=NP.IdTercero INNER JOIN Terceros AS CDT ON L.IdConductor=CDT.IdTercero
 INNER JOIN Trn_TraOrdenServ AS O ON L.TipOds=O.TipDoc AND L.NumOrden=O.NumOrden AND L.IdCiaOds=O.IdCia
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion 
 INNER JOIN Sys_TiposDoc AS TD ON L.TipDoc=TD.IdDoc INNER JOIN Localidades AS LT ON T.IdLocal=LT.IdLocal
 INNER JOIN Vehiculos AS V ON L.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal LEFT JOIN Sys_Um AS UM ON L.UndMed=UM.UndMed
 LEFT JOIN Plazos AS PZ ON L.CdPlazo=PZ.IdPlazo LEFT JOIN CentroCosto AS CC ON L.CdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON L.CdSubCos=SC.IdSubCos
 WHERE L.TipDoc=@pmTipDoc AND L.Liquidacion BETWEEN @pmLiquidacionIni AND @pmLiquidacionFin AND L.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDespachosLtap] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdRuta VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
--despachos-detalles de programacion
 SELECT D.TipDoc AS CdTipDoc,D.Manifiesto AS NumManif,D.IdCia AS CdCia,Compania,D.Fecha AS FecMuc,FecCargue,FecDespacho,IdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,IdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,D.IdRuta AS CdRuta,R.Ruta AS DescRuta,D.IdVehiculo AS PlacaVeh,D.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,T.TipoId AS PosTipoId,D.IdPoseedor AS NitPoseedor,T.Dv AS PosDv,T.RazonSocial AS NomPoseedor,NitRemite,Remitente,NitDestntario,Destinatario,DescMcia,PesoTotal,NumOrden,IdCiaOrd,Remesa,IdCiaRem
,D.Estado AS EdoDespacho,D.Anulado AS DesAnulado,FecDev,D.Observacion AS Observ,TimeSys,FecUpdate,D.IdUsuario AS CdUsuario,Usuario
--datos del vehíclo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
,PgmItem,PgmIdPunto,HoraProg,TipoProg,ObservProg,RepItem,RepIdPunto,FecReporte,TipoRep,EstadoRep,CdNovedad,Novedad,ObservRep,TipoPunto,CdPuesto,PtoControl,TagPuesto,PC.Ubicacion AS UbicacPto,RepConductor
 FROM Trn_TraDespachos AS D INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS CO ON  D.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON D.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON D.IdRuta=R.IdRuta  INNER JOIN Terceros AS  CDT ON D.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS T ON D.IdPoseedor=T.IdTercero 
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd  LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero
 LEFT JOIN TercCndtores AS CT ON D.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN (SELECT  ISNULL(PG.TipDoc,RP.TipDoc) AS PgmTipDoc,ISNULL(PG.Manifiesto,RP.Manifiesto) AS PgmManif,ISNULL(PG.IdCia,RP.IdCia) AS PgmCdCia
 ,PG.Item AS PgmItem,PG.IdPunto AS PgmIdPunto,HoraProg,TipoProg,ObservProg,RP.Item AS RepItem,RP.IdPunto AS RepIdPunto,FecReporte,TipoRep,EstadoRep
,CdNovedad,ObservRep,TipoPunto,CdPuesto,RP.NomConductor AS RepConductor
 FROM Trn_TraDespProgram AS PG  FULL JOIN Trn_TraReportes AS RP  ON PG.TipDoc=RP.TipDoc AND  PG.Manifiesto=RP.Manifiesto AND PG.IdCia=RP.IdCia  
 AND PG.CdRuta=RP.CdRuta AND PG.IdPunto=RP.IdPunto) AS PGM ON D.TipDoc=PGM.PgmTipDoc AND  D.Manifiesto=PGM.PgmManif AND D.IdCia=PGM.PgmCdCia
 LEFT JOIN NovedadRep AS NV ON PGM.CdNovedad=NV.IdNov  LEFT JOIN Puestos AS PC ON PGM.CdPuesto=PC.IdPto
 WHERE D.TipDoc=@pmTipDoc AND  D.FecDespacho BETWEEN @pmFechaIni AND @pmFechaFin AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdRuta LIKE ISNULL(@pmIdRuta,'%') AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 AND D.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDespachosLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdRuta VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
 SELECT D.TipDoc AS CdTipDoc,D.Manifiesto AS NumManif,D.IdCia AS CdCia,Compania,D.Fecha AS FecMuc,FecCargue,D.FecDespacho,D.IdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,D.IdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,D.IdRuta AS CdRuta,R.Ruta AS DescRuta,D.IdVehiculo AS PlacaVeh,M.nRemolque AS MucRemolque,D.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,T.TipoId AS PosTipoId,D.IdPoseedor AS NitPoseedor,T.Dv AS PosDv,T.RazonSocial AS NomPoseedor,NitRemite,Remitente,NitDestntario,Destinatario,DescMcia,D.PesoTotal,NumOrden,IdCiaOrd,D.Remesa,D.IdCiaRem
,D.Estado AS EdoDespacho,D.Anulado AS DesAnulado,D.FecDev,D.Observacion AS Observ,D.TimeSys,D.FecUpdate,D.IdUsuario AS CdUsuario,Usuario
--datos del vehíclo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,TPR.VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
--datos del MAnifiesto
,M.VrFletes AS ManVrFletes,M.VrRetencion AS ManVrRetencion,M.VrReteIca As ManVrRteIca,ISNULL(AC.VlrAnticipo,0)-ISNULL(DV.VrDevAnt,0) AS ManVrAnticipo,M.VrNeto AS ManVrNeto,M.VrPagos AS ManVrPagos,M.VrCargos AS ManVrCargos
,MO.NumCheque AS ManNumChe,AC.NumAnt As ManNumAnt
,CP.TipCom AS TipEgr,CP.Comprobante AS NumEgr,CP.IdCia AS IdCiaEgr,CP.Fecha AS FecCheEgr
,M.TipOdp AS TipManOdp,M.OrdPago As ManNumOdp,M.IdCiaOdp AS ManCiaOdp,M.FechaOdp AS FecMucOdp,ISNULL(EGO.ABONO,0) As ManVrAbono,MA.NomDestino AS MaNomDest,MA.NomRemite AS ManNomRemi
 FROM Trn_TraDespachos AS D INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Trn_TraManifiesto M ON D.TipDoc=M.TipDoc AND D.Manifiesto=M.Manifiesto AND D.IdCia=M.IdCia
 INNER JOIN Trn_TraManifAnexo MA ON D.TipDoc=MA.TipDoc AND D.Manifiesto=MA.Manifiesto AND D.IdCia=MA.IdCia 
 INNER JOIN Localidades AS CO ON  D.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON D.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON D.IdRuta=R.IdRuta  INNER JOIN Terceros AS  CDT ON D.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS T ON D.IdPoseedor=T.IdTercero 
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero
 LEFT JOIN TercCndtores AS CT ON D.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN Trn_TraOrdenManif AS MO ON M.TipOdp=MO.TipDoc AND M.OrdPago=MO.OrdPago AND M.IdCiaOdp=MO.IdCia
 LEFT JOIN Trn_Comprobantes AS CP ON MO.TipEgr=CP.TipCom AND MO.Egreso=CP.Comprobante AND MO.IdCiaEGR=CP.IdCia
 LEFT JOIN (SELECT TipMuc,Manifiesto,IdCiaMuc,MAX(Anticipo) AS NumAnt,SUM(VrAnticipo) AS VlrAnticipo
 FROM Trn_TraAnticipos GROUP BY TipMuc,Manifiesto,IdCiaMuc) AS AC ON M.TipDoc=AC.TipMuc AND M.Manifiesto=AC.Manifiesto AND M.IdCia=AC.IdCiaMuc 
 LEFT JOIN (SELECT TipMuc,Manifiesto,IdCiaMuc,SUM(AB.VrAbono)AS VrDevAnt FROM Trn_TraAnticipos AS A LEFT JOIN Trn_TraAntAbonos AS AB 
 ON A.TipDoc=AB.TipAnt AND A.Anticipo=AB.Anticipo AND A.IdCia=AB.IdCiaAnt WHERE AB.TipDoc ='DVA' GROUP BY TipMuc,Manifiesto,IdCiaMuc)
 AS DV ON M.TipDoc=DV.TipMuc AND M.Manifiesto=DV.Manifiesto AND M.IdCia=DV.IdCiaMuc 
 LEFT JOIN (SELECT TipOdp,OrdPago,IdCiaOdp,SUM(VrAbonado) AS ABONO FROM Trn_TraEgrOrden GROUP BY TipOdp,OrdPago,IdCiaOdp) AS EGO ON M.TipOdp=EGO.TipOdp AND M.OrdPago=EGO.OrdPago AND M.IdCiaOdp=EGO.IdCiaOdp 
 WHERE D.TipDoc=@pmTipDoc AND  D.FecDespacho BETWEEN @pmFechaIni AND @pmFechaFin AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdRuta LIKE ISNULL(@pmIdRuta,'%') AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 AND D.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDespachosFmt] @pmTipDoc VARCHAR(3),@pmManifiestoIni INT,@pmManifiestoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT D.TipDoc AS CdTipMuc,D.Manifiesto AS NumManif,D.IdCia AS CdCia,Compania,D.Fecha AS FecMuc,FecCargue,FecDespacho,IdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,IdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,D.IdRuta AS CdRuta,R.Ruta AS DescRuta,D.IdVehiculo AS PlacaVeh,D.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,T.TipoId AS PosTipoId,D.IdPoseedor AS NitPoseedor,T.Dv AS PosDv,T.RazonSocial AS NomPoseedor,NitRemite,Remitente,NitDestntario,Destinatario,DescMcia,PesoTotal,NumOrden,IdCiaOrd,Remesa,IdCiaRem
,D.Estado AS EdoDespacho,D.Anulado AS DesAnulado,FecDev,D.Observacion AS Observ,TimeSys,FecUpdate,D.IdUsuario AS CdUsuario,Usuario
--detalles reportes-program.
,PgmItem,PgmIdPunto,HoraProg,TipoProg,ObservProg,RepItem,RepIdPunto,FecReporte,TipoRep,EstadoRep,CdNovedad,Novedad,ObservRep,TipoPunto,CdPuesto,PtoControl,TagPuesto,PC.Ubicacion AS UbicacPto,RepConductor
--datos del vehíclo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraDespachos AS D INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS CO ON  D.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON D.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON D.IdRuta=R.IdRuta  INNER JOIN Terceros AS  CDT ON D.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS T ON D.IdPoseedor=T.IdTercero 
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd  LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero
 LEFT JOIN (SELECT  ISNULL(PG.TipDoc,RP.TipDoc) AS PgmTipDoc,ISNULL(PG.Manifiesto,RP.Manifiesto) AS PgmManif,ISNULL(PG.IdCia,RP.IdCia) AS PgmCdCia
 ,PG.Item AS PgmItem,PG.IdPunto AS PgmIdPunto,HoraProg,TipoProg,ObservProg,RP.Item AS RepItem,RP.IdPunto AS RepIdPunto,FecReporte,TipoRep,EstadoRep
,CdNovedad,ObservRep,TipoPunto,RP.CdPuesto AS RepCodPuesto,RP.NomConductor AS RepConductor,RT.Descripcion AS PuntDescrip,RT.ClasePto AS PuntClase,RT.CdPto AS CdPuesto
 FROM Trn_TraDespProgram AS PG  FULL JOIN Trn_TraReportes AS RP  ON PG.TipDoc=RP.TipDoc AND  PG.Manifiesto=RP.Manifiesto AND PG.IdCia=RP.IdCia  
 AND PG.CdRuta=RP.CdRuta AND PG.IdPunto=RP.IdPunto LEFT JOIN RutasPlan AS RT ON PG.CdRuta=RT.IdRuta AND PG.IdPunto=RT.IdPunto) AS PGM 
 ON D.TipDoc=PGM.PgmTipDoc AND  D.Manifiesto=PGM.PgmManif AND D.IdCia=PGM.PgmCdCia
 LEFT JOIN NovedadRep AS NV ON PGM.CdNovedad=NV.IdNov  LEFT JOIN Puestos AS PC ON PGM.CdPuesto=PC.IdPto
 WHERE D.TipDoc=@pmTipDoc AND D.Manifiesto BETWEEN @pmManifiestoIni AND  @pmManifiestoFin  AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraManifiestoFmt] @pmTipDoc VARCHAR(3),@pmManifiestoIni INT,@pmManifiestoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT M.TipDoc AS CdTipDoc,TipoDoc,M.Manifiesto AS NumManif,M.IdCia AS CdCia,CN.Compania AS NomCompania,M.Fecha AS FechaMuc,M.FecDespacho,M.FecEntrega,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,RT.Ruta AS DescRuta,IdRutaFle,RF.Ruta AS RutaFletes
,M.IdVehiculo AS PlacaVeh,M.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,M.nRemolque,M.TipoAfiVehic,M.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,M.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,M.VrFletes,VrRetencion,VrReteIca,VrDescuento,VrAnticipo,VrAntAdic,VrNeto,M.VrPagos,M.VrCargos,M.VrDctos,TarifaFlete,M.Cantidad AS CantTotal,M.PesoTotal,BaseRet,TarifaRet,TarifaIca
,M.IdLocFletes,CF.Localidad AS CiuLugarFletes,FechaPago,PagoCargue,PagoDescargue,NumMintrans,EdoMintrans,M.Cumplido AS NumCumplido,M.IdCiaCump AS CdCiaCump,M.FechaCump AS FecCumplido,M.EstCumplido
,M.TipOdp AS TipoOdp,M.OrdPago AS NumOrdPago,M.IdCiaOdp AS CdCiaOdp,FechaOdp,EstOrden,VrFleteNeto,TipInfVia,NumViaje,CiaInfViaje,CdCatPeaje,M.OrigenAdd,M.Anulado,M.FecDev,M.Observacion AS Observ,M.IdEstado AS CdEstado,Estado,M.TipCom,M.Comprobante,M.IdCiaCom
,M.TimeSys,M.FecUpdate,M.IdCiaCrea,M.IdUsuario AS CdUsuario,Usuario,Leyenda,kmsTotal,VrFleteTon,MA.TipoRuta AS MucTipoRuta,CdLocTrao,CdLocTrad,KmsTraOri,KmsTraDes,MA.IdMneda,MA.VrTasa,MA.NomRemite AS NomRmtente,MA.NomDestino AS NomDestnatario
,MA.LugarFletes AS Lugar_Fletes,NumAnticipo,NumCheque,MA.TipoMintrans AS TipoMucMintrans,MucMintrans,ContIntegral,VolumenCarga,MA.NumPoliza AS NumPolizaMuc,DescFirma,CodEmpresa,TomadorPoliza,PolizaSeguro,NitCiaPoliza,NomCiaPoliza,FecVencePol,MA.MvoAnulacion,MvoSuspension
,PuntosRuta,MA.CantViajes,Transbordo,NumMucAnu,CiaMucAnu,MA.AceptaFirma
--detalles
,MR.Item AS ItemMuc,TipRem,MR.Remesa AS NumRemesa,MR.IdCiaRem AS CdCiaRem,ItemRem,D.IdMercancia AS CdMercancia,DescripMcias,CodigoMcia,D.Cantidad AS Cant,PesoNeto,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol
,Cases,Cajas,Palets,NitRemite,Remitente,DirOrigen,D.IdOrigen AS RemCodOrigen,LO.Localidad AS RemCiudadOrigen,LO.IdDep AS RemCdepOrigen,DOR.Departamento AS RemDptoOrigen,NitDestntario,Destinatario,DirDestino
,D.IdDestino AS RemCodDestino,LD.Localidad AS RemCiudadDestino,LD.IdDep AS RemCdepDestino,DDN.Departamento AS RemDptoDestino,TarifClie,MR.TarifTabla AS TarifaTabla,MR.TarifPago AS TarifaPago,D.VrDeclarado AS DetVrDeclarado,D.VrSeguro AS DetVrSeguro
,TarifSeguro,UndTarifa,UndTarifPago,Remision,DocCliente,Referencia1,Referencia2,Referencia3,Contenedor1,Contenedor2,D.CdAgencia AS DetCdAgencia,DA.Agencia AS DetAgencia
,D.Cumplido AS RemCumplido,D.IdCiaCump AS RemCiaCump,D.FechaCump AS RemFechaCum,DetalleCump,CantidadCump,PesoCump,VolCump,CasesCump,CajasCump,PaletsCump,TarifCump,PagoCump,UndTarCump,UndTarPagoCump
,TipFac,Factura,IdCiaFac,FechaFac,TarifClieFac,D.TipOdp AS RemTipoOdp,D.NumeroOdp AS RemNumOdp,D.IdCiaOdp AS RemCiaOdp,TarifOdp,PesoCont,MR.RemMintrans AS RemMucMintrans,EdoRemMin
,D.IdUnd AS CdUnd,UM.Unidad AS UndMedida,D.IdEmp AS CdEmp,Empaque,D.IdNat AS CdNat,Natlzaprod,D.IdMnjo AS CdMnjo,ManejoMcia,D.IdTmcia AS CdTmcia,TipoMcia,CdRango,DescripRango,CodBodDtno,TipTraslado,CodigoUN
,HorasCargue,HorasDesc,FecInicioCargue,FecFinCargue,FecIniciodesc,FecFindesc,TiempoCargue,TiempoDesc,FecLlegCargue,FecSalidaCargue,FecLlegdesc,FecSalidadesc
,IdCliente,CL.RazonSocial AS RemNomCliente,R.IdClieFact AS RemIdPropMcia,PM.RazonSocial AS RemPropMcia
--Datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,V.Modelo,V.Config
,V.PesoVacio,V.PesoMax,V.NumMotor,V.SerieChasis,V.NumSerie,V.CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,RQ.PesoKg AS PesoVacioRq
,CT.CertJudicial,CT.FecJudicial,CT.VigJudicial,CT.Licencia,CT.CatLicencia,CT.IdLugar AS CdLugar,EL.LugarLic,CT.FecLicencia,CT.VigLicencia,CDT.Direccion AS CdtDireccion,CDT.Telefono AS CdtTelefono,CDT.TelMovil AS CdtMovil,CDT.e_mail AS CdtEmail
,LC.Localidad AS CdtCiudad,DC.Departamento AS CdtDpto
,CdCondRelev,CRV.RazonSocial AS SegConductor,CTR.CertJudicial  AS CertJudicialRelev,CTR.FecJudicial AS FecJudicialRelev,CTR.VigJudicial AS VigJudicialRelev,CTR.Licencia AS LicenciaRelev,CTR.CatLicencia AS CatLicenciaRelev
,CTR.IdLugar AS CdLugarRelev,ELR.LugarLic AS LugarLicRelev,CTR.FecLicencia AS FecLicenciaRelev,CTR.VigLicencia AS VigLicenciaRelev,CRV.Direccion AS CdtDireccionRelev,CRV.Telefono AS CdtTelefonoRelev,CRV.e_mail AS CdtEmailRelev
,CRV.TelMovil AS CdtMovilRelev,LCR.Localidad AS CdtCiudadRelev, DCR.Departamento AS CdtDptoRelev
 FROM Trn_TraManifiesto AS M INNER JOIN  Trn_TraManifAnexo AS MA ON M.TipDoc=MA.TipDoc AND  M.Manifiesto=MA.Manifiesto AND  M.IdCia=MA.IdCia
 INNER JOIN Trn_TraManifRem AS MR ON M.TipDoc=MR.TipDoc AND  M.Manifiesto=MR.Manifiesto AND  M.IdCia=MR.IdCia
 INNER JOIN Trn_TraRemMcias AS D ON MR.TipRem=D.TipDoc  AND MR.Remesa=D.NumOrden AND MR.IdCiaRem=D.IdCia AND MR.ItemRem=D.Item
 INNER JOIN Trn_TraRemesa AS R ON MR.TipRem=R.TipDoc AND MR.Remesa=R.NumOrden AND MR.IdCiaRem=R.IdCia 
 INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia INNER JOIN Sys_TiposDoc AS TD ON M.TipDoc=TD.IdDoc
 INNER JOIN EstadoDoc AS ED ON M.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS RT ON M.IdRuta=RT.IdRuta  INNER JOIN Terceros AS CDT ON M.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS NP ON M.IdPropietario=NP.IdTercero INNER JOIN Terceros AS T ON M.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Mercancias AS MCA ON D.IdMercancia=MCA.IdMercancia
 INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DOR ON LO.IdDep=DOR.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DDN ON LD.IdDep=DDN.IdDep
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN Empaques AS EMP ON D.IdEmp=EMP.IdEmp
 INNER JOIN TiposNat AS NZ ON D.IdNat=NZ.IdNat INNER JOIN TiposMnjo AS MJ ON D.IdMnjo=MJ.IdMnjo
 INNER JOIN TiposMcia AS TM ON D.IdTmcia=TM.IdTmcia 
 INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS COL ON V.IdColor=COL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 INNER JOIN Terceros AS CL ON R.IdCliente=CL.IdTercero LEFT JOIN Terceros AS PM ON R.IdClieFact=PM.IdTercero
 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 M.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN Agencias AS DA ON D.CdAgencia=DA.IdAgencia  LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed 
 LEFT JOIN RangosPeso AS RP ON D.CdRango=RP.IdRango    LEFT JOIN Rutas AS  RF ON M.IdRutaFle=RF.IdRuta
 LEFT JOIN Polizas AS PS ON MA.NumPoliza=PS.NumPoliza
 LEFT JOIN Trn_TraRemAnexo AS RA ON MR.TipRem=RA.TipDoc AND MR.Remesa=RA.NumOrden AND MR.IdCiaRem=RA.IdCia
 LEFT JOIN VehRemolq AS RQ ON M.nRemolque=RQ.IdRemque
 LEFT JOIN Terceros AS CRV ON M.CdCondRelev=CRV.IdTercero
 LEFT JOIN Localidades AS LCR ON CRV.IdLocal=LCR.IdLocal LEFT JOIN Departamentos AS DCR ON LCR.IdDep=DCR.IdDep
 LEFT JOIN TercCndtores AS CTR ON M.CdCondRelev=CTR.IdConductor LEFT JOIN ExpLicencias AS ELR ON CTR.IdLugar=ELR.IdLugar
 LEFT JOIN Localidades AS LC ON CDT.IdLocal=LC.IdLocal LEFT JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
 WHERE M.TipDoc=@pmTipDoc AND M.Manifiesto BETWEEN @pmManifiestoIni AND @pmManifiestoFin AND M.IdCia=@pmIdCia
 ORDER BY M.Manifiesto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraInfViajesFmt] @pmTipDoc VARCHAR(3),@pmNumViajeIni INT,@pmNumViajeFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT M.TipDoc AS CdTipDoc,TipoDoc,M.NumViaje AS Num_Viaje,M.IdCia AS CdCia,Compania,M.Fecha AS FecViaje,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,Ruta
,M.IdVehiculo AS PlacaVeh,M.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,M.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,M.nRemolque AS NumRemolque,M.TipoAfiVehic AS TipoAfiVeh,M.VrFletes AS VrTotFlete,M.PesoTotal AS Peso_Total,M.NumManif AS Num_Manif,M.IdCiaManif AS CdCiaManif,NumMintrans,MvoAnulacion,M.Anulado AS Anuldo,M.FecDev AS FecAnulado
,M.Observacion AS Observ,M.IdEstado AS CdEstado,Estado,M.TimeSys,M.FecUpdate,M.IdCiaCrea,M.IdUsuario AS CdUsuario,Usuario
--detalles
,MR.Item AS ItemDet,TipOrden,MR.NumOrden AS Num_Orden,MR.IdCiaOrd AS CdCiaOrd,ItemOrd,D.IdMercancia AS CdMercancia,DescripMcias,D.Cantidad AS Cant,PesoNeto,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,Volumen,UndVol
,NitRemite,Remitente,DirOrigen,D.IdOrigen AS OrdCodOrigen,LO.Localidad AS OrdCiudadOrigen,LO.IdDep AS OrdCdepOrigen,DOR.Departamento AS OrdDptoOrigen,NitDestntario,Destinatario,DirDestino
,D.IdDestino AS OrdCodDestino,LD.Localidad AS OrdCiudadDestino,LD.IdDep AS OrdCdepDestino,DDN.Departamento AS OrdDptoDestino,TarifClie,TarifTabla,TarifPago,D.VrDeclarado AS DetVrDeclarado,D.VrSeguro AS DetVrSeguro
,TarifSeguro,UndTarifa,UndTarifPago,Remision,DocCliente,Referencia1,Referencia2,Referencia3,Contenedor1,Contenedor2,PesoCont,D.IdUnd AS CdUnd,UM.Unidad AS UndMedida,D.IdEmp AS CdEmp,Empaque,D.IdNat AS CdNat,Natlzaprod
,D.IdMnjo AS CdMnjo,ManejoMcia,D.IdTmcia AS CdTmcia,TipoMcia,CdRango,DescripRango,IdCliente,CL.RazonSocial AS OrdNomCliente
--Datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraInfViajes AS M INNER JOIN Trn_TraInfViajesOrd AS MR ON M.TipDoc=MR.TipDoc AND M.NumViaje=MR.NumViaje AND M.IdCia=MR.IdCia
 INNER JOIN Trn_TraRemMcias AS D ON MR.TipOrden=D.TipDoc AND MR.NumOrden=D.NumOrden AND MR.IdCiaOrd=D.IdCia AND MR.ItemOrd=D.Item
 INNER JOIN Trn_TraRemesa AS R ON MR.TipOrden=R.TipDoc AND MR.NumOrden=R.NumOrden AND MR.IdCiaOrd=R.IdCia 
 INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia INNER JOIN Sys_TiposDoc AS TD ON M.TipDoc=TD.IdDoc
 INNER JOIN EstadoDoc AS ED ON M.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS CO ON M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS RT ON M.IdRuta=RT.IdRuta  INNER JOIN Terceros AS  CDT ON M.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS T ON M.IdPoseedor=T.IdTercero INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DOR ON LO.IdDep=DOR.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DDN ON LD.IdDep=DDN.IdDep
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN Empaques AS EMP ON D.IdEmp=EMP.IdEmp
 INNER JOIN TiposNat AS NZ ON D.IdNat=NZ.IdNat INNER JOIN TiposMnjo AS MJ ON D.IdMnjo=MJ.IdMnjo
 INNER JOIN TiposMcia AS TM ON D.IdTmcia=TM.IdTmcia 
 INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS COL ON V.IdColor=COL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 INNER JOIN Terceros AS CL ON R.IdCliente=CL.IdTercero 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed LEFT JOIN RangosPeso AS RP ON D.CdRango=RP.IdRango
 WHERE M.TipDoc=@pmTipDoc AND M.NumViaje BETWEEN @pmNumViajeIni AND @pmNumViajeFin AND M.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraLiqViajesRel] @pmTipDoc VARCHAR(3),@pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
 SELECT LQ.TipDoc AS TipoLiq,LQ.Liquidacion AS NumLiquida,LQ.IdCia AS CdCia,Compania,Fecha,LQ.IdConcepto AS CdConcepto,C.Concepto AS DescConcepto,Modalidad,TipMuc,Manifiesto,IdCiaMuc,FechaMuc
,LQ.IdVehiculo AS PlacaVeh,LQ.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,LQ.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,LQ.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,LQ.NitEmpresa AS Nit_Empresa,ETR.RazonSocial AS NomEmpresa,VrAnticipos,VrGastos,VrCombustible,VrOtrosCred,VrDiferencia,galsComb,PesoTotal,Cantidad,Volumen
,LQ.IdRuta AS CdRuta,Ruta,IdLocOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,IdLocDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,Referencia,DescMcias,LiqMixto,kmtInicial,kmtFinal,EstFactura,TipDocRef,NumDocRef,IdCiaRef,FecDocRef,Num_Ppto,TipCom,TipoCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,LQ.Observacion AS Observ,LQ.IdEstado AS CdEstado,TimeSys,LQ.FecUpdate AS FechaAct,IdCiaCrea,LQ.IdUsuario AS CdUsuario,Usuario
--datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,V.NitEmpresa AS VehNitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,V.IdGrupo AS CdGrupo,GrupoProp
 FROM Trn_TraLiqViajes AS LQ INNER JOIN Companias AS CN ON LQ.IdCia=CN.IdCia  INNER JOIN adm_Usuarios AS U ON LQ.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON LQ.IdConcepto=C.IdConcepto
 INNER JOIN Localidades AS CO ON  LQ.IdLocOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON LQ.IdLocDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON LQ.IdRuta=R.IdRuta  INNER JOIN Terceros AS  CDT ON LQ.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS NP ON LQ.IdPropietario=NP.IdTercero   INNER JOIN Terceros AS T ON LQ.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON LQ.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS ETR ON LQ.NitEmpresa=ETR.IdTercero LEFT JOIN TiposCom AS TCM ON LQ.TipCom=TCM.IdCom 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 WHERE LQ.TipDoc=@pmTipDoc  AND LQ.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND LQ.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND LQ.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND LQ.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND LQ.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 AND LQ.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 ORDER BY LQ.IdCia,LQ.Liquidacion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraLiqViajesFmt] @pmTipDoc VARCHAR(3),@pmLiquidacionIni INT,@pmLiquidacionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT LQ.TipDoc AS TipoLiq,TipoDoc,LQ.Liquidacion AS NumLiquida,LQ.IdCia AS CdCia,Compania,Fecha,LQ.IdConcepto AS CdConcepto,C.Concepto AS DescConcepto,Modalidad,TipMuc,Manifiesto,IdCiaMuc,FechaMuc
,LQ.IdVehiculo AS PlacaVeh,LQ.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,LQ.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,LQ.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,LQ.NitEmpresa AS Nit_Empresa,ETR.RazonSocial AS NomEmpresa,VrAnticipos,VrGastos,VrCombustible,VrOtrosCred,VrDiferencia,galsComb,PesoTotal,Cantidad,Volumen
,LQ.IdRuta AS CdRuta,Ruta,IdLocOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,IdLocDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,Referencia,DescMcias,LiqMixto,Num_Ppto,kmtInicial,kmtFinal,EstFactura,TipDocRef,NumDocRef,IdCiaRef,FecDocRef,TipCom,TipoCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,LQ.Observacion AS Observ,LQ.IdEstado AS CdEstado,Estado,TimeSys,LQ.FecUpdate AS FechaAct,IdCiaCrea,LQ.IdUsuario AS CdUsuario,Usuario,Leyenda
,Item,D.IdConcepto AS DetCodConc,CG.Concepto AS DetNomConc,DescConcepto,D.IdCuenta AS CdCuenta,NomCuenta,VrDebito,VrCredito,D.IdTercero AS NitTercero,N.RazonSocial AS NomTercero
,CdCCosto,CCosto,CdSubCos,SubCosto,D.RubroConc,VrBase,TarifBase,CantComb,TipFac,Factura,IdCiaFac,ItemFac,FecVence,RefConcep,D.TipSop,D.NumSoporte,D.IdCiaSop
--datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,V.NitEmpresa AS VehNitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraLiqViajes AS LQ  INNER JOIN Companias AS CN ON LQ.IdCia=CN.IdCia INNER JOIN Sys_TiposDoc AS TD ON LQ.TipDoc=TD.IdDoc
 INNER JOIN EstadoDoc AS ED ON LQ.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON LQ.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON LQ.IdConcepto=C.IdConcepto
 INNER JOIN Localidades AS CO ON  LQ.IdLocOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON LQ.IdLocDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON LQ.IdRuta=R.IdRuta  INNER JOIN Terceros AS  CDT ON LQ.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS NP ON LQ.IdPropietario=NP.IdTercero   INNER JOIN Terceros AS T ON LQ.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON LQ.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS ETR ON LQ.NitEmpresa=ETR.IdTercero LEFT JOIN TiposCom AS TCM ON LQ.TipCom=TCM.IdCom 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero
 LEFT JOIN Trn_TraLiqDetalle AS D ON LQ.TipDoc=D.TipDoc AND LQ.Liquidacion=D.Liquidacion AND LQ.IdCia=D.IdCia
 LEFT JOIN ConcDiversos AS CG ON D.IdConcepto=CG.IdConcepto LEFT JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 LEFT JOIN Terceros AS N ON  D.IdTercero=N.IdTercero
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos 
 WHERE LQ.TipDoc=@pmTipDoc AND LQ.Liquidacion BETWEEN @pmLiquidacionIni AND @pmLiquidacionFin AND LQ.IdCia=@pmIdCia
 ORDER BY LQ.Liquidacion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraLiquidaRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null
 AS 
 SELECT LQ.TipDoc AS TipLiq,LQ.Liquidacion AS NumLiquida,LQ.IdCia AS CdCia,Compania,Fecha,LQ.IdConcepto AS CdConcepto,Concepto,LQ.IdVehiculo AS PlacaVeh
,LQ.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,LQ.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,VrTotal,CxPagar,TipCom,TipoCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,LQ.Observacion AS Observ,LQ.IdEstado AS CdEstado,Estado,FecIniCau,FecFinCau,FecIniOdp,FecFinOdp,ModLiquida,TimeSys,LQ.FecUpdate AS FechaAct,IdCiaCrea,LQ.IdUsuario AS CdUsuario,Usuario
,Item,TipCau,Causacion,IdCiaCau,ItemCau,FechaCau,D.IdConcepto AS CdConcCausac,D.Descripcion AS DetDescripcion,VrAbono,NitPoseedor,N.RazonSocial AS NomPoseedor,pVehiculo,EsVence
,TipOdp,OrdenPago,IdCiaOdp,TipMuc,Manifiesto,IdCiaMuc,VrSaldoOrden,CdCuenta,CdCuePagar,NumFactura,CdCCosto,CCosto,CdSubCos,SubCosto
--datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraLiquida AS LQ INNER JOIN Companias AS CN ON LQ.IdCia=CN.IdCia 
 INNER JOIN EstadoDoc AS ED ON LQ.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON LQ.IdUsuario=U.IdUsuario 
 INNER JOIN Conceptos AS C ON LQ.IdConcepto=C.IdConcepto
 INNER JOIN Terceros AS  CDT ON LQ.IdConductor=CDT.IdTercero  INNER JOIN Terceros AS T ON LQ.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON LQ.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 INNER JOIN Trn_TraLiquidaDed AS D ON LQ.TipDoc=D.TipDoc  AND LQ.Liquidacion=D.Liquidacion AND LQ.IdCia=D.IdCia
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos 
 LEFT JOIN Terceros AS N ON  D.NitPoseedor=N.IdTercero  LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero 
 LEFT JOIN TiposCom AS TCM ON LQ.TipCom=TCM.IdCom 
 WHERE LQ.TipDoc=@pmTipDoc AND  LQ.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND LQ.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND LQ.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND LQ.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND LQ.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 AND LQ.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 ORDER BY LQ.IdCia,LQ.Liquidacion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevOdpFmt] @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 TipoOdp,D.OrdPago AS NumOdp,IdCiaDoc,FecDoc
,D.IdVehiculo AS PlacaVeh,D.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,D.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,CxPagar,ModdDev
,D.TipCom AS CdTipComp,TipoCom,D.Comprobante AS NumComp,D.IdCiaCom AS CdCiaComp,D.Observacion AS Observ,D.TimeSys AS FechaCrea,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario,Leyenda
,O.TipMuc AS TipManif,O.Manifiesto AS NumManif,O.IdCiaMuc AS CdCiaManif,VrTotalFletes,O.VrDescuento AS Vr_Dcto,O.VrRetencion AS Vr_RetFte,O.VrReteIca AS Vr_RetIca,VrAnticipos
,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,VrImpAvTa,O.VrNeto AS Vr_Neto
,TarifaTabla,O.TarifaFlete AS Tarifa_Flete,UnidTarifa,O.PesoTotal AS Peso_Total,Unidades,O.Volumen AS VolmTotal,PesoOrigen,UnidOrigen,VolOrigen,PesoDestino,UnidDestino,VolDestino
,O.BaseRet,O.BaseIca,BaseSeg,BaseImp,O.TarifaRet,O.TarifaIca,TarifaSeg,TarifaImp,TarifaAvta,TipoLiq,TipEgr,Egreso,IdCiaEgr,NumCheque,Referencia,CantFaltante,UnidadFalt,TolFaltNeto,VrConcPagos
,VrConcDctos,VrConcFondo,VrConcSeguro,VrConcAporte,VrConcImpuesto,VrRemesas,VrRecCaja,CdForma,EdoLiqCausac
,M.Fecha AS FecManif,FecDespacho,FecEntrega,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,nRemolque,TipoAfiVehic
,IdLocFletes,CF.Localidad AS LugarFletes,M.Observacion AS MucObserv
--Datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraDevOdp AS D INNER JOIN Trn_TraOrdenManif AS O ON D.TipDoc=O.TipDoc AND D.OrdPago=O.OrdPago AND D.IdCiaDoc=O.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 C ON D.IdConcepto=C.IdConcepto
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN Terceros AS  CDT ON D.IdConductor=CDT.IdTercero
 INNER JOIN Terceros AS T ON D.IdPoseedor=T.IdTercero INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Trn_TraManifiesto AS M ON O.TipMuc=M.TipDoc AND O.Manifiesto=M.Manifiesto AND O.IdCiaMuc=M.IdCia
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta    INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero  LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom 
 WHERE D.TipDev=@pmTipDev AND D.Devolucion  BETWEEN @pmDevolucionIni AND @pmDevolucionFin  AND D.IdCia=@pmIdCia

 ORDER BY D.Devolucion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevLegFmt] @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,C.Concepto AS DescConcepto,DV.TipDoc AS TipoLiq,DV.Liquidacion AS NumLiquida,IdCiaDoc,FecDoc,Modalidad,TipMuc,Manifiesto,IdCiaMuc
,DV.IdVehiculo AS PlacaVeh,DV.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,DV.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,DV.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,DV.NitEmpresa AS Nit_Empresa,ETR.RazonSocial AS NomEmpresa,VrAnticipos,VrGastos,VrCombustible,VrDiferencia,VrOtrosCred,galsComb,PesoTotal,Cantidad,Volumen
,ModdDev,TipCom,TipoCom,Comprobante,IdCiaCom,DV.Observacion AS Observ,TimeSys,IdCiaCrea,DV.IdUsuario AS CdUsuario,Usuario,Leyenda
,Item,D.IdConcepto AS DetCodConc,CG.Concepto AS DetNomConc,DescConcepto,D.IdCuenta AS CdCuenta,NomCuenta,VrDebito,VrCredito,D.IdTercero AS NitTercero,N.RazonSocial AS NomTercero
,CdCCosto,CCosto,CdSubCos,SubCosto,D.RubroConc,VrBase,TarifBase,CantComb,TipFac,Factura,IdCiaFac,ItemFac,FecVence,RefConcep
--datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,V.NitEmpresa AS VehNitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraDevLeg AS DV  INNER JOIN Companias AS CN ON DV.IdCia=CN.IdCia INNER JOIN Sys_TiposDoc AS TD ON DV.TipDev=TD.IdDoc
 INNER JOIN adm_Usuarios AS U ON DV.IdUsuario=U.IdUsuario INNER JOIN Conceptos AS C ON DV.IdConcepto=C.IdConcepto
 INNER JOIN Terceros AS  CDT ON DV.IdConductor=CDT.IdTercero  INNER JOIN Terceros AS NP ON DV.IdPropietario=NP.IdTercero 
 INNER JOIN Terceros AS T ON DV.IdPoseedor=T.IdTercero   INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON DV.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS ETR ON DV.NitEmpresa=ETR.IdTercero LEFT JOIN TiposCom AS TCM ON DV.TipCom=TCM.IdCom 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero
 LEFT JOIN Trn_TraLiqDetalle AS D ON DV.TipDoc=D.TipDoc AND DV.Liquidacion=D.Liquidacion AND DV.IdCiaDoc=D.IdCia
 LEFT JOIN ConcDiversos AS CG ON D.IdConcepto=CG.IdConcepto LEFT JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 LEFT JOIN Terceros AS N ON  D.IdTercero=N.IdTercero
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos 
 WHERE DV.TipDev=@pmTipDev AND DV.Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin  AND DV.IdCia=@pmIdCia
 ORDER BY  DV.Devolucion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevCauRel] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null
 AS 
 SELECT TipDev,Devolucion,DV.IdCia AS CdCia,Compania,Fecha,DV.IdConcepto AS CdConcepto,CO.Concepto AS DescConcepto,DV.TipDoc AS TipoCau,DV.Causacion AS NumCausac,IdCiaDoc,FecDoc
,IdProv,T.RazonSocial AS Proveedor,DV.Factura AS NumFactura,VrTotal,VrOtros,VrNeto,ModdDev,TipCom,TipoCom,Comprobante,IdCiaCom
,DV.Observacion AS Observ,TimeSys,IdCiaCrea,DV.IdUsuario AS CdUsuario,Usuario
,D.IdConcepto AS CdConcCausac,CCA.Concepto AS ConcCausac,D.Descripcion AS DetDescripcion,D.TipoConc AS TipConcept,VrDebito,VrCredito,CdCuenta,NomCuenta,pVehiculo,NitTercero,NP.RazonSocial AS Razon_Social
,CdCCosto,CCosto,CdSubCos,SubCosto,TipoAfiVehic,VrBase,TarifBase,Referencia,TipDocRef,DocRef,TipFac,D.Factura AS DetFactura,IdCiaFac,ItemFac,FecVence,NumLiquida,CiaLiquida,VrAbonado
--datos del proveedor
,T.TipoId AS TercTipId,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 CdDpto,DP.Departamento AS DescDpto
,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraDevCau AS DV  INNER JOIN Companias AS CN ON DV.IdCia=CN.IdCia 
 INNER JOIN Conceptos AS CO ON DV.IdConcepto=CO.IdConcepto INNER JOIN adm_Usuarios AS U ON DV.IdUsuario=U.IdUsuario 
 INNER JOIN Terceros AS T ON DV.IdProv=T.IdTercero 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 INNER JOIN Trn_TraCauDetalle AS D ON DV.TipDoc=D.TipDoc AND DV.Causacion=D.Causacion AND DV.IdCiaDoc=D.IdCia
 INNER JOIN TraConcCausac AS CCA ON D.IdConcepto=CCA.IdConcepto 
 LEFT JOIN  Vehiculos AS V ON D.pVehiculo=V.IdVehiculo   LEFT JOIN  TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 LEFT JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 LEFT JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  LEFT JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 LEFT JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd 
 LEFT JOIN Terceros AS NP ON D.NitTercero=NP.IdTercero  LEFT JOIN Puc AS P ON D.CdCuenta=P.IdCuenta 
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos 
 LEFT JOIN TiposCom AS TCM ON DV.TipCom=TCM.IdCom 
 WHERE DV.TipDev=@pmTipDev AND DV.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND DV.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND DV.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND DV.IdProv LIKE ISNULL(@pmIdProv,'%')
 ORDER BY DV.IdCia,DV.Devolucion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevAntRel] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null
 AS 
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,VrDevolucion,D.TipDoc AS TipoAnt,D.Anticipo AS NumAnticipo,IdCiaDoc,D.FecDoc AS FechaAnt
,D.IdVehiculo AS PlacaVeh,D.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,D.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,CdCta,NumeroCta,CTA.IdBanco,Banco,CdCuenta,NomCuenta,A.VrDeduccion,ModdDev,D.TipCom AS TipComp,TipoCom,D.Comprobante AS NumComp,D.IdCiaCom AS CdCiaComp,D.Observacion AS Observ
,D.TimeSys AS FechaCrea,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
,TipMuc,A.Manifiesto AS NumManif,IdCiaMuc,Modalidad,TipoPago,NumCheque,FecCheque,Beneficiario,A.Cantidad AS CantGals,PreAnticipo
--datos del vehículo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TPR.TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,V.IdGrupo AS CdGrupo,GrupoProp
 FROM Trn_TraDevAnt AS D  INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN Terceros AS  CDT ON D.IdConductor=CDT.IdTercero  INNER JOIN Terceros AS T ON D.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 INNER JOIN Trn_TraAnticipos AS A ON D.TipDoc=A.TipDoc AND D.Anticipo=A.Anticipo AND D.IdCiaDoc=A.IdCia
 LEFT JOIN CtasCorrientes AS CTA ON D.CdCta=CTA.IdCta LEFT JOIN Bancos AS B ON CTA.IdBanco=B.IdBanco
 LEFT JOIN Puc AS P ON D.CdCuenta=P.IdCuenta LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 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.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 AND D.IdConductor LIKE ISNULL(@pmIdConductor,'%') 
 ORDER BY D.IdCia,D.Devolucion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraRemesaRelDet] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null
 AS 
 SELECT R.TipDoc AS CdTipDoc,R.NumOrden AS Num_Orden,R.IdCia AS CdCia,Compania,Fecha,FecDespacho,FecEntrega,HoraLlegada,IdCliente,NomCliente,T.RazonSocial AS RazonSocialCli
,R.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,IdClieFact,IdLocFletes,CF.Localidad AS LugarFletes,R.IdVehiculo AS PlacaVeh,R.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,nRemolque,TipoAfiVehic,Modalidad,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos,R.VrDeclarado AS ValDeclarado,R.VrSeguro AS ValSeguro,R.Cantidad AS CantTotal,PesoTotal,Items
,IdMneda,VrTasa,NumPedido,IdCiaPed,FechaPed,TipDcm,NumDocmto,IdCiaDcm,FechaDcm,NumManif,IdCiaManif,EstCumplido,EstFactura,CdConcepto,Concepto,CdRutaTarif,TipCom,Comprobante,IdCiaCom
,SerieGuia,NumGuia,CdForma,NumCausac,CdCiaCausac,EdoCausac,Anulado,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS CdUsuario,Usuario
,RA.IdVend AS CdVend,VN.RazonSocial AS Vendedor,LugarCargue,LugarDescargue,NomContacto,TelContacto,emlContacto,ContacDestino,TelContacDest,emlContacDest,TomadorPoliza,PolizaSeguro
,NitCiaPoliza,NomCiaPoliza,FecVencePol,ManifMintrans,RemMintrans,TipSal,NumSalida,IdCiaSal,TipOrdMud,NumInvent,IdCiaInv,TipoRuta,TipoMintrans,CdBodega,Bodega,RA.TipoDevRemesa,RA.CodCiuTransb,LT.Localidad AS CiudadTransb,RA.MvoTransbordo
--detalles
,Item,IdMercancia,DescripMcias,D.Cantidad AS Cant,PesoNeto,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol,Cases,Cajas,Palets,NitRemite,Remitente,NitDestntario,Destinatario
,DirOrigen,IdOrigen,LO.Localidad AS CiuOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,DirDestino,IdDestino,LD.Localidad AS CiuDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,TarifClie,TarifPago,TarifTabla,D.VrDeclarado AS DetVrDeclarado,D.VrSeguro AS DetVrSeguro,TarifSeguro,UndTarifa,UndTarifPago,Remision,DocCliente,Referencia1,Referencia2,Referencia3
,Contenedor1,Contenedor2,D.CdAgencia AS DetCdAgencia,Cumplido,IdCiaCump,FechaCump,DetalleCump,CantidadCump,PesoCump,VolCump,CasesCump,CajasCump,PaletsCump,TarifCump,PagoCump,UndTarCump,UndTarPagoCump
,TipFac,Factura,IdCiaFac,FechaFac,TarifClieFac,TipOdp,NumeroOdp,IdCiaOdp,TarifOdp,PesoCont
,D.IdUnd AS CdUnd,UM.Unidad AS UndMedida,D.IdEmp AS CdEmp,Empaque,D.IdNat AS CdNat,Natlzaprod,D.IdMnjo AS CdMnjo,ManejoMcia,D.IdTmcia AS CdTmcia,TipoMcia,CdRango,DescripRango
,D.SedeRem,SR.NomSede AS NomSedeRem,D.SedeDest,SD.NomSede AS NomSedeDest
--Datos del cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NitRepLeg,NomRepLeg,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,A.DirAgncia AS AgeDireccion,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,A.TelAgncia AS AgeTelefono,A.Referencia AS AgeReferencia
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPoseedor AS NitPoseedor,NPO.RazonSocial AS Poseedor
,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,V.IdGrupo AS CdGrupoPro,GrupoProp
 FROM Trn_TraRemesa AS R  INNER JOIN Trn_TraRemAnexo AS RA ON R.TipDoc=RA.TipDoc  AND R.NumOrden=RA.NumOrden AND R.IdCia=RA.IdCia
 INNER JOIN Trn_TraRemMcias AS D ON R.TipDoc=D.TipDoc  AND R.NumOrden=D.NumOrden AND R.IdCia=D.IdCia
 INNER JOIN Companias AS CN ON R.IdCia=CN.IdCia  INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 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 CF ON R.IdLocFletes=CF.IdLocal  INNER JOIN Terceros AS  CDT ON R.IdConductor=CDT.IdTercero 
 INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN Empaques AS EMP ON D.IdEmp=EMP.IdEmp
 INNER JOIN TiposNat AS NZ ON D.IdNat=NZ.IdNat INNER JOIN TiposMnjo AS MJ ON D.IdMnjo=MJ.IdMnjo
 INNER JOIN TiposMcia AS TM ON D.IdTmcia=TM.IdTmcia 
 INNER JOIN Vehiculos AS V ON R.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 MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN Terceros AS NPO ON V.IdPoseedor=NPO.IdTercero  INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN Terceros AS VN ON RA.IdVend=VN.IdTercero  LEFT JOIN Agencias AS A ON R.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN  Conceptos AS C ON R.CdConcepto=C.IdConcepto 
 LEFT JOIN AlmBodegas AS BG ON R.CdBodega=BG.IdBodega LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed LEFT JOIN RangosPeso AS RP ON D.CdRango=RP.IdRango
 LEFT JOIN TercSedes AS SR ON D.NitRemite=SR.IdTercero AND D.SedeRem=SR.IdSede LEFT JOIN TercSedes AS SD ON D.NitDestntario=SD.IdTercero AND D.SedeDest=SD.IdSede
 LEFT JOIN Localidades AS LT ON RA.CodCiuTransb=LT.IdLocal
 WHERE R.TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND R.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND R.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 ORDER BY R.IdCia,R.NumOrden
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevOdpRel] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
 AS 
 SELECT D.TipDev,D.Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipoOdp,D.OrdPago AS NumOdp,IdCiaDoc,FecDoc
,D.IdVehiculo AS PlacaVeh,D.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,D.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,CxPagar,ModdDev
,D.TipCom AS CdTipComp,TipoCom,D.Comprobante AS NumComp,D.IdCiaCom AS CdCiaComp,D.Observacion AS Observ,D.TimeSys AS FechaCrea,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
,O.TipMuc AS TipManif,O.Manifiesto AS NumManif,O.IdCiaMuc AS CdCiaManif,VrTotalFletes,O.VrDescuento AS Vr_Dcto,O.VrRetencion AS Vr_RetFte,O.VrReteIca AS Vr_RetIca,VrAnticipos
,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,O.VrNeto AS Vr_Neto
,TarifaTabla,O.TarifaFlete AS Tarifa_Flete,UnidTarifa,O.PesoTotal AS Peso_Total,Unidades,O.Volumen AS VolmTotal,PesoOrigen,UnidOrigen,VolOrigen,PesoDestino,UnidDestino,VolDestino
,O.BaseRet,O.BaseIca,BaseSeg,BaseImp,O.TarifaRet,O.TarifaIca,TarifaSeg,TarifaImp,TipoLiq,TipEgr,Egreso,IdCiaEgr,NumCheque,Referencia,CantFaltante,UnidadFalt,TolFaltNeto,VrConcPagos
,VrConcDctos,VrConcFondo,VrConcSeguro,VrConcAporte,VrConcImpuesto,VrRemesas,VrRecCaja,CdForma,EdoLiqCausac
,M.Fecha AS FecManif,FecDespacho,FecEntrega,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,nRemolque,TipoAfiVehic
,IdLocFletes,CF.Localidad AS LugarFletes,M.Observacion AS MucObserv
--Datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraDevOdp  AS D INNER JOIN Trn_TraOrdenManif AS O ON D.TipDoc=O.TipDoc AND D.OrdPago=O.OrdPago AND D.IdCiaDoc=O.IdCia
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN Terceros AS  CDT ON D.IdConductor=CDT.IdTercero
 INNER JOIN Terceros AS T ON D.IdPoseedor=T.IdTercero INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Trn_TraManifiesto AS M ON O.TipMuc=M.TipDoc AND O.Manifiesto=M.Manifiesto AND O.IdCiaMuc=M.IdCia
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta    INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero  LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom 
 WHERE D.TipDev=@pmTipDev AND D.Devolucion BETWEEN ISNULL(@pmDevolucionIni,0) AND ISNULL(@pmDevolucionFin,2147483647)
 AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
-- AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
-- AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdConductor LIKE ISNULL(@pmIdConductor,'%')
-- AND D.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraRemesaFmt] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT R.TipDoc AS CdTipDoc,TipoDoc,R.NumOrden AS Num_Orden,R.IdCia AS CdCia,Compania,Fecha,FecDespacho,FecEntrega,HoraLlegada,IdCliente,NomCliente,T.RazonSocial AS RazonSocialCli
,R.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,IdClieFact,IdRemitente,NomRemite,IdDestinatario,NomDestino,IdLocOrigen,CO.Localidad AS CiuOrigen,IdLocDestino,CD.Localidad AS CiuDestino
,IdLocFletes,CF.Localidad AS LugarFletes,R.IdVehiculo AS PlacaVeh,R.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,TipoAfiVehic,Modalidad,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue
,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos,R.VrDeclarado AS ValDeclarado,R.VrSeguro AS ValSeguro,R.Cantidad AS CantTotal,PesoTotal,Items,IdMneda,VrTasa,NumPedido,IdCiaPed,FechaPed
,TipDcm,NumDocmto,IdCiaDcm,FechaDcm,NumManif,IdCiaManif,EstCumplido,EstFactura,CdConcepto,Concepto,CdRutaTarif,SerieGuia,NumGuia,CdForma,NumCausac,CdCiaCausac,EdoCausac,TipCom,TipoCom,Comprobante,IdCiaCom
,Anulado,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS CdUsuario,Usuario,Leyenda
,RA.IdVend AS CdVend,VN.RazonSocial AS Vendedor,LugarCargue,LugarDescargue,NomContacto,TelContacto,emlContacto,ContacDestino,TelContacDest,emlContacDest,TomadorPoliza,PolizaSeguro,RA.CantViajes
,NitCiaPoliza,NomCiaPoliza,FecVencePol,ManifMintrans,RemMintrans,TipSal,NumSalida,IdCiaSal,TipOrdMud,NumInvent,IdCiaInv,TipoRuta,TipoMintrans,NitEmpTransp,NumFactEmp,CdBodega,Bodega,RA.TipoDevRemesa,RA.CodCiuTransb,LT.Localidad AS CiudadTransb,RA.MvoTransbordo
,PactoCargue,HorasPactoCargue,MinPactoCargue,HoraCitaCargue,HoraLlegaCargue,HoraEntraCargue,HoraSaleCargue,PactoDescargue,HorasPactoDescargue,MinPactoDescargue,HoraCitaDescargue,HoraLlegaDescargue,HoraEntraDescargue,HoraSaleDescargue
--detalle mcias
,Item,D.IdMercancia,DescripMcias,D.Cantidad AS Cant,PesoNeto,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol,Cases,Cajas,Palets,NitRemite,Remitente
,DirOrigen,IdOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,NitDestntario,Destinatario,DirDestino,IdDestino,LD.Localidad AS CiudadDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,TarifClie,TarifPago,TarifTabla,D.VrDeclarado AS DetVrDeclarado,D.VrSeguro AS DetVrSeguro,TarifSeguro,UndTarifa,UndTarifPago,Remision,DocCliente,Referencia1,Referencia2,Referencia3
,Contenedor1,Contenedor2,D.CdAgencia AS DetCdAgencia,DA.Agencia AS DetAgencia,Cumplido,IdCiaCump,FechaCump,DetalleCump,CantidadCump,PesoCump,VolCump,CasesCump,CajasCump,PaletsCump,TarifCump,PagoCump,UndTarCump,UndTarPagoCump
,TipFac,Factura,IdCiaFac,FechaFac,TarifClieFac,TipOdp,NumeroOdp,IdCiaOdp,TarifOdp,PesoCont,TipDocInv,NumDocInv,IdCiaDocInv,ItemDocInv,D.SedeRem,SDR.NomSede AS NomSedeRem,D.SedeDest,SDD.NomSede AS NomSedeDest
,D.IdUnd AS CdUnd,UM.Unidad AS UndMedida,D.IdEmp AS CdEmp,Empaque,D.IdNat AS CdNat,Natlzaprod,D.IdMnjo AS CdMnjo,ManejoMcia,D.IdTmcia AS CdTmcia,TipoMcia,CdRango,DescripRango,CodigoUN
--Datos del cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NitRepLeg,NomRepLeg,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,A.DirAgncia AS AgeDireccion,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,A.TelAgncia AS AgeTelefono,A.Referencia AS AgeReferencia
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPoseedor AS NitPoseedor,NPO.RazonSocial AS Poseedor
,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
 FROM Trn_TraRemesa AS R  INNER JOIN Trn_TraRemAnexo AS RA ON R.TipDoc=RA.TipDoc  AND R.NumOrden=RA.NumOrden AND R.IdCia=RA.IdCia
 INNER JOIN Trn_TraRemMcias AS D ON R.TipDoc=D.TipDoc AND R.NumOrden=D.NumOrden AND R.IdCia=D.IdCia
 INNER JOIN Companias AS CN ON R.IdCia=CN.IdCia  INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 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 Sys_TiposDoc AS TD ON R.TipDoc=TD.IdDoc  INNER JOIN Localidades AS CO ON R.IdLocOrigen=CO.IdLocal INNER JOIN Localidades AS CD ON R.IdLocDestino=CD.IdLocal
 INNER JOIN Localidades AS CF ON R.IdLocFletes=CF.IdLocal  INNER JOIN Terceros AS  CDT ON R.IdConductor=CDT.IdTercero 
 INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN Empaques AS EMP ON D.IdEmp=EMP.IdEmp
 INNER JOIN TiposNat AS NZ ON D.IdNat=NZ.IdNat INNER JOIN TiposMnjo AS MJ ON D.IdMnjo=MJ.IdMnjo
 INNER JOIN TiposMcia AS TM ON D.IdTmcia=TM.IdTmcia INNER JOIN Mercancias AS MC ON D.IdMercancia=MC.IdMercancia
 INNER JOIN Vehiculos AS V ON R.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 MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN Terceros AS NPO ON V.IdPoseedor=NPO.IdTercero  INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN Terceros AS VN ON RA.IdVend=VN.IdTercero  LEFT JOIN Agencias AS A ON R.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal LEFT JOIN Agencias AS DA ON D.CdAgencia=DA.IdAgencia
 LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed LEFT JOIN RangosPeso AS RP ON D.CdRango=RP.IdRango
 LEFT JOIN Conceptos AS C ON R.CdConcepto=C.IdConcepto LEFT JOIN TiposCom AS TCM ON R.TipCom=TCM.IdCom
 LEFT JOIN AlmBodegas AS BG ON R.CdBodega=BG.IdBodega
 LEFT JOIN TercCndtores AS CT ON R.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN TercSedes AS SDR ON D.NitRemite=SDR.IdTercero AND D.SedeRem=SDR.IdSede LEFT JOIN TercSedes AS SDD ON D.NitDestntario=SDD.IdTercero AND D.SedeDest=SDD.IdSede
 LEFT JOIN Localidades AS LT ON RA.CodCiuTransb=LT.IdLocal
 WHERE R.TipDoc=@pmTipDoc AND R.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND R.IdCia=@pmIdCia
 ORDER BY R.NumOrden
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenServFmt] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT O.TipDoc AS IdTipDoc,TipoDoc,O.NumOrden,O.IdCia AS CdCia,Compania,O.Fecha,FecDespacho,FecRecibo,O.IdCliente,T.RazonSocial AS NomCliente
,O.IdAgencia AS CdAgencia,Agencia,O.IdVehiculo AS PlacaVeh,NumVeh,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,nRemolque,O.TipoAfiVehic,O.Modalidad,O.VrTotal,O.VrCosto,O.Cantidad AS Cant_Total,VrAnticipo,O.CdRuta,Ruta,O.IdOrigen AS CdOrigen,LO.Localidad AS NomOrigen,O.IdDestino AS CdDestino,LD.Localidad AS NomDestino
,TipoOrden,O.IdEstacion AS CdCampo,EstServicio AS NomCampo,NomEstacion,TipoResiduo,RemCliente,NomRecibido,FecIngCertif,FecCertificado,NumRemesa,CdCiaRem,EstCumplido,NumCump,CdCiaCum,EstFactura,TipFact,NumFactura,CdCiaFact
,NumAprob,IdCiaApr,FecAprob,UsuAprob,ObservAprob,kmtInicial,kmtFinal,Cantkms,EstOrden,TipLiq,NumLiquida,CdCiaLiq,O.Anulado,FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.TimeSys AS FechaCrea,O.FecUpdate AS FechaModif,O.IdCiaCrea AS CdCiaCrea
,OrigenAdd,O.IdUsuario AS CdUsuario,Usuario
--Detalles
,Item,D.IdMercancia AS CdMercancia,DescripMcia,D.Descripcion,D.Cantidad,D.UndMed,Unidad,D.VrUnitario,D.TarifaPago
,CantCump,TarifaCump,PagoCump,DocCliente
--Cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad
,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Vehiculo
,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPoseedor AS NitPoseedor,NPO.RazonSocial AS Poseedor
,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia
 FROM Trn_TraOrdenServ AS O 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 Terceros AS NP ON O.IdPoseedor=NP.IdTercero
 INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN Trn_TraOrdenDet AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
 INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed  INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Vehiculos AS V ON O.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 TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria
 INNER JOIN Terceros AS NPO ON V.IdPoseedor=NPO.IdTercero  INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 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 O.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN Mercancias AS MC ON D.IdMercancia=MC.IdMercancia
 LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia  LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 LEFT JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal LEFT JOIN Rutas AS RT ON O.CdRuta=RT.IdRuta
 WHERE O.TipDoc=@pmTipDoc AND O.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND O.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraCumplidoFmt] @pmTipDoc VARCHAR(3),@pmCumplidoIni INT,@pmCumplidoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT CU.TipDoc AS TipCum,TipoDoc,CU.Cumplido AS NumCumplido,CU.IdCia AS CdCia,Compania,CU.Fecha AS FechaCum,TipMuc,CU.Manifiesto AS NumManif,IdCiaMuc,CU.IdVehiculo AS PlacaVeh,Modalidad,DiasPlazo,FecPago
,TipoMargen,MargenFalt,UndCalcFalt,TarifFaltPago,TarifFaltCobro,CU.Observacion AS Observ
,CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,CdRuta,R.Ruta AS DescRuta
,NRadicaDoc,IdCiaRadic,CdCiaOfic,FecRadic,CU.IdEstado AS CdEstado,Estado,CU.Anulado AS Anuldo,CU.FecDev AS FechaDev
,CU.TimeSys AS FechaCrea,CU.FecUpdate AS FechaAct,CU.IdCiaCrea AS CdCiaCrea,CU.IdUsuario AS CdUsuario,Usuario,Leyenda
,M.Fecha AS FecManif,FecDespacho,M.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,TipoAfiVehic,M.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,M.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,VrFletes,VrRetencion,VrReteIca,VrDescuento,VrAnticipo,VrAntAdic,VrNeto,VrPagos,VrCargos,VrDctos,TarifaFlete,M.Cantidad AS CantTotal,PesoTotal
,IdLocFletes,CF.Localidad AS LugarFletes,FechaPago,PagoCargue,PagoDescargue,M.TipOdp AS TipoOdp,M.OrdPago AS NumOrdPago,M.IdCiaOdp AS CdCiaOdp,FechaOdp,EstOrden,M.Observacion AS Observ,TipoComp AS CdTipComp,NumComp AS Comprobante
,TipoCumpMT,MotivoSusp,ConsecSusp,VrAdicCargue,VrAdicDescargue,VrAdicFlete,MotivoVrAdic,VrDctoFlete,MotivoVrDcto,VrAdicAnticipo,FecEntregaDoc,NumRadicaMT,MvoAnulaCump,ObservAnulado,NumViajesCum,PesoLiqPago,PesoLiqFact
--detalle de cumplidos
,D.Item AS DetItem,TipRem,D.Remesa AS NumRemesa,D.IdCiaRem AS CdCiaRem,ItemRem,D.Cantidad AS Cant,D.PesoNeto AS PesoCump,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,D.Volumen AS VolCump,D.UndVol AS Und_Vol
,D.Cases AS CasesCump,D.Cajas AS CajasCump,D.Palets AS PaletsCump,D.TarifClie AS Tarif_Clie,D.TarifPago AS Tarif_Pago,TarifFlete,UndTarifClie,D.UndTarifPago AS UndTarifPag,CantCargue,PesoCargue,VolCargue,CasesCargue,CajasCargue,PaletsCargue
,EstadoCump,D.Remision AS NumRemision,D.DocCliente AS CumDocClie,D.Referencia1 AS CumRef1,D.Referencia2 AS CumRef2,D.Referencia3 AS CumRef3,D.Detalle AS CumDetalle
,IdMercancia,DescripMcias,RM.Cantidad AS RemCant,RM.PesoNeto AS RemPeso,NitRemite,Remitente,NitDestntario,Destinatario,TipoCumRemesa,MotivoSuspRem,HoraLlegaCargue,HoraEntraCargue,HoraSaleCargue
,HoraLlegaDescargue,HoraEntraDescargue,HoraSaleDescargue
--Datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,CU.CdPlazoPago,PZ.Plazo,PZ.NVmto,PZ.DiasPago
 FROM Trn_TraCumplido AS CU INNER JOIN Companias AS CN ON CU.IdCia=CN.IdCia INNER JOIN Sys_TiposDoc AS TD ON CU.TipDoc=TD.IdDoc
 INNER JOIN EstadoDoc AS ED ON CU.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON CU.IdUsuario=U.IdUsuario
 INNER JOIN Trn_TraCumRemesas AS D ON CU.TipDoc=D.TipDoc  AND CU.Cumplido=D.Cumplido AND CU.IdCia=D.IdCia
 INNER JOIN Trn_TraManifiesto AS M ON CU.TipMuc=M.TipDoc AND CU.Manifiesto=M.Manifiesto AND CU.IdCiaMuc=M.IdCia
 INNER JOIN Terceros AS  CDT ON M.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS NP ON M.IdPropietario=NP.IdTercero   INNER JOIN Terceros AS T ON M.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON CU.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN Localidades AS CO ON CU.CdOrigen=CO.IdLocal LEFT JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 LEFT JOIN Localidades AS CD ON CU.CdDestino=CD.IdLocal LEFT JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 LEFT JOIN Rutas AS R ON CU.CdRuta=R.IdRuta 
 LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed 
 LEFT JOIN Trn_TraRemMcias AS RM ON D.TipRem=RM.TipDoc  AND D.Remesa=RM.NumOrden AND D.IdCiaRem=RM.IdCia  AND D.ItemRem=RM.Item
 LEFT JOIN Plazos AS PZ ON CU.CdPlazoPago=PZ.IdPlazo
 WHERE CU.TipDoc=@pmTipDoc AND CU.Cumplido BETWEEN @pmCumplidoIni AND @pmCumplidoFin AND CU.IdCia=@pmIdCia
 ORDER BY CU.Cumplido
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NumParte
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,TipoInvers,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,TipoContable
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NumParte,Cantidad
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNomDotacionLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT DT.IdCia AS CdCia,CI.Compania,DT.Numero AS NumReg,DT.Fecha AS FechaEnt,TipoDotac,TipoEntrega,PlazoEntrega,FecVence
,TipoReg,DT.Anulado,FechaCrea,DT.IdUsuario AS CdUsuario,Usuario,DT.Observacion AS Observ,CritFiltros
,Item,D.IdEmpleado AS CdEmpleado,Apellidos,Nombres,D.NContrato AS NumCont,D.IdPrend AS CdPrend,DescPrenda,D.Descripcion,Cantidad,Unidad,D.VrUnitario
,C.IdTipCon AS CdTipCon,TipoContrato,FecIngreso,C.IdArea AS CdArea,Area,C.IdDep AS CdDep,Dependencia,C.IdInstala AS IdInstla,Instlacion,C.IdCargo AS CodCargo,Cargo
,C.IdCia AS ConCdCia,CN.Compania AS ConCompania,C.IdNom AS CdNom,TipoNomina,C.TipoLiquida AS TipoLiq,DchDotacion,VrSalario,VrAuxTrans,SalMinimo,SalIntegral
,E.Codigo,e_mail,E.Sexo,E.IdProf AS CodProf,Profesion,FecUltDot
 FROM Trn_NomDotacion AS DT INNER JOIN Trn_NomDotacionDet AS D ON DT.IdCia=D.IdCia AND DT.Numero=D.Numero
 INNER JOIN Companias AS CI ON DT.IdCia=CI.IdCia INNER JOIN adm_Usuarios AS U ON DT.IdUsuario=U.IdUsuario
 INNER JOIN NomPrendas AS PN ON D.IdPrend=PN.IdPrend
 INNER JOIN Empleados AS E ON D.IdEmpleado=E.IdEmpleado INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf 
 LEFT JOIN Emp_Contrato AS C ON D.NContrato=C.NContrato LEFT JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 LEFT JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo LEFT JOIN Dependencias AS DP ON C.IdDep=DP.IdDep
 LEFT JOIN Areas AS AR ON C.IdArea=AR.IdArea LEFT JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala
 LEFT JOIN Companias AS CN ON C.IdCia=CN.IdCia  LEFT JOIN TiposNom AS TN ON C.IdNom=TN.IdNom
 WHERE DT.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND DT.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,DiasAjuste,TipCom,TipoCom,Comprobante,IdCiaCom,Q.Observacion AS Observ,LogPmdio,LiqParcial,DiasDom,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NumParte
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_Dotacion] @pmtmEst CHAR(2)
 AS 
 SELECT tmIdEmpleado,Apellidos,Nombres,tmNContrato,tmTipo,tmFecha,tmDiasPlazo,tmFecVence,tmIdCia,CI.Compania,tmNumReg,tmDetalle
,FecIngreso,Indefinido,C.IdNom AS CdNom,TipoNomina,C.IdTipCon AS CdTipCon,TipoContrato,C.IdArea AS CdArea,Area,C.IdDep AS CdDep,Dependencia,C.IdInstala AS IdInstla,Instlacion
,C.IdCargo AS CodCargo,Cargo,C.IdCia AS ConCdCia,CN.Compania AS ConCompania,C.TipoLiquida AS TipoLiq,DchDotacion,VrSalario,VrAuxTrans,SalMinimo,SalIntegral
,E.Codigo,e_mail,E.Sexo,E.IdProf AS CodProf,Profesion,FecUltDot,TallaCam,TallaPan,TallaZap
FROM tm_Dotacion AS T INNER JOIN Empleados AS E ON T.tmIdEmpleado=E.IdEmpleado INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf
INNER JOIN Emp_Contrato AS C ON T.tmNContrato=C.NContrato INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Dependencias AS DP ON C.IdDep=DP.IdDep
INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala
INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom
LEFT JOIN Companias AS CI ON T.tmIdCia=CI.IdCia
WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,FspFinmes,DenyPEN,DenyEPS,DenyARP,DenyCaj,DenyCes,DenyPri,DenyVac
,DenyHex,DenyDcto,BasePrest,BaseSeg,DchDotacion,DchCafeteria,Reclmto,Notas,DsoSabado,TipoPrendDot,TipoMedSent,C.TipoTraspaso,C.FechaTraspaso,C.ContratoRef,C.IdEstado AS IdEstdCon,EC.Estado AS EstdoCon,C.Inactivo AS Inactvo,IdCot,C.IdGrupo AS GrupAR,VrUpc,SubTipoCot,FecFinPrueba,TN.NomDiario
--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,DecRenta
,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,LiqEspSalud,DctoSalud,PagoSalud
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,DiasAjuste,TipCom,TipoCom,Comprobante,IdCiaCom,Q.Observacion AS Observ,LogPmdio,LiqParcial,DiasDom,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,UndsProd,FecFinDep,MetodoDep,Ajustable,Contable,CueEnGrupo,VrAjuInfAcum,VrDepAcuHis,VrDepAcuAju,VrAjuInfDep,NumMesesDep
,FecUltDep,Marca,Modelo,NumPlaca,NumDoc,Referencia,TipComp,NumComp,CdCiaComp,VUExtend,A.IdEstado AS CdEstado,Estado,FecRetiro,A.IdCausal AS CdCausal,Causal,VrVenta,TipoContable,VrSalvamento,DepPartes,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_AfDepLta] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdActivo,NomActivo,tmPartes,(tmCostoIni+tmMejoras+tmAcuAjustes)-tmDisminucion AS TotalBase,tmSalvamento,tmMetodo,tmVidaUtil
,(tmVidaUtil/12) AS VUA,tmUnidades,tmCantidad,tmDepreciaMes,tmDepreciaHis,tmCostoIni,tmMejoras,tmDisminucion,tmDepreciaAcum
,tmAcuAjustes,tmTarifa,tmAjustesInf,tmAjuDepAcum,tmDepreciaAcum+tmAjuDepAcum AS BaseAjuDepAcum,tmAjustesDep,tmDepMejoras,tmEstadoReg,tmObservacion,tmTipoContab
,tmFechaIni,FecFinDep,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,tmCdGrupo
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,FecMatMerc,ExonParaf,ExonCajas
--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,VrIBCparaf,CantHoras,TipoSalario
--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,DiasSan,DiasDnrPen,DiasDnrSal,DN.NovDsr,DN.FecNovIng,DN.FecNovRet,DN.FecNovVsp,DN.FecIniSln,DN.FecFinSln,DN.FecIniIge,DN.FecFinIge,DN.FecIniLma,DN.FecFinLma
,DN.FecIniVac,DN.FecFinVac,DN.FecIniVct,DN.FecFinVct,DN.FecIniIrl,DN.FecFinIrl
--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,CR.CodClase AS CodClaseRie,TN.NomDiario
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmDiasSan,tmDiasDnrPen,tmDiasDnrSal,tmVrIBCparaf,tmCantHoras,tmTipSalario
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomDiarioLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdEmpleado VARCHAR(16)=Null
 AS 
SELECT N.IdEmpleado AS CdEmpleado,Apellidos,Nombres,N.Fecha,N.ValorDia,N.DctoPension,N.DctoSalud,N.AporteRP,N.AporteParaf,N.PrestSocial
,E.NContrato,C.FecIngreso,C.FecRetiro,C.Indefinido,C.IdCargo AS CdCargo,Cargo,C.IdArea AS CdArea,Area,C.IdDep AS CdDep,Dependencia
,C.IdInstala AS CdInstala,Instlacion,C.IdCia AS CdCia,Compania,C.IdNom AS CdNom,TipoNomina,C.TipoLiquida,C.VrSalario,C.VrAuxTrans,C.SalMinimo
,C.ATFinMes,SegFinMes,DenyPEN,DenyEPS,DenyARP,DenyCaj,DenyCes,DenyPri,DenyVac,E.Direccion,E.IdLocal AS CdLocal,Localidad,E.Telefono,E.TelMovil,E.e_mail
,E.IdProf AS CdProf,Profesion,N.FechaCrea,N.IdUsuario AS CdUsuario,Usuario,N.Id
FROM Trn_NomDiario AS N INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado INNER JOIN Emp_Contrato AS C ON E.NContrato=C.NContrato
INNER JOIN Dependencias AS DP ON C.IdDep=DP.IdDep INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala
INNER JOIN Areas AS A ON C.IdArea=A.IdArea INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom 
INNER JOIN Localidades AS L ON E.IdLocal=L.IdLocal INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf
INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
WHERE N.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (@pmIdEmpleado IS NULL OR N.IdEmpleado=@pmIdEmpleado)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 VARCHAR(5)=Null
,@pmTallaPan VARCHAR(5)=Null,@pmTallaZap VARCHAR(5)=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,FspFinmes,DenyPEN,DenyEPS,DenyARP
,DenyCaj,DenyCes,DenyPri,DenyVac,DenyHex,DenyDcto,BasePrest,BaseSeg,DchDotacion,DchCafeteria,Reclmto,Notas,DsoSabado,FecFinPrueba,TipoPrendDot,TipoMedSent,BenefPacto,C.TipoTraspaso,C.FechaTraspaso,C.ContratoRef,C.IdMedPago,MedioPago
,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
 LEFT JOIN NomMediosPago AS MP ON C.IdMedPago=MP.IdMedPago
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmNovDsr,tmNovAvp,tmNovIrp,tmNovVct,tmCodNewEps,FS.Fondo AS NuevaEps,tmCodNewPen,FP.Fondo AS NuevoFdoPen,tmCodNewIns,Instlacion
,tmCodNewTar,tmCodNewGru,tmDiasNov,tmDiasSan,tmDiasDnrPen,tmDiasDnrSal,tmFecNovIng,tmFecNovRet,tmFecNovVsp,tmFecIniSln,tmFecFinSln,tmFecIniIge,tmFecFinIge,tmFecIniLma,tmFecFinLma
,tmFecIniVac,tmFecFinVac,tmFecIniVct,tmFecFinVct,tmFecIniIrl,tmFecFinIrl
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoActualiza,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,TipoContable,VrSalvamento,DepPartes,UndsProd,TipComp,NumComp,CdCiaComp,VUExtend,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryEmpleadosCnt] @pmIdNom VARCHAR(4),@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null
,@pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT E.IdEmpleado AS IdEmpledo,Apellidos,Nombres,E.TipoId,E.Dv,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,FecUltParc,EsCondtor,pVehiculo,DecRenta,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,SubTipoCot,IdGrupo,VrUpc,Reclmto,Notas,C.IdEstado AS IdEst_Con,EC.Estado AS Estad_Con,C.Inactivo AS Inactvo,CdFonAvp,FspFinmes,FecFinPrueba
,LiqEspSalud,DctoSalud,PagoSalud,TN.NomDiario,C.TipoTraspaso,C.FechaTraspaso,C.ContratoRef
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrSalvamnto,Unidades,Cantidad,Dep_Partes,A.TipoContable,A.DepPartes
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,Num_Parte,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,DiasAjuste,TipCom,TipoCom,Comprobante,IdCiaCom,V.Observacion AS Observ,LogPmdio,LiqParcial,DiasDom,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,Num_Parte,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,E.Codigo,E.TipoId,E.IdLugarCed,LC.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,E.IdLocal AS EmpIdLocal,L.Localidad AS NomCiudad,L.IdDep AS CodDpto,DT.Departamento AS NombreDpto,DT.IdPais,PA.NombrePais
--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 LC ON E.IdLugarCed=LC.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 
 INNER JOIN Localidades AS L ON E.IdLocal=L.IdLocal INNER JOIN Departamentos AS DT ON L.IdDep=DT.IdDep
 INNER JOIN adm_Paises AS PA ON DT.IdPais=PA.IdPais
 WHERE tmEst=@pmtmEst
 ORDER BY Apellidos,Nombres,tmNMes,tmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,FecUltParc,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNomDotacion_Cr] @pmIdCia CHAR(2),@pmNumeroIni INT,@pmNumeroFin INT
 AS 
 SELECT DT.IdCia AS CdCia,CI.Compania,DT.Numero AS NumReg,DT.Fecha AS FechaEnt,TipoDotac,TipoEntrega,PlazoEntrega,FecVence
,TipoReg,DT.Anulado,FechaCrea,DT.IdUsuario AS CdUsuario,Usuario,DT.Observacion AS Observ,CritFiltros
,Item,D.IdEmpleado AS CdEmpleado,Apellidos,Nombres,D.NContrato AS NumCont,D.IdPrend AS CdPrend,DescPrenda,D.Descripcion,Cantidad,Unidad,D.VrUnitario
,C.IdTipCon AS CdTipCon,TipoContrato,FecIngreso,C.IdArea AS CdArea,Area,C.IdDep AS CdDep,Dependencia,C.IdInstala AS IdInstla,Instlacion,C.IdCargo AS CodCargo,Cargo
,C.IdCia AS ConCdCia,CN.Compania AS ConCompania,C.IdNom AS CdNom,TipoNomina,C.TipoLiquida AS TipoLiq,DchDotacion,VrSalario,VrAuxTrans,SalMinimo,SalIntegral
,E.Codigo,e_mail,E.Sexo,E.IdProf AS CodProf,Profesion,FecUltDot
 FROM Trn_NomDotacion AS DT INNER JOIN Trn_NomDotacionDet AS D ON DT.IdCia=D.IdCia AND DT.Numero=D.Numero
 INNER JOIN Companias AS CI ON DT.IdCia=CI.IdCia INNER JOIN adm_Usuarios AS U ON DT.IdUsuario=U.IdUsuario
 INNER JOIN NomPrendas AS PN ON D.IdPrend=PN.IdPrend
 INNER JOIN Empleados AS E ON D.IdEmpleado=E.IdEmpleado INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf 
 LEFT JOIN Emp_Contrato AS C ON D.NContrato=C.NContrato LEFT JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 LEFT JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo LEFT JOIN Dependencias AS DP ON C.IdDep=DP.IdDep
 LEFT JOIN Areas AS AR ON C.IdArea=AR.IdArea LEFT JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala
 LEFT JOIN Companias AS CN ON C.IdCia=CN.IdCia  LEFT JOIN TiposNom AS TN ON C.IdNom=TN.IdNom
 WHERE DT.IdCia=@pmIdCia AND DT.Numero BETWEEN @pmNumeroIni AND @pmNumeroFin
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryInstalaciones] @pmIdInstala VARCHAR(4)
 AS 
 SELECT IdInstala,Instlacion,DirInstala,CodCiu,CodCia,IdClase,Inactivo
 FROM Instalaciones
 WHERE IdInstala=@pmIdInstala
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VUExtend,A.IdEstado AS CdEstado,ED.Estado AS ActEstado,FecRetiro,A.IdCausal AS CdCausal,Causal,VrVenta,TipoContable,VrSalvamento,DepPartes,UndsProd,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryActivos_PartesLta] @pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT P.IdActivo AS CdActivo,NomActivo,TipoContable,P.Item,DescParte,NumParte,P.FecIniDep,P.MetDeprec,P.VidaUtil,P.UndsProd,P.VrCompra
,P.VrBaseDep,P.VrMejoras,P.VrDisminuc,P.VrSalvamento,P.VrDepAcum,P.VrDepAcumHis,P.VrAjuAcum,P.VrAjuDepAcum,NitProv,T.RazonSocial AS Proveedor,CueEnAfijo,P.IdCuenta AS CdCuenta,IdCueDep,IdCueGasto
,CodNiifAct,CodNiifDep,CodNiifGasto,VU_Extend,P.IdEstado AS CdEstado,EP.Estado AS DescEstado,Fec_Retiro,P.FecCreacion,P.IdUsuario AS CdUsuario,Usuario
--datos activo
,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,FecActivacion,A.VrMejoras AS AfVrMejoras,A.VrDisminucion,Ajustable,Contable,Marca,Modelo,NumPlaca,NumDoc,Referencia
,A.IdEstado AS AfCodEstado,ET.Estado AS AFEstado,FecRetiro,A.IdCausal AS CdCausal,Causal,VrVenta,DepPartes,A.FecCreacion AS AfFechaCrea
--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_Partes AS P INNER JOIN Activos AS A ON P.IdActivo=A.IdActivo INNER JOIN Terceros AS T ON P.NitProv=T.IdTercero
 INNER JOIN AfEstados AS EP ON P.IdEstado=EP.IdEstado  INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 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 AfEstados AS ET ON A.IdEstado=ET.IdEstado 
 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 (P.FecIniDep>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND P.FecIniDep<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,TipoInvers,TipoContable,CodNiifDif,CodNiifAmtza,TipComp,NumComp,CdCiaComp
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmDiasSan,tmDiasDnrPen,tmDiasDnrSal
,tmNovDsr,tmFecNovIng,tmFecNovRet,tmFecNovVsp,tmFecIniSln,tmFecFinSln,tmFecIniIge,tmFecFinIge,tmFecIniLma,tmFecFinLma,tmFecIniVac,tmFecFinVac,tmFecIniVct,tmFecFinVct,tmFecIniIrl,tmFecFinIrl,tmVrIBCparaf,tmCantHoras,tmTipSalario
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,VrSalvamnto,Unidades,Cantidad,Dep_Partes,VrDepMejoras
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NumParte,Cantidad
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,Num_Contrato,Nit_Tercero,T.RazonSocial AS NomTercero,N.HoraInicial,N.HoraFinal
--Datos empleado
,E.Codigo,E.IdLugarCed,Localidad,E.Direccion AS Dirccion,E.Telefono,E.TelMovil,E.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
 LEFT JOIN Terceros AS T ON N.Nit_Tercero=T.IdTercero
 WHERE IdPeriodo=@pmIdPeriodo AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 ORDER BY  N.IdEmpleado,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,FecFinPrueba,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,FecFinPrueba,TipoPrendDot,TipoMedSent,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.TipoTraspaso,C.FechaTraspaso,C.ContratoRef,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,C.IdMedPago
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmBasExceso,tmNitTercero,tmHoraInicial,tmHoraFinal,tmTipoInc
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraPedidoFmt] @pmTipDoc VARCHAR(3),@pmPedidoIni INT,@pmPedidoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT P.TipDoc AS CdTipDoc,TipoDoc,P.Pedido AS NumPedido,P.IdCia AS CdCia,Compania,Fecha,FechaVence,FecDespacho,FecEntrega,IdCliente,NomCliente,T.RazonSocial AS RazonSocialCli,P.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia
,IdClieFact,IdRemitente,NomRemite,IdDestinatario,NomDestino,IdLocOrigen,CO.Localidad AS CiuOrigen,IdLocDestino,CD.Localidad AS CiuDestino
,LugarCargue,LugarDescargue,P.IdVend AS CdVend,VN.RazonSocial AS Vendedor,Modalidad,Vigencia,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos
,P.VrDeclarado AS ValDeclarado,P.VrSeguro AS ValSeguro,P.Cantidad AS CantTotal,CantDesp,IdMneda,VrTasa,Cotizacion,IdCiaCot,NumAprob,IdCiaApr,FecAprob,TipOdc,OCargue,IdCiaOdc,FechaOdc,TipRem,Remesa,IdCiaRem,FechaRem,TipFac,Factura,IdCiaFac,FechaFac
,Anulado,FecDev,P.Observacion AS Observ,P.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,P.IdUsuario AS CdUsuario,Usuario,Leyenda
--datos de anexo
,NomContacto,TelContacto,emlContacto,ContacDestino,TelContacDest,emlContacDest,PolizaEsp,NumPolizaEsp,NitCiaPoliza,NomCiaPoliza,FecVencePol,VrLimiteDesp,Seguros,Cargue,Descargue,CdTipoEsc,NitSIA,NombreSIA,ContactoSIA,TelContacSIA
,IdLocCont,LC.Localidad AS CiudadCont,PatioCont,CdTipoVeh,TipoVehiculo,TipoMargen,MargenFalt,EmbAdicional,TipoRuta,TipoTrans,Embarque,CdTipCarga,TipoCarga,DevContenedor,UndCalcFalt,TarifFaltPago,TarifFaltCobro,CdRutaTarif,TipoServicio
--datos de mcias
,Item,D.IdMercancia,DescripMcias,D.Cantidad AS Cant,PesoNeto,D.UndMed AS CdUm,UMP.Unidad AS UmPeso,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol,Cases,Cajas,Palets,Remitente,DirOrigen,IdOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,Destinatario,DirDestino,IdDestino,LD.Localidad AS CiudadDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,TarifClie,TarifPago,TarifTabla,D.VrDeclarado AS DetVrDeclarado,D.VrSeguro AS DetVrSeguro,TarifSeguro,Referencia1,Referencia2,Contenedor1,Contenedor2
,NitRemite,NitDestntario,UndTarifa,UndTarifPago,D.IdUnd AS CdUnd,UM.Unidad AS UndMedida,D.IdEmp AS CdEmp,Empaque,D.IdNat AS CdNat,Natlzaprod,D.IdMnjo AS CdMnjo,ManejoMcia,D.IdTmcia AS CdTmcia,TipoMcia,CdRango,DescripRango,CodigoUN
--Datos del cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NitRepLeg,NomRepLeg,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.Referencia AS AgeReferencia
 FROM  Trn_TraPedido AS P INNER JOIN Trn_TraPedAnexo AS PA ON P.TipDoc=PA.TipDoc  AND P.Pedido=PA.Pedido AND P.IdCia=PA.IdCia
 INNER JOIN Trn_TraPedMcias AS D ON P.TipDoc=D.TipDoc AND P.Pedido=D.Pedido AND P.IdCia=D.IdCia
 INNER JOIN Companias AS CN ON P.IdCia=CN.IdCia  INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero 
 INNER JOIN TercCliente AS CLI ON P.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 Terceros AS VN ON P.IdVend=VN.IdTercero  INNER JOIN Sys_TiposDoc AS TD ON P.TipDoc=TD.IdDoc
 INNER JOIN Localidades AS CO ON P.IdLocOrigen=CO.IdLocal INNER JOIN Localidades AS CD ON P.IdLocDestino=CD.IdLocal
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 INNER JOIN Mercancias AS MC ON D.IdMercancia=MC.IdMercancia
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN Empaques AS EMP ON D.IdEmp=EMP.IdEmp
 INNER JOIN TiposNat AS NZ ON D.IdNat=NZ.IdNat INNER JOIN TiposMnjo AS MJ ON D.IdMnjo=MJ.IdMnjo
 INNER JOIN TiposMcia AS TM ON D.IdTmcia=TM.IdTmcia  
 LEFT JOIN Agencias AS A ON P.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LC ON PA.IdLocCont=LC.IdLocal LEFT JOIN TiposVeh AS TV ON PA.CdTipoVeh=TV.IdTipoVeh
 LEFT JOIN TiposCarga AS TCG ON PA.CdTipCarga=TCG.IdTipCarga
 LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed LEFT JOIN RangosPeso AS RP ON D.CdRango=RP.IdRango
 WHERE P.TipDoc=@pmTipDoc AND P.Pedido BETWEEN @pmPedidoIni AND @pmPedidoFin AND P.IdCia=@pmIdCia
 ORDER BY P.Pedido
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraPedAnexo] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2),@pmNomCliente VARCHAR(150),@pmNomRemite VARCHAR(150),@pmNomDestino VARCHAR(150),@pmLugarCargue VARCHAR(150),@pmLugarDescargue VARCHAR(150),@pmNomContacto VARCHAR(150),@pmTelContacto VARCHAR(30),@pmemlContacto VARCHAR(100),@pmContacDestino VARCHAR(150),@pmTelContacDest VARCHAR(30),@pmemlContacDest VARCHAR(100),@pmPolizaEsp BIT
,@pmNumPolizaEsp VARCHAR(50),@pmNitCiaPoliza VARCHAR(16),@pmNomCiaPoliza VARCHAR(150),@pmFecVencePol SMALLDATETIME,@pmVrLimiteDesp MONEY,@pmSeguros VARCHAR(10),@pmCargue VARCHAR(20),@pmDescargue VARCHAR(20),@pmCdTipoEsc VARCHAR(4),@pmNitSIA VARCHAR(16),@pmNombreSIA VARCHAR(150),@pmContactoSIA VARCHAR(150),@pmTelContacSIA VARCHAR(30),@pmIdLocCont VARCHAR(8),@pmPatioCont VARCHAR(50),@pmCdTipoVeh VARCHAR(4)
,@pmTipoMargen VARCHAR(10),@pmMargenFalt DECIMAL(14,4),@pmEmbAdicional VARCHAR(150),@pmTipoRuta VARCHAR(10),@pmTipoTrans VARCHAR(10),@pmEmbarque VARCHAR(10),@pmCdTipCarga VARCHAR(4),@pmDevContenedor BIT,@pmUndCalcFalt VARCHAR(10),@pmTarifFaltPago MONEY,@pmTarifFaltCobro MONEY,@pmCdRutaTarif VARCHAR(4),@pmTipoServicio VARCHAR(10),@pmCodTipoOper VARCHAR(4)
 AS 
 INSERT INTO Trn_TraPedAnexo (TipDoc,Pedido,IdCia,NomCliente,NomRemite,NomDestino,LugarCargue,LugarDescargue,NomContacto,TelContacto,emlContacto,ContacDestino,TelContacDest,emlContacDest,PolizaEsp,NumPolizaEsp,NitCiaPoliza,NomCiaPoliza,FecVencePol,VrLimiteDesp,Seguros,Cargue,Descargue,CdTipoEsc,NitSIA,NombreSIA,ContactoSIA,TelContacSIA
,TipoRuta,TipoTrans,Embarque,CdTipCarga,DevContenedor,IdLocCont,PatioCont,CdTipoVeh,MargenFalt,TipoMargen,UndCalcFalt,TarifFaltPago,TarifFaltCobro,EmbAdicional,CdRutaTarif,TipoServicio,CodTipoOper)
 VALUES (@pmTipDoc,@pmPedido,@pmIdCia,@pmNomCliente,@pmNomRemite,@pmNomDestino,@pmLugarCargue,@pmLugarDescargue,@pmNomContacto,@pmTelContacto,@pmemlContacto,@pmContacDestino,@pmTelContacDest,@pmemlContacDest,@pmPolizaEsp,@pmNumPolizaEsp,@pmNitCiaPoliza,@pmNomCiaPoliza,@pmFecVencePol,@pmVrLimiteDesp,@pmSeguros,@pmCargue,@pmDescargue,@pmCdTipoEsc,@pmNitSIA,@pmNombreSIA,@pmContactoSIA,@pmTelContacSIA
,@pmTipoRuta,@pmTipoTrans,@pmEmbarque,@pmCdTipCarga,@pmDevContenedor, @pmIdLocCont,@pmPatioCont,@pmCdTipoVeh,@pmMargenFalt,@pmTipoMargen,@pmUndCalcFalt,@pmTarifFaltPago,@pmTarifFaltCobro,@pmEmbAdicional,@pmCdRutaTarif,@pmTipoServicio,@pmCodTipoOper)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraPedidoRelCon] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT P.TipDoc AS CdTipDoc,P.Pedido AS NumPedido,P.IdCia AS CdCia,Compania,Fecha,FechaVence,FecDespacho,FecEntrega,IdCliente,NomCliente,T.RazonSocial AS RazonSocialCli,P.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia
,IdClieFact,IdRemitente,NomRemite,IdDestinatario,NomDestino,IdLocOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,IdLocDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,LugarCargue,LugarDescargue,P.IdVend AS CdVend,VN.RazonSocial AS Vendedor,Modalidad,Vigencia,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos
,P.VrDeclarado AS ValDeclarado,P.VrSeguro AS ValSeguro,P.Cantidad AS CantTotal,CantDesp,IdMneda,VrTasa,Cotizacion,IdCiaCot,NumAprob,IdCiaApr,FecAprob,TipOdc,OCargue,IdCiaOdc,FechaOdc,TipRem,Remesa,IdCiaRem,FechaRem,TipFac,Factura,IdCiaFac,FechaFac
,Anulado,FecDev,P.Observacion AS Observ,P.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,P.IdUsuario AS CdUsuario,Usuario
--datos de anexo
,NomContacto,TelContacto,emlContacto,ContacDestino,TelContacDest,emlContacDest,PolizaEsp,NumPolizaEsp,NitCiaPoliza,NomCiaPoliza,FecVencePol,VrLimiteDesp,Seguros,Cargue,Descargue,NitSIA,NombreSIA,ContactoSIA,TelContacSIA
,IdLocCont,LC.Localidad AS CiudadCont,PatioCont,CdTipoVeh,TipoVehiculo,TipoMargen,MargenFalt,EmbAdicional,TipoRuta,TipoTrans,Embarque,CdTipCarga,TipoCarga,TipoServicio,DevContenedor,UndCalcFalt,TarifFaltPago,TarifFaltCobro,CdRutaTarif
--conceptos
,Item,Descripcion,Tarifa,TipoConc,RubroConcep,CT.Cantidad AS Cant,VrUnitario,TarifIva,CT.IdConcepto AS CdConcTrans,Concepto,CdCuenta,NomCuenta,NitTercero,NTE.RazonSocial AS NomTercero,CT.CdTipoEsc AS CodTipoEsc,TipoEscolta,FechaNov,VrBase,TipoTarif,RefConc
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NitRepLeg,NomRepLeg,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.Referencia AS AgeReferencia
 FROM  Trn_TraPedido AS P INNER JOIN Trn_TraPedAnexo AS PA ON P.TipDoc=PA.TipDoc  AND P.Pedido=PA.Pedido AND P.IdCia=PA.IdCia
 INNER JOIN Companias AS CN ON P.IdCia=CN.IdCia  INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero 
 INNER JOIN TercCliente AS CLI ON P.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 Terceros AS VN ON P.IdVend=VN.IdTercero  
 INNER JOIN Localidades AS CO ON P.IdLocOrigen=CO.IdLocal  INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON P.IdLocDestino=CD.IdLocal  INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 LEFT JOIN Agencias AS A ON P.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LC ON PA.IdLocCont=LC.IdLocal LEFT JOIN TiposVeh AS TV ON PA.CdTipoVeh=TV.IdTipoVeh
 LEFT JOIN TiposCarga AS TCG ON PA.CdTipCarga=TCG.IdTipCarga
 LEFT JOIN  Trn_TraConceptos AS CT ON P.TipDoc=CT.TipDoc AND P.Pedido=CT.Documento AND P.IdCia=CT.IdCia
 LEFT JOIN ConcDiversos AS CV ON CT.IdConcepto=CV.IdConcepto LEFT JOIN Puc AS PU ON CT.CdCuenta=PU.IdCuenta
 LEFT JOIN TiposEsc AS TE ON CT.CdTipoEsc=TE.IdTipoEsc LEFT JOIN Terceros AS NTE ON CT.NitTercero=NTE.IdTercero
 WHERE P.TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND  @pmFechaFin AND   P.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND P.IdCliente  LIKE ISNULL(@pmIdCliente,'%') AND P.IdVend LIKE ISNULL(@pmIdVend,'%') AND P.Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY P.IdCia,P.Pedido
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraPedAnexo] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2),@pmNomCliente VARCHAR(150),@pmNomRemite VARCHAR(150),@pmNomDestino VARCHAR(150),@pmLugarCargue VARCHAR(150),@pmLugarDescargue VARCHAR(150),@pmNomContacto VARCHAR(150)
,@pmTelContacto VARCHAR(30),@pmemlContacto VARCHAR(100),@pmContacDestino VARCHAR(150),@pmTelContacDest VARCHAR(30),@pmemlContacDest VARCHAR(100),@pmPolizaEsp BIT,@pmNumPolizaEsp VARCHAR(50),@pmNitCiaPoliza VARCHAR(16),@pmNomCiaPoliza VARCHAR(150),@pmFecVencePol SMALLDATETIME
,@pmVrLimiteDesp MONEY,@pmSeguros VARCHAR(10),@pmCargue VARCHAR(20),@pmDescargue VARCHAR(20),@pmCdTipoEsc VARCHAR(4),@pmNitSIA VARCHAR(16),@pmNombreSIA VARCHAR(150),@pmContactoSIA VARCHAR(150),@pmTelContacSIA VARCHAR(30),@pmIdLocCont VARCHAR(8),@pmPatioCont VARCHAR(50)
,@pmCdTipoVeh VARCHAR(4),@pmTipoMargen VARCHAR(10),@pmMargenFalt DECIMAL(14,4),@pmEmbAdicional VARCHAR(150),@pmTipoRuta VARCHAR(10),@pmTipoTrans VARCHAR(10),@pmEmbarque VARCHAR(10),@pmCdTipCarga VARCHAR(4),@pmDevContenedor BIT,@pmUndCalcFalt VARCHAR(10),@pmTarifFaltPago MONEY
,@pmTarifFaltCobro MONEY,@pmCdRutaTarif VARCHAR(4),@pmTipoServicio VARCHAR(10),@pmCodTipoOper VARCHAR(4)
 AS 
 UPDATE Trn_TraPedAnexo SET NomCliente=@pmNomCliente,NomRemite=@pmNomRemite,NomDestino=@pmNomDestino,LugarCargue=@pmLugarCargue,LugarDescargue=@pmLugarDescargue,NomContacto=@pmNomContacto,TelContacto=@pmTelContacto,emlContacto=@pmemlContacto,ContacDestino=@pmContacDestino,TelContacDest=@pmTelContacDest,emlContacDest=@pmemlContacDest
,PolizaEsp=@pmPolizaEsp,NumPolizaEsp=@pmNumPolizaEsp,NitCiaPoliza=@pmNitCiaPoliza,NomCiaPoliza=@pmNomCiaPoliza,FecVencePol=@pmFecVencePol,VrLimiteDesp=@pmVrLimiteDesp,Seguros=@pmSeguros,Cargue=@pmCargue,Descargue=@pmDescargue,CdTipoEsc=@pmCdTipoEsc,NitSIA=@pmNitSIA,NombreSIA=@pmNombreSIA,ContactoSIA=@pmContactoSIA
,TelContacSIA=@pmTelContacSIA,IdLocCont=@pmIdLocCont,PatioCont=@pmPatioCont,CdTipoVeh=@pmCdTipoVeh,TipoMargen=@pmTipoMargen,MargenFalt=@pmMargenFalt,EmbAdicional=@pmEmbAdicional,UndCalcFalt=@pmUndCalcFalt,TarifFaltPago=@pmTarifFaltPago,TarifFaltCobro=@pmTarifFaltCobro
,TipoRuta=@pmTipoRuta,TipoTrans=@pmTipoTrans,Embarque=@pmEmbarque,CdTipCarga=@pmCdTipCarga,DevContenedor=@pmDevContenedor,CdRutaTarif=@pmCdRutaTarif,TipoServicio=@pmTipoServicio,CodTipoOper=@pmCodTipoOper
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraPedAnexo] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Pedido,IdCia,NomCliente,NomRemite,NomDestino,LugarCargue,LugarDescargue,NomContacto,TelContacto,emlContacto,ContacDestino,TelContacDest,emlContacDest,PolizaEsp,NumPolizaEsp
,NitCiaPoliza,NomCiaPoliza,FecVencePol,VrLimiteDesp,Seguros,Cargue,Descargue,CdTipoEsc,NitSIA,NombreSIA,ContactoSIA,TelContacSIA,IdLocCont,PatioCont,CdTipoVeh,TipoMargen,MargenFalt,EmbAdicional
,TipoRuta,TipoTrans,Embarque,CdTipCarga,DevContenedor,UndCalcFalt,TarifFaltPago,TarifFaltCobro,CdRutaTarif,TipoServicio,CodTipoOper
 FROM Trn_TraPedAnexo
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraPedidoRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT P.TipDoc AS CdTipDoc,P.Pedido AS NumPedido,P.IdCia AS CdCia,Compania,Fecha,FechaVence,FecDespacho,FecEntrega,IdCliente,NomCliente,T.RazonSocial AS RazonSocialCli,P.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia
,IdClieFact,IdRemitente,NomRemite,IdDestinatario,NomDestino,IdLocOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,IdLocDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,LugarCargue,LugarDescargue,P.IdVend AS CdVend,VN.RazonSocial AS Vendedor,Modalidad,Vigencia,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos
,P.VrDeclarado AS ValDeclarado,P.VrSeguro AS ValSeguro,P.Cantidad AS CantTotal,CantDesp,IdMneda,VrTasa,Cotizacion,IdCiaCot,NumAprob,IdCiaApr,FecAprob,TipOdc,OCargue,IdCiaOdc,FechaOdc,TipRem,Remesa,IdCiaRem,FechaRem,TipFac,Factura,IdCiaFac,FechaFac
,Anulado,FecDev,P.Observacion AS Observ,P.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,P.IdUsuario AS CdUsuario,Usuario
--datos de anexo
,NomContacto,TelContacto,emlContacto,ContacDestino,TelContacDest,emlContacDest,PolizaEsp,NumPolizaEsp,NitCiaPoliza,NomCiaPoliza,FecVencePol,VrLimiteDesp,Seguros,Cargue,Descargue,CdTipoEsc,NitSIA,NombreSIA,ContactoSIA,TelContacSIA
,IdLocCont,LC.Localidad AS CiudadCont,PatioCont,CdTipoVeh,TipoVehiculo,TipoMargen,MargenFalt,EmbAdicional,TipoRuta,TipoTrans,TipoServicio,Embarque,CdTipCarga,TipoCarga,DevContenedor,UndCalcFalt,TarifFaltPago,TarifFaltCobro,CdRutaTarif
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NitRepLeg,NomRepLeg,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.Referencia AS AgeReferencia
 FROM  Trn_TraPedido AS P INNER JOIN Trn_TraPedAnexo AS PA ON P.TipDoc=PA.TipDoc  AND P.Pedido=PA.Pedido AND P.IdCia=PA.IdCia
 INNER JOIN Companias AS CN ON P.IdCia=CN.IdCia  INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero 
 INNER JOIN TercCliente AS CLI ON P.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 Terceros AS VN ON P.IdVend=VN.IdTercero  
 INNER JOIN Localidades AS CO ON P.IdLocOrigen=CO.IdLocal  INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON P.IdLocDestino=CD.IdLocal  INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 LEFT JOIN Agencias AS A ON P.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LC ON PA.IdLocCont=LC.IdLocal LEFT JOIN TiposVeh AS TV ON PA.CdTipoVeh=TV.IdTipoVeh
 LEFT JOIN TiposCarga AS TCG ON PA.CdTipCarga=TCG.IdTipCarga
 WHERE P.TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND  @pmFechaFin AND   P.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND P.IdCliente  LIKE ISNULL(@pmIdCliente,'%') AND P.IdVend LIKE ISNULL(@pmIdVend,'%') AND P.Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY P.IdCia,P.Pedido
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraPedidoRelDet]  @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT P.TipDoc AS CdTipDoc,P.Pedido AS NumPedido,P.IdCia AS CdCia,Compania,Fecha,FechaVence,FecDespacho,FecEntrega,IdCliente,NomCliente,T.RazonSocial AS RazonSocialCli,P.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia
,IdClieFact,IdRemitente,NomRemite,IdDestinatario,NomDestino,IdLocOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,IdLocDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,LugarCargue,LugarDescargue,P.IdVend AS CdVend,VN.RazonSocial AS Vendedor,Modalidad,Vigencia,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos
,P.VrDeclarado AS ValDeclarado,P.VrSeguro AS ValSeguro,P.Cantidad AS CantTotal,CantDesp,IdMneda,VrTasa,Cotizacion,IdCiaCot,NumAprob,IdCiaApr,FecAprob,TipOdc,OCargue,IdCiaOdc,FechaOdc,TipRem,Remesa,IdCiaRem,FechaRem,TipFac,Factura,IdCiaFac,FechaFac
,Anulado,FecDev,P.Observacion AS Observ,P.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,P.IdUsuario AS CdUsuario,Usuario
--detalles
,Item,IdMercancia,DescripMcias,D.Cantidad AS Cant,PesoNeto,D.UndMed AS CdUm,UMP.Unidad AS UmPeso,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol,Cases,Cajas,Palets,Remitente,DirOrigen,IdOrigen,LO.Localidad AS CiudadOrigen
,Destinatario,DirDestino,IdDestino,LD.Localidad AS CiudadDestino,TarifClie,TarifPago,TarifTabla,D.VrDeclarado AS DetVrDeclarado,D.VrSeguro AS DetVrSeguro,TarifSeguro,Referencia1,Referencia2,Contenedor1,Contenedor2
,NitRemite,NitDestntario,UndTarifa,UndTarifPago,D.IdUnd AS CdUnd,UM.Unidad AS UndMedida,D.IdEmp AS CdEmp,Empaque,D.IdNat AS CdNat,Natlzaprod,D.IdMnjo AS CdMnjo,ManejoMcia,D.IdTmcia AS CdTmcia,TipoMcia,CdRango,DescripRango
,D.Tipo_Servicio AS TipoServicio,D.CdTipoVehic AS CdTipoVeh,TipoVehiculo
--datos de anexo
,NomContacto,TelContacto,emlContacto,ContacDestino,TelContacDest,emlContacDest,PolizaEsp,NumPolizaEsp,NitCiaPoliza,NomCiaPoliza,FecVencePol,VrLimiteDesp,Seguros,Cargue,Descargue,CdTipoEsc,NitSIA,NombreSIA,ContactoSIA,TelContacSIA
,IdLocCont,LC.Localidad AS CiudadCont,PatioCont,TipoMargen,MargenFalt,EmbAdicional,TipoRuta,TipoTrans,Embarque,CdTipCarga,TipoCarga,DevContenedor,UndCalcFalt,TarifFaltPago,TarifFaltCobro,CdRutaTarif
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NitRepLeg,NomRepLeg,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.Referencia AS AgeReferencia
 FROM  Trn_TraPedido AS P INNER JOIN Trn_TraPedAnexo AS PA ON P.TipDoc=PA.TipDoc  AND P.Pedido=PA.Pedido AND P.IdCia=PA.IdCia
 INNER JOIN Trn_TraPedMcias AS D ON P.TipDoc=D.TipDoc AND P.Pedido=D.Pedido AND P.IdCia=D.IdCia
 INNER JOIN Companias AS CN ON P.IdCia=CN.IdCia  INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero 
 INNER JOIN TercCliente AS CLI ON P.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 Terceros AS VN ON P.IdVend=VN.IdTercero  
 INNER JOIN Localidades AS CO ON P.IdLocOrigen=CO.IdLocal  INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON P.IdLocDestino=CD.IdLocal  INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal  INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal 
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN Empaques AS EMP ON D.IdEmp=EMP.IdEmp
 INNER JOIN TiposNat AS NZ ON D.IdNat=NZ.IdNat INNER JOIN TiposMnjo AS MJ ON D.IdMnjo=MJ.IdMnjo
 INNER JOIN TiposMcia AS TM ON D.IdTmcia=TM.IdTmcia 
 LEFT JOIN Agencias AS A ON P.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LC ON PA.IdLocCont=LC.IdLocal LEFT JOIN TiposVeh AS TV ON D.CdTipoVehic=TV.IdTipoVeh
 LEFT JOIN TiposCarga AS TCG ON PA.CdTipCarga=TCG.IdTipCarga
 LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed LEFT JOIN RangosPeso AS RP ON D.CdRango=RP.IdRango
 WHERE P.TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND  @pmFechaFin AND   P.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND P.IdCliente  LIKE ISNULL(@pmIdCliente,'%') AND P.IdVend LIKE ISNULL(@pmIdVend,'%') AND P.Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY P.IdCia,P.Pedido
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraPedidoRem] @pmTipRem VARCHAR(3),@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT P.TipDoc AS CdTipPed,P.Pedido AS NumPedido,P.IdCia AS CdCia,Compania,Fecha,FechaVence,FecDespacho,FecEntrega,T.TipoId AS TercTipId,IdCliente,T.Dv AS TercDv,T.RazonSocial AS NomCliente
,P.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,P.IdVend AS CdVend,VN.RazonSocial AS Vendedor,Modalidad,Vigencia
,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos
,P.VrDeclarado AS ValDeclarado,P.VrSeguro AS ValSeguro,P.Cantidad AS CantTotal,CantDesp,NumAprob,IdCiaApr,FecAprob
,P.Observacion AS Observ,P.IdEstado AS CdEstado,Estado,P.Anulado AS PedAnulado,TimeSys,P.IdUsuario AS CdUsuario,Usuario
,PolizaEsp,NumPolizaEsp,PA.NitCiaPoliza AS NitCiaPlza,PA.NomCiaPoliza AS NomCiaPlza,PA.FecVencePol AS FecVcePlza,VrLimiteDesp,Seguros,Cargue,Descargue
,IdLocCont,LC.Localidad AS CiudadCont,PatioCont,CdTipoVeh,TV.TipoVehiculo AS TipoVehic,TipoMargen,MargenFalt,EmbAdicional,PA.TipoRuta AS TipRutaPed
,TipoTrans,Embarque,CdTipCarga,TipoCarga,DevContenedor,UndCalcFalt,TarifFaltPago,TarifFaltCobro
,Item,IdMercancia,DescripMcias,D.Cantidad AS CantPed,PesoNeto,D.UndMed AS CdUm,UMP.Unidad AS UmPeso,TarifClie,D.UndTarifa AS UndTarifClie,TarifPago,D.UndTarifPago AS UndTarif_Pago
,TarifTabla,NitRemite,Remitente,DirOrigen,IdOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,NitDestntario,Destinatario,DirDestino,IdDestino,LD.Localidad AS CiudadDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,Referencia1,Referencia2,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol,Cases,Cajas,Palets,D.VrDeclarado AS DetVrDeclarado,D.VrSeguro AS DetVrSeguro,TarifSeguro
,D.IdUnd AS CdUnd,UM.Unidad AS UndMedida,D.IdEmp AS CdEmp,Empaque,D.IdNat AS CdNat,Natlzaprod,D.IdMnjo AS CdMnjo,ManejoMcia,D.IdTmcia AS CdTmcia,TipoMcia,CdRango,DescripRango
--remesas
,CdTipRem,NumRemesa,CdCiaRem,NomCiaRem,FecRemesa,FecDespRem,FecEntgaRem,PlacaVeh,NumVeh,CdTipVeh,RM.TipoVehiculo AS TipoVehicRem,CedConductor,NomConductor
,nRemolque,TipoAfiVehic,TipoRemesa,NumManif,IdCiaManif,EstCumplido,EstFactura,ManifMintrans,RemMintrans,RM.TipoRuta AS TipoRutaRem,TipoMintrans
,CantRemesa,PesoNetoRem,CdUmPeso,UmPeso,CasesRem,CajasRem,PaletsRem,TarifClieRem,UndTarifRem,TarifPagoRem,TarifTablaRem,UndTarifPagoRem
,NitRemteRem,NomRemteRem,DirOrigenRem,CdCiuOrigRem,CiudadOrigen,CodDepOrigen,DptoOrigen
,NitDtnatario,NomDestnatario,DirDestinoRem,CdDestinoRem,CiudadDestino,CodDepDestino,DptoDestino
,Remision,DocClie,Refencia1,Refencia2,Refencia3,VlmenRem,CdUndVol,DetVrDeclarado,DetVrSeguro,TarifSegRem
,Cumplido,IdCiaCump,FechaCump,RM.TipFac AS TipFact,RM.Factura AS NumFact,RM.IdCiaFac AS CdCiaFact,RM.FechaFac AS FecFact,TarifClieFac,TipOdp,NumeroOdp,IdCiaOdp,TarifOdp
 FROM  Trn_TraPedido AS P INNER JOIN Trn_TraPedAnexo AS PA ON P.TipDoc=PA.TipDoc  AND P.Pedido=PA.Pedido AND P.IdCia=PA.IdCia
 INNER JOIN Trn_TraPedMcias AS D ON P.TipDoc=D.TipDoc AND P.Pedido=D.Pedido AND P.IdCia=D.IdCia
 INNER JOIN Companias AS CN ON P.IdCia=CN.IdCia  INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero 
 INNER JOIN TercCliente AS CLI ON P.IdCliente=CLI.IdClie  INNER JOIN Terceros AS VN ON P.IdVend=VN.IdTercero  
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN Empaques AS EMP ON D.IdEmp=EMP.IdEmp
 INNER JOIN TiposNat AS NZ ON D.IdNat=NZ.IdNat INNER JOIN TiposMnjo AS MJ ON D.IdMnjo=MJ.IdMnjo
 INNER JOIN TiposMcia AS TM ON D.IdTmcia=TM.IdTmcia 
 LEFT JOIN Agencias AS A ON P.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LC ON PA.IdLocCont=LC.IdLocal LEFT JOIN TiposVeh AS TV ON PA.CdTipoVeh=TV.IdTipoVeh
 LEFT JOIN TiposCarga AS TCG ON PA.CdTipCarga=TCG.IdTipCarga
 LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed LEFT JOIN RangosPeso AS RP ON D.CdRango=RP.IdRango
--consultas de remesas -detalles
 LEFT JOIN (SELECT R.TipDoc AS CdTipRem,R.NumOrden AS NumRemesa,R.IdCia AS CdCiaRem,CN.Compania AS NomCiaRem,R.Fecha AS FecRemesa,R.FecDespacho AS FecDespRem,R.FecEntrega AS FecEntgaRem
 ,R.IdCliente AS NitClieRem,NomCliente,R.IdAgencia AS CdAgeRem,A.Agencia AS NomAgenRem,R.IdVehiculo AS PlacaVeh,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo
 ,R.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,TipoAfiVehic,R.Modalidad AS TipoRemesa,NumPedido,IdCiaPed,NumManif,IdCiaManif,EstCumplido,EstFactura
,TomadorPoliza,PolizaSeguro,NitCiaPoliza,NomCiaPoliza,FecVencePol,ManifMintrans,RemMintrans,TipoRuta,TipoMintrans
 ,D.IdMercancia AS CdMcia,D.DescripMcias AS DescMcia,D.Cantidad AS CantRemesa,D.PesoNeto AS PesoNetoRem,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,D.Cases AS CasesRem,D.Cajas AS CajasRem,D.Palets AS PaletsRem
 ,D.TarifClie AS TarifClieRem,D.UndTarifa AS UndTarifRem,D.TarifPago AS TarifPagoRem,D.TarifTabla AS TarifTablaRem,D.UndTarifPago AS UndTarifPagoRem
 ,D.NitRemite AS NitRemteRem,D.Remitente AS NomRemteRem,D.DirOrigen AS DirOrigenRem,D.IdOrigen AS CdCiuOrigRem,LO.Localidad AS CiudadOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
 ,D.NitDestntario AS NitDtnatario,D.Destinatario AS NomDestnatario,D.DirDestino AS DirDestinoRem,D.IdDestino AS CdDestinoRem,LD.Localidad AS CiudadDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
 ,Remision,D.DocCliente AS DocClie,D.Referencia1 AS Refencia1,D.Referencia2 AS Refencia2,D.Referencia3 AS Refencia3,D.Volumen AS VlmenRem,D.UndVol AS CdUndVol,D.VrDeclarado AS DetVrDeclarado,D.VrSeguro AS DetVrSeguro,D.TarifSeguro AS TarifSegRem
 ,Cumplido,IdCiaCump,FechaCump,TipFac,Factura,IdCiaFac,FechaFac,TarifClieFac,TipOdp,NumeroOdp,IdCiaOdp,TarifOdp
 FROM Trn_TraRemesa AS R INNER JOIN Trn_TraRemAnexo AS RA ON R.TipDoc=RA.TipDoc  AND R.NumOrden=RA.NumOrden AND R.IdCia=RA.IdCia
 INNER JOIN Trn_TraRemMcias AS D ON R.TipDoc=D.TipDoc  AND R.NumOrden=D.NumOrden AND R.IdCia=D.IdCia
 INNER JOIN Companias AS CN ON R.IdCia=CN.IdCia INNER JOIN Terceros AS  CDT ON R.IdConductor=CDT.IdTercero 
 INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 LEFT JOIN Agencias AS A ON R.IdAgencia=A.IdAgencia LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed 
 WHERE R.TipDoc=@pmTipRem AND R.IdCliente LIKE ISNULL(@pmIdCliente,'%') ) AS RM ON P.Pedido=RM.NumPedido AND P.IdCia=RM.IdCiaPed AND D.IdMercancia=RM.CdMcia
WHERE P.TipDoc='PDT' AND P.Anulado=0  AND P.IdCia  LIKE ISNULL(@pmIdCia ,'%%') AND P.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND P.Modalidad LIKE ISNULL(@pmModalidad,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoOCargue,Remolque,NumVerifica,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,K.Temperatura As Temdetalle,galsneto ,Densidad ,codsicom
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoOCargue,Remolque,Anulado,FecDev,Observacion,IdEstado,NumVerifica,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Guia
 WHERE TipDoc=@pmTipDoc AND Guia=@pmGuia AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@pmTipoTemp VARCHAR(10),@pmTemperatura DECIMAL(14,4),@pmTipoOCargue INT,@pmRemolque VARCHAR(10),@pmNumVerifica INT,@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,TipoOCargue=@pmTipoOCargue,Remolque=@pmRemolque,NumVerifica=@pmNumVerifica,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Guia=@pmGuia AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT
,@pmTipoTemp VARCHAR(10),@pmTemperatura DECIMAL(14,4),@pmTipoOCargue INT,@pmRemolque VARCHAR(10),@pmNumVerifica INT,@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,Remolque,Anulado,FecDev
,Observacion,IdEstado,CdCiuOrigen,TimeSys,IdCiaCrea,IdUsuario,TipoOCargue,NumVerifica)
 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,@pmRemolque,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmCdCiuOrigen,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmTipoOCargue,@pmNumVerifica)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoOCargue,Remolque,NumVerifica,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoOCargue,Remolque,NumVerifica,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoOCargue,Remolque,NumVerifica,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelJurClaseAcc] @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM JurClaseAcc
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpJurClaseAcc] @pmIdClase VARCHAR(4),@pmClaseAccdte VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE JurClaseAcc SET ClaseAccdte=@pmClaseAccdte,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsJurClaseAcc] @pmIdClase VARCHAR(4),@pmClaseAccdte VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO JurClaseAcc (IdClase,ClaseAccdte,Inactivo)
 VALUES (@pmIdClase,@pmClaseAccdte,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpEmp_Parientes] @pmIdEmpleado VARCHAR(16),@pmItem INT,@pmParntsco VARCHAR(10),@pmNombre VARCHAR(50)
,@pmFecNacmto SMALLDATETIME,@pmUltGrado VARCHAR(20),@pmDirccion VARCHAR(100),@pmTelefno VARCHAR(20),@pmTipoRef VARCHAR(10),@pmNitRef VARCHAR(16)
,@pmEmailRef VARCHAR(100),@pmCargoRef VARCHAR(50)
 AS 
 UPDATE Emp_Parientes SET Parntsco=@pmParntsco,Nombre=@pmNombre,FecNacmto=@pmFecNacmto,UltGrado=@pmUltGrado,Dirccion=@pmDirccion,Telefno=@pmTelefno
,TipoRef=@pmTipoRef,NitRef=@pmNitRef,EmailRef=@pmEmailRef,CargoRef=@pmCargoRef
 WHERE IdEmpleado=@pmIdEmpleado AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsEmp_Parientes] @pmIdEmpleado VARCHAR(16),@pmItem INT,@pmParntsco VARCHAR(10),@pmNombre VARCHAR(50)
,@pmFecNacmto SMALLDATETIME,@pmUltGrado VARCHAR(20),@pmDirccion VARCHAR(100),@pmTelefno VARCHAR(20),@pmTipoRef VARCHAR(10),@pmNitRef VARCHAR(16)
,@pmEmailRef VARCHAR(100),@pmCargoRef VARCHAR(50)
 AS 
 INSERT INTO Emp_Parientes (IdEmpleado,Item,Parntsco,Nombre,FecNacmto,UltGrado,Dirccion,Telefno,TipoRef,NitRef,EmailRef,CargoRef)
 VALUES (@pmIdEmpleado,@pmItem,@pmParntsco,@pmNombre,@pmFecNacmto,@pmUltGrado,@pmDirccion,@pmTelefno,@pmTipoRef,@pmNitRef,@pmEmailRef,@pmCargoRef)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEmp_ParientesDso] @pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT P.IdEmpleado AS IdEmpldo,Apellidos,Nombres,Item,Parntsco,Nombre,P.FecNacmto AS FechaNaci,UltGrado,Dirccion,Telefno
,TipoRef,NitRef,EmailRef,CargoRef
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEmp_Parientes] @pmIdEmpleado VARCHAR(16),@pmItem INT
 AS 
 SELECT IdEmpleado,Item,Parntsco,Nombre,FecNacmto,UltGrado,Dirccion,Telefno,TipoRef,NitRef,EmailRef,CargoRef
 FROM Emp_Parientes
 WHERE IdEmpleado=@pmIdEmpleado  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647)) 
 ORDER BY Item
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryEmpleadosFam] @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,E.IdEmpleado,IdLugarCed,LC.Localidad AS LugarCed,E.Direccion AS Dirccion,E.IdLocal AS CodCiu,L.Localidad AS Ciudad,Telefono,TelMovil,e_mail,VvdaPropia,E.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,FecUltParc,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo,DecRenta,NivelEduc,FotoEmp,FotoLic,FotoFirma,Observacion,E.IdEstado AS IdEstdo,Estado,E.Inactivo AS Inctivo,Dv,Codigo,NColor,FechaAdd,FechaUpdate,E.IdUsuario AS IdUsuari,Usuario,CdFonAvp
--familiares
,Item,Parntsco,Nombre,EF.FecNacmto AS FechaNcmto,UltGrado,Dirccion,Telefno
 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
 LEFT JOIN Emp_Parientes AS EF ON E.IdEmpleado=EF.IdEmpleado
 WHERE E.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 (E.FecNacmto>=ISNULL(@pmFecNacmtoIni,CAST('19100101' AS SMALLDATETIME)) AND E.FecNacmto<=ISNULL(@pmFecNacmtoFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Apellidos,Nombres
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraCentrega] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipRem VARCHAR(3),@pmtmRemesa INT,@pmtmIdCiaRem CHAR(2),@pmtmVrTotal MONEY
,@pmtmVrCosto MONEY,@pmtmVrDeclarado MONEY,@pmtmVrSeguro MONEY,@pmtmTipoVehic VARCHAR(10),@pmtmCdConc VARCHAR(4),@pmtmCdCuenta VARCHAR(16),@pmtmCdCueIng VARCHAR(16)
,@pmtmCdCueSeg VARCHAR(16),@pmtmCdCueOcg VARCHAR(16),@pmtmCdCueOdc VARCHAR(16),@pmtmCdCiudad VARCHAR(8)
 AS 
 INSERT INTO tm_TraCentrega (tmNumero,tmItem,tmTipRem,tmRemesa,tmIdCiaRem,tmVrTotal,tmVrCosto,tmVrDeclarado,tmVrSeguro,tmTipoVehic,tmCdConc,tmCdCuenta,tmCdCueIng,tmCdCueSeg,tmCdCueOcg,tmCdCueOdc,tmCdCiudad)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipRem,@pmtmRemesa,@pmtmIdCiaRem,@pmtmVrTotal,@pmtmVrCosto,@pmtmVrDeclarado,@pmtmVrSeguro,@pmtmTipoVehic,@pmtmCdConc,@pmtmCdCuenta
,@pmtmCdCueIng,@pmtmCdCueSeg,@pmtmCdCueOcg,@pmtmCdCueOdc,@pmtmCdCiudad)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,NumTurno,TipoInt,VrDctoClie,VrExcedClie,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTipoInt VARCHAR(10),@pmVrDctoClie MONEY,@pmNumTurno INT,@pmVrExcedClie MONEY,@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,TipoInt=@pmTipoInt,VrDctoClie=@pmVrDctoClie,NumTurno=@pmNumTurno,VrExcedClie=@pmVrExcedClie
 WHERE TipDoc=@pmTipDoc AND Planilla=@pmPlanilla AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrDctoClie,VrExcedClie
,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,TipoInt,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEdsFormasVC] @pmTipDcm VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2)
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.VrPrecio AS VrPrecioPub,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 TipDcm=@pmTipDcm AND Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin AND IdCiaDcm=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,K.pVehiculo AS KarPlacaVeh
,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,K.pVehiculo
,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,K.pVehiculo
,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,K.pVehiculo
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEdsFormasRel] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT F.TipoRango,F.NumSerie,F.NumForma,F.Planilla AS NumPlanilla,F.IdCia AS CdCia,Compania,F.Fecha AS FechaVale,F.VrTotal,F.IdCliente,T.RazonSocial AS NomCliente
,F.IdAgencia AS Id_Agencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,F.pVehiculo,F.nVehiculo,F.IdCajero AS CdCajero,Usuario,F.IdVend AS NitVend,V.RazonSocial AS Vendedor
,F.CdProducto,DescripProd,F.Cantidad AS Cant,F.VrPrecio AS VrPrecioPub,F.IdCuenta AS CodCuenta,NomCuenta,F.IdForma AS CdForma,FormaPago,F.Referencia AS Referncia,F.NumRef2,F.Observacion AS Observ
,F.TipDcm,F.Documento,F.IdCiaDcm,F.FechaDcm,AgencDcm,AD.Agencia AS DocAgencia,F.Anulado AS EstaAnulado,F.FecDev AS FechaDev
,F.IdRango,F.Item,CP.IdJornada AS CdJornada,Jornada,NumCorte,FecCorte
,ISNULL(D.Cant_Neto,0) AS Cant_Neto,ISNULL(D.Cant_Dev,0) AS Cant_Dev,(ISNULL(D.STOTDEV,0)-ISNULL(D.SDCTDEV,0)+ISNULL(D.SIVADEV,0)) AS TotalDev
--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,DP.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 DP ON L.IdDep=DP.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
 LEFT JOIN (SELECT TipoRango,IdRango,NumSerie,NumForma,SUM(Cantidad-CantDev) AS Cant_Neto,SUM(CantDev) AS Cant_Dev,SUM((Cantidad-CantDev)*VrPrecio) AS SUBTOTAL
,SUM(CASE WHEN (CantDev>0 AND TarifaDct>0 AND VrDcto>0) THEN (((Cantidad-CantDev)*VrPrecio)*TarifaDct)/100 ELSE VrDcto END) AS STOTDCT
,SUM(CASE WHEN (CantDev>0 AND TarifaIva>0 AND VrIva>0) THEN ((((Cantidad-CantDev)*VrPrecio)-((((Cantidad-CantDev)*VrPrecio)*TarifaDct)/100))*TarifaIva)/100 ELSE VrIva END) AS STOTIVA
,SUM(CantDev*VrPrecio) AS STOTDEV,SUM(((CantDev*VrPrecio)*TarifaDct)/100) AS SDCTDEV
,SUM((((CantDev*VrPrecio)-(((CantDev*VrPrecio)*TarifaDct)/100))*TarifaIva)/100) AS SIVADEV
,SUM(Cantidad) AS Det_Cant,SUM(Cantidad*VrPrecio) AS Det_Total,SUM(VrDcto) AS Det_VrDcto,SUM(VrIva) AS Det_VrIva
FROM Trn_EdsForItems GROUP BY TipoRango,IdRango,NumSerie,NumForma) AS D ON F.TipoRango=D.TipoRango AND F.IdRango=D.IdRango AND F.NumSerie=D.NumSerie AND F.NumForma=D.NumForma
 WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (F.IdCia=@pmIdCia OR @pmIdCia IS NULL)
 AND (F.IdCliente=@pmIdCliente OR @pmIdCliente IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEdsCortesCor] @pmFecCorte SMALLDATETIME,@pmNumCorte INT,@pmModalidad VARCHAR(10)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 SELECT TipDoc,Planilla,IdCia,Fecha,Modalidad,FecCorte,NumCorte,NumTurno,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrDctoClie,VrExcedClie
,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,NumTurno,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,TipoInt,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,NumTurno,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TipoInt,VrDctoClie,VrExcedClie,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_EdsCortes
 WHERE TipDoc=@pmTipDoc AND Planilla=@pmPlanilla AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTipoInt VARCHAR(10),@pmVrDctoClie MONEY,@pmNumTurno INT,@pmVrExcedClie MONEY
,@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,TipoInt,VrDctoClie,NumTurno,VrExcedClie)
 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,@pmTipoInt,@pmVrDctoClie,@pmNumTurno,@pmVrExcedClie)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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.CdProducto,FE_Ticket,Cant_Gals,PrecioBase,PrecioUnit,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,@pmFechaVigRes SMALLDATETIME,@pmClaveTecDian VARCHAR(250),@pmCodResol VARCHAR(4),@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
,FechaVigRes=@pmFechaVigRes,ClaveTecDian=@pmClaveTecDian,CodResol=@pmCodResol
 WHERE IdDoc=@pmIdDoc AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,FechaVigRes,ClaveTecDian,N.CodResol,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,FechaVigRes,ClaveTecDian,CodResol,FechaAdd,FechaUpdate
 FROM TiposDocCons
 WHERE IdDoc=@pmIdDoc AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,@pmFechaVigRes SMALLDATETIME,@pmClaveTecDian VARCHAR(250),@pmCodResol VARCHAR(4),@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,FechaVigRes,ClaveTecDian,CodResol)
 VALUES (@pmIdDoc,@pmIdCia,@pmLDesde,@pmLHasta,@pmResolucion,@pmRangoNum,@pmFechaRes,@pmPrefijo,@pmNumero
,@pmNumManual,@pmIntLotes,@pmConfigFecha,@pmFormato,@pmTipoPapel,@pmOrientacion,@pmVistaPrevia,@pmVerSetup,@pmNumCopias,@pmFechaAdd,@pmFechaVigRes,@pmClaveTecDian,@pmCodResol)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposDocCons] @pmIdDoc VARCHAR(3),@pmIdCia CHAR(2)
 AS 
 DELETE FROM TiposDocCons
 WHERE IdDoc=@pmIdDoc AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsProdPrecCrudo] @pmTipoTarif VARCHAR(3),@pmNumero INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16),@pmIdTercero VARCHAR(16),@pmDuracion VARCHAR(10),@pmFecInicio SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmTarifa DECIMAL(16,6)
,@pmIdMoneda VARCHAR(5),@pmUndMed VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO ProdPrecCrudo (TipoTarif,Numero,IdCia,IdProducto,IdTercero,Duracion,FecInicio,FecFinal,Tarifa,IdMoneda,UndMed,Inactivo)
 VALUES (@pmTipoTarif,@pmNumero,@pmIdCia,@pmIdProducto,@pmIdTercero,@pmDuracion,@pmFecInicio,@pmFecFinal,@pmTarifa,@pmIdMoneda,@pmUndMed,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpProdPrecCrudo] @pmTipoTarif VARCHAR(3),@pmNumero INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16),@pmIdTercero VARCHAR(16),@pmDuracion VARCHAR(10),@pmFecInicio SMALLDATETIME,@pmFecFinal SMALLDATETIME
,@pmTarifa DECIMAL(16,6),@pmIdMoneda VARCHAR(5),@pmUndMed VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE ProdPrecCrudo SET IdCia=@pmIdCia,IdProducto=@pmIdProducto,IdTercero=@pmIdTercero,Duracion=@pmDuracion,FecInicio=@pmFecInicio,FecFinal=@pmFecFinal,Tarifa=@pmTarifa,IdMoneda=@pmIdMoneda,UndMed=@pmUndMed,Inactivo=@pmInactivo
 WHERE TipoTarif=@pmTipoTarif AND Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryRequisicionMtto] @pmTipDoc VARCHAR(3),@pmRequisicionIni INT,@pmRequisicionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT R.TipDoc AS CdTipo,TipoDoc,R.Requisicion,R.IdCia AS CdCia,Compania,Fecha,FechaVence,R.IdConcepto AS CdConcepto,Concepto,R.IdRespons AS CdOperario,T.RazonSocial AS NomOperario
,R.IdCCosto AS CdCCosto,CCosto,R.IdSubCos AS CdSubCos,R.IdDep AS CdDep,Dependencia,VrSubTotal,R.Cantidad AS CantTotal,R.NContrato AS NumOrden,R.IdCiaCont AS CdCiaOrden,R.Num_Vehic,R.Num_Trailer
,R.NitCliente AS NitCentServ,CL.RazonSocial AS NomCentServ,R.Modalidad,DirEntrega,R.IdLocEnt,Localidad,Departamento,NumAprob,FecAprob,CdUsuAprob,OrigenAdd,R.Anulado
,NomContacto,TelsContacto,EmailContacto,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,R.TimeSys AS FecCrea,R.FecUpdate,R.IdCiaCrea,R.IdUsuario AS CdUsuario,U.Usuario
,Item,TipoItem,D.IdProducto AS CdProducto,D.Descripcion,DescripProd,D.Cantidad,D.IdUnd AS CdUnd,Unidad,D.VrUnitario,D.CdBodega,Bodega,TipoRef,D.TipOdt,D.NumOrden AS Num_Orden,D.IdCiaOdt,EstadoReq
,D.TipSal,D.NumSalida,D.IdCiaSal,D.CantSalida,D.IdOperario AS DetIdOperario,NP.RazonSocial AS DetOperario,D.CdCenServ,CentroServ,D.NitTercero,NC.RazonSocial
,D.NumVehic,D.NumParte,D.VrPrecioUnd,D.Observacion AS DetObserv,D.FechaCrea AS DetFecCrea,D.IdUsuario AS DetCdUsuario,DU.Usuario AS DetUsuario
,T.TipoId,T.Codigo,T.Dv,T.Telefono,T.TelMovil,T.e_mail
 FROM Trn_Requisicion AS R INNER JOIN Trn_MttoOrdenReq AS D ON R.TipDoc=D.TipReq AND R.Requisicion=D.Requisicion AND R.IdCia=D.IdCia
 INNER JOIN Terceros AS T ON R.IdRespons=T.IdTercero INNER JOIN Conceptos AS C ON R.IdConcepto=C.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 CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto INNER JOIN Dependencias AS DP ON R.IdDep=DP.IdDep
 INNER JOIN Localidades AS L ON R.IdLocEnt=L.IdLocal INNER JOIN Departamentos AS DT ON L.IdDep=DT.IdDep
 INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd
 LEFT JOIN Terceros AS CL ON R.NitCliente =CL.IdTercero LEFT JOIN Terceros AS NP ON D.IdOperario=NP.IdTercero
 LEFT JOIN Terceros AS NC ON D.NitTercero=NC.IdTercero LEFT JOIN Bodegas AS BG ON D.CdBodega=BG.IdBodega
 LEFT JOIN CentrosServ AS CS ON D.CdCenServ=CS.IdCenSer LEFT JOIN adm_Usuarios AS DU ON D.IdUsuario=DU.IdUsuario 
 WHERE R.TipDoc=@pmTipDoc AND R.Requisicion BETWEEN  @pmRequisicionIni AND @pmRequisicionFin AND R.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryCentrosServ] @pmIdCenSer VARCHAR(4)
 AS 
 SELECT IdCenSer,CentroServ,DirCentro,IdLocal,Telefonos,TipoCentro,NitResp,Respnsable,Inactivo
 FROM CentrosServ
 WHERE IdCenSer=@pmIdCenSer
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,kmRuta,kmOtro,EquipoAdic,Descripcion,CdCenSer,CentroServ,CdLocal,LU.Localidad AS CiuUbicacion
,LU.IdDep AS CodDpto,Departamento,Ubicacion,R.Observacion AS Observ,R.ArcFotoRem,R.IdEstado AS CodEstado,Estado,R.Inactivo AS Inactiv,R.IdUsuario AS IdUsuari,Usuario
,FecAdd,FecUpdate,EV.NColor AS NumColor,OutDemand,TarjetaProp,FecTarjProp,VigTarjProp,CdLocTarj,Hidrostatica,FecPruebaHid,VigPruebaHid,Kingpin,FecKingpin,VigKingpin,FecVigExtintor
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMttoOrdenReqFmt] @pmTipOdt VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCiaOdt CHAR(2)
 AS 
 SELECT R.TipDoc,R.Requisicion,R.IdCia AS CdCia,Compania,R.Fecha,Item,TipoItem,D.IdProducto AS CdProducto,D.Descripcion,DescripProd
,D.Cantidad,D.IdUnd AS CdUnd,Unidad,D.VrUnitario,D.CdBodega,Bodega,TipoRef,D.TipOdt,D.NumOrden AS Num_Orden,D.IdCiaOdt,EstadoReq
,D.TipSal,D.NumSalida,D.IdCiaSal,D.CantSalida,D.IdOperario AS CedOperario,T.RazonSocial AS Operario,D.CdCenServ,CentroServ
,D.NitTercero,NC.RazonSocial,D.NumVehic,D.NumParte,D.Observacion AS Observ,D.CdConcServ,Concepto,D.VrPrecioUnd,D.FechaCrea,D.IdUsuario AS CdUsuario,U.Usuario
,R.Modalidad,NumAprob,FecAprob,CdUsuAprob,CN.TarifaClie
,T.TipoId,T.Codigo,T.Dv,T.Telefono,T.TelMovil,T.e_mail
 FROM Trn_MttoOrdenReq AS D INNER JOIN Trn_Requisicion AS R ON D.TipReq=R.TipDoc AND D.Requisicion=R.Requisicion AND D.IdCia=R.IdCia
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd LEFT JOIN Terceros AS T ON D.IdOperario=T.IdTercero
 LEFT JOIN Terceros AS NC ON D.NitTercero=NC.IdTercero LEFT JOIN Bodegas AS BG ON D.CdBodega=BG.IdBodega
 LEFT JOIN CentrosServ AS CS ON D.CdCenServ=CS.IdCenSer LEFT JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 LEFT JOIN MttoConceptos AS CN ON D.CdConcServ=CN.IdConc
 WHERE D.TipOdt=@pmTipOdt AND D.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND D.IdCiaOdt=@pmIdCiaOdt
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelCentrosServ] @pmIdCenSer VARCHAR(4)
 AS 
 DELETE FROM CentrosServ
 WHERE IdCenSer=@pmIdCenSer
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[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,CdTipoPro,TipoProceso,CdCausal,Causal,Cedtraparte,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
 LEFT JOIN JurTipos AS TP ON C.CdTipoPro=TP.IdTipoPro LEFT JOIN JurCausales AS CS ON C.CdCausal=CS.IdCausal
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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
,CdTipoPro,TipoProceso,CdCausal,Causal,Cedtraparte,Contraparte, DirContraparte,TelContraparte,ArchivoCaso,C.Observacion AS Observ,ActualizaID, FechaUp, Historial, EstadoAct, Pruebas, Documntos, CierreProc, ArchivoProc,CedContparte, 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 
 LEFT JOIN JurTipos AS TP ON C.CdTipoPro=TP.IdTipoPro LEFT JOIN JurCausales AS CS ON C.CdCausal=CS.IdCausal
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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
,CdTipoPro,TipoProceso,CdCausal,Causal,C.Observacion AS Observ, Cedtraparte,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
 LEFT JOIN JurTipos AS TP ON C.CdTipoPro=TP.IdTipoPro LEFT JOIN JurCausales AS CS ON C.CdCausal=CS.IdCausal
 WHERE  TipDoc=@pmTipDoc AND CasoID BETWEEN  @pmCasoIDIni AND @pmCasoIDFin 
 AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%')
ORDER BY C.IdCia,CasoID
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,Cedtraparte,CdTipoPro,CdCausal,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_JurInvestiga
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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,@pmCedtraparte VARCHAR(16)
AS
 UPDATE  Trn_JurInvestiga  SET Contraparte=@pmContraparte, DirContraparte=@pmDirContraparte,TelContraparte=@pmTelContraparte
,Cedtraparte=@pmCedtraparte,IdEstado=ISNULL(@pmIdEstado,IdEstado),FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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,Cedtraparte,CdTipoPro,CdCausal,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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, CdTipoPro,TipoProceso,CdCausal,Causal,Cedtraparte,Contraparte, DirContraparte,TelContraparte,ArchivoCaso,ActualizaID, FechaUp, Historial, EstadoAct, Pruebas, Documntos, CierreProc, ArchivoProc,CedContparte, 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 
 LEFT JOIN JurTipos AS TP ON C.CdTipoPro=TP.IdTipoPro LEFT JOIN JurCausales AS CS ON C.CdCausal=CS.IdCausal
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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, CdTipoPro,TipoProceso,CdCausal,Causal,Cedtraparte,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
 LEFT JOIN JurTipos AS TP ON C.CdTipoPro=TP.IdTipoPro LEFT JOIN JurCausales AS CS ON C.CdCausal=CS.IdCausal
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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)
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND  IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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)
,@pmCedtraparte VARCHAR(16),@pmCdTipoPro VARCHAR(4),@pmCdCausal 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,Cedtraparte=@pmCedtraparte,CdTipoPro=@pmCdTipoPro,CdCausal=@pmCdCausal
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCedtraparte VARCHAR(16),@pmCdTipoPro VARCHAR(4)
,@pmCdCausal 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,Cedtraparte,CdTipoPro,CdCausal)
 VALUES (@pmTipDoc,@pmCasoID,@pmIdCia,@pmFecha,@pmRadicacion,@pmIdAbogado,@pmIdAuxiliar,@pmIdAutoridad,@pmIdEmpresa,@pmObservacion,@pmContraparte,@pmDirContraparte,@pmTelContraparte,@pmArchivoCaso,@pmAnulado,@pmFecDev
,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmCedtraparte,@pmCdTipoPro,@pmCdCausal)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTipoCompNiif VARCHAR(3),@pmNumCompNiif INT
,@pmTipoCompFis VARCHAR(3),@pmNumCompFis INT,@pmFecCreacion SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_AfMovimiento (IdMov,Numero,IdCia,Fecha,Documento,Anulado,Observacion,TipCom,Comprobante,IdCiaCom,FecCreacion,IdUsuario,TipoCompNiif,NumCompNiif,TipoCompFis,NumCompFis)
 VALUES (@pmIdMov,@pmNumero,@pmIdCia,@pmFecha,@pmDocumento,@pmAnulado,@pmObservacion,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmFecCreacion,@pmIdUsuario,@pmTipoCompNiif,@pmNumCompNiif,@pmTipoCompFis,@pmNumCompFis)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryAfMovimiento] @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdMov,Numero,IdCia,Fecha,Documento,Anulado,Observacion,TipCom,Comprobante,IdCiaCom
,TipoCompNiif,NumCompNiif,TipoCompFis,NumCompFis,FecCreacion,IdUsuario
 FROM Trn_AfMovimiento
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasVeht_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum VARCHAR(3),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuNiifCuentasVeht (nAnno,nMes,IdCia,IdCuenta,IdTercero,IdVehiculo,TipoAfiVehic,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdTercero,S.IdVehiculo,S.TipoAfiVehic,S.TipoAcum,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuNiifCuentasVeht AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt AND S.TipoAcum=@pmTipoAcum
 AND NOT EXISTS (SELECT * FROM AcuNiifCuentasVeht AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes AND AC.TipoAcum=@pmTipoAcum
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdTercero=S.IdTercero AND AC.IdVehiculo=S.IdVehiculo AND AC.TipoAfiVehic=S.TipoAfiVehic)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuNiifCuentasVehtSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum VARCHAR(3),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuNiifCuentasVeht SET AcuNiifCuentasVeht.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuNiifCuentasVeht INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,IdVehiculo,TipoAfiVehic,TipoAcum,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuNiifCuentasVeht WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt AND TipoAcum=@pmTipoAcum) AS SA ON AcuNiifCuentasVeht.IdCia=SA.IdCia AND AcuNiifCuentasVeht.IdCuenta=SA.IdCuenta
 AND AcuNiifCuentasVeht.IdTercero=SA.IdTercero AND AcuNiifCuentasVeht.IdVehiculo=SA.IdVehiculo AND AcuNiifCuentasVeht.TipoAfiVehic=SA.TipoAfiVehic AND AcuNiifCuentasVeht.TipoAcum=SA.TipoAcum
 WHERE AcuNiifCuentasVeht.nAnno=@pmnAnno AND AcuNiifCuentasVeht.nMes=@pmnMes AND AcuNiifCuentasVeht.TipoAcum=@pmTipoAcum
 AND (@pmIdCia IS NULL OR AcuNiifCuentasVeht.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuNiifCuentasVeht.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrPstoAnual
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,TipoFactInt,TipoFE,FechaFE,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,CodTarRtc,TarifaRtc
,F.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
,FE.EstadoFE,FE.Prefijo,FE.NumFace,FE.CUFE,FE.Resolucion,FE.RangoNum,FE.FecVigencia,FE.CUFE_QR AS QR,FE.FechaValidacion
 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
 LEFT JOIN Trn_Face AS FE ON F.TipDoc=FE.TipDoc AND F.Factura=FE.Documento AND F.IdCia=FE.IdCia
 WHERE F.TipDoc=@pmTipDoc  AND  F.Factura  BETWEEN  @pmFacturaIni AND @pmFacturaFin  AND F.IdCia=@pmIdCia
 ORDER BY  F.Factura
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,D.VrNeto,D.TarifaArf,D.VrAutRetFte,D.TarifaAri,D.VrAutRetIca,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,D.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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMttoEstados] @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 UPDATE MttoEstados SET Estado=@pmEstado,NColor=@pmNColor,Inactivo=@pmInactivo
 WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMttoEstados] @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM MttoEstados WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMttoEstados] @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 INSERT INTO MttoEstados (IdEstado,Estado,NColor,Inactivo)
 VALUES (@pmIdEstado,@pmEstado,@pmNColor,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoEstados] @pmIdEstado VARCHAR(4)
 AS 
 IF @pmIdEstado IS NULL
  BEGIN
  SELECT IdEstado,Estado,NColor FROM MttoEstados WHERE Inactivo=0
  END 
 ELSE
  BEGIN
   SELECT IdEstado,Estado,NColor,Inactivo FROM MttoEstados WHERE IdEstado=@pmIdEstado
  END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpNiifPoliticas] @pmIdPolitica INT,@pmDescripcion VARCHAR(250),@pmClasePol VARCHAR(100),@pmObjetivo VARCHAR(1000),@pmAlcance VARCHAR(2500),@pmReconocimiento VARCHAR(2500),@pmMed_Inicial VARCHAR(2000)
,@pmMed_Final VARCHAR(2000),@pmPdoVigencia INT,@pmVigencia VARCHAR(250),@pmNotas VARCHAR(1000),@pmAnexo VARCHAR(150),@pmInactivo BIT
 AS 
 UPDATE Trn_NiifPoliticas SET Descripcion=@pmDescripcion,ClasePol=@pmClasePol,Objetivo=@pmObjetivo,Alcance=@pmAlcance,Reconocimiento=@pmReconocimiento,Med_Inicial=@pmMed_Inicial,Med_Final=@pmMed_Final
,PdoVigencia=@pmPdoVigencia,Vigencia=@pmVigencia,Notas=@pmNotas,Anexo=@pmAnexo,Inactivo=@pmInactivo
 WHERE IdPolitica=@pmIdPolitica
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsNiifPoliticas] @pmIdPolitica INT,@pmDescripcion VARCHAR(250),@pmClasePol VARCHAR(100),@pmObjetivo VARCHAR(1000),@pmAlcance VARCHAR(2500),@pmReconocimiento VARCHAR(2500),@pmMed_Inicial VARCHAR(2000)
,@pmMed_Final VARCHAR(2000),@pmPdoVigencia INT,@pmVigencia VARCHAR(250),@pmNotas VARCHAR(1000),@pmAnexo VARCHAR(150),@pmInactivo BIT,@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NiifPoliticas (IdPolitica,Descripcion,ClasePol,Objetivo,Alcance,Reconocimiento,Med_Inicial,Med_Final,PdoVigencia,Vigencia,Notas,Anexo,Inactivo,FechaCrea,IdUsuario)
 VALUES (@pmIdPolitica,@pmDescripcion,@pmClasePol,@pmObjetivo,@pmAlcance,@pmReconocimiento,@pmMed_Inicial,@pmMed_Final,@pmPdoVigencia,@pmVigencia,@pmNotas,@pmAnexo,@pmInactivo,@pmFechaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNiifPoliticasLta]
 AS 
 SELECT [Id],P.IdPolitica,P.Descripcion,ClasePol,Objetivo,Alcance,Reconocimiento,Med_Inicial,Med_Final
,PdoVigencia,Vigencia,Notas,Anexo,P.Inactivo,P.FechaCrea,P.IdUsuario AS CdUsuario,Usuario
,CdCuenta,NomCuenta
 FROM Trn_NiifPoliticas AS P INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 LEFT JOIN Trn_NiifPolCuentas AS C ON P.IdPolitica=C.IdPolitica
 LEFT JOIN (SELECT IdCuenta,NomCuenta,Movimiento FROM Puc UNION SELECT IdCuenta,NomCuenta,Movimiento FROM PucNiif) AS PC ON C.CdCuenta=PC.IdCuenta
 WHERE P.Inactivo=0
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsJurReclamac] @pmTipDoc VARCHAR(3),@pmReclamac INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaRec SMALLDATETIME,@pmTipSin VARCHAR(3),@pmSiniestro INT,@pmIdCiaSin CHAR(2)
,@pmIdReclamnte VARCHAR(16),@pmIdTipoRec VARCHAR(4),@pmVrReclamac MONEY,@pmObservacion VARCHAR(1000),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_JurReclamac (TipDoc,Reclamac,IdCia,Fecha,FechaRec,TipSin,Siniestro,IdCiaSin,IdReclamnte,IdTipoRec,VrReclamac,Observacion,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmReclamac,@pmIdCia,@pmFecha,@pmFechaRec,@pmTipSin,@pmSiniestro,@pmIdCiaSin,@pmIdReclamnte,@pmIdTipoRec,@pmVrReclamac,@pmObservacion,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryJurReclamac] @pmTipDoc VARCHAR(3),@pmReclamac INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Reclamac,IdCia,Fecha,FechaRec,TipSin,Siniestro,IdCiaSin,IdReclamnte,IdTipoRec,VrReclamac,Observacion,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario
 FROM Trn_JurReclamac
 WHERE TipDoc=@pmTipDoc AND Reclamac=@pmReclamac AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelJurReclamac] @pmTipDoc VARCHAR(3),@pmReclamac INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_JurReclamac
 WHERE TipDoc=@pmTipDoc AND Reclamac=@pmReclamac AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpJurReclamac] @pmTipDoc VARCHAR(3),@pmReclamac INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaRec SMALLDATETIME,@pmTipSin VARCHAR(3),@pmSiniestro INT,@pmIdCiaSin CHAR(2),@pmIdReclamnte VARCHAR(16)
,@pmIdTipoRec VARCHAR(4),@pmVrReclamac MONEY,@pmObservacion VARCHAR(1000)
 AS 
 UPDATE Trn_JurReclamac SET Fecha=@pmFecha,FechaRec=@pmFechaRec,TipSin=@pmTipSin,Siniestro=@pmSiniestro,IdCiaSin=@pmIdCiaSin,IdReclamnte=@pmIdReclamnte,IdTipoRec=@pmIdTipoRec,VrReclamac=@pmVrReclamac,Observacion=@pmObservacion
 WHERE TipDoc=@pmTipDoc AND Reclamac=@pmReclamac AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsEstaciones] @pmIdEstacion CHAR(2),@pmEstacion VARCHAR(50)
 AS 
 INSERT INTO Sys_Estaciones (IdEstacion,Estacion)
 VALUES (@pmIdEstacion,@pmEstacion)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEstaciones] @pmIdEstacion CHAR(2)
 AS 
 SELECT IdEstacion,Estacion
 FROM Sys_Estaciones
 WHERE IdEstacion LIKE ISNULL(@pmIdEstacion,'%%')
 ORDER BY IdEstacion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpEstaciones] @pmIdEstacion CHAR(2),@pmEstacion VARCHAR(50)
 AS 
 UPDATE Sys_Estaciones SET Estacion=@pmEstacion
 WHERE IdEstacion=@pmIdEstacion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryInvFisicoRel] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
SELECT F.nInventario,F.IdCia AS CdCia,Compania,F.Fecha,F.Modalidad,F.CdBodega,B.Bodega,CantFisico,VrTotalFisico,Existencias,VrTotalCosto
,F.Procesado,F.FecProceso,F.NumAjuSob,F.NumAjuFal,F.Anulado,F.FecDev,F.Observacion AS Observ
,FCodBodega,FB.Bodega AS InvBodega,FCodMarca,FM.Marca AS InvMarca,FCodSubGrupo,FS.Subgrupo AS InvSubgrupo
,F.IdEstado AS CdEstado,Estado,F.TimeSys AS FechaCrea,F.FecUpdate AS FechaModif,F.IdCiaCrea,F.IdUsuario AS CdUsuario,U.Usuario,F.OrigenAdd
--detalles
,D.Item,D.IdProducto AS CdProducto,DescripProd,D.IdBodega AS DetCdBodega,BD.Bodega AS DetBodega,D.CdTanque,D.Cantidad,D.VrUnitario,D.CantExist,D.Descripcion
,D.Unidades,D.UndAforo,D.UndAgua,D.CantAgua,D.NoConteo,D.IdUsuario AS DetCdUsuario,UD.Usuario DetUsuario,D.IdEstacion AS CdEstacion,ET.Estacion,D.FechaAdd AS DetFechaCrea,D.CdUbic
,P.UndMed,UP.Unidad AS UmPeso,P.IdUnd AS CdUnid,UM.Unidad,P.TipoRef,P.IdSubgrupo AS CdSubgrupo,SG.Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea
,P.Referencia,P.CodBarras,P.DescripAbrv,P.ExtciaMin,P.ExtciaMax,P.ExtciaAct,P.Seriales,P.Lotes,P.Combo,P.Tanques
,TQ.Descripcion AS TanqDescrip,TQ.CapTanq AS TanqCapacidad,TQ.NivAgua AS TanqNivAgua,TQ.CapNeta AS TanqCapNeta,TQ.SaldoActual AS TanqSaldo,TQ.TnqGas,TQ.TnqUnido,TQ.TnqVirtual
,P.IdMarca AS CdMarca,M.Marca
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
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 P ON D.IdProducto=P.IdProducto INNER JOIN Bodegas AS BD ON D.IdBodega=BD.IdBodega
INNER JOIN Sys_Um AS UP ON P.UndMed=UP.UndMed 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 L ON G.IdLinea=L.IdLinea
INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
LEFT JOIN Tanques AS TQ ON D.CdTanque=TQ.IdTanque
LEFT JOIN Sys_Estaciones AS ET ON D.IdEstacion=ET.IdEstacion LEFT JOIN Bodegas AS B ON F.CdBodega=B.IdBodega
LEFT JOIN adm_Usuarios AS UD ON D.IdUsuario=UD.IdUsuario
LEFT JOIN Marcas AS FM ON F.FCodMarca=FM.IdMarca LEFT JOIN Bodegas AS FB ON F.FCodBodega=FB.IdBodega
LEFT JOIN SubGrupos AS FS ON F.FCodSubGrupo=FS.IdSubgrupo
WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND F.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEstacionesDso]
 AS 
 SELECT IdEstacion,Estacion,IdEstacion+' '+Estacion AS DsEst
 FROM Sys_Estaciones
 ORDER BY IdEstacion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,C.VrImpCon,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
,K.VrImpCon AS KarImpConsumo,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelEstaciones] @pmIdEstacion CHAR(2)
 AS 
 DELETE FROM Sys_Estaciones
 WHERE IdEstacion=@pmIdEstacion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuCuentasVeht_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum VARCHAR(3),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuCuentasVeht (nAnno,nMes,IdCia,IdCuenta,IdTercero,IdVehiculo,TipoAfiVehic,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdTercero,S.IdVehiculo,S.TipoAfiVehic,S.TipoAcum,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuCuentasVeht AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt AND S.TipoAcum=@pmTipoAcum
 AND NOT EXISTS (SELECT * FROM AcuCuentasVeht AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes AND AC.TipoAcum=@pmTipoAcum
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdTercero=S.IdTercero AND AC.IdVehiculo=S.IdVehiculo AND AC.TipoAfiVehic=S.TipoAfiVehic)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuCuentasVehtSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum VARCHAR(3),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuCuentasVeht SET AcuCuentasVeht.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuCuentasVeht INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,IdVehiculo,TipoAfiVehic,TipoAcum,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuCuentasVeht WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt AND TipoAcum=@pmTipoAcum) AS SA ON AcuCuentasVeht.IdCia=SA.IdCia AND AcuCuentasVeht.IdCuenta=SA.IdCuenta
 AND AcuCuentasVeht.IdTercero=SA.IdTercero AND AcuCuentasVeht.IdVehiculo=SA.IdVehiculo AND AcuCuentasVeht.TipoAfiVehic=SA.TipoAfiVehic AND AcuCuentasVeht.TipoAcum=SA.TipoAcum
 WHERE AcuCuentasVeht.nAnno=@pmnAnno AND AcuCuentasVeht.nMes=@pmnMes AND AcuCuentasVeht.TipoAcum=@pmTipoAcum
 AND (@pmIdCia IS NULL OR AcuCuentasVeht.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuCuentasVeht.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuCuentasVeht_Uni] @pmIdVehiculo VARCHAR(10),@pmIdVehiculoDos VARCHAR(10),@pmNewVehiculo VARCHAR(10)
 AS 
 IF EXISTS (SELECT * FROM AcuCuentasVeht WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos)
  INSERT INTO AcuCuentasVeht (nAnno,nMes,IdCia,IdCuenta,IdTercero,IdVehiculo,TipoAfiVehic,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,@pmNewVehiculo,TipoAfiVehic,TipoAcum,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  FROM AcuCuentasVeht WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos
  GROUP BY nAnno,nMes,IdCia,IdCuenta,IdTercero,TipoAfiVehic,TipoAcum
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpNomCapacitaciones] @pmIdClase VARCHAR(4),@pmItem INT,@pmVigencia INT,@pmCdEmpleado VARCHAR(16),@pmCdProf VARCHAR(4)
,@pmCdCargo VARCHAR(4),@pmCdArea VARCHAR(4),@pmCdDep VARCHAR(4),@pmCumplimto VARCHAR(20),@pmInactivo BIT
 AS 
 UPDATE NomCapacitaciones SET Vigencia=@pmVigencia,CdEmpleado=@pmCdEmpleado,CdProf=@pmCdProf,CdCargo=@pmCdCargo
,CdArea=@pmCdArea,CdDep=@pmCdDep,Cumplimto=@pmCumplimto,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase AND Item=@pmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNomCapacitacionesLta] @pmIdClase VARCHAR(4)=Null
 AS 
 SELECT C.IdClase AS CdClase,NomClase,Item,Vigencia,C.CdEmpleado,E.Apellidos,E.Nombres
,CdProf,Profesion,CdCargo,Cargo,CdArea,Area,CdDep,Dependencia,Cumplimto,C.Inactivo AS Inactvo
 FROM NomCapacitaciones AS C INNER JOIN ClaseCapac AS CP ON C.IdClase=CP.IdClase
 LEFT JOIN Empleados AS E ON C.CdEmpleado=E.IdEmpleado
 LEFT JOIN Cargos AS CG ON C.CdCargo=CG.IdCargo LEFT JOIN Profesiones AS P ON C.CdProf=P.IdProf
 LEFT JOIN Dependencias AS D ON C.CdDep=D.IdDep LEFT JOIN Areas AS A ON C.CdArea=A.IdArea
 WHERE C.IdClase LIKE ISNULL(@pmIdClase,'%') AND CP.Inactivo=0
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsNomCapacitaciones] @pmIdClase VARCHAR(4),@pmItem INT,@pmVigencia INT,@pmCdEmpleado VARCHAR(16),@pmCdProf VARCHAR(4)
,@pmCdCargo VARCHAR(4),@pmCdArea VARCHAR(4),@pmCdDep VARCHAR(4),@pmCumplimto VARCHAR(20),@pmInactivo BIT
 AS 
 INSERT INTO NomCapacitaciones (IdClase,Item,Vigencia,CdEmpleado,CdProf,CdCargo,CdArea,CdDep,Cumplimto,Inactivo)
 VALUES (@pmIdClase,@pmItem,@pmVigencia,@pmCdEmpleado,@pmCdProf,@pmCdCargo,@pmCdArea,@pmCdDep,@pmCumplimto,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryVehConductorId] @pmIdVehiculo VARCHAR(10)
 AS 
 SELECT MAX(Numero) AS MxNum
 FROM Trn_VehConductor
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_TraMovAnt] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdEst CHAR(2)
 AS 
 SELECT MA.TipDoc,TipoDoc,MA.Numero AS Anticipo,MA.IdCia AS CdCia,Compania,MA.Fecha,T.TipoId AS NitTipId,MA.IdPoseedor AS NitPoseedor,T.Dv AS NitDv,T.RazonSocial AS Poseedor,MA.IdVehiculo AS PlacaVeh,NumVeh
,MA.SaldoAnt,MA.VrAnticipo,MA.VrPagos AS VrAbonos,A.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,MA.TipMuc,MA.Manifiesto,MA.IdCiaMuc,A.Modalidad,A.TipoPago,A.NumCheque,A.FecCheque,A.Beneficiario,A.Anulado,A.Observacion AS Detalle,A.IdConcepto AS CdConcepto,Concepto,A.TipCom,A.Comprobante,A.IdCiaCom
,A.TipoAplica,A.VrPago,A.TimeSys
FROM tm_TraMovAnt AS MA INNER JOIN Companias AS CN ON MA.IdCia=CN.IdCia INNER JOIN Terceros AS T ON MA.IdPoseedor=T.IdTercero
INNER JOIN Vehiculos AS V ON MA.IdVehiculo=V.IdVehiculo
LEFT JOIN (SELECT TipDoc,Anticipo,IdCia,Fecha,IdConcepto,TipMuc,Manifiesto,IdCiaMuc,IdVehiculo,IdPoseedor,IdConductor,VrAnticipo,VrAbonado,Modalidad,TipoPago,NumCheque,FecCheque,IdCta,IdCuenta,Beneficiario
,CedBenef,FechaVence,NumPresAnt,PreAnticipo,NumCargue,CiaCargue,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys,IdUsuario,'' AS TipoAplica,0 AS VrPago 
FROM Trn_TraAnticipos WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin
UNION ALL
SELECT P.TipDoc,P.Documento,P.IdCia,P.Fecha,A.IdConcepto,A.TipMuc,A.Manifiesto,A.IdCiaMuc,A.IdVehiculo,A.IdPoseedor,A.IdConductor,A.VrAnticipo,A.VrAbonado,A.Modalidad,A.TipoPago,A.NumCheque,A.FecCheque,A.IdCta,P.CdCuentaAnt,A.Beneficiario
,A.CedBenef,A.FechaVence,A.NumPresAnt,A.PreAnticipo,A.NumCargue,A.CiaCargue,P.TipAnt,P.Anticipo,P.IdCiaAnt,A.Anulado,A.NumDev,A.FecDev,P.Detalle,A.TimeSys,A.IdUsuario,P.TipoAplica,P.VrAbono
FROM Trn_TraAntAbonos AS P INNER JOIN Trn_TraAnticipos AS A ON P.TipAnt=A.TipDoc AND P.Anticipo=A.Anticipo AND P.IdCiaAnt=A.IdCia
WHERE P.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
UNION ALL
SELECT A.TipDoc,A.Anticipo,A.IdCia,A.Fecha,A.IdConcepto,A.TipOds,A.NumOrden,A.IdCiaOds,O.IdVehiculo,O.IdPoseedor,O.IdConductor,A.VrAnticipo,A.VrAbonado,A.TipoAncpo,A.TipoPago,A.NumCheque,A.FecCheque,A.IdCta,A.IdCuenta,A.Beneficiario
,A.CedBenef,A.FechaVence,A.NumPresAnt,0,0,'',A.TipCom,A.Comprobante,A.IdCiaCom,A.Anulado,A.NumDev,A.FecDev,A.Observacion,A.TimeSys,A.IdUsuario,'',0
FROM Trn_TraOrdenAnt AS A INNER JOIN Trn_TraOrdenServ AS O ON A.TipOds=O.TipDoc AND A.NumOrden=O.NumOrden AND A.IdCiaOds=O.IdCia
WHERE A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
UNION ALL
SELECT P.TipDoc,P.Documento,P.IdCia,P.Fecha,A.IdConcepto,A.TipOds,A.NumOrden,A.IdCiaOds,O.IdVehiculo,O.IdPoseedor,O.IdConductor,A.VrAnticipo,A.VrAbonado,A.TipoAncpo,A.TipoPago,A.NumCheque,A.FecCheque,A.IdCta,P.CodCuenta,A.Beneficiario
,A.CedBenef,A.FechaVence,A.NumPresAnt,0,0,'',P.TipAnt,P.Anticipo,P.IdCiaAnt,A.Anulado,A.NumDev,A.FecDev,P.Observacion,A.TimeSys,A.IdUsuario,P.TipoAplica,P.VrAbono
FROM Trn_TraOrdenAbo AS P INNER JOIN Trn_TraOrdenAnt AS A ON P.TipAnt=A.TipDoc AND P.Anticipo=A.Anticipo AND P.IdCiaAnt=A.IdCia
INNER JOIN Trn_TraOrdenServ AS O ON A.TipOds=O.TipDoc AND A.NumOrden=O.NumOrden AND A.IdCiaOds=O.IdCia
WHERE P.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
--Transporte especial
--UNION ALL 
--SELECT TipDoc,Anticipo,IdCia,Fecha,IdConcepto,TipRes,Reserva,IdCiaRes,IdVehiculo,IdPoseedor,IdConductor,VrAnticipo,VrAbonado,Modalidad,TipoPago,NumCheque,FecCheque,IdCta,IdCuenta,Beneficiario
--,CedBenef,FechaVence,NumPresAnt,PreAnticipo,0,'',TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys,IdUsuario,'' AS TipoAplica,0 AS VrPago 
--FROM Trn_TraEspAnticipos
--WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
--UNION ALL 
--SELECT P.TipDoc,P.Documento,P.IdCia,P.Fecha,A.IdConcepto,A.TipRes,A.Reserva,A.IdCiaRes,A.IdVehiculo,A.IdPoseedor,A.IdConductor,A.VrAnticipo,A.VrAbonado,A.Modalidad,A.TipoPago,A.NumCheque,A.FecCheque,A.IdCta,P.CdCuentaAnt,A.Beneficiario
--,A.CedBenef,A.FechaVence,A.NumPresAnt,A.PreAnticipo,0,'',P.TipAnt,P.Anticipo,P.IdCiaAnt,A.Anulado,A.NumDev,A.FecDev,P.Detalle,A.TimeSys,A.IdUsuario,P.TipoAplica,P.VrAbono
--FROM Trn_TraEspAntAbonos AS P INNER JOIN Trn_TraEspAnticipos AS A ON P.TipAnt=A.TipDoc AND P.Anticipo=A.Anticipo AND P.IdCiaAnt=A.IdCia
--WHERE P.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
) AS A ON MA.TipDoc=A.TipDoc AND MA.Numero=A.Anticipo AND MA.IdCia=A.IdCia
LEFT JOIN Sys_TiposDoc AS TD ON MA.TipDoc=TD.IdDoc
LEFT JOIN Terceros AS CDT ON A.IdConductor=CDT.IdTercero LEFT JOIN Conceptos AS C ON A.IdConcepto=C.IdConcepto
WHERE MA.IdEst=@pmIdEst
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudContNovRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS 
SELECT C.NumContrato,C.IdCia AS CdCia,C.Fecha,C.FechaInicio,C.FechaFinal,C.IdCliente,C.TipOrden,C.NumOrden,C.IdCiaOrden
,D.Item,D.Fecha,D.TipoNov,D.Descripcion,D.TarifaServ,D.TipInv,D.NumInvent,D.CdCiaInv,D.ItemInv
,D.CdArticulo,Articulo,A.IdSeccion AS CdSeccion,Seccion,D.Cantidad,D.CdEstado,Estado,D.CdBodega,B.Bodega AS BodegaOrig
,D.CdBodDest,BD.Bodega AS BodegaDest,D.NumDocRef,D.IdUsuario,Usuario,D.Notas
FROM Trn_MudContratos AS C INNER JOIN Trn_MudContNov AS D ON C.TipDoc=D.TipDoc AND C.NumContrato=D.NumContrato AND C.IdCia=D.IdCia
INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
LEFT JOIN AlmBodegas AS B ON D.CdBodega=B.IdBodega LEFT JOIN AlmBodegas AS BD ON D.CdBodDest=BD.IdBodega
LEFT JOIN MudEstados AS E ON D.CdEstado=E.IdEstado LEFT JOIN MudArticulos AS A ON D.CdArticulo=A.IdArticulo
LEFT JOIN MudSecciones AS SC ON A.IdSeccion=SC.IdSeccion
WHERE C.TipDoc=@pmTipDoc AND C.FechaInicio BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMudContratos] @pmTipDoc VARCHAR(3),@pmNumContrato INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_MudContratos
 WHERE TipDoc=@pmTipDoc AND NumContrato=@pmNumContrato AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMudContratos] @pmTipDoc VARCHAR(3),@pmNumContrato INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaInicio SMALLDATETIME,@pmFechaFinal SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdBodega VARCHAR(4),@pmCdBodega2 VARCHAR(4),@pmVrServicio MONEY,@pmVrImpuesto MONEY,@pmVrDeclarado MONEY
,@pmVrSeguro MONEY,@pmTipOrden VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrden CHAR(2),@pmFechaRetiro SMALLDATETIME,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2500),@pmIdEstado VARCHAR(4),@pmNomContacto VARCHAR(150),@pmTelsContacto VARCHAR(30),@pmemlContacto VARCHAR(150),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_MudContratos SET Fecha=@pmFecha,FechaInicio=@pmFechaInicio,FechaFinal=@pmFechaFinal,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdBodega=@pmIdBodega,CdBodega2=@pmCdBodega2,VrServicio=@pmVrServicio,VrImpuesto=@pmVrImpuesto,VrDeclarado=@pmVrDeclarado,VrSeguro=@pmVrSeguro,TipOrden=@pmTipOrden,NumOrden=@pmNumOrden,IdCiaOrden=@pmIdCiaOrden
,FechaRetiro=@pmFechaRetiro,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,NomContacto=@pmNomContacto,TelsContacto=@pmTelsContacto,emlContacto=@pmemlContacto,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumContrato=@pmNumContrato AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMudContratos] @pmTipDoc VARCHAR(3),@pmNumContrato INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaInicio SMALLDATETIME,@pmFechaFinal SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdBodega VARCHAR(4),@pmCdBodega2 VARCHAR(4),@pmVrServicio MONEY,@pmVrImpuesto MONEY,@pmVrDeclarado MONEY
,@pmVrSeguro MONEY,@pmTipOrden VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrden CHAR(2),@pmFechaRetiro SMALLDATETIME,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2500),@pmIdEstado VARCHAR(4),@pmNomContacto VARCHAR(150),@pmTelsContacto VARCHAR(30),@pmemlContacto VARCHAR(150),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_MudContratos (TipDoc,NumContrato,IdCia,Fecha,FechaInicio,FechaFinal,IdCliente,IdAgencia,IdBodega,CdBodega2,VrServicio,VrImpuesto,VrDeclarado,VrSeguro,TipOrden,NumOrden,IdCiaOrden,FechaRetiro,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,NomContacto,TelsContacto,emlContacto)
 VALUES (@pmTipDoc,@pmNumContrato,@pmIdCia,@pmFecha,@pmFechaInicio,@pmFechaFinal,@pmIdCliente,@pmIdAgencia,@pmIdBodega,@pmCdBodega2,@pmVrServicio,@pmVrImpuesto,@pmVrDeclarado,@pmVrSeguro,@pmTipOrden,@pmNumOrden,@pmIdCiaOrden,@pmFechaRetiro,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmNomContacto,@pmTelsContacto,@pmemlContacto)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudContratos] @pmTipDoc VARCHAR(3),@pmNumContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumContrato,IdCia,Fecha,FechaInicio,FechaFinal,IdCliente,IdAgencia,IdBodega,CdBodega2,VrServicio,VrImpuesto,VrDeclarado,VrSeguro,TipOrden,NumOrden,IdCiaOrden,FechaRetiro,NomContacto,TelsContacto,emlContacto
,Anulado,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_MudContratos
 WHERE TipDoc=@pmTipDoc AND NumContrato=@pmNumContrato AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudContratosRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT C.NumContrato,C.IdCia AS CdCia,Compania,C.Fecha,C.FechaInicio,C.FechaFinal,C.IdCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS CdAgencia,Agencia
,C.IdBodega,B.Bodega,CdBodega2,BA.Bodega AS BodegaAdic,C.VrServicio,C.VrImpuesto,C.VrDeclarado,C.VrSeguro,C.TipOrden,C.NumOrden,C.IdCiaOrden
,O.FecDespacho AS OrdFecDespacho,O.IdTipoServ AS CdTipoServ,TipoServicio,O.IdOrigen,LO.Localidad AS CiuOrigen,LO.IdDep AS CdDepOrig,DPO.Departamento AS DptoOrigen
,O.IdDestino,LD.Localidad AS CiuDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino
,O.IdVend,VN.RazonSocial AS NomVendedor,O.TipInv,O.NumInvent,O.IdCiaInv,O.TipoRuta,O.EstadoBod,C.NomContacto,C.TelsContacto,C.emlContacto
--detalle
,D.Item,D.IdConcepto AS CdConcepto,D.Descripcion,CD.Concepto,D.ValorTotal,D.TarifaIva,D.TarifSeguro,D.TipoRubro,D.CdCuenta,D.CodTarIva,D.CodTarSeg
,C.Anulado,C.FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,C.FechaRetiro,C.TimeSys AS FechaCrea,C.FecUpdate,C.IdCiaCrea,C.OrigenAdd,C.IdUsuario AS CdUsuario,Usuario
--datos del cliente
,T.Codigo AS TercCod,T.TipoId,T.Dv,T.Direccion,T.IdLocal AS TercCdLocal,L.Localidad AS TercLocalidad,T.Telefono AS TercTelefono,T.TelMovil AS TercTerCelular,T.e_mail AS TercEmail
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupo,GrupoClie,CLI.IdPlazo AS CdPlazo,Plazo
,ISNULL(FC.FacNumero,0) AS FacNumero,ISNULL(FC.FacTotal,0) AS FacVrTotal
 FROM Trn_MudContratos AS C INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero
 INNER JOIN TercCliente AS CLI ON C.IdCliente=CLI.IdClie
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Trn_MudContConc AS D ON C.TipDoc=D.TipDoc AND C.NumContrato=D.NumContrato AND C.IdCia=D.IdCia
 INNER JOIN ConcDiversos AS CD ON D.IdConcepto=CD.IdConcepto INNER JOIN AlmBodegas AS B ON C.IdBodega=B.IdBodega
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona 
 LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo LEFT JOIN Plazos AS PZ ON CLI.IdPlazo=PZ.IdPlazo
 LEFT JOIN AlmBodegas AS BA ON C.CdBodega2=BA.IdBodega LEFT JOIN Agencias AS A ON C.IdAgencia=A.IdAgencia 
 LEFT JOIN Trn_MudOrdenServ AS O ON C.TipOrden=O.TipDoc AND C.NumOrden=O.NumOrden AND C.IdCiaOrden=O.IdCia
 LEFT JOIN Terceros AS VN ON O.IdVend=VN.IdTercero LEFT JOIN MudServicios AS TS ON O.IdTipoServ=TS.IdTipoServ
 LEFT JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 LEFT JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep LEFT JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 LEFT JOIN (SELECT TipDoc,NumContrato,IdCia,COUNT(Item) AS FacItems,MAX(NumFactura) AS FacNumero,SUM(ValorTotal) AS FacTotal FROM Trn_MudContFact
 WHERE Anulado=0 GROUP BY TipDoc,NumContrato,IdCia) AS FC ON C.TipDoc=FC.TipDoc AND C.NumContrato=FC.NumContrato AND C.IdCia=FC.IdCia
 WHERE C.TipDoc=@pmTipDoc AND C.FechaInicio BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudContratosLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT C.NumContrato,C.IdCia AS CdCia,Compania,C.Fecha,C.FechaInicio,C.FechaFinal,C.IdCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS CdAgencia,Agencia
,C.IdBodega,B.Bodega,CdBodega2,BA.Bodega AS BodegaAdic,C.VrServicio,C.VrImpuesto,C.VrDeclarado,C.VrSeguro,C.TipOrden,C.NumOrden,C.IdCiaOrden,C.FechaRetiro,C.NomContacto,C.TelsContacto,C.emlContacto
,C.Anulado,C.FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,C.TimeSys AS FechaCrea,C.FecUpdate,C.IdCiaCrea,C.OrigenAdd,C.IdUsuario AS CdUsuario,Usuario
 FROM Trn_MudContratos AS C INNER JOIN Companias AS CN ON C.IdCia=CN.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 AlmBodegas AS B ON C.IdBodega=B.IdBodega 
 LEFT JOIN AlmBodegas AS BA ON C.CdBodega2=BA.IdBodega
 LEFT JOIN Agencias AS A ON C.IdAgencia=A.IdAgencia 
 WHERE C.TipDoc=@pmTipDoc AND C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudContratos_Cr] @pmTipDoc VARCHAR(3),@pmContratoIni INT,@pmContratoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipDoc,TipoDoc,C.NumContrato,C.IdCia AS CdCia,Compania,C.Fecha,C.FechaInicio,C.FechaFinal,C.IdCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS CdAgencia,Agencia
,C.IdBodega,B.Bodega,CdBodega2,BA.Bodega AS BodegaAdic,C.VrServicio,C.VrImpuesto,C.VrDeclarado,C.VrSeguro,C.TipOrden,C.NumOrden,C.IdCiaOrden,C.FechaRetiro,C.NomContacto,C.TelsContacto,C.emlContacto
,C.Anulado,C.FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado
,D.Item,D.IdConcepto AS CdConcepto,D.Descripcion,CD.Concepto,D.ValorTotal,D.TarifaIva,D.TarifSeguro,D.TipoRubro,D.CdCuenta,D.CodTarIva,D.CodTarSeg
,C.TimeSys AS FechaCrea,C.FecUpdate,C.IdCiaCrea,C.OrigenAdd,C.IdUsuario AS CdUsuario,Usuario
 FROM Trn_MudContratos AS C INNER JOIN Companias AS CN ON C.IdCia=CN.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 Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 INNER JOIN Trn_MudContConc AS D ON C.TipDoc=D.TipDoc AND C.NumContrato=D.NumContrato AND C.IdCia=D.IdCia
 INNER JOIN ConcDiversos AS CD ON D.IdConcepto=CD.IdConcepto 
 INNER JOIN AlmBodegas AS B ON C.IdBodega=B.IdBodega 
 LEFT JOIN AlmBodegas AS BA ON C.CdBodega2=BA.IdBodega LEFT JOIN Agencias AS A ON C.IdAgencia=A.IdAgencia 
 WHERE C.TipDoc=@pmTipDoc AND C.NumContrato BETWEEN @pmContratoIni AND @pmContratoFin AND C.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMayInventarioItm] @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT MAX(Item) AS MXITM  FROM Trn_MayInventario
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsVeh_SaldosUni] @pmIdVehiculo VARCHAR(10),@pmIdVehiculoDos VARCHAR(10),@pmNewVehiculo VARCHAR(10)
AS 
 IF EXISTS (SELECT * FROM Veh_Saldos WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos)
  INSERT INTO Veh_Saldos (IdVehiculo,IdClase,IdCia,ValorSaldo)
  SELECT @pmNewVehiculo,IdClase,IdCia,SUM(ValorSaldo) 
  FROM Veh_Saldos WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos
  GROUP BY IdClase,IdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_CxcAbonosItm] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_CxcAbonos
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraManifAnexo] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmkmsTotal DECIMAL(14,4),@pmVrFleteTon MONEY,@pmTipoRuta VARCHAR(10)
,@pmCdLocTrao VARCHAR(8),@pmCdLocTrad VARCHAR(8),@pmKmsTraOri DECIMAL(14,4),@pmKmsTraDes DECIMAL(14,4),@pmIdMneda VARCHAR(5),@pmVrTasa DECIMAL(14,4)
,@pmNomRemite VARCHAR(150),@pmNomDestino VARCHAR(150),@pmLugarFletes VARCHAR(100),@pmNumAnticipo INT,@pmNumCheque VARCHAR(20),@pmTipoMintrans VARCHAR(10)
,@pmMucMintrans INT,@pmContIntegral INT,@pmVolumenCarga DECIMAL(14,4),@pmNumPoliza VARCHAR(30),@pmCdBodDtno VARCHAR(4),@pmTipoTrslado INT,@pmMvoAnulacion VARCHAR(5),@pmMvoSuspension VARCHAR(5)
,@pmWsSeguro BIT,@pmNumRadSeguro DECIMAL(18,2),@pmPuntosRuta DECIMAL(14,4),@pmCantViajes INT,@pmTransbordo INT,@pmNumMucAnu INT,@pmCiaMucAnu CHAR(2),@pmAceptaFirma BIT
 AS 
 INSERT INTO Trn_TraManifAnexo (TipDoc,Manifiesto,IdCia,kmsTotal,VrFleteTon,TipoRuta,CdLocTrao,CdLocTrad,KmsTraOri,KmsTraDes,IdMneda,VrTasa,NomRemite,NomDestino
,LugarFletes,NumAnticipo,NumCheque,TipoMintrans,MucMintrans,ContIntegral,VolumenCarga,NumPoliza,CdBodDtno,TipoTrslado,MvoAnulacion,MvoSuspension,WsSeguro,NumRadSeguro,PuntosRuta,CantViajes,Transbordo,NumMucAnu,CiaMucAnu,AceptaFirma)
 VALUES (@pmTipDoc,@pmManifiesto,@pmIdCia,@pmkmsTotal,@pmVrFleteTon,@pmTipoRuta,@pmCdLocTrao,@pmCdLocTrad,@pmKmsTraOri,@pmKmsTraDes,@pmIdMneda,@pmVrTasa
,@pmNomRemite,@pmNomDestino,@pmLugarFletes,@pmNumAnticipo,@pmNumCheque,@pmTipoMintrans,@pmMucMintrans,@pmContIntegral,@pmVolumenCarga,@pmNumPoliza,@pmCdBodDtno,@pmTipoTrslado,@pmMvoAnulacion,@pmMvoSuspension,@pmWsSeguro
,@pmNumRadSeguro,@pmPuntosRuta,@pmCantViajes,@pmTransbordo,@pmNumMucAnu,@pmCiaMucAnu,@pmAceptaFirma)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraManifiestoLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdRuta VARCHAR(4)=Null,@pmIdOrigen VARCHAR(8)=Null,@pmIdDestino VARCHAR(8)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
 SELECT M.Manifiesto AS NumManif,M.IdCia AS CdCia,Compania,Fecha,FecDespacho,FecEntrega,IdOrigen,LO.Localidad AS CiudadOrigen,IdDestino,LD.Localidad AS CiudadDestino,IdRuta,IdRutaFle,M.IdVehiculo AS PlacaVeh,NumVeh
,M.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,nRemolque,TipoAfiVehic,M.IdPropietario AS NitPropietario,T.RazonSocial AS Propietario,M.IdPoseedor AS NitPoseedor,NP.RazonSocial AS Poseedor
,VrFletes,VrRetencion,VrReteIca,VrDescuento,VrAnticipo,VrAntAdic,VrNeto,VrPagos,VrCargos,VrDctos,TarifaFlete,Cantidad,PesoTotal,VolumenCarga,BaseRet,TarifaRet,TarifaIca,CodTarRet
,CodTarIca,IdLocFletes,LugarFletes,FechaPago,PagoCargue,PagoDescargue,NomRemite,NomDestino,NumMintrans,EdoMintrans,Remesa,IdCiaRem,Cumplido,IdCiaCump,FechaCump,EstCumplido
,TipOdp,OrdPago,IdCiaOdp,FechaOdp,EstOrden,VrFleteNeto,TipInfVia,NumViaje,CiaInfViaje,CdCatPeaje,NumAnticipo,NumCheque,TipoMintrans,TipoRuta,MucMintrans,PuntosRuta,AceptaFirma,CdCondRelev,A.CantViajes
,OrigenAdd,Anulado,FecDev,Transbordo,NumMucAnu,CiaMucAnu,TipCom,Comprobante,IdCiaCom,M.Observacion AS Observ,M.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,M.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraManifiesto AS M INNER JOIN Trn_TraManifAnexo AS A ON M.TipDoc=A.TipDoc AND M.Manifiesto=A.Manifiesto AND M.IdCia=A.IdCia
 INNER JOIN Localidades AS LO ON M.IdOrigen=LO.IdLocal INNER JOIN Localidades AS LD ON M.IdDestino=LD.IdLocal
 INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo INNER JOIN Companias AS CI ON M.IdCia=CI.IdCia
 INNER JOIN Terceros AS NC ON M.IdConductor=NC.IdTercero  INNER JOIN Terceros AS T ON M.IdPropietario=T.IdTercero
 INNER JOIN Terceros AS NP ON M.IdPoseedor=NP.IdTercero 
 INNER JOIN EstadoDoc AS ED ON M.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 WHERE M.TipDoc=@pmTipDoc AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdRuta LIKE ISNULL(@pmIdRuta,'%')  AND IdOrigen LIKE ISNULL(@pmIdOrigen,'%') AND IdDestino LIKE ISNULL(@pmIdDestino,'%')
 AND M.IdVehiculo  LIKE ISNULL(@pmIdVehiculo ,'%') AND M.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 ORDER BY M.IdCia,M.Manifiesto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraManifAnexo] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Manifiesto,IdCia,kmsTotal,VrFleteTon,TipoRuta,CdLocTrao,CdLocTrad,KmsTraOri,KmsTraDes,IdMneda,VrTasa
,NomRemite,NomDestino,LugarFletes,NumAnticipo,NumCheque,TipoMintrans,MucMintrans,ContIntegral,VolumenCarga
,NumPoliza,CdBodDtno,TipoTrslado,MvoAnulacion,MvoSuspension,WsSeguro,NumRadSeguro,PuntosRuta,CantViajes,Transbordo,NumMucAnu,CiaMucAnu,AceptaFirma
 FROM Trn_TraManifAnexo
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmCdGrupo,tmSalvamento,tmTipoContab,tmPartes,tmUnidades,tmCantidad,tmDepMejoras
 FROM tm_AfDep
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmCdGrupo,tmSalvamento,tmTipoContab,tmPartes,tmUnidades,tmCantidad,tmDepMejoras)
 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,'',CdGrupoAct,VrSalvamnto,'0',Dep_Partes,Unidades,Cantidad,VrDepMejoras
 FROM Trn_AfDetalle
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmtmCdGrupo VARCHAR(4)
,@pmtmSalvamento MONEY,@pmtmTipoContab VARCHAR(10),@pmtmPartes BIT,@pmtmUnidades INT,@pmtmCantidad INT,@pmtmDepMejoras MONEY
 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,tmCdGrupo,tmSalvamento,tmTipoContab,tmPartes,tmUnidades,tmCantidad,tmDepMejoras)
 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,@pmtmCdGrupo
,@pmtmSalvamento,@pmtmTipoContab,@pmtmPartes,@pmtmUnidades,@pmtmCantidad,@pmtmDepMejoras)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsJurDocumtos] @pmIdDoc VARCHAR(4),@pmDocumnto VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO JurDocumtos (IdDoc,Documnto,Inactivo)
 VALUES (@pmIdDoc,@pmDocumnto,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpJurDocumtos] @pmIdDoc VARCHAR(4),@pmDocumnto VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE JurDocumtos SET Documnto=@pmDocumnto,Inactivo=@pmInactivo
 WHERE IdDoc=@pmIdDoc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelJurDocumtos] @pmIdDoc VARCHAR(4)
 AS 
 DELETE FROM JurDocumtos
 WHERE IdDoc=@pmIdDoc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraPorteIntMcia] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmItem INT,@pmCdMercancia VARCHAR(16),@pmDescripMcias VARCHAR(500),@pmCantidad DECIMAL(14,4),@pmPesoNeto DECIMAL(14,4),@pmPesoBruto DECIMAL(14,4),@pmVolumen DECIMAL(14,4),@pmValorMcia MONEY
,@pmIdMoneda VARCHAR(5),@pmIdUnd VARCHAR(4),@pmIdEmp VARCHAR(4),@pmIdNat VARCHAR(4),@pmUndMed VARCHAR(10),@pmUndVol VARCHAR(10),@pmBultos VARCHAR(150),@pmObservacion VARCHAR(1000),@pmDescripUnid VARCHAR(500)
 AS 
 INSERT INTO Trn_TraPorteIntMcia (TipDoc,NumOrden,IdCia,Item,CdMercancia,DescripMcias,Cantidad,PesoNeto,PesoBruto,Volumen,ValorMcia,IdMoneda,IdUnd,IdEmp,IdNat,UndMed,UndVol,Bultos,Observacion,DescripUnid)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmItem,@pmCdMercancia,@pmDescripMcias,@pmCantidad,@pmPesoNeto,@pmPesoBruto,@pmVolumen,@pmValorMcia,@pmIdMoneda,@pmIdUnd,@pmIdEmp,@pmIdNat,@pmUndMed,@pmUndVol,@pmBultos,@pmObservacion,@pmDescripUnid)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraPorteIntLtd] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT O.NumOrden,O.IdCia AS CdCia,Compania,O.Fecha,O.FecEmision,O.FecCargue,O.FecEntrega,DPO.IdPais AS CdPaisOrig,POR.NombrePais AS PaisOrig,LO.IdDep AS CdDepOrig,DPO.Departamento AS DptoOrigen,O.IdOrigen,LO.Localidad AS DescOrigen
,DPD.IdPais AS CdPaisDest,PDE.NombrePais AS PaisDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino,O.IdDestino,LD.Localidad AS DescDestino,O.IdNotificado,O.Notificado,O.IdRemitente,O.Remitente,O.DirOrigen
,O.IdDestinatario,O.Destinatario,O.DirDestino,O.IdConsig,O.Consignatario,O.DirConsig,O.InsTransporte,O.DocRemitente,O.ValorTotal,M.Simbolo,O.IdMoneda AS CdMoneda,M.Mneda AS Moneda,O.PesoTotal,O.Manifiesto,O.CdCiaManif
,O.Anulado,O.FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.TimeSys AS FechaCrea,O.FecUpdate,O.IdCiaCrea,O.OrigenAdd,O.IdUsuario AS CdUsuario,Usuario,O.Negociacion,O.Condic_Pago
--detalles
,D.Item,D.CdMercancia,D.DescripMcias,D.Cantidad,D.PesoNeto,D.PesoBruto,D.Volumen,D.ValorMcia,D.IdMoneda AS DetIdMoneda,MN.Mneda AS DetMoneda,MN.Simbolo AS DetMonSimb,D.IdUnd AS CdUnd,UM.Unidad,D.IdEmp AS CdEmp,Empaque
,D.IdNat AS CdNat,Natlzaprod,D.UndMed,UP.Unidad AS DesUndPeso,D.UndVol,UV.Unidad AS DesUndVol,D.Bultos,D.Observacion AS DetObservacion,D.DescripUnid
 FROM Trn_TraPorteInt AS O INNER JOIN Companias AS C ON O.IdCia=C.IdCia
 INNER JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep INNER JOIN adm_Paises AS POR ON DPO.IdPais=POR.IdPais
 INNER JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep INNER JOIN adm_Paises AS PDE ON DPD.IdPais=PDE.IdPais
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS E ON O.IdEstado=E.IdEstado
 INNER JOIN Trn_TraPorteIntMcia AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN adm_monedas AS MN ON D.IdMoneda=MN.IdMneda
 INNER JOIN TiposNat AS NZ ON D.IdNat=NZ.IdNat
 LEFT JOIN adm_monedas AS M ON O.IdMoneda=M.IdMneda LEFT JOIN Empaques AS EP ON D.IdEmp=EP.IdEmp 
 LEFT JOIN Sys_Um AS UP ON D.UndMed=UP.UndMed LEFT JOIN Sys_Um AS UV ON D.UndVol=UV.UndMed
 WHERE O.TipDoc=@pmTipDoc AND O.FecEmision BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraPorteInt_Cr] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT O.TipDoc,TipoDoc,O.NumOrden,O.IdCia AS CdCia,Compania,O.Fecha,O.FecEmision,O.FecCargue,O.FecEntrega,DPO.IdPais AS CdPaisOrig,POR.NombrePais AS PaisOrig,LO.IdDep AS CdDepOrig,DPO.Departamento AS DptoOrigen,O.IdOrigen,LO.Localidad AS DescOrigen
,DPD.IdPais AS CdPaisDest,PDE.NombrePais AS PaisDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino,O.IdDestino,LD.Localidad AS DescDestino,O.IdNotificado,O.Notificado,NF.Telefono AS TelNoti,NF.TelMovil AS MovNoti,O.IdRemitente,O.Remitente,RM.Telefono AS TelRemi,RM.TelMovil AS MovRemi,O.DirOrigen
,O.IdDestinatario,O.Destinatario,DT.Telefono AS TelDest,DT.TelMovil AS MovDest,O.DirDestino,O.IdConsig,O.Consignatario,CS.Telefono AS TelCons,CS.TelMovil AS MovCons,O.DirConsig,O.InsTransporte,O.DocRemitente,O.ValorTotal,M.Simbolo,O.IdMoneda AS CdMoneda,M.Mneda AS Moneda,O.PesoTotal,O.Manifiesto,O.CdCiaManif
,O.Anulado,O.FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.TimeSys AS FechaCrea,O.FecUpdate,O.IdCiaCrea,O.OrigenAdd,O.IdUsuario AS CdUsuario,Usuario,O.Negociacion,O.Condic_Pago
,D.Item,D.CdMercancia,D.DescripMcias,D.Cantidad,D.PesoNeto,D.PesoBruto,D.Volumen,D.ValorMcia,D.IdMoneda AS DetIdMoneda,MN.Mneda AS DetMoneda,MN.Simbolo AS DetMonSimb,D.IdUnd AS CdUnd,UM.Unidad,D.IdEmp AS CdEmp,Empaque
,D.IdNat AS CdNat,Natlzaprod,D.UndMed,UP.Unidad AS DesUndPeso,D.UndVol,UV.Unidad AS DesUndVol,D.Bultos,D.Observacion AS DetObservacion,D.DescripUnid
 FROM Trn_TraPorteInt AS O INNER JOIN Companias AS C ON O.IdCia=C.IdCia INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Trn_TraPorteIntMcia AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
 INNER JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep INNER JOIN adm_Paises AS POR ON DPO.IdPais=POR.IdPais
 INNER JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep INNER JOIN adm_Paises AS PDE ON DPD.IdPais=PDE.IdPais
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS E ON O.IdEstado=E.IdEstado
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN adm_monedas AS MN ON D.IdMoneda=MN.IdMneda  
 INNER JOIN TiposNat AS NZ ON D.IdNat=NZ.IdNat
 INNER JOIN Terceros AS RM ON O.IdRemitente=RM.IdTercero INNER JOIN Terceros AS DT ON O.IdDestinatario=DT.IdTercero
 INNER JOIN Terceros AS NF ON O.IdNotificado=NF.IdTercero INNER JOIN Terceros AS CS ON O.IdConsig=CS.IdTercero
 LEFT JOIN Sys_Um AS UP ON D.UndMed=UP.UndMed LEFT JOIN Sys_Um AS UV ON D.UndVol=UV.UndMed
 LEFT JOIN adm_monedas AS M ON O.IdMoneda=M.IdMneda LEFT JOIN Empaques AS EP ON D.IdEmp=EP.IdEmp
 WHERE O.TipDoc=@pmTipDoc AND O.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND O.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoOrdenDet] @pmTipOdt VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipOdt,NumOrden,IdCia,Item,FechaServ,IdConc,Descripcion,Cantidad,VrUnitario,UndMed,CdMtto
,IdEstado,IdOperario,NumVehic,NumParte,Req_Rep,Req_MO,TipoServ,FechaCrea,IdUsuario,CodCentro,NitCenServ
 FROM Trn_MttoOrdenDet
 WHERE TipOdt=@pmTipOdt AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_MttoDetalle_Sel] @pmtmNumero VARCHAR(5),@pmTipOdt VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_MttoDetalle (tmNumero,tmItem,tmFecha,tmIdConc,tmDescripcion,tmCantidad,tmVrUnitario,tmUndMed,tmCdMtto,tmIdEstado,tmIdOperario,tmNumVehic,tmNumParte,tmReqRep,tmReqMO,tmTipoServ,tmCdCentro,tmNitTercero)
 SELECT @pmtmNumero,Item,FechaServ,IdConc,Descripcion,Cantidad,VrUnitario,UndMed,CdMtto,IdEstado,IdOperario,NumVehic,NumParte,Req_Rep,Req_MO,TipoServ,CodCentro,NitCenServ
 FROM Trn_MttoOrdenDet
 WHERE TipOdt=@pmTipOdt AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMttoOrdenDet] @pmTipOdt VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmItem INT,@pmFechaServ SMALLDATETIME,@pmIdConc VARCHAR(4),@pmDescripcion VARCHAR(1000)
,@pmCantidad DECIMAL(14,4),@pmVrUnitario DECIMAL(16,6),@pmUndMed VARCHAR(50),@pmCdMtto VARCHAR(4),@pmIdEstado VARCHAR(4)
,@pmIdOperario VARCHAR(16),@pmNumVehic VARCHAR(10),@pmNumParte VARCHAR(20),@pmReq_Rep BIT,@pmReq_MO BIT,@pmTipoServ VARCHAR(10),@pmCodCentro VARCHAR(4),@pmNitCenServ VARCHAR(16)
 AS 
 UPDATE Trn_MttoOrdenDet SET FechaServ=@pmFechaServ,IdConc=@pmIdConc,Descripcion=@pmDescripcion,Cantidad=@pmCantidad,VrUnitario=@pmVrUnitario,UndMed=@pmUndMed
,CdMtto=@pmCdMtto,IdEstado=@pmIdEstado,IdOperario=@pmIdOperario,NumVehic=@pmNumVehic,NumParte=@pmNumParte,Req_Rep=@pmReq_Rep,Req_MO=@pmReq_MO,TipoServ=@pmTipoServ
,CodCentro=@pmCodCentro,NitCenServ=@pmNitCenServ
 WHERE TipOdt=@pmTipOdt AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMttoOrdenDet] @pmTipOdt VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmItem INT,@pmFechaServ SMALLDATETIME,@pmIdConc VARCHAR(4),@pmDescripcion VARCHAR(1000),@pmCantidad DECIMAL(14,4)
,@pmVrUnitario DECIMAL(16,6),@pmUndMed VARCHAR(50),@pmCdMtto VARCHAR(4),@pmIdEstado VARCHAR(4),@pmIdOperario VARCHAR(16),@pmNumVehic VARCHAR(10),@pmNumParte VARCHAR(20)
,@pmReq_Rep BIT,@pmReq_MO BIT,@pmTipoServ VARCHAR(10),@pmCodCentro VARCHAR(4),@pmNitCenServ VARCHAR(16),@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_MttoOrdenDet (TipOdt,NumOrden,IdCia,Item,FechaServ,IdConc,Descripcion,Cantidad,VrUnitario,UndMed,CdMtto,IdEstado,IdOperario,NumVehic,NumParte,Req_Rep,Req_MO,FechaCrea,IdUsuario,TipoServ,CodCentro,NitCenServ)
 VALUES (@pmTipOdt,@pmNumOrden,@pmIdCia,@pmItem,@pmFechaServ,@pmIdConc,@pmDescripcion,@pmCantidad,@pmVrUnitario,@pmUndMed,@pmCdMtto,@pmIdEstado,@pmIdOperario,@pmNumVehic,@pmNumParte
,@pmReq_Rep,@pmReq_MO,@pmFechaCrea,@pmIdUsuario,@pmTipoServ,@pmCodCentro,@pmNitCenServ)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMayContDocLta] @pmNContratoIni INT,@pmNContratoFin 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 BETWEEN @pmNContratoIni AND @pmNContratoFin AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsMayTiposDoc] @pmIdDoc VARCHAR(4),@pmNombreDoc VARCHAR(150),@pmInactivo BIT
 AS 
 INSERT INTO MayTiposDoc (IdDoc,NombreDoc,Inactivo)
 VALUES (@pmIdDoc,@pmNombreDoc,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelMayTiposDoc] @pmIdDoc VARCHAR(4)
 AS 
 DELETE FROM MayTiposDoc
 WHERE IdDoc=@pmIdDoc
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpMayTiposDoc] @pmIdDoc VARCHAR(4),@pmNombreDoc VARCHAR(150),@pmInactivo BIT
 AS 
 UPDATE MayTiposDoc SET NombreDoc=@pmNombreDoc,Inactivo=@pmInactivo
 WHERE IdDoc=@pmIdDoc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraDecMcia] @pmTipDoc VARCHAR(3),@pmNumDeclara INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmCdMercancia VARCHAR(16),@pmDescripMcias VARCHAR(500),@pmCantidad DECIMAL(14,4),@pmPesoNeto DECIMAL(14,4)
,@pmPesoBruto DECIMAL(14,4),@pmVolumen DECIMAL(14,4),@pmValorMcia MONEY,@pmIdMoneda VARCHAR(5),@pmIdUnd VARCHAR(4),@pmIdEmp VARCHAR(4),@pmIdNat VARCHAR(4),@pmUndMed VARCHAR(10),@pmUndVol VARCHAR(10),@pmBultos VARCHAR(150),@pmDescripUnid VARCHAR(500)
 AS 
 INSERT INTO Trn_TraDecMcia (TipDoc,NumDeclara,IdCia,Item,TipOrd,NumOrden,IdCiaOrd,CdMercancia,DescripMcias,Cantidad,PesoNeto,PesoBruto,Volumen,ValorMcia,IdMoneda,IdUnd,IdEmp,IdNat,UndMed,UndVol,Bultos,DescripUnid)
 VALUES (@pmTipDoc,@pmNumDeclara,@pmIdCia,@pmItem,@pmTipOrd,@pmNumOrden,@pmIdCiaOrd,@pmCdMercancia,@pmDescripMcias,@pmCantidad,@pmPesoNeto,@pmPesoBruto,@pmVolumen,@pmValorMcia,@pmIdMoneda,@pmIdUnd,@pmIdEmp,@pmIdNat,@pmUndMed,@pmUndVol,@pmBultos,@pmDescripUnid)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDecAduanaLtd] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT O.NumDeclara,O.IdCia AS CdCia,Compania,O.Fecha,O.FecEmision,DPO.IdPais AS CdPaisOrig,POR.NombrePais AS PaisOrig,LO.IdDep AS CdDepOrig,DPO.Departamento AS DptoOrigen,O.IdOrigen,LO.Localidad AS DescOrigen
,DPD.IdPais AS CdPaisDest,PDE.NombrePais AS PaisDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino,O.IdDestino,LD.Localidad AS DescDestino
,O.IdAduCarga,AC.NomAduana AS AduanaCargo,PAC.NombrePais AS PaisAduCargo,O.IdAduOrigen,AP.NomAduana AS AduanaOrigen,PAP.NombrePais AS PaisAduOrigen,O.IdAduDestino,AD.NomAduana AS AduanaDestino,PAD.NombrePais AS PaisAduDestino
,O.IdAduFrontera,AF.NomAduana AS AduanaFrontera,PAF.NombrePais AS PaisAduFront,O.IdDeclarante,Declarante,DirDeclara,CdLocalDec,LDC.Localidad AS CiudadDec,O.IdRemitente,O.Remitente,DirOrigen,CdLocalRem,LRM.Localidad AS CiudadRemite
,O.IdDestinatario,Destinatario,DirDestino,CdLocalDest,LDT.Localidad AS CiudadDest,O.IdConsig,Consignatario,DirConsig,CdLocalConsig,LCN.Localidad AS CiudadConsig,O.IdMoneda AS CdMoneda,M.Simbolo,M.Mneda AS Moneda
,O.ValorTotal,O.PesoTotal,O.DocAnexos,O.Vehiculos,O.Remolques,O.Manifiestos,O.Contenedores,O.Precintos,O.Anulado,O.FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.Negociacion,O.CondicPago
,O.OrigenAdd,O.TimeSys AS FechaCrea,O.FecUpdate,O.IdCiaCrea,O.IdUsuario AS CdUsuario,Usuario
,D.Item,D.TipOrd,D.NumOrden,D.IdCiaOrd,D.CdMercancia,D.DescripMcias,D.Cantidad,D.PesoNeto,D.PesoBruto,D.Volumen,D.ValorMcia,D.IdMoneda AS DetIdMoneda,MN.Mneda AS DetMoneda,MN.Simbolo AS DetMonSimb,D.IdUnd AS CdUnd,UM.Unidad
,D.IdEmp AS CdEmp,Empaque,D.IdNat AS CdNat,Natlzaprod,D.UndMed,UP.Unidad AS DesUndPeso,D.UndVol,UV.Unidad AS DesUndVol,D.Bultos,D.DescripUnid
 FROM Trn_TraDecAduana AS O INNER JOIN Companias AS C ON O.IdCia=C.IdCia
 INNER JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep INNER JOIN adm_Paises AS POR ON DPO.IdPais=POR.IdPais
 INNER JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep INNER JOIN adm_Paises AS PDE ON DPD.IdPais=PDE.IdPais
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS E ON O.IdEstado=E.IdEstado
 INNER JOIN Aduanas AS AC ON O.IdAduCarga=AC.IdAduana INNER JOIN adm_Paises AS PAC ON AC.IdPais=PAC.IdPais
 INNER JOIN Aduanas AS AP ON O.IdAduOrigen=AP.IdAduana INNER JOIN adm_Paises AS PAP ON AP.IdPais=PAP.IdPais
 INNER JOIN Aduanas AS AD ON O.IdAduDestino=AD.IdAduana INNER JOIN adm_Paises AS PAD ON AD.IdPais=PAD.IdPais
 INNER JOIN Trn_TraDecMcia AS D ON O.TipDoc=D.TipDoc AND O.NumDeclara=D.NumDeclara AND O.IdCia=D.IdCia
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN adm_monedas AS MN ON D.IdMoneda=MN.IdMneda  
 INNER JOIN TiposNat AS NZ ON D.IdNat=NZ.IdNat
 LEFT JOIN adm_monedas AS M ON O.IdMoneda=M.IdMneda
 LEFT JOIN Aduanas AS AF ON O.IdAduFrontera=AF.IdAduana LEFT JOIN adm_Paises AS PAF ON AF.IdPais=PAF.IdPais
 LEFT JOIN Localidades AS LDC ON O.CdLocalDec=LDC.IdLocal LEFT JOIN Localidades AS LRM ON O.CdLocalRem=LRM.IdLocal
 LEFT JOIN Localidades AS LDT ON O.CdLocalDest=LDT.IdLocal LEFT JOIN Localidades AS LCN ON O.CdLocalConsig=LCN.IdLocal
 LEFT JOIN Sys_Um AS UP ON D.UndMed=UP.UndMed LEFT JOIN Sys_Um AS UV ON D.UndVol=UV.UndMed
 LEFT JOIN Empaques AS EP ON D.IdEmp=EP.IdEmp
 WHERE O.TipDoc=@pmTipDoc AND O.FecEmision BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDecAduana_Cr] @pmTipDoc VARCHAR(3),@pmNumDeclaraIni INT,@pmNumDeclaraFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT O.TipDoc,TipoDoc,O.NumDeclara,O.IdCia AS CdCia,Compania,O.Fecha,O.FecEmision,DPO.IdPais AS CdPaisOrig,POR.NombrePais AS PaisOrig,LO.IdDep AS CdDepOrig,DPO.Departamento AS DptoOrigen,O.IdOrigen,LO.Localidad AS DescOrigen
,DPD.IdPais AS CdPaisDest,PDE.NombrePais AS PaisDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino,O.IdDestino,LD.Localidad AS DescDestino
,O.IdAduCarga,AC.NomAduana AS AduanaCargo,PAC.NombrePais AS PaisAduCargo,O.IdAduOrigen,AP.NomAduana AS AduanaOrigen,PAP.NombrePais AS PaisAduOrigen,O.IdAduDestino,AD.NomAduana AS AduanaDestino,PAD.NombrePais AS PaisAduDestino
,O.IdAduFrontera,AF.NomAduana AS AduanaFrontera,PAF.NombrePais AS PaisAduFront,O.IdDeclarante,Declarante,DirDeclara,DC.Telefono AS TelDecla,DC.TelMovil AS MovDecla,CdLocalDec,LDC.Localidad AS CiudadDec,O.IdRemitente,O.Remitente,RM.Telefono AS TelRemi,RM.TelMovil AS MovRemi
,DirOrigen,CdLocalRem,LRM.Localidad AS CiudadRemite,O.IdDestinatario,Destinatario,DT.Telefono AS TelDest,DT.TelMovil AS MovDest,DirDestino,CdLocalDest,LDT.Localidad AS CiudadDest,O.IdConsig,Consignatario,DirConsig,CS.Telefono AS TelCons,CS.TelMovil AS MovCons,CdLocalConsig,LCN.Localidad AS CiudadConsig
,O.IdMoneda AS CdMoneda,M.Simbolo,M.Mneda AS Moneda,O.ValorTotal,O.PesoTotal,O.DocAnexos,O.Vehiculos,O.Remolques,O.Manifiestos,O.Contenedores,O.Precintos,O.Anulado,O.FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.Negociacion,O.CondicPago
,O.OrigenAdd,O.TimeSys AS FechaCrea,O.FecUpdate,O.IdCiaCrea,O.IdUsuario AS CdUsuario,Usuario
--detalles
,D.Item,D.TipOrd,D.NumOrden,D.IdCiaOrd,D.CdMercancia,D.DescripMcias,D.Cantidad,D.PesoNeto,D.PesoBruto,D.Volumen,D.ValorMcia,D.IdMoneda AS DetIdMoneda,MN.Mneda AS DetMoneda,MN.Simbolo AS DetMonSimb,D.IdUnd AS CdUnd,UM.Unidad
,D.IdEmp AS CdEmp,Empaque,D.IdNat AS CdNat,Natlzaprod,D.UndMed,UP.Unidad AS DesUndPeso,D.UndVol,UV.Unidad AS DesUndVol,D.Bultos,D.DescripUnid
 FROM Trn_TraDecAduana AS O INNER JOIN Companias AS C ON O.IdCia=C.IdCia INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario 
 INNER JOIN EstadoDoc AS E ON O.IdEstado=E.IdEstado INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep INNER JOIN adm_Paises AS POR ON DPO.IdPais=POR.IdPais
 INNER JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep INNER JOIN adm_Paises AS PDE ON DPD.IdPais=PDE.IdPais
 INNER JOIN Trn_TraDecMcia AS D ON O.TipDoc=D.TipDoc AND O.NumDeclara=D.NumDeclara AND O.IdCia=D.IdCia
 INNER JOIN Aduanas AS AC ON O.IdAduCarga=AC.IdAduana INNER JOIN adm_Paises AS PAC ON AC.IdPais=PAC.IdPais
 INNER JOIN Aduanas AS AP ON O.IdAduOrigen=AP.IdAduana INNER JOIN adm_Paises AS PAP ON AP.IdPais=PAP.IdPais
 INNER JOIN Aduanas AS AD ON O.IdAduDestino=AD.IdAduana INNER JOIN adm_Paises AS PAD ON AD.IdPais=PAD.IdPais
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN adm_monedas AS MN ON D.IdMoneda=MN.IdMneda  
 INNER JOIN TiposNat AS NZ ON D.IdNat=NZ.IdNat
 INNER JOIN Terceros AS DC ON O.IdDeclarante=DC.IdTercero INNER JOIN Terceros AS RM ON O.IdRemitente=RM.IdTercero
 INNER JOIN Terceros AS DT ON O.IdDestinatario=DT.IdTercero INNER JOIN Terceros AS CS ON O.IdConsig=CS.IdTercero
 LEFT JOIN adm_monedas AS M ON O.IdMoneda=M.IdMneda
 LEFT JOIN Aduanas AS AF ON O.IdAduFrontera=AF.IdAduana LEFT JOIN adm_Paises AS PAF ON AF.IdPais=PAF.IdPais
 LEFT JOIN Localidades AS LDC ON O.CdLocalDec=LDC.IdLocal LEFT JOIN Localidades AS LRM ON O.CdLocalRem=LRM.IdLocal
 LEFT JOIN Localidades AS LDT ON O.CdLocalDest=LDT.IdLocal LEFT JOIN Localidades AS LCN ON O.CdLocalConsig=LCN.IdLocal
 LEFT JOIN Sys_Um AS UP ON D.UndMed=UP.UndMed LEFT JOIN Sys_Um AS UV ON D.UndVol=UV.UndMed
 LEFT JOIN Empaques AS EP ON D.IdEmp=EP.IdEmp
 WHERE O.TipDoc=@pmTipDoc AND O.NumDeclara BETWEEN @pmNumDeclaraIni AND @pmNumDeclaraFin AND O.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,TR.LtaPre,CdCia,Compania,CdLinea,Linea,CdGrupo,Grupo,CdMarca,Marca,CdVend,V.RazonSocial AS NombreVend,CdCiudad,Localidad,CdZona,Zona,CdSzona,Subzona
,CdGruCli,GrupoClie,OtrosCrit,CodBod,Bodega,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
 LEFT JOIN Bodegas AS B ON TR.CodBod=B.IdBodega
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,A.CodRuta,Ruta,CodDiaEnt
,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,G.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,TipoFactFletes,A.CdGruClie AS CdAgeGru,GA.GrupoClie AS GrupoAgenc,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,AgencOperando,CdCCBonif,CdSubCCBonif,CupoElectcom
 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 LEFT JOIN Rutas AS RT ON A.CodRuta=RT.IdRuta
 LEFT JOIN GruposCli AS GA ON A.CdGruClie=GA.IdGrupo 
 WHERE  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 (A.Inactivo=ISNULL(@pmInactivo,0) or  A.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY RazonSocial,Agencia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinCuotasAbo] @pmTipDoc VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT C.TipDoc AS CdTipDoc,C.IdPrestamo AS NumDcmto,C.IdCia AS CdCia,CI.Compania AS NomCiaDoc,C.Item AS ItemDcmto,NumCuota,C.Fecha AS FecEmision,FechaVence
,Concepto,VrTotal,VrAbonado,VrTotal-VrAbonado AS VrSaldo,C.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS CdAgencia,NomAgencia AS Agencia
,C.Detalle AS DetalleCuota,TipoCuota,C.TipoCausac AS TipoCau,NumCausacion,IdCiaCau,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,FecUltCausac,DiasLiquida,NumFactura,VrInteresFact,VrAcumCapital
,CodLinCred,LinCredito,Convenio,NitConvenio,NumPreMora,FecUltPago,FecLiqMora,FacturaInt,TipFacInt,NumFacInt,CdCiaFacInt,FechaFacInt,C.FechaCrea AS Fecha_Crea
--Datos del prestamo
,FecPrestamo,VrPrestamo,VrSeguro,VrCuota,NPlazos,Causacion,P.NContrato AS NumContrato,CdCiaCon,NActaJunta,NumCredito,P.IdVend AS CdAsesor,VN.RazonSocial AS Asesor
,TipoCredito,P.TipoCausac AS PreTipoCausac,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas,CuotasMora,P.Observacion AS Observ
,P.Anulado AS PreAnulado,P.IdEstado AS CdEstado
--datos de pagos
,TipRec,Recibo,A.IdCia AS CdCiaAbo,CA.Compania AS NomCiaAbono,A.Item AS ItemAbo,A.Fecha AS FecAbono,A.FecPago AS Fec_Pago,TotalAbono,A.Detalle AS DetalleAbono,TipoAplica
,A.IdVend AS NitVend,VNC.RazonSocial AS NomVendedor,VrBaseCms,Comision,OrigenRc 
--Datos del tercero
,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.e_mail AS TercEmail
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,Comentarios
,TipoSalario,MaxDcto,VrSalBasico
,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 TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN LineasCred AS LC ON C.CodLinCred=LC.IdLinea
 LEFT JOIN Trn_FinAbonos AS A ON C.TipDoc=A.TipDoc AND C.IdPrestamo=A.IdPrestamo AND C.IdCia=A.IdCiaPre AND C.Item=A.ItemPre
 LEFT JOIN Terceros AS VNC ON A.IdVend=VNC.IdTercero LEFT JOIN Companias AS CA ON A.IdCia=CA.IdCia
 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 Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN (SELECT R.TipDoc AS RecTipo,R.Recibo AS RecNumero,R.IdCia AS RecCia,R.OrigenAdd AS OrigenRc FROM Trn_FinRecibos AS R 
 UNION ALL SELECT D.TipDev,D.Devolucion,D.IdCia,RC.OrigenAdd FROM Trn_FinDevRec AS D INNER JOIN Trn_FinRecibos AS RC ON D.TipDoc=RC.TipDoc AND D.Recibo=RC.Recibo AND D.IdCiaDoc=RC.IdCia)
 AS RCJ ON A.TipRec=RCJ.RecTipo AND A.Recibo=RCJ.RecNumero AND A.IdCia=RCJ.RecCia
 WHERE C.TipDoc LIKE ISNULL(@pmTipDoc,'%')   AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CdTipoOper,TipoOperacion,T.ImagenDoc1,T.ImagenDoc2,T.ImagenDoc3
 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 LEFT JOIN TiposOperac AS TP ON C.CdTipoOper=TP.IdTipoOper
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryFinCuotasInt] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT F.TipDoc AS TipoPrest,F.IdPrestamo AS NumPrest,F.IdCia AS CdCia,Compania,Item,NumCuota,F.Fecha AS FecCausac,FechaVence,VrTotal,VrAbonado
,TipoTasa,F.DTF_EA AS TasasDTFea,TasaNom,F.Spread_TA AS TasaPuntos,TasaEfe,F.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS  CdAgencia,NomAgencia AS Agencia
,TipoCuota,F.Concepto AS ConcepCuota,F.TipoCausac AS TipoDocCau,NumCausacion,IdCiaCau,Detalle,FecUltPago,FecLiqMora,NumFactura,DiasLiquida,FecUltCausac,VrAcumCapital,Convenio,NitConvenio
,NumPreMora,FacturaInt,TipFacInt,NumFacInt,CdCiaFacInt,FechaFacInt
--datos del prestamo
,NumCredito,P.Fecha AS FecRadica,FecPrestamo,FecCorte,VrPrestamo,VrSeguro,VrCuota,NPlazos,TipoPlazo,Causacion,P.IdLinea AS CdLinea,LinCredito,TipoCredito,P.TipoCausac AS Tipo_Causac,P.IdTasa AS CdTasa,DescTasa
,AcumCapital,AcumIntereses,VrPrestamo-AcumCapital AS SaldoCapital,AcumCuotas,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas
,P.IdVend AS CdVend,VN.RazonSocial AS NomAsesor,Refinanciado,NumPrestRef,CiaPrestRef,VrRefinanc,DescRefinanc,P.IdConcepto AS CdConcepto,C.Concepto AS DescConcepto,P.Anulado AS PreAnulado,P.Observacion AS Observ
,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.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_FinCuotas AS F INNER JOIN Companias AS CI ON F.IdCia=CI.IdCia  INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero 
 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
 LEFT JOIN Trn_FinPrestamo AS P ON F.TipDoc=P.TipDoc AND F.IdPrestamo=P.IdPrestamo AND  F.IdCia=P.IdCia
 LEFT JOIN Terceros AS VN ON P.IdVend=VN.IdTercero  LEFT JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea 
 LEFT JOIN TasasInteres AS TI ON P.IdTasa=TI.IdTasa LEFT JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto
 LEFT JOIN Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND  F.Concepto IN ('INTERESES','INTMORA')
 AND  F.IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdCliente LIKE ISNULL(@pmIdCliente,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,OrigenAdd,Anulado,FecDev,O.Observacion AS Observ
,NumAprob,FecAprob,CdUsuAprob,NivelAprob,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEgrAnticipoLta] @pmSaldo DECIMAL(14,2),@pmIdCuenta VARCHAR(16)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
,@pmTipCom VARCHAR(3)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT A.IdCuenta AS CdCuenta,NomCuenta,A.IdTercero AS NitTercero,RazonSocial,TipCom,TipoCom,Comprobante,A.IdCia AS CdCia,Compania
,Item,Fecha,VrAnticipo,VrAplicado,VrAnticipo-VrAplicado AS ValorSaldo
--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_EgrAnticipo AS A INNER JOIN Terceros AS T ON A.IdTercero=T.IdTercero
 INNER JOIN Puc AS P ON A.IdCuenta=P.IdCuenta INNER JOIN  Companias AS CI ON A.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
 INNER JOIN TiposCom AS TC ON A.TipCom=TC.IdCom  
 LEFT JOIN TercProvee AS TP ON A.IdTercero=TP.IdProv  LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Bancos AS BP ON TP.IdBanco=BP.IdBanco
 WHERE (VrAnticipo-VrAplicado)>@pmSaldo AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 AND A.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND  A.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.TipCom LIKE ISNULL(@pmTipCom,'%' )
 AND (A.Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND A.Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY A.IdCuenta,RazonSocial,Fecha
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEgrAnticipoApl] @pmSaldo DECIMAL(14,2),@pmIdCuenta VARCHAR(16)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
,@pmTipCom VARCHAR(3)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT A.IdCuenta AS CdCuenta,NomCuenta,A.IdTercero AS NitTercero,RazonSocial,A.TipCom AS CdTipCom,TC.TipoCom AS TipoComAnt,A.Comprobante AS NumComp
,A.IdCia AS CdCia,CI.Compania AS CompaniaAnt,A.Item AS ItemAnt,A.Fecha AS FechaAnt,VrAnticipo,VrAplicado,VrAnticipo-VrAplicado AS ValorSaldo
,F.TipCom AS FacCdTipo,TCF.TipoCom AS FacTipoComp,F.Comprobante AS FacNumComp,F.IdCia AS FacCiaCom,CIF.Compania AS FacCompania,F.Item AS FacItemCom,VrAbonado,TipFac,Factura,IdCiaFac
,ItemFac,FecFactura,ItemCom
--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_EgrAnticipo AS A INNER JOIN Terceros AS T ON A.IdTercero=T.IdTercero
 INNER JOIN Puc AS P ON A.IdCuenta=P.IdCuenta INNER JOIN  Companias AS CI ON A.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
 INNER JOIN TiposCom AS TC ON A.TipCom=TC.IdCom  
 LEFT JOIN TercProvee AS TP ON A.IdTercero=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_EgrAntAbonos AS F ON A.TipCom=F.TipCan AND A.Comprobante=F.nAnticipo AND A.IdCia=F.IdCiaAnt AND A.Item=F.ItemAnt
 LEFT JOIN TiposCom AS TCF ON F.TipCom=TCF.IdCom  LEFT JOIN  Companias AS CIF ON F.IdCia=CIF.IdCia
 WHERE (A.VrAnticipo-A.VrAplicado)>@pmSaldo AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 AND A.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND  A.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.TipCom LIKE ISNULL(@pmTipCom,'%' )
 AND (A.Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND A.Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY A.IdCuenta,RazonSocial,A.Fecha
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,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,NivelAprob,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,nRemesa,CdCiaRem,ItemRem,CdProducto,DescripProd,CdForma,NumCheque,CdBanco,BN.Banco AS NomBanco,MulPlazos,N.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,PrestCaja,Modalidad,OrigenAdd
,TipCom,Comprobante,IdCiaCom,VrReteCREE,TarifaRtc,CodTarRtc,N.IdConcFE,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,CdFntePago,FuentePago
 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
 LEFT JOIN FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpCarbono,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,FletesCosto,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpuCons,VrSobtasaCons
,BaseIvaIgp,VrIvaIngProd,ImpAviTab,TarifaAvta,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CauAnticipado
,P.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,P.IdUsuario AS Cdusuario,Usuario
--Datos del detalle
,PC.Item,FecCausac,FecVence,NumDias,PC.VrCuota AS TotalCuota,VrCapital,VrInteres,VrSaldo,TasaEfeMes,TasaEfeDia,PC.Causacion AS NumCausac,CdCiaCausac,VrCuoCausac,VrIntCausac,VrSaldoCausac,VrCustodia,VrProrroga,TasaCustodia
--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,TPS.Tarifa AS TarSeg
,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,Comentarios,CLI.IncRetFte AS CalcularReteFte 
 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 Trn_FinPrestSeguro PS ON P.TipDoc =PS.TipDoc AND P.IdPrestamo=PS.IdPrestamo AND P.IdCia =PS.IdCia 
LEFT JOIN Tablapor TPS ON PS.CdTarSeguro = TPS.IdTarifa 
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,D.VrImpCarbono,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,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc,BaseIvaObsq,VrIvaObsequio
,D.TarifaArf,D.VrAutRetFte,D.TarifaAri,D.VrAutRetIca,DevMasivo,NumRecibo,CdCiaRec,D.CdMotDev,MotivoDev,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,CdFntePago,FuentePago
 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 LEFT JOIN FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente
 LEFT JOIN TiposMotdev AS MD ON D.CdMotDev=MD.IdMotDev
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrImpCarbono,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,O.CdRuta,Ruta,RefPedido,TipFac,Factura,IdCiaFac,FechaFact
,TipRem,Remision,IdCiaRem,FechaRem,NumCotizac,CdCiaCotizac,NumAutoriza,Modalidad,Vigencia,NumAprob,IdCiaApr,FecAprob,CdUsuAprob,DetalleAprob,OrigenAdd,Anulado,FecDev,O.Observacion AS Observ
,O.IdEstado AS CdEstado,Estado,ZonaFrontera,TipoTrans,TipoOrden,TipoModifica,CdBodega,DesBodega,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
 --subconsulta para filtrar por bodega
 LEFT JOIN (SELECT K.TipDoc AS CdTipDoc,K.Documento AS NumDoc,K.IdCia AS CodCia,K.IdBodega AS CdBodega,Bodega AS DesBodega,SUM(K.Salidas) AS SSAL
 FROM Trn_Kardex AS K INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega WHERE K.TipDoc='PED' AND Item=1 GROUP BY K.TipDoc,K.Documento,K.IdCia,K.IdBodega,Bodega) 
 AS KD ON O.TipDoc=KD.CdTipDoc AND O.Pedido=KD.NumDoc AND O.IdCia=KD.CodCia
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrReteCREE,TarifaRtc,CodTarRtc,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,CdFntePago,FuentePago
 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
 LEFT JOIN FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraRemesaNov]  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT R.TipDoc AS TipRem,R.NumOrden AS NumRemesa,R.IdCia AS CdCia,Compania,Fecha,FecDespacho,FecEntrega,HoraLlegada,IdCliente,NomCliente,T.RazonSocial AS NombreClie
,R.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,IdClieFact,IdRemitente,NomRemite,IdDestinatario,NomDestino,IdLocOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,IdLocDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,IdLocFletes,CF.Localidad AS LugarFletes,R.IdVehiculo AS PlacaVeh,R.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,nRemolque,TipoAfiVehic,Modalidad,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos,R.VrDeclarado AS ValDeclarado,R.VrSeguro AS ValSeguro,R.Cantidad AS CantTotal,PesoTotal,Items
,IdMneda,VrTasa,NumPedido,IdCiaPed,FechaPed,TipDcm,NumDocmto,IdCiaDcm,FechaDcm,NumManif,IdCiaManif,EstCumplido,EstFactura,CdConcepto,CR.Concepto AS DesConcRem,CdRutaTarif,TipCom,Comprobante,IdCiaCom,SerieGuia,NumGuia,CdForma,NumCausac,CdCiaCausac,EdoCausac
,Anulado,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,RA.IdVend AS CdVend,VN.RazonSocial AS Vendedor,LugarCargue,LugarDescargue,NomContacto,TelContacto,emlContacto,ContacDestino,TelContacDest,emlContacDest,ManifMintrans,RemMintrans,TipoRuta,TipoMintrans
--datos de conceptos
,Item,C.Descripcion AS Descrpcion,Tarifa,TipoConc,RubroConcep,C.Cantidad AS CantConc,VrUnitario,TarifIva,C.IdConcepto AS CodConcepto,CDV.Concepto AS DescConcepto,CdCuenta,NomCuenta
,NitTercero,TC.RazonSocial AS NomTercero,CdTipoEsc,TipoEscolta,FechaNov,VrBase,TipoTarif,RefConc
--Datos del cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NitRepLeg,NomRepLeg,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,Modelo,Config,V.Descripcion AS VehDescripcion,V.IdPoseedor AS NitPoseedor
 FROM Trn_TraRemesa AS R INNER JOIN Trn_TraRemAnexo AS RA ON R.TipDoc=RA.TipDoc  AND R.NumOrden=RA.NumOrden AND R.IdCia=RA.IdCia
 INNER JOIN Companias AS CN ON R.IdCia=CN.IdCia  INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 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 CO ON R.IdLocOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON R.IdLocDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Localidades AS CF ON R.IdLocFletes=CF.IdLocal  INNER JOIN Terceros AS  CDT ON R.IdConductor=CDT.IdTercero 
 INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado 
 INNER JOIN Vehiculos AS V ON R.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 Trn_TraConceptos AS C ON R.TipDoc=C.TipDoc AND R.NumOrden=C.Documento AND R.IdCia=C.IdCia
 LEFT JOIN ConcDiversos AS CDV ON C.IdConcepto=CDV.IdConcepto
 LEFT JOIN TiposEsc AS TE ON C.CdTipoEsc=TE.IdTipoEsc LEFT JOIN Terceros AS TC ON C.NitTercero=TC.IdTercero
 LEFT JOIN Puc AS P ON C.CdCuenta=P.IdCuenta
 LEFT JOIN Agencias AS A ON R.IdAgencia=A.IdAgencia LEFT JOIN Terceros AS VN ON RA.IdVend=VN.IdTercero  
 LEFT JOIN Conceptos AS CR ON R.CdConcepto=CR.IdConcepto 
 WHERE R.TipDoc='RMT'  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.Modalidad LIKE ISNULL(@pmModalidad,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CdBodega,DesBodega,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
 --subconsulta para filtrar por bodega
 LEFT JOIN (SELECT K.TipDoc AS CdTipDoc,K.Documento AS NumDoc,K.IdCia AS CodCia,K.IdBodega AS CdBodega,Bodega AS DesBodega,SUM(K.Salidas) AS SSAL
 FROM Trn_Kardex AS K INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega WHERE K.TipDoc='REM' AND Item=1 GROUP BY K.TipDoc,K.Documento,K.IdCia,K.IdBodega,Bodega) 
 AS KD ON O.TipDoc=KD.CdTipDoc AND O.Remision=KD.NumDoc AND O.IdCia=KD.CodCia
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,C.TipoCausac AS TipoCau,NumCausacion,IdCiaCau,FecUltPago,FecLiqMora,C.FechaCrea AS Fecha_Crea,CodLinCred
,NumFactura,VrInteresFact,DiasLiquida,FecUltCausac,VrAcumCapital,Convenio,NitConvenio,NumPreMora,FacturaInt,TipFacInt,NumFacInt,CdCiaFacInt,FechaFacInt,TarifIvaInt,VrIvaInt,TarifRetInt,VrRetInt
--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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrAboOtros,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,VrAboCapital,LiqAboCapital
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_PreExtoLta] @pmtmEst CHAR(2),@pmFecInicio SMALLDATETIME,@pmFecCorte SMALLDATETIME,@pmtmTipoRep INT
AS 
SELECT tmIdCliente,T.RazonSocial AS NomCliente,tmIdAgencia,NomAgencia AS Agencia,tmFecha,tmFecVence,tmTipDoc,TipoDoc,tmNumero,tmIdCia,Compania,tmItem,tmCargos,tmAbonos
,tmIdLinea,LinCredito,CupoCredito,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota,tmTotAbonos,tmTipoAbono,tmNCuota,tmIdVend,VN.RazonSocial AS NomVendedor
,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev,tmDTF_EA,tmTasaNom,tmTasaEfe
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia,IdReg,tmTimeSys
,NumCredito,NPlazos,Causacion,FecPrestamo,TipoCredito,P.Observacion AS Observ,ISNULL(SaldoMora,0) AS VrSaldoMora
--Total pagos a capital
,ISNULL(CASE tmTipDoc WHEN 'PR1' THEN (SELECT SUM(Trn_FinAbonos.TotalAbono) 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=EX.tmTipDoc AND Trn_FinAbonos.IdPrestamo=EX.tmNumero AND Trn_FinAbonos.IdCiaPre=EX.tmIdCia
 AND Trn_FinAbonos.Fecha<=@pmFecCorte AND Trn_FinCuotas.Concepto='CAPITAL') ELSE 0 END,0) AS VrPagosCapital
,ISNULL(CASE tmTipDoc WHEN 'PR1' THEN (SELECT COUNT(NumCuota) FROM Trn_FinCuotas WHERE Trn_FinCuotas.TipDoc=EX.tmTipDoc AND Trn_FinCuotas.IdPrestamo=EX.tmNumero AND Trn_FinCuotas.IdCia=EX.tmIdCia
 AND Trn_FinCuotas.Fecha<=@pmFecCorte AND Trn_FinCuotas.Concepto='CAPITAL') ELSE 0 END,0) AS CuotasCausadas
,ISNULL(CASE tmTipDoc WHEN 'PR1' THEN (SELECT SUM(VrTotal) FROM Trn_FinCuotas WHERE Trn_FinCuotas.TipDoc=EX.tmTipDoc AND Trn_FinCuotas.IdPrestamo=EX.tmNumero AND Trn_FinCuotas.IdCia=EX.tmIdCia
 AND Trn_FinCuotas.Fecha<=@pmFecCorte AND Trn_FinCuotas.Concepto='CAPITAL') ELSE 0 END,0) AS TotalCausac
--datos del cliente
,T.TipoId AS ClieTipoId,T.Dv AS ClieDv,T.Direccion AS ClieDireccion,T.IdLocal AS ClieCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS ClieDpto
,T.Telefono AS ClieTelefono,T.Fax AS ClieFax,T.e_mail AS ClieEmail,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento
FROM tm_PreExto AS EX INNER JOIN Terceros AS T ON EX.tmIdCliente=T.IdTercero INNER JOIN  Companias AS CI ON EX.tmIdCia=CI.IdCia
INNER JOIN LineasCred AS LC ON EX.tmIdLinea=LC.IdLinea
INNER JOIN  TercCliePrestamo AS CLI ON EX.tmIdCliente=CLI.IdClie AND EX.tmIdAgencia=CLI.IdAgencia
INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo  INNER JOIN  TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
LEFT JOIN Terceros AS VN ON EX.tmIdVend=VN.IdTercero
LEFT JOIN TercClieLineas AS CL ON EX.tmIdCliente=CL.IdClie AND EX.tmIdAgencia=CL.IdAgencia AND EX.tmIdLinea=CL.IdLinea
LEFT JOIN Sys_TiposDoc AS TD ON EX.tmTipDoc=TD.IdDoc 
LEFT JOIN Trn_FinPrestamo AS P ON EX.tmTipDoc=P.TipDoc AND EX.tmNumero=P.IdPrestamo AND EX.tmIdCia=P.IdCia
LEFT JOIN (SELECT IdCliente,IdAgencia,CodLinCred,SUM(VrTotal-VrAbonado) AS SaldoMora FROM Trn_FinCuotas WHERE Fecha<@pmFecCorte AND VrTotal>VrAbonado GROUP BY IdCliente,IdAgencia,CodLinCred)
AS SM ON EX.tmIdCliente=SM.IdCliente AND EX.tmIdAgencia=SM.IdAgencia AND EX.tmIdLinea=SM.CodLinCred
WHERE tmEst=@pmtmEst AND tmTipoRep=@pmtmTipoRep
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,D.VrImpCarbono,D.VrIvaIngProd,VrNeto,Cantidad,CantPuntos,BaseImp,BaseRet,D.BaseIvaIgp
,VrReteCREE,TarifaRtc,CodTarRtc,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,VrImpCons,BaseIvaObsq,VrIvaObsequio,TarifaArf,VrAutRetFte,TarifaAri,VrAutRetIca,DevMasivo,NumRecibo,CdCiaRec,CdMotDev,MotivoDev
,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,BaseIvp,TarifaIvp,IvaIngProd,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
,FE.EstadoFE,FE.Prefijo,FE.NumFace,FE.CUFE,FE.Resolucion,FE.RangoNum,FE.FecVigencia,FE.CUFE_QR AS QR,FE.FechaValidacion
 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 LEFT JOIN TiposMotdev AS MD ON D.CdMotDev=MD.IdMotDev
 LEFT JOIN Trn_Face AS FE ON D.TipDev=FE.TipDoc AND D.Devolucion=FE.Documento AND D.IdCia=FE.IdCia
 WHERE TipDev=@pmTipDev  AND Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
 AND EsProdBase=0
 ORDER BY Devolucion,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,C.IdProveedor,RazonSocial,C.TipFac,TipoDoc,C.Factura,C.IdCia AS CdCia,Compania
,Item,VrFactura,VrAbonado,VrFactura-VrAbonado AS ValorSaldo,FecEmision,FecVence,DATEDIFF(day,FecVence,@pmFecActual) AS DiasMora
,C.TipDoc,C.Documento,C.IdCiaDoc,C.TipCom,TipoCom,C.Comprobante,C.ItemCom,CM.Fecha AS FechaComp,Detalle,C.Referencia,C.pVehiculo,TipRef,DocRef,IdCiaRef,EstadoApr,MontoAprob,FecProgPago,ObservAprob
--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
 LEFT JOIN Trn_Comprobantes AS CM ON C.TipCom=CM.TipCom AND C.Comprobante=CM.Comprobante AND C.IdCia=CM.IdCia
 WHERE (VrFactura-VrAbonado)>@pmSaldo AND 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDevFcrRelTra] @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 TipDev,Devolucion,DF.IdCia AS CdCia,Compania,Fecha,DF.IdConcepto AS CdConcepto,C.Concepto AS ConcDesc,DF.TipDoc AS TipoFac,DF.Factura AS NumFactura,IdCiaDoc,FecDoc,IdCliente,T.RazonSocial AS NomCliente
,DF.IdAgencia AS IdAgncia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,VrSubTotal,DF.VrDescuento AS VrDcto,DF.VrImpuesto AS VrIva,DF.VrRetencion AS VrRetFte,DF.VrReteICA AS VrRetIca,VrReteIVA,VrFletes,VrOtros,VrCargos
,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal,VrFaltantes,VrAnticipos,VrNeto,DF.Cantidad AS CantTotal,CantPuntos,BaseImp,BaseRet,DF.VrImpCons AS VrAutoICA,DF.ZonaFrontera AS EstAutoICA
,DF.IdVend AS NitVend,VN.RazonSocial AS Vendedor,TarifaCom,DF.IdLocEnv AS CodCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,Pedido,IdCiaPed,Cotizacion,IdCiaCot,FecPedido,VrReteCREE,TarifaRtc,CodTarRtc,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,DF.Observacion AS Observ,DF.IdEstado AS CdEstado
,TimeSys,IdCiaCrea,DF.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,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 DF  INNER JOIN Companias AS CN ON DF.IdCia=CN.IdCia   INNER JOIN Terceros AS T ON DF.IdCliente=T.IdTercero 
 INNER JOIN  Conceptos AS C ON DF.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON DF.IdUsuario=U.IdUsuario
 INNER JOIN  Terceros AS VN ON DF.IdVend=VN.IdTercero INNER JOIN Agencias  AS A ON DF.IdAgencia=A.IdAgencia 
 INNER JOIN TercCliente AS CLI ON DF.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 DF.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 WHERE  DF.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND  DF.TipDev LIKE ISNULL(@pmTipDev,'%')
 AND DF.IdCia LIKE ISNULL(@pmIdCia,'%%') AND DF.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND DF.IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND DF.IdVend LIKE ISNULL(@pmIdVend,'%')
 ORDER BY DF.IdCia,DF.Devolucion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelGruposCli] @pmIdGrupo VARCHAR(4)
 AS 
 DELETE FROM GruposCli
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEntradasNetDet] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS 
 SELECT E.TipDoc,E.Entrada,E.IdCia AS CdCia,Compania,E.Fecha,E.IdConcepto AS CdConcepto,Concepto,E.IdProv AS NitPrevee,T.RazonSocial AS NomProveedor,E.Factura,E.FechaFac,E.FechaVence
,E.VrSubTotal,E.VrDescuento,E.VrImpuesto,E.VrRetencion,E.VrReteICA,E.VrReteIVA,E.VrReteCREE,E.VrFletes,E.VrOtros,E.VrSobretasa,E.VrImpGlobal,E.VrCargos,E.VrOtrDcto,E.VrBomberil,E.VrNeto
,E.NitFletes,TF.RazonSocial AS NomFletes,E.VrRetFlete,E.VrIcaFlete,E.FletesCosto,E.TipOdc,E.OCompra,E.IdCiaOdc,E.MulPlazos,E.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,E.CxPagar,E.DocEquiv,E.BaseImp,E.BaseRet
,E.TarifaRet,E.TarifaIca,E.TarifaRiv,E.TarifaRtc,E.TarifaBom,E.VrIvaMvc,E.VrImpuCons,E.VrSobtasaCons,E.ImpAviTab,E.TarifaAvta,E.CodTarifAvta,E.Modalidad
--detalles
 ,K.Item,K.IdProducto AS CdProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,K.Entradas,K.Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed,K.VrUnitario
,VrCostoEnt,VrCostoSal,K.TarifaDct,VrDctoEnt,VrDctoSal,K.TarifaIva,VrIvaEnt,VrIvaSal,K.Sobretasa,K.Soldicom,K.ImpGlobal,K.VrCostProm,K.Unidades,K.Descripcion,K.Referencia,Referencia2
,K.TipOrd,K.NumOrden,K.IdCiaOrd,TipDocDev,K.NumDocDev,OtroImpto,K.VrBruto,K.VrBase,K.Servcios,K.VrImvCosto,K.TarifaIco,K.VrImpCon,K.CantObseq,K.VrIvaObseq,K.BaseIvp,K.TarifaIvp,K.IvaIngProd,K.CdTanque,K.CdCCosto,CCosto,K.CdSubCos,SubCosto
,E.TipCom,E.Comprobante,E.IdCiaCom,E.Anulado,E.NumDev,FecDev,E.Observacion AS Observ,E.IdEstado AS CdEstado,Estado,E.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 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 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
 LEFT JOIN CentroCosto AS CC ON K.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON K.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 E.TipDoc=@pmTipDoc AND E.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND E.IdCia LIKE ISNULL(@pmIdCia,'%%')
 UNION ALL
 SELECT D.TipDev,D.Devolucion,D.IdCia,Compania,D.Fecha,D.IdConcepto,Concepto,D.IdProv,T.RazonSocial,D.Factura,E.FechaFac,E.FechaVence
,D.VrSubTotal,D.VrDescuento,D.VrImpuesto,D.VrRetencion,D.VrReteICA,D.VrReteIVA,D.VrReteCREE,D.VrFletes,D.VrOtros,D.VrSobretasa,D.VrImpGlobal,D.VrCargos,D.VrOtrDcto,D.VrBomberil,D.VrNeto
,D.NitFletes,TF.RazonSocial,D.VrRetFlete,D.VrIcaFlete,D.FletesCosto,E.TipOdc,E.OCompra,E.IdCiaOdc,MulPlazos,E.IdPlazo,Plazo,NVmto,DiasPago,D.CxPagar,D.DocEquiv,D.BaseImp,D.BaseRet
,E.TarifaRet,E.TarifaIca,E.TarifaRiv,D.TarifaRtc,D.TarifaBom,D.VrIvaMvc,D.VrImpuCons,D.VrSobtasaCons,D.ImpAviTab,D.TarifaAvta,E.CodTarifAvta,D.Modalidad
,K.Item,K.IdProducto,DescripProd,K.IdBodega,Bodega,K.Entradas,K.Salidas,K.IdUnd,UM.Unidad,K.VrUnitario
,VrCostoEnt,VrCostoSal,K.TarifaDct,VrDctoEnt,VrDctoSal,K.TarifaIva,VrIvaEnt,VrIvaSal,K.Sobretasa,K.Soldicom,K.ImpGlobal,K.VrCostProm,K.Unidades,K.Descripcion,K.Referencia,Referencia2
,K.TipOrd,K.NumOrden,K.IdCiaOrd,TipDocDev,K.NumDocDev,OtroImpto,K.VrBruto,K.VrBase,K.Servcios,K.VrImvCosto,K.TarifaIco,K.VrImpCon,K.CantObseq,K.VrIvaObseq,K.BaseIvp,K.TarifaIvp,K.IvaIngProd,K.CdTanque,K.CdCCosto,CCosto,K.CdSubCos,SubCosto
,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Entrada,FecDoc,D.Observacion,D.IdEstado,Estado,D.TimeSys,D.FecUpdate,D.IdCiaCrea,D.IdUsuario,Usuario
,T.TipoId,T.Dv,T.Codigo,T.NomCial,T.Direccion,T.IdLocal,Localidad,L.IdDep,Departamento,T.Telefono,T.Fax,T.e_mail,T.IdSector,SectorEco,T.IdRegimen,Regimen,T.TipEnte
,TP.IdGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga,Autoret
 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 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 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 Plazos AS PZ ON E.IdPlazo=PZ.IdPlazo
 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 CC ON K.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON K.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,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,F.TarifaArf,F.VrAutRetFte,F.TarifaAri,F.VrAutRetIca,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.TarifaArf,D.VrAutRetFte,D.TarifaAri,D.VrAutRetIca,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDevFcrRelTraDet] @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 TipDev,Devolucion,DF.IdCia AS CdCia,Compania,Fecha,DF.IdConcepto AS CdConcepto,C.Concepto AS ConcDesc,DF.TipDoc AS TipoFac,DF.Factura AS NumFactura,IdCiaDoc,FecDoc,IdCliente,T.RazonSocial AS NomCliente
,DF.IdAgencia AS IdAgncia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,VrSubTotal,DF.VrDescuento AS VrDcto,DF.VrImpuesto AS VrIva,DF.VrRetencion AS VrRetFte,DF.VrReteICA AS VrRetIca,VrReteIVA,VrFletes,VrOtros,VrCargos
,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal,VrFaltantes,VrAnticipos,VrNeto,DF.Cantidad AS CantTotal,CantPuntos,BaseImp,BaseRet
,DF.IdVend AS NitVend,VN.RazonSocial AS Vendedor,TarifaCom,DF.IdLocEnv AS CodCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,Pedido,IdCiaPed,Cotizacion,IdCiaCot,FecPedido,VrReteCREE,TarifaRtc,CodTarRtc,DF.VrImpCons AS VrAutoICA,DF.ZonaFrontera AS EstAutoICA,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,DF.Observacion AS Observ,DF.IdEstado AS CdEstado
,TimeSys,IdCiaCrea,DF.IdUsuario AS IdUsuari,Usuario
--detalles
,Item,TipoReg,FechaFact,D.TipRem AS TipRemesa,D.Remesa AS NumRemesa,D.IdCiaRem AS CdCiaRem,ItemRem,FecRemesa,Descripcion,D.Cantidad AS Cant,VrUnitario,VrCosto,UndTarifa,UndCosto,Unidades,PesoNeto,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,Volumen,UndVol,Cases,Cajas,Palets,CantPago
,D.TarifaIva AS DetTarifIva,D.VrImpuesto AS DetVrIva,TarifaDct,D.VrDescuento AS DetVrDcto,D.TarifaRet AS DetTarifRet,D.VrRetencion AS DetVrRetFte,D.TarifaIca AS DetTarifIca,D.VrReteIca AS DetVrRetIca,D.VrAutRetIca AS DetAutoICA,VrFaltante,D.Remision AS DetNumRemsion,DocCliente,Referencia1,Referencia2,Referencia3
,CdMercancia,DescripMcia,CdConcepto,CF.Concepto AS DetConcepto,CdCCosto,CCosto,D.CdSubCos AS CdSubCosto,SubCosto,NitTercero,NT.RazonSocial AS DetTercero,CdAgencia,D.pVehiculo AS PlacaVeh,TipoAfiVehic,IdOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,IdDestino,LD.Localidad AS CiudadDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,D.Anulado AS DetAnulado,TipDocRef,NumDocRef,IdCiaRef,FecDocRef,CantidadFalt,UnidadFalt,NumPedRem,CiaPedRem
--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
 FROM Trn_DevFcr AS DF  INNER JOIN  Trn_TraFacRemesas AS D ON DF.TipDev=D.TipDoc  AND DF.Devolucion=D.Factura  AND DF.IdCia=D.IdCia
 INNER JOIN Companias AS CN ON DF.IdCia=CN.IdCia   INNER JOIN Terceros AS T ON DF.IdCliente=T.IdTercero 
 INNER JOIN  Conceptos AS C ON DF.IdConcepto=C.IdConcepto
 INNER JOIN adm_Usuarios AS U ON DF.IdUsuario=U.IdUsuario
 INNER JOIN  Terceros AS VN ON DF.IdVend=VN.IdTercero INNER JOIN Agencias  AS A ON DF.IdAgencia=A.IdAgencia 
 INNER JOIN TercCliente AS CLI ON DF.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 Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 LEFT JOIN  Localidades AS LE ON DF.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed
 LEFT JOIN Mercancias AS MC ON D.CdMercancia=MC.IdMercancia LEFT JOIN  ConcDiversos AS CF ON D.CdConcepto=CF.IdConcepto
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS NT ON D.NitTercero=NT.IdTercero
 WHERE  DF.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND  DF.TipDev LIKE ISNULL(@pmTipDev,'%')
 AND DF.IdCia LIKE ISNULL(@pmIdCia,'%%') AND DF.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND DF.IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND DF.IdVend LIKE ISNULL(@pmIdVend,'%')
 ORDER BY DF.IdCia,DF.Devolucion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryFinPrestamoFac] @pmTipDoc VARCHAR(3), @pmIdPrestamoIni INT,@pmIdPrestamoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT P.TipDoc,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,CauAnticipado
,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,DiasPlazo
--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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenServReq] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT O.TipDoc,TipoDoc,O.NumOrden,O.IdCia AS CdCia,Compania,O.Fecha,O.FecDespacho,O.IdTipoServ AS CdTipoServ,TipoServicio,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia
,O.IdOrigen,LO.Localidad AS CiuOrigen,LO.IdDep AS CdDepOrig,DPO.Departamento AS DptoOrigen,O.IdDestino,LD.Localidad AS CiuDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino
,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest,O.VrServicio,O.VrEmpaque,O.VrBodega,O.VrOtros,O.VrDescuento,O.VrImpuesto,O.VrNeto,O.VrDeclarado,O.VrSeguro
,O.IdVend,VN.RazonSocial AS NomVendedor,O.NomContacto,O.TelsContacto,O.emlContacto,O.ContactoDest,O.emlContDest,O.Servicios,O.FormaPago,O.Referencia,O.TipInv,O.NumInvent,O.IdCiaInv,O.TipRem,O.NumRemesa,O.IdCiaRem
,O.EstFactura,O.TipCausac,O.Causacion,O.CdCiaCau,O.EstCumplido,O.FechaCump,O.TipoRuta,O.Volumen,EstadoBod,O.CdBodega,B.Bodega AS NomBodega,FechaIngBod,FechaRetBod,O.Anulado,O.FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,ED.Estado
,O.OrigenAdd,O.TimeSys AS FechaCrea,O.FecUpdate,O.IdCiaCrea,O.IdUsuario AS CdUsuario,Usuario
--datos de la requisc
,RQ.TipReq,RQ.Requisicion,RQ.CdCiaReq,RQ.FechaReq,RQ.IdRespons,NR.RazonSocial AS Responsable,RQ.CdDep,Dependencia,RQ.NumAprob,RQ.FecAprob,RQ.TipoVigencia,RQ.Observacion AS ReqObservacion,RQ.IdEstado AS ReqIdEstado,ER.Estado AS ReqEstado
,RQ.Item,RQ.CdProducto,DescripProd,RQ.Descripcion,RQ.IdSubgrupo,Subgrupo,RQ.CdBodega AS ReqCdBodega,BG.Bodega AS ReqBodega,RQ.Cantidad,RQ.IdUnd AS CdUnd,Unidad,RQ.VrUnitario,RQ.TipOrd AS TipOdc,RQ.OrdenComp,RQ.IdCiaOc
,RQ.TipSal,RQ.NSalida,RQ.IdCiaSal,RQ.FechaSal,RQ.CantSalida,RQ.CantDevSal,RQ.Referencia AS ReqReferencia
--datos del tercero
,T.Codigo AS TercCod,T.TipoId,T.Dv,T.Direccion,T.IdLocal AS TercCdLocal,L.Localidad AS TercLocalidad,T.Telefono AS TercTelefono,T.TelMovil AS TercTerCelular,T.e_mail AS TercEmail
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupo,GrupoClie,CLI.IdPlazo AS CdPlazo,Plazo
 FROM Trn_MudOrdenServ AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie
 INNER JOIN Terceros AS VN ON O.IdVend=VN.IdTercero INNER JOIN MudServicios AS TS ON O.IdTipoServ=TS.IdTipoServ
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal INNER JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona 
 LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo LEFT JOIN Plazos AS PZ ON CLI.IdPlazo=PZ.IdPlazo
 LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia LEFT JOIN AlmBodegas AS B ON O.CdBodega=B.IdBodega
 --requisiciones
 LEFT JOIN (SELECT R.NContrato AS NumOrden,R.IdCiaCont AS CdCiaOrden,R.TipDoc AS TipReq,R.Requisicion,R.IdCia AS CdCiaReq,R.Fecha AS FechaReq
,R.IdRespons,R.IdDep AS CdDep,R.NumAprob,R.FecAprob,R.NivelAprob,R.TipoVigencia,R.Num_Vehic,R.Observacion,R.IdEstado
,D.Item,D.CdProducto,D.Descripcion,D.IdSubgrupo,D.CdBodega,D.Cantidad,D.IdUnd,D.VrUnitario,D.TipOrd,D.NumOrden AS OrdenComp,D.IdCiaOrd AS IdCiaOc
,D.TipSal,D.NSalida,D.IdCiaSal,D.FechaSal,D.CantSalida,D.CantDevSal,D.Referencia
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='REQ' AND R.Modalidad='MUDANZAS' AND R.Anulado=0) AS RQ ON O.NumOrden=RQ.NumOrden AND O.IdCia=RQ.CdCiaOrden
 LEFT JOIN Terceros AS NR ON RQ.IdRespons=NR.IdTercero LEFT JOIN Dependencias AS DP ON RQ.CdDep=DP.IdDep
 LEFT JOIN EstadoDoc AS ER ON RQ.IdEstado=ER.IdEstado LEFT JOIN ProdMcias AS P ON RQ.CdProducto=P.IdProducto
 LEFT JOIN UndMed AS UM ON RQ.IdUnd=UM.IdUnd LEFT JOIN SubGrupos AS SG ON RQ.IdSubgrupo=SG.IdSubgrupo
 LEFT JOIN Bodegas AS BG ON RQ.CdBodega=BG.IdBodega
WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmImpConEnt,tmImpConSal,tmImpGlobalEnt,tmImpGlobalSal,tmOtrosEnt,tmOtrosSal
,tmUnidVolumen,tmEntradas2,tmSalidas2,tmCostoEnt2,tmCostoSal2,tmValorEnt2,tmValorSal2,tmDctosEnt2,tmDctosSal2,tmIvaEnt2,tmIvaSal2,tmImpConEnt2,tmImpConSal2,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinPrestamoDsb] @pmTipDoc VARCHAR(3),@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmTipoCredito VARCHAR(10)=Null
,@pmIdLinea VARCHAR(4)=Null
AS 
 SELECT P.IdPrestamo AS NumPrest,P.IdCia AS CdCia,Compania,P.Fecha AS FecRadica,FecPrestamo,FecCorte
,T.RazonSocial AS NomCliente,IdCliente,NomAgencia AS Agencia,P.IdAgencia AS CdAgencia,P.IdConcepto AS CdConcepto,Concepto
,VrPrestamo,VrSeguro,VrIntereses,VrNeto,VrCuota,P.IdLinea AS CdLinea,LinCredito,P.IdTasa AS CdTasa,DescTasa,DTF_EA,Spread_TA,TasaEfectiva
,NPlazos,TipoPlazo,Causacion,TipoCredito,TipoCausac,P.CalcIntMora AS CalcMora,P.IdMora AS CdMora,DescMora,TasaEA,TasaEM,NumCredito,P.IdVend AS CdVend,VN.RazonSocial AS NomAsesor
,CxPagar,NumCuotaIni,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas,CuotasMora
,CuotasConv,TipoAprob,Refinanciado,NumPrestRef,CiaPrestRef,VrRefinanc,DescRefinanc,CauAnticipado,P.IdEstado AS CdEstado,Estado,P.Observacion AS Observ
,Anulado,NumDev,FecDev
,TipEgr,NumEgreso,IdCiaEgr,FecEgreso,VrEgreso
--cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,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,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 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 LineasCred AS LC ON P.IdLinea=LC.IdLinea
 LEFT JOIN Trn_FinPrestEgreso AS EG ON P.TipDoc=EG.TipDoc AND P.IdPrestamo=EG.IdPrestamo AND P.IdCia=EG.IdCia
 LEFT JOIN TasasIntmora AS TIM ON P.IdMora=TIM.IdMora
 WHERE P.TipDoc=@pmTipDoc AND P.IdCia LIKE ISNULL(@pmIdCia,'%%') AND P.IdCliente LIKE ISNULL(@pmIdCliente,'%') 
 AND P.TipoCredito LIKE ISNULL(@pmTipoCredito,'%') AND P.IdLinea LIKE ISNULL(@pmIdLinea,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinAbonosRca] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=NULL
,@pmIdCliente VARCHAR(16)=Null
AS 
SELECT RA.IdCliente AS NitClie,T.RazonSocial AS NomCliente,RA.IdAgencia AS CdAgencia,NomAgencia AS Agencia,RA.TipDoc AS TipPre,RA.IdPrestamo AS NumPrestamo
,IdCiaPre,CP.Compania AS NomCiaPre,ItemPre,NumCuota,C.Fecha AS FecEmision,FechaVence,Concepto,C.VrTotal AS VrCuota,VrAbonado
--datos del pago
,TipRec,Recibo,RA.IdCia AS CdCia,CI.Compania AS NomCiaAbono,RA.Item AS ItemPago,RA.Fecha AS FecRecibo,FecPago,TotalAbono,RA.Detalle AS DescPago,TipoAplica
,RA.IdVend AS CedVend,VN.RazonSocial AS Vendedor,Comision,DTF_EA,TasaNom,Spread_TA,TasaEfe,TipoCuota,C.TipoCausac AS TipCausac,NumCausacion,IdCiaCau,C.Detalle AS DescCuota
,CodLinCred,LinCredito,NumFactura,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,CLI.IdGrupo AS CdGrupo,GrupoClie
,VrPrestamo,P.VrCuota AS VrCuoPrest,NPlazos,Causacion,TipoCredito,P.TipoCausac AS Tipo_Causac,NumCredito,P.IdVend AS PreCedVend,VND.RazonSocial AS PreVendedor,P.Observacion AS Observ
FROM Trn_FinAbonos AS RA INNER JOIN Trn_FinCuotas AS C ON RA.TipDoc=C.TipDoc AND RA.IdPrestamo=C.IdPrestamo AND RA.IdCiaPre=C.IdCia AND RA.ItemPre=C.Item
INNER JOIN Companias AS CI ON RA.IdCia=CI.IdCia INNER JOIN Terceros AS T ON RA.IdCliente=T.IdTercero
INNER JOIN Terceros AS VN ON RA.IdVend=VN.IdTercero INNER JOIN Companias AS CP ON RA.IdCiaPre=CP.IdCia 
LEFT JOIN LineasCred AS L ON C.CodLinCred=L.IdLinea
LEFT JOIN TercCliePrestamo AS CLI ON RA.IdCliente=CLI.IdClie AND RA.IdAgencia=CLI.IdAgencia
LEFT JOIN TiposTerc AS TC ON CLI.IdTipoTerc=TC.IdTipoTerc LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
LEFT JOIN Trn_FinPrestamo AS P ON RA.TipDoc=P.TipDoc AND RA.IdPrestamo=P.IdPrestamo AND RA.IdCiaPre=P.IdCia
LEFT JOIN Terceros AS VND ON P.IdVend=VND.IdTercero
WHERE RA.FecPago BETWEEN @pmFechaIni AND @pmFechaFin AND RA.IdCia LIKE ISNULL(@pmIdCia,'%%')
AND RA.IdCliente LIKE ISNULL(@pmIdCliente,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpCarbono,VrIvaIngProd,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,BaseIvaIgp
,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,CodTarifBom,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpuCons,VrSobtasaCons,E.ImpAviTab,E.TarifaAvta,E.CodTarifAvta,CdCCosto,CCosto,CdSubCos,SubCosto
,TipEgr,Egreso,IdCiaEgr,E.Modalidad,TipoSA,NumSalAlm,CdCiaSA,TipSop,NumDocSop,CdCiaSop,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 F.Factura,F.IdCia AS CdCia,Compania,F.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
,TipoFactInt,TipoFE,FechaFE,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado
,F.TimeSys,F.FecUpdate AS FechaAct,IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario,F.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
,FE.EstadoFE,FE.Prefijo,FE.NumFace,FE.CUFE,FE.Resolucion,FE.RangoNum,FE.FecVigencia
 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
 LEFT JOIN Trn_Face AS FE ON F.TipDoc=FE.TipDoc AND F.Factura=FE.Documento AND F.IdCia=FE.IdCia
 WHERE F.TipDoc=@pmTipDoc  AND F.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,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmImpConEnt,tmImpConSal,tmEntradas2,tmSalidas2,tmCostoEnt2,tmCostoSal2,tmValorEnt2,tmValorSal2,tmDctosEnt2,tmDctosSal2,tmIvaEnt2,tmIvaSal2,tmImpGlobalEnt2,tmImpGlobalSal2,tmOtrosEnt2,tmOtrosSal2,tmUnidVolumen2
,tmImpConEnt2,tmImpConSal2,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,Factor1,Factor2
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CauAnticipado,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrImpCarbono,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,TipoOrden,Remolque,Anulado,FecDev,O.Observacion AS Observ,IdTerminal,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,AutoIca,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,TipoBandera,PrendGarant,FecUpCupo,TipoCliente,CodClieSicom,CdDiaEnt,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,ComIndustrial,NumLista,Termicas,CodRetCom,CodCCosto,CodSubCosto,T.FechaNac,T.ImagenDoc1,T.ImagenDoc2,T.ImagenDoc3
 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 LEFT JOIN TiposBan TB ON C.CdBandera=TB.IdBandera
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,NumPedido,CdCiaPed,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,nRemesa,CdCiaRem,ItemRem,CdProducto,DescripProd
,CdForma,N.NumCheque AS NroCheque,CdBanco,BN.Banco AS NomBanco,MulPlazos,N.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,PrestCaja,Modalidad,VrReteCREE,TarifaRtc,CodTarRtc,IdConcFE,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
,FE.EstadoFE,FE.Prefijo,FE.NumFace,FE.CUFE,FE.Resolucion,FE.RangoNum,FE.FecVigencia,FE.CUFE_QR AS QR,FE.FechaValidacion
 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
 LEFT JOIN Trn_Face AS FE ON N.TipDoc=FE.TipDoc AND N.NumNota=FE.Documento AND N.IdCia=FE.IdCia
 WHERE N.TipDoc=@pmTipDoc AND NumNota BETWEEN @pmNumNotaIni AND @pmNumNotaFin AND N.IdCia=@pmIdCia
 ORDER BY  NumNota
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEdsFormasRelDet] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente 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,FD.VrPrecio,FD.Cantidad*FD.VrPrecio AS ValorTotal,TarifaDct,VrDcto,TarifaIva,VrIva
,Servicios,Descripcion,ListaPrec,FD.CantDev,FD.CantFact,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.VrPrecio AS VrPrecioPub,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.NumRef2,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,FD.CdOperario,OP.RazonSocial AS Operario,FD.Comision
 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 LEFT JOIN Terceros AS OP ON FD.CdOperario=OP.IdTercero
 WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (F.IdCia=@pmIdCia OR @pmIdCia IS NULL)
 AND (F.IdCliente=@pmIdCliente OR @pmIdCliente IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrReteCREE,TarifaRtc,CodTarRtc,VrDevFact,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,SM.Unidad
,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,Tallaje,TipoZonaFront,ExcluidoImp,Electrocomb,IvaDetCombo,LtaBaseIva,FecUltcom,FecUltVta,CodMcia,DescripLong,CodFEDian,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
 LEFT JOIN Sys_Um AS SM ON P.UndMed=SM.UndMed
 --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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinPrestResEgr]  @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
,p.Anulado,TI.TipoInteres AS Tipo_Int,TasaEfectiva,TipoPeriodo,CausaInteres,DTF_EA,Spread_TA,OpcCompra
,FA.Almacen 
--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
,PE.VrEgreso ,PE.FecEgreso As Fechaegreso,CE.NumCheque, CE.FecCheque,CE.FecDev, CE.IdCta,CP.NumeroCta, CP.IdCuenta AS CtaContBanco,CE.EnEfectivo  
 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
 LEFT JOIN FinAlmacenes AS FA ON P.CdAlmacen=FA.IdAlmacen
 LEFT JOIN Trn_FinPrestEgreso PE ON P.TipDoc=PE.TipDoc AND P.IdPrestamo =PE.IdPrestamo AND P.IdCia =PE.IdCia 
 LEFT JOIN Trn_Comprobantes CE ON PE.TipEgr =CE.TipCom AND PE.NumEgreso =CE.Comprobante AND PE.IdCiaEgr=CE.IdCia 
 LEFT JOIN CtasCorrientes CP ON CE.IdCta=CP.IdCta 
  WHERE  P.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND P.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND P.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND P.TipoCredito LIKE ISNULL(@pmTipoCredito,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrReteCREE,TarifaRtc,CodTarRtc,VrDevFact,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,CdFntePago,FuentePago
 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
 LEFT JOIN FuentesPago AS FPG ON A.CdFntePago=FPG.IdFuente
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,D.ImpAviTab,D.TarifaAvta
,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,FletesCosto,VrIvaMvc,D.VrImpCarbono,CxPagar,DocEquiv,BaseImp,BaseRet,D.CdCCosto AS CodCenCosto,CC.CCosto AS CentCosto
,VrReteCREE,TarifaRtc,CodTarRtc,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,D.VrImpuCons,D.VrSobtasaCons,D.BaseIvaIgp,D.VrIvaIngProd,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,CodTarIco,TarifaIco,VrImpCon,TarifaStc,SobtasaCons,VrImvCosto,K.BaseIvp,K.TarifaIvp,K.IvaIngProd
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryFinFacturasInt]  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME, @pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT P.TipDoc AS TipPrest,P.IdPrestamo AS NumPrest,P.IdCia AS CdCia,Compania,Item,NumFactura,F.Fecha AS FecCausac,FechaVence,VrFactura,VrInteres,DiasLiq,TasaEfeMes,TasaEfeDia,FechaPago
,FechaLiq,FecInicio,FecFinal,VrAbono,ItemPago,Facturado,TipFac,FacturaInt,CdCiaFac,FecFactura,DiasPlazo
,T.RazonSocial AS NomCliente,IdCliente,NomAgencia AS Agencia,P.IdAgencia AS CdAgencia,NumCredito,P.Fecha AS FecRadica,FecPrestamo,FecCorte
,VrPrestamo,VrSeguro,VrCuota,NPlazos,TipoPlazo,Causacion,P.IdLinea AS CdLinea,LinCredito,TipoCredito,TipoCausac,P.IdTasa AS CdTasa,DescTasa
,AcumCapital,AcumIntereses,VrPrestamo-AcumCapital AS SaldoCapital,AcumCuotas,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas
,P.IdVend AS CdVend,VN.RazonSocial AS NomAsesor,Refinanciado,NumPrestRef,CiaPrestRef,VrRefinanc,DescRefinanc,P.IdConcepto AS CdConcepto,Concepto,P.Observacion AS Observ
,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.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 Trn_FinFacturas AS F ON P.TipDoc=F.TipDoc AND P.IdPrestamo=F.IdPrestamo AND P.IdCia=F.IdCia
 INNER JOIN TasasInteres AS TI ON P.IdTasa=TI.IdTasa INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto
 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 LineasCred AS LC ON P.IdLinea=LC.IdLinea  INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia
 INNER JOIN TercCliePrestamo AS CLI ON P.IdCliente=CLI.IdClie AND P.IdAgencia=CLI.IdAgencia
 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 Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE F.FechaLiq BETWEEN @pmFechaIni AND @pmFechaFin AND P.TipoCredito='FACTURAS'  AND P.Anulado=0
 AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND P.IdCliente LIKE ISNULL(@pmIdCliente,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrImpCarbono,O.VrIvaIngProd,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,IdTerminal,ZonaFrontera,TipoOrden,Remolque,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,BaseIvp,TarifaIvp,IvaIngProd,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
,A.codsicom,DM.Comentarios
 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
 LEFT JOIN Trn_DocMemo DM On O.TipDoc=DM.TipDoc AND O.Ocargue=DM.Documento AND O.idcia=DM.Idcia
  WHERE O.TipDoc=@pmTipDoc AND OCargue BETWEEN @pmOCargueIni AND @pmOCargueFin AND O.IdCia=@pmIdCia
 AND EsProdBase=0
 ORDER BY OCargue,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrReteCREE,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrBomberil,VrIvaIngProd,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,BaseIvaIgp
,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,TarifaBom,VrIvaMvc,BaseIvaObsq,VrIvaObsequio,VrImpuCons,VrSobtasaCons,E.ImpAviTab,E.TarifaAvta,E.CodTarifAvta,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.VrReteCREE,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.VrBomberil,D.VrIvaIngProd,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,D.BaseIvaIgp,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,D.TarifaRtc,D.TarifaBom,D.VrIvaMvc,D.BaseIvaObsq,D.VrIvaObsequio,D.VrImpuCons,D.VrSobtasaCons
,D.ImpAviTab,D.TarifaAvta,E.CodTarifAvta,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDevFcrFmt] @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,TipoDoc,Devolucion,DF.IdCia AS CdCia,Compania,Fecha,DF.IdConcepto AS CdConcepto,C.Concepto AS DescConcepto,DF.TipDoc AS TipoFac,DF.Factura AS NumFactura,IdCiaDoc,FecDoc
,IdCliente,T.RazonSocial AS NomCliente,DF.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,VrSubTotal,DF.VrDescuento AS VrDcto,DF.VrImpuesto AS VrIva,DF.VrRetencion AS VrRetFte,DF.VrReteICA AS VrRetIca,VrReteIVA
,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal,VrFaltantes,VrAnticipos,VrNeto,DF.Cantidad AS CantTotal,BaseImp,BaseRet,VrReteCREE,TarifaRtc,CodTarRtc,DF.TarifaArf,DF.VrAutRetFte,DF.TarifaAri,DF.VrAutRetIca AS VrTotalAri
,DF.IdVend AS CdVend,VN.RazonSocial AS Vendedor,TarifaCom,DF.IdLocEnv AS CdLocEnv,LE.Localidad AS CiudadEnvio,Pedido,IdCiaPed,Cotizacion,IdCiaCot
,FecPedido,Modalidad,ModdDev,TipCom,TipoCom,Comprobante,IdCiaCom,DF.Observacion AS Observ,TimeSys,IdCiaCrea,DF.IdUsuario AS CdUsuario,Usuario,Leyenda
--detalles
,Item,TipoReg,FechaFact,D.TipRem AS TipRemesa,D.Remesa AS NumRemesa,D.IdCiaRem AS CdCiaRem,ItemRem,Descripcion,D.Cantidad AS Cant,VrUnitario,VrCosto,UndTarifa,UndCosto,Unidades,PesoNeto,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,Volumen,UndVol,Cases,Cajas,Palets,CantPago
,D.TarifaIva AS DetTarifIva,D.VrImpuesto AS DetVrIva,TarifaDct,D.VrDescuento AS DetVrDcto,D.TarifaRet AS DetTarifRet,D.VrRetencion AS DetVrRetFte,D.TarifaIca AS DetTarifIca,D.VrReteIca AS DetVrRetIca,VrFaltante,D.Remision AS DetNumRemsion,DocCliente,Referencia1,Referencia2,Referencia3
,CdMercancia,DescripMcia,CdConcepto,CF.Concepto AS DetConcepto,CdCCosto,CCosto,D.CdSubCos AS CdSubCosto,SubCosto,NitTercero,NT.RazonSocial AS DetTercero,CdAgencia,D.pVehiculo AS PlacaVeh,TipoAfiVehic,IdOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,IdDestino,LD.Localidad AS CiudadDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,D.Anulado AS DetAnulado,TipDocRef,NumDocRef,IdCiaRef,FecDocRef,CantidadFalt,UnidadFalt,NumPedRem,CiaPedRem
--Información 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.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,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,ExcIva,LiqFletes,Autoret,VrCupo,VrSaldo
,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
,FE.EstadoFE,FE.Prefijo,FE.NumFace,FE.CUFE,FE.Resolucion,FE.RangoNum,FE.FecVigencia,FE.CUFE_QR AS QR,FE.FechaValidacion
 FROM Trn_DevFcr AS DF INNER JOIN  Trn_TraFacRemesas AS D ON DF.TipDev=D.TipDoc  AND DF.Devolucion=D.Factura  AND DF.IdCia=D.IdCia
 INNER JOIN Companias AS CN ON DF.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON DF.TipDev=TD.IdDoc
 INNER JOIN  Conceptos AS C ON DF.IdConcepto=C.IdConcepto INNER JOIN adm_Usuarios AS U ON DF.IdUsuario=U.IdUsuario
 INNER JOIN  Terceros AS VN ON DF.IdVend=VN.IdTercero
 INNER JOIN Terceros AS T ON DF.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 TercCliente AS CLI ON DF.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 DF.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 Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 LEFT JOIN  Localidades AS LE ON DF.IdLocEnv=LE.IdLocal   LEFT JOIN TiposCom AS TCM ON DF.TipCom=TCM.IdCom  
 LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed
 LEFT JOIN Mercancias AS MC ON D.CdMercancia=MC.IdMercancia LEFT JOIN  ConcDiversos AS CF ON D.CdConcepto=CF.IdConcepto
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS NT ON D.NitTercero=NT.IdTercero
 LEFT JOIN Trn_Face AS FE ON DF.TipDev=FE.TipDoc AND DF.Devolucion=FE.Documento AND DF.IdCia=FE.IdCia
 WHERE DF.TipDev=@pmTipDev AND DF.Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND DF.IdCia=@pmIdCia
 ORDER BY  DF.Devolucion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryGruposCli] @pmIdGrupo VARCHAR(4)
 AS 
 SELECT IdGrupo,GrupoClie,ModApp,Inactivo
 FROM GruposCli
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 D.Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc,D.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,D.TarifaArf,D.VrAutRetFte,D.TarifaAri,D.VrAutRetIca
,D.TimeSys,D.FecUpdate,D.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
,FE.EstadoFE,FE.Prefijo,FE.NumFace,FE.CUFE,FE.Resolucion,FE.RangoNum,FE.FecVigencia,FE.CUFE_QR AS QR,FE.FechaValidacion
 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
 LEFT JOIN Trn_Face AS FE ON D.TipDev=FE.TipDoc AND D.Devolucion=FE.Documento AND D.IdCia=FE.IdCia
 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,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrAboCapital,LiqAboCapital,VrAboOtros,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrImpCarbono,O.VrIvaIngProd,VrNeto,Cantidad
,O.BaseIvaIgp,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,NumCotizac,CdCiaCotizac,NumAutoriza,NumAutCupo,NumAutCheq,Modalidad,Vigencia,NumAprob,IdCiaApr,FecAprob,DetalleAprob,CdUsuAprob,OrigenAdd,ZonaFrontera
,TipoTrans,TipoOrden,TipoModifica,NumAutSicom,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,BaseIvp,TarifaIvp,IvaIngProd,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 F.TipDoc,F.Factura,F.IdCia AS CdCia,Compania,F.Fecha,FE.Prefijo,FE.NumFace,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,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc,BaseIvaObsq,VrIvaObsequio
,F.TarifaArf,F.VrAutRetFte,F.TarifaAri,F.VrAutRetIca,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  
 LEFT JOIN Trn_Face AS FE ON F.TipDoc=FE.TipDoc AND F.Factura=FE.Documento AND F.IdCia=FE.IdCia
 WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND F.TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND F.IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND F.nClieCon LIKE ISNULL(@pmnClieCon,'%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND F.NitCliente LIKE ISNULL(@pmNitCliente,'%')  AND F.IdVend LIKE ISNULL(@pmIdVend,'%')
 AND F.Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (F.EnEfectivo=ISNULL(@pmEnEfectivo,0) or F.EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND (F.Anulado=ISNULL(@pmAnulado,0) or F.Anulado=ISNULL(@pmAnulado,1))
 ORDER BY F.IdCia,Factura
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CdAlmacen,Almacen
--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
 LEFT JOIN FinAlmacenes AS FA ON P.CdAlmacen=FA.IdAlmacen
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CauAnticipado,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,VrReteCREE,TarifaRtc,CodTarRtc,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrReteCREE,VrFletes
,VrCargos,VrOtrDcto,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,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,VrImpCons,F.TarifaArf,F.VrAutRetFte,F.TarifaAri,F.VrAutRetIca
,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.VrReteCREE,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.TarifaRtc,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.VrImpCons,D.TarifaArf,D.VrAutRetFte,D.TarifaAri,D.VrAutRetIca,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpCons
,VrReteCREE,TarifaRtc,CodTarRtc,BaseIvaObsq,VrIvaObsequio,D.TarifaArf,D.VrAutRetFte,D.TarifaAri,D.VrAutRetIca,D.CdMotDev,MotivoDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ
,D.IdEstado AS CdEstado,Estado,D.TimeSys,D.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
 LEFT JOIN TiposMotdev AS MD ON D.CdMotDev=MD.IdMotDev
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinPrestamoExto] @pmFecCorte SMALLDATETIME, @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
 AS 
 SELECT P.TipDoc AS TipPrest,P.IdPrestamo AS NumPrest,P.IdCia AS CdCia,Compania,FecPrestamo,P.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,P.IdAgencia AS CdAgencia,NomAgencia AS Agencia
,VrPrestamo,P.VrSeguro AS VrCuoSeguro,VrCuota,NPlazos,Causacion,NumCuotaIni,P.IdLinea AS CdLinea,LinCredito,TipoCredito,P.TipoCausac AS PreTipoCausac,P.IdTasa AS CdTasa,DescTasa,P.CalcIntMora AS CalcIMora
,NumCredito,P.IdVend AS CdVend,VN.RazonSocial AS NomAsesor,P.TipoGarantia AS TipoGarntia,P.VrGarantia AS VlrGarantia,P.DescGarantia AS DesGarantia,Refinanciado,NumPrestRef,CiaPrestRef,VrRefinanc,P.IdEstado AS CdEstado,Estado,P.Observacion AS Observ
,TI.TipoInteres AS TipoIntPre,TasaEfectiva,TipoPeriodo,CausaInteres,TI.DTF_EA AS DTFEA,TI.Spread_TA AS Puntos,OpcCompra
--acumulados 
,AcumCuotas,CuotasPagadas,CuotasMora,AcumCapital,PagosCapital,AcumIntereses,PagosIntereses,AcumIntMora,PagosIntMora
--acum Causaciones
,ISNULL((SELECT SUM(VrTotal) FROM Trn_FinCuotas WHERE Trn_FinCuotas.TipDoc=P.TipDoc AND Trn_FinCuotas.IdPrestamo=P.IdPrestamo AND Trn_FinCuotas.IdCia=P.IdCia
   AND Trn_FinCuotas.Fecha<=@pmFecCorte AND Trn_FinCuotas.Concepto='CAPITAL'),0) AS TotCapitalCau
--causaciones de intereses prstamos y facturas
,ISNULL((SELECT SUM(VrTotal) FROM Trn_FinCuotas WHERE Trn_FinCuotas.TipDoc=P.TipDoc AND Trn_FinCuotas.IdPrestamo=P.IdPrestamo AND Trn_FinCuotas.IdCia=P.IdCia
   AND Trn_FinCuotas.Fecha<=@pmFecCorte AND Trn_FinCuotas.Concepto='INTERESES'),0) AS TotInteresCau
,ISNULL((SELECT COUNT(NumCuota) FROM Trn_FinCuotas WHERE Trn_FinCuotas.TipDoc=P.TipDoc AND Trn_FinCuotas.IdPrestamo=P.IdPrestamo AND Trn_FinCuotas.IdCia=P.IdCia
   AND Trn_FinCuotas.Fecha<=@pmFecCorte AND Trn_FinCuotas.Concepto=(CASE WHEN P.TipoCredito='PRESTAMO' AND P.TipoCausac=2 THEN 'INTERESES' ELSE 'CAPITAL' END)),0) AS CantCuotasCau
,ISNULL((SELECT SUM(VrTotal) FROM Trn_FinCuotas WHERE Trn_FinCuotas.TipDoc='ND2' AND Trn_FinCuotas.NumPreMora=P.IdPrestamo AND Trn_FinCuotas.IdCiaCau=P.IdCia
   AND Trn_FinCuotas.Fecha<=@pmFecCorte  AND Trn_FinCuotas.Concepto='INTMORA'),0) AS TotInteresMora
--INT MORA DEL PERIODO
,ISNULL((SELECT SUM(VrTotal) FROM Trn_FinCuotas WHERE Trn_FinCuotas.TipDoc='ND2' AND Trn_FinCuotas.NumPreMora=P.IdPrestamo AND Trn_FinCuotas.IdCiaCau=P.IdCia
   AND Trn_FinCuotas.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Trn_FinCuotas.Concepto='INTMORA'),0) AS IntMoraPeriodo
,ISNULL((SELECT SUM(VrTotal) FROM Trn_FinCuotas WHERE Trn_FinCuotas.TipDoc=P.TipDoc AND Trn_FinCuotas.IdPrestamo=P.IdPrestamo AND Trn_FinCuotas.IdCia=P.IdCia
   AND Trn_FinCuotas.Fecha<=@pmFecCorte AND Trn_FinCuotas.Concepto='SEGURO'),0) AS TotSeguroCau
,ISNULL(PC.VrPagosCapital,0) AS TotPagosCapital,P.VrPrestamo-ISNULL(PC.VrPagosCapital,0) AS PreSaldoCap
--PAGOS DE INTERESES
,ISNULL(CASE TipoCredito WHEN 'FACTURAS' THEN (SELECT SUM(Trn_FinAbonos.TotalAbono) 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_FinCuotas.NumPreMora=P.IdPrestamo AND Trn_FinCuotas.IdCiaCau=P.IdCia  AND (CASE Trn_FinAbonos.Tipoaplica WHEN 2 THEN Trn_FinCuotas.Fecha ELSE Trn_FinAbonos.Fecha END)<=@pmFecCorte  AND Trn_FinCuotas.TipDoc='FIF')
 ELSE (SELECT SUM(Trn_FinAbonos.TotalAbono) 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=P.TipDoc AND Trn_FinAbonos.IdPrestamo=P.IdPrestamo AND Trn_FinAbonos.IdCiaPre=P.IdCia AND  (CASE  Trn_FinAbonos.Tipoaplica WHEN 2 THEN Trn_FinCuotas.Fecha ELSE Trn_FinAbonos.Fecha END)<=@pmFecCorte
  AND Trn_FinCuotas.Concepto='INTERESES') END,0) AS TotPagosInteres
,ISNULL((SELECT SUM(Trn_FinAbonos.TotalAbono) 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_FinCuotas.TipDoc='ND2' AND Trn_FinCuotas.NumPreMora=P.IdPrestamo AND Trn_FinCuotas.IdCiaCau=P.IdCia AND Trn_FinCuotas.Concepto='INTMORA' AND Trn_FinAbonos.Fecha<=@pmFecCorte),0) AS TotPagosIMora
,ISNULL((SELECT SUM(Trn_FinAbonos.TotalAbono) 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=P.TipDoc AND Trn_FinAbonos.IdPrestamo=P.IdPrestamo AND Trn_FinAbonos.IdCiaPre=P.IdCia AND Trn_FinAbonos.Fecha<=@pmFecCorte AND Trn_FinCuotas.Concepto='SEGURO'),0) AS TotPagosSeguro
,ISNULL((SELECT COUNT(*) FROM Trn_FinCuotas WHERE Trn_FinCuotas.TipDoc=P.TipDoc AND Trn_FinCuotas.IdPrestamo=P.IdPrestamo AND Trn_FinCuotas.IdCia=P.IdCia
   AND Trn_FinCuotas.Fecha<=@pmFecCorte AND Trn_FinCuotas.VrTotal<=(Trn_FinCuotas.VrAbonado+0.5) AND Trn_FinCuotas.Concepto=(CASE WHEN P.TipoCredito='PRESTAMO' AND P.TipoCausac=2 THEN 'INTERESES' ELSE 'CAPITAL' END)),0) AS CantCuotasPagos
,ISNULL((SELECT COUNT(*) FROM Trn_FinCuotas WHERE Trn_FinCuotas.TipDoc=P.TipDoc AND Trn_FinCuotas.IdPrestamo=P.IdPrestamo AND Trn_FinCuotas.IdCia=P.IdCia
   AND Trn_FinCuotas.Fecha<=@pmFecCorte AND Trn_FinCuotas.FechaVence<@pmFecCorte AND Trn_FinCuotas.VrTotal>Trn_FinCuotas.VrAbonado
  AND Trn_FinCuotas.Concepto=(CASE WHEN P.TipoCredito='PRESTAMO' AND P.TipoCausac=2 THEN 'INTERESES' ELSE 'CAPITAL' END)),0) AS CantCuotasMora
,ISNULL((SELECT SUM(Trn_FinCuotas.VrTotal-Trn_FinCuotas.VrAbonado) FROM Trn_FinCuotas WHERE Trn_FinCuotas.TipDoc=P.TipDoc AND Trn_FinCuotas.IdPrestamo=P.IdPrestamo AND Trn_FinCuotas.IdCia=P.IdCia
  AND Trn_FinCuotas.Fecha<=@pmFecCorte AND Trn_FinCuotas.FechaVence<@pmFecCorte AND Trn_FinCuotas.VrTotal>Trn_FinCuotas.VrAbonado),0) AS TotCuotasMora
--datos de cuota del periodo
,NumCuota,CT.Fecha AS FecCausac,CT.FechaVence AS FecVence,NumDias,TasaEfe,TasaDia,CT.DTF_EA AS TasaDTF,VrCapital,CT.VrIntereses AS VrInteresCorr,CT.VrSeguro AS Vr_Seguro,VrTotalCuota,VrPagado
--datos del cliente
,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,D.Departamento AS TercDpto
,T.Telefono AS TercTelefono,T.e_mail AS TercEmail,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,T.TipEnte AS TercEnte,EsExento,FecIngreso,VrSalBasico,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,CupoCredito
 FROM Trn_FinPrestamo AS P INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia 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 TercCliePrestamo AS CLI ON P.IdCliente=CLI.IdClie AND P.IdAgencia=CLI.IdAgencia
 INNER JOIN Terceros AS VN ON P.IdVend=VN.IdTercero  INNER JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 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 TercClieLineas TLC ON P.IdLinea =TLC.IdLinea AND P.IdCliente =TLC.IdClie AND P.IdAgencia=TLC.IdAgencia 
   --Cuotas del periodo
 LEFT JOIN (SELECT TipDoc,IdPrestamo,IdCia,NumCuota,Fecha,FechaVence,DTF_EA,TasaEfe,TasaNom AS TasaDia,SUM(CASE Concepto WHEN 'CAPITAL' THEN VrTotal ELSE 0 END) AS VrCapital
 ,SUM(CASE Concepto WHEN 'INTERESES' THEN VrTotal ELSE 0 END) AS VrIntereses,SUM(CASE Concepto WHEN 'SEGURO' THEN VrTotal ELSE 0 END) AS VrSeguro
 ,SUM(VrTotal) AS VrTotalCuota,SUM(VrAbonado) AS VrPagado,SUM(CASE Concepto WHEN 'INTERESES' THEN DiasLiquida ELSE 0 END) AS NumDias FROM Trn_FinCuotas WHERE Trn_FinCuotas.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 GROUP BY TipDoc,IdPrestamo,IdCia,NumCuota,Fecha,FechaVence,DTF_EA,TasaEfe,TasaNom) AS CT ON P.TipDoc=CT.TipDoc AND P.IdPrestamo=CT.IdPrestamo AND P.IdCia=CT.IdCia
--pagos a capital
 LEFT JOIN (SELECT A.TipDoc AS TipoPrest,A.IdPrestamo AS NumPrest,A.IdCiaPre AS CdCiaPrest,SUM(A.TotalAbono) AS  VrPagosCapital 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.Fecha<=@pmFecCorte AND C.Concepto='CAPITAL'  GROUP BY A.TipDoc,A.IdPrestamo,A.IdCiaPre) AS PC ON P.TipDoc=PC.TipoPrest AND P.IdPrestamo=PC.NumPrest AND P.IdCia=PC.CdCiaPrest
  LEFT JOIN Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE FecPrestamo<=@pmFechaFin AND P.Anulado=0
 ORDER BY P.TipDoc,P.IdPrestamo,P.IdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,U.Unidades
 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
 LEFT JOIN (SELECT tmIdProducto AS IdProducto,SUM(tmSalidas-tmEntradas) AS Unidades FROM tm_MovInvent
 WHERE tmEst=@pmtmEst GROUP BY tmIdProducto) AS U ON V.tmIdProducto=U.IdProducto
 WHERE tmEst=@pmtmEst
 ORDER BY DescripProd,tmIdCia,tmFecha
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,NumPedido,CdCiaPed,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,ImagenBin,NomArchivo
 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
 LEFT JOIN (SELECT IdProducto,ImagenBin,NomArchivo FROM ProdImagenes WHERE IdImagen=1) AS IMG ON K.IdProducto=IMG.IdProducto
 WHERE O.TipDoc=@pmTipDoc AND O.Cotizacion BETWEEN @pmCotizacionIni AND @pmCotizacionFin AND O.IdCia=@pmIdCia
 ORDER BY O.Cotizacion,Item
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,IncRetFte,NumTarj,FecVenceTc,FechaImp,CodCCosto,CodSubCosto
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenServRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT O.TipDoc,TipoDoc,O.NumOrden,O.IdCia AS CdCia,Compania,O.Fecha,O.FecDespacho,O.IdTipoServ AS CdTipoServ,TipoServicio,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia
,O.IdOrigen,LO.Localidad AS CiuOrigen,LO.IdDep AS CdDepOrig,DPO.Departamento AS DptoOrigen,O.IdDestino,LD.Localidad AS CiuDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino
,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest,O.VrServicio,O.VrEmpaque,O.VrBodega,O.VrOtros,O.VrDescuento,O.VrImpuesto,O.VrNeto,O.VrDeclarado,O.VrSeguro
,O.IdVend,VN.RazonSocial AS NomVendedor,O.NomContacto,O.TelsContacto,O.emlContacto,O.ContactoDest,O.emlContDest,O.NitRemitente,O.Remitente,O.NitDestinatario,O.Destinatario,O.Servicios,O.FormaPago,O.Referencia,O.TipInv,O.NumInvent,O.IdCiaInv,O.TipRem,O.NumRemesa,O.IdCiaRem
,O.EstFactura,O.TipCausac,O.Causacion,O.CdCiaCau,O.EstCumplido,O.FechaCump,O.TipoRuta,O.Volumen,EstadoBod,O.CdBodega,B.Bodega AS NomBodega,FechaIngBod,FechaRetBod,O.Anulado,O.FecDev,O.Observacion AS Observ,O.Referencias,O.IdEstado AS CdEstado,Estado
--Detalles de orden
,D.Item,D.IdConcepto AS CdConcepto,D.Descripcion,CD.Concepto,D.ValorTotal,D.TarifaIva,D.TipoConc,D.TipoRubro,D.CdCuenta,D.NitTercero,NT.RazonSocial AS NomTercero
,D.Referencia AS DetReferencia,D.NumDocRef,D.TipoReg,D.TarifSeguro,D.CodTarSeg,D.PlacaVehic,D.TarifaPago,D.NumDocFac,D.IndFactItem,D.FechaReg,D.CdUsuario AS DetCdUsuario,UC.Usuario AS DetUsuario,TS.Transporte,TS.Bodega,TS.Empaque,TS.Personal
--datos de facturas
,FC.FacNumero,FC.FacFecha,ISNULL(FC.FacValor,0) AS FacVrTotal,ISNULL(FC.FacCosto,0) AS FacVrCosto,ISNULL(FC.FacImpuesto,0) AS FacVrIva,ISNULL(FC.FacSeguro,0) AS FacVrSeguro,FacBaseIngreso,DfcBaseIngreso,FacReajuste,DfcReajuste
--datos del tercero
,T.Codigo AS TercCod,T.TipoId,T.Dv,T.Direccion,T.IdLocal AS TercCdLocal,L.Localidad AS TercLocalidad,T.Telefono AS TercTelefono,T.TelMovil AS TercTerCelular,T.e_mail AS TercEmail
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupo,GrupoClie,CLI.IdPlazo AS CdPlazo,Plazo
,V.NumVeh,V.TipoAfil,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,V.Modelo,V.IdPoseedor AS NitPoseedor,NP.RazonSocial AS Poseedor
,V.IdConductor AS CedConductor,CDT.RazonSocial AS Conductor,V.NumSoat,V.VigSoat
,O.NumAutCupo,O.NumAutMora,O.OrigenAdd,O.TimeSys AS FechaCrea,O.FecUpdate,O.IdCiaCrea,O.IdUsuario AS CdUsuario,U.Usuario
 FROM Trn_MudOrdenServ AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie
 INNER JOIN Terceros AS VN ON O.IdVend=VN.IdTercero INNER JOIN MudServicios AS TS ON O.IdTipoServ=TS.IdTipoServ
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal INNER JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Trn_MudOrdenConc AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
 INNER JOIN ConcDiversos AS CD ON D.IdConcepto=CD.IdConcepto 
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona 
 LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo LEFT JOIN Plazos AS PZ ON CLI.IdPlazo=PZ.IdPlazo
 LEFT JOIN Terceros AS NT ON D.NitTercero=NT.IdTercero LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia 
 LEFT JOIN AlmBodegas AS B ON O.CdBodega=B.IdBodega
 LEFT JOIN Vehiculos AS V ON D.PlacaVehic=V.IdVehiculo LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
 LEFT JOIN Marcas AS M ON V.IdMarca=M.IdMarca LEFT JOIN TiposCol AS CLR ON V.IdColor=CLR.IdColor
 LEFT JOIN Terceros AS NP ON V.IdPoseedor=NP.IdTercero LEFT JOIN Terceros AS CDT ON V.IdConductor=CDT.IdTercero
 LEFT JOIN adm_Usuarios AS UC ON D.CdUsuario=UC.IdUsuario
 --Facturas item por item
 LEFT JOIN (SELECT TipRem AS FacTipOrd,Remesa AS FacNumOrden,IdCiaRem AS FacIdCiaOrd,ItemRem AS FacItemOrd,MAX(FecRemesa) AS FacFecha
,SUM(CASE WHEN SUBSTRING(FR.TipDoc,1,2)='DF' THEN (Cantidad*VrUnitario)*-1 ELSE Cantidad*VrUnitario END) AS FacValor
,SUM(CASE WHEN SUBSTRING(FR.TipDoc,1,2)='DF' THEN (Cantidad*VrCosto)*-1 ELSE Cantidad*VrCosto END) AS FacCosto
,SUM(CASE WHEN SUBSTRING(FR.TipDoc,1,2)='DF' THEN VrImpuesto*-1 ELSE VrImpuesto END) AS FacImpuesto
,SUM(CASE WHEN SUBSTRING(FR.TipDoc,1,2)='DF' THEN VrSeguroRem*-1 ELSE VrSeguroRem END) AS FacSeguro
,SUM(CASE WHEN SUBSTRING(FR.TipDoc,1,2)='FC' THEN VrDeclMcia ELSE 0 END) AS FacDeclarado
,MAX(CASE WHEN SUBSTRING(FR.TipDoc,1,2)='FC' THEN FR.Factura ELSE 0 END) AS FacNumero
,SUM(CASE WHEN FR.Referencia3='TRANSPORTE' AND SUBSTRING(FR.TipDoc,1,2)='FC' THEN Cantidad*VrUnitario ELSE 0 END) AS FacBaseIngreso
,SUM(CASE WHEN FR.Referencia3='TRANSPORTE' AND SUBSTRING(FR.TipDoc,1,2)='DF' THEN (Cantidad*VrUnitario)*-1 ELSE 0 END) AS DfcBaseIngreso
,SUM(CASE WHEN FR.Referencia3='TRANSPORTE' AND C.MudReajuste=1 AND SUBSTRING(FR.TipDoc,1,2)='FC' THEN Cantidad*VrUnitario ELSE 0 END) AS FacReajuste
,SUM(CASE WHEN FR.Referencia3='TRANSPORTE' AND C.MudReajuste=1 AND SUBSTRING(FR.TipDoc,1,2)='DF' THEN Cantidad*VrUnitario ELSE 0 END) AS DfcReajuste
 FROM Trn_TraFacRemesas AS FR LEFT JOIN ConcDiversos AS C ON FR.CdConcepto=C.IdConcepto
 WHERE FR.TipRem='OSM' GROUP BY TipRem,Remesa,IdCiaRem,ItemRem) AS FC ON D.TipDoc=FC.FacTipOrd AND D.NumOrden=FC.FacNumOrden AND D.IdCia=FC.FacIdCiaOrd AND D.Item=FC.FacItemOrd
 WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,AutoIca,IncRet,IncRiv,IncIca,LiqFletes,CdRet,CdRiv,CodRetIca,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,CodRetCom
,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
,T.FechaNac,T.ImagenDoc1,T.ImagenDoc2,T.ImagenDoc3,CupoElectcom
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_BalComNiif] @pmtmEst CHAR(2)
 AS 
 SELECT BC.tmIdCuenta AS CdCuenta,P.NomCuenta,tmIdCueNiif,PN.NomCuenta AS NomCueNiif,tmIdCia,Compania,tmTipo
,tmSaldoAnt1,tmDebitos1,tmCreditos1,tmSaldoAnt1+tmDebitos1-tmCreditos1 AS NuevoSaldo1
,tmSaldoAnt2,tmDebitos2,tmCreditos2,tmSaldoAnt2+tmDebitos2-tmCreditos2 AS NuevoSaldo2
,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 
,NC.tmNivel1,NC.tmNivel2,NC.tmNivel4,NC.tmNivel6,NC.tmNivel9,NC.tmNivel12
,NCN.tmNivel1 AS NiifNivel1,NCN.tmNivel2 AS NiifNivel2,NCN.tmNivel4 AS NiifNivel4,NCN.tmNivel6 AS NiifNivel6
,NCN.tmNivel9 AS NiifNivel9,NCN.tmNivel12 AS NiifNivel12,tmId,tmEst
 FROM tm_BalComNiif AS BC 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 Puc AS P ON BC.tmIdCuenta=P.IdCuenta LEFT JOIN PucNiif AS PN ON BC.tmIdCueNiif=PN.IdCuenta
 LEFT JOIN tm_NivCue AS NC ON BC.tmIdCuenta=NC.tmIdCuenta LEFT JOIN tm_NivCueNiif AS NCN ON BC.tmIdCueNiif=NCN.tmIdCuenta
 LEFT JOIN Companias AS CI ON BC.tmIdCia=CI.IdCia LEFT JOIN Agencias AS A ON BC.tmCodAgncia=A.IdAgencia
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTraReportes] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmItem INT,@pmCdRuta VARCHAR(4),@pmIdPunto INT,@pmFecReporte SMALLDATETIME,@pmTipoRep VARCHAR(10)
,@pmEstadoRep INT,@pmIdUsuario VARCHAR(11),@pmFechaCrea SMALLDATETIME,@pmCdNovedad VARCHAR(4),@pmObservRep VARCHAR(250),@pmTipoPunto VARCHAR(10),@pmCdPuesto VARCHAR(4),@pmNomConductor VARCHAR(150)
 AS 
 INSERT INTO Trn_TraReportes (TipDoc,Manifiesto,IdCia,Item,CdRuta,IdPunto,FecReporte,TipoRep,EstadoRep,IdUsuario,FechaCrea,CdNovedad,ObservRep,TipoPunto,CdPuesto,NomConductor)
 VALUES (@pmTipDoc,@pmManifiesto,@pmIdCia,@pmItem,@pmCdRuta,@pmIdPunto,@pmFecReporte,@pmTipoRep,@pmEstadoRep,@pmIdUsuario,@pmFechaCrea,@pmCdNovedad,@pmObservRep,@pmTipoPunto,@pmCdPuesto,@pmNomConductor)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraReportes] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmItem INT,@pmCdRuta VARCHAR(4),@pmIdPunto INT,@pmFecReporte SMALLDATETIME,@pmTipoRep VARCHAR(10)
,@pmEstadoRep INT,@pmCdNovedad VARCHAR(4),@pmObservRep VARCHAR(250),@pmTipoPunto VARCHAR(10),@pmCdPuesto VARCHAR(4),@pmNomConductor VARCHAR(150)
 AS 
 UPDATE Trn_TraReportes SET CdRuta=@pmCdRuta,IdPunto=@pmIdPunto,FecReporte=@pmFecReporte,TipoRep=@pmTipoRep,EstadoRep=@pmEstadoRep,CdNovedad=@pmCdNovedad,ObservRep=@pmObservRep
,TipoPunto=@pmTipoPunto,CdPuesto=@pmCdPuesto,NomConductor=@pmNomConductor
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposBan] @pmIdBandera VARCHAR(4),@pmTipoBandera VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposBan (IdBandera,TipoBandera,Inactivo)
 VALUES (@pmIdBandera,@pmTipoBandera,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposBan] @pmIdBandera VARCHAR(4)
 AS 
 DELETE FROM TiposBan
 WHERE IdBandera=@pmIdBandera
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposBan] @pmIdBandera VARCHAR(4),@pmTipoBandera VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposBan SET TipoBandera=@pmTipoBandera,Inactivo=@pmInactivo
 WHERE IdBandera=@pmIdBandera
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNomVacDetCon] @pmIdEmpleado VARCHAR(16),@pmIdConcepto VARCHAR(4),@pmMes VARCHAR(6)
,@pmIdPeriodo VARCHAR(8)=Null,@pmNContrato INT=Null
 AS 
 --Obsoleto Nov 22/2016
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,DiasAjuste
,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 Q.Anulado=0
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,DiasAjuste,LiqParcial,DiasDom,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncNomVacParcRem] (@pmIdEmpleado VARCHAR(16),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME)
RETURNS INT
 AS  
BEGIN 
 DECLARE @DiasRem INT
  SET  @DiasRem=(SELECT ISNULL(SUM(CantRem),0)
  FROM Trn_NomVac
  WHERE IdEmpleado=@pmIdEmpleado AND FecIniPmd BETWEEN @pmFechaIni AND @pmFechaFin
  AND Anulado=0 AND LiqParcial=1)
 RETURN @DiasRem
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,DiasAjuste,TipCom,Comprobante,IdCiaCom,Observacion,LogPmdio,Anulado,IdEstado,LiqParcial,DiasDom,TimeSys,FecUpdate,IdUsuario
 FROM Trn_NomVac
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmDiasAjuste INT,@pmLiqParcial BIT,@pmDiasDom INT,@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,DiasAjuste=@pmDiasAjuste,LiqParcial=@pmLiqParcial,DiasDom=@pmDiasDom,FecUpdate=@pmFecUpdate
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,DiasAjuste,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,DiasAjuste,TipCom,Comprobante,IdCiaCom,Observacion,LogPmdio,IdEstado,Anulado,TimeSys,IdUsuario
 FROM  Trn_NomVac WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmDiasAjuste INT,@pmLiqParcial BIT,@pmDiasDom INT,@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,DiasAjuste,TipCom,Comprobante,IdCiaCom,Observacion,LogPmdio,IdEstado,Anulado,TimeSys,IdUsuario,LiqParcial,DiasDom)
 VALUES (@pmIdEmpleado,@pmNumero,@pmFecha,@pmNContrato,@pmFecInicial,@pmFecFinal,@pmCantidad,@pmVrLiquida,@pmVrBase,@pmFecIniPmd,@pmFecFinPmd,@pmDiasBase,@pmDiasNoRem,@pmDiasVac,@pmNNovedad,@pmNPeriodos,@pmCantRem,@pmVrRemndo,@pmVrTotDed,@pmVrSalario,@pmpVehiculo,@pmDiasAjuste
,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmLogPmdio,@pmIdEstado,@pmAnulado,@pmTimeSys,@pmIdUsuario,@pmLiqParcial,@pmDiasDom)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncNomVacParcial] (@pmIdEmpleado VARCHAR(16),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME)
RETURNS INT
 AS  
BEGIN 
 DECLARE @DiasDisf INT
  SET  @DiasDisf=(SELECT ISNULL(SUM(DiasVac-DiasDom),0)
  FROM Trn_NomVac
  WHERE IdEmpleado=@pmIdEmpleado AND FecIniPmd BETWEEN @pmFechaIni AND @pmFechaFin
  AND Anulado=0 AND LiqParcial=1)
 RETURN @DiasDisf
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUptm_PagosCam] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmVrCambio MONEY
 AS 
 UPDATE  tm_Pagos  SET tmVrCambio=@pmtmVrCambio
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_PagosItm] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_Pagos
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenOperRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS 
SELECT O.TipDoc,O.NumOrden,O.IdCia,O.FecDespacho,O.IdCliente,D.Item,D.IdOperario,NomOperario,D.IdTipoOper,TipoOperario
,D.CdConcepto,C.Concepto,C.IdCuenta,C.TipoConc,D.TarifaCargo,D.PlacaVehic,D.Descripcion,D.TipoReg,D.FechaReg,D.CdUsuario,Usuario
,V.NumVeh,V.IdMarca AS CdMarca,M.Marca,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.Modelo,V.IdColor AS CdColor,NomColor,V.Config
,V.IdPoseedor AS CdPoseedor,T.RazonSocial AS Poseedor,V.TipoAfil
FROM Trn_MudOrdenServ AS O INNER JOIN Trn_MudOrdenOper AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
INNER JOIN TiposOperarios AS TP ON D.IdTipoOper=TP.IdOper LEFT JOIN TraConcCausac AS C ON D.CdConcepto=C.IdConcepto
LEFT JOIN Vehiculos AS V ON D.PlacaVehic=V.IdVehiculo
LEFT JOIN Marcas AS M ON V.IdMarca=M.IdMarca LEFT JOIN TiposCol AS CL ON V.IdColor=CL.IdColor
LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh LEFT JOIN Terceros AS T ON V.IdPoseedor=T.IdTercero
LEFT JOIN adm_Usuarios AS U ON D.CdUsuario=U.IdUsuario 
WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraTurnLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdVehiculo VARCHAR(10)=Null 
 AS  
SELECT T.Numero,T.IdCia AS CdCia,Compania,T.IdVehiculo AS PlacaVeh,V.NumVeh AS NumVehiculo,T.nRemolque
,T.IndTurno AS Ind_Turno,T.Fecha,T.IdConductor AS CedConductor,NC.RazonSocial AS NomConductor,T.Estado
,CASE T.Estado WHEN 1 THEN 'EN ESPERA' WHEN 2 THEN 'EN TRANSITO' ELSE 'INACTIVO' END AS DescEstado
,TipOrd,NumOrden,IdCiaOrd,FecOrden,NitCliente,CLI.RazonSocial AS Cliente,T.FechaCrea,SedeClie,Autorizac,T.IdUsuario AS CdUsuario,Usuario
,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.TipoAfil,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdColor AS CdColor,NomColor,V.Modelo
,Config,V.IdCat AS CdCateg,Catpeaje,CarrCapac,UndCapc,V.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,NumSoat,FecSoat,VigSoat,KmActual
,CdLocal,CU.Localidad,V.Ubicacion
,NC.TelMovil AS Cond_Celular,NC.Telefono AS Cond_Telefono,NC.e_mail AS Cond_Email,CND.Licencia AS NumLicencia,CND.CatLicencia,VigLicencia
FROM Trn_TraTurn AS T INNER JOIN Vehiculos AS V ON T.IdVehiculo=V.IdVehiculo INNER JOIN adm_Usuarios AS U ON T.IdUsuario=U.IdUsuario
INNER JOIN Terceros AS NC ON T.IdConductor=NC.IdTercero INNER JOIN Companias AS CN ON T.IdCia=CN.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 CL ON V.IdColor=CL.IdColor
INNER JOIN Terceros AS NP ON V.IdPoseedor=NP.IdTercero
LEFT JOIN PeajesCat AS CP ON V.IdCat=CP.IdCat LEFT JOIN Localidades AS CU ON V.CdLocal=CU.IdLocal
LEFT JOIN TercCndtores AS CND ON T.IdConductor=CND.IdConductor LEFT JOIN Terceros AS CLI ON T.NitCliente=CLI.IdTercero
WHERE T.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND T.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraAnticiposMov] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
--Obsoleto despues del dia 07/Dic/2017- Se cambio por paQrytm_TraMovAnt
 SELECT TipDoc,TipoDoc,Anticipo,A.IdCia AS CdCia,Compania,Fecha,T.TipoId AS NitTipId,A.IdPoseedor AS NitPoseedor,T.Dv AS NitDv,T.RazonSocial AS Poseedor,A.IdVehiculo AS PlacaVeh,NumVeh,VrAnticipo,(VrAbonado*0) AS VrAbonos,A.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
 ,TipMuc,Manifiesto,IdCiaMuc,Modalidad,TipoPago,NumCheque,FecCheque,Beneficiario,Anulado,A.Observacion AS Detalle,A.IdConcepto AS CdConcepto,Concepto,TipCom,Comprobante,IdCiaCom,TimeSys  
 FROM Trn_TraAnticipos AS A INNER JOIN Sys_TiposDoc AS TD ON A.TipDoc=TD.IdDoc INNER JOIN Companias AS CN ON A.IdCia=CN.IdCia
 INNER JOIN Conceptos AS C ON A.IdConcepto=C.IdConcepto INNER JOIN Terceros AS T ON A.IdPoseedor=T.IdTercero  
 INNER JOIN Terceros AS CDT ON A.IdConductor=CDT.IdTercero INNER JOIN Vehiculos AS V ON A.IdVehiculo=V.IdVehiculo
 WHERE  A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdVehiculo  LIKE ISNULL(@pmIdVehiculo ,'%') AND A.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
UNION ALL
SELECT P.TipDoc,ISNULL(TipoDoc,'COMPROBANTE'),Documento,P.IdCia,Compania,P.Fecha,T.TipoId,A.IdPoseedor,T.Dv,T.RazonSocial,A.IdVehiculo,NumVeh,0,P.VrAbono,A.IdConductor,CDT.RazonSocial
,A.TipMuc,A.Manifiesto,A.IdCiaMuc,Modalidad,TipoAplica,NumCheque,FecCheque,Beneficiario,0,Detalle,A.IdConcepto,Concepto,TipAnt,P.Anticipo,IdCiaAnt,P.Fecha
FROM Trn_TraAntAbonos AS P INNER JOIN Trn_TraAnticipos AS A ON P.TipAnt=A.TipDoc AND P.Anticipo=A.Anticipo AND P.IdCiaAnt=A.IdCia
INNER JOIN Companias AS CN ON P.IdCia=CN.IdCia INNER JOIN Conceptos AS C ON A.IdConcepto=C.IdConcepto
INNER JOIN Terceros AS T ON A.IdPoseedor=T.IdTercero  INNER JOIN Terceros AS CDT ON A.IdConductor=CDT.IdTercero
INNER JOIN Vehiculos AS V ON A.IdVehiculo=V.IdVehiculo LEFT JOIN Sys_TiposDoc AS TD ON P.TipDoc=TD.IdDoc 
 WHERE  P.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdVehiculo  LIKE ISNULL(@pmIdVehiculo ,'%') AND A.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
--saldos anteriores
UNION ALL
SELECT 'SAN','SALDO ANTERIOR',0,A.IdCia,Compania,@pmFechaIni,T.TipoId,A.IdPoseedor,T.Dv,T.RazonSocial,A.IdVehiculo,NumVeh,(SaldoAnt+VrAnticipos-VrPagos),0,'0',''
,'',0,'','','','',Null,'',0,'saldo anterior','SAN','Anticipos','',0,'',@pmFechaIni
FROM AcuAnticipos AS A INNER JOIN Companias AS CN ON A.IdCia=CN.IdCia
INNER JOIN Terceros AS T ON A.IdPoseedor=T.IdTercero  INNER JOIN Vehiculos AS V ON A.IdVehiculo=V.IdVehiculo
 WHERE  A.nAnno=YEAR(DATEADD (Month ,-1,@pmFechaIni))  AND A.nMes=MONTH(DATEADD (Month ,-1, @pmFechaIni))
  AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND A.IdVehiculo  LIKE ISNULL(@pmIdVehiculo ,'%') AND A.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenVeh_Cr] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT VH.TipDoc,VH.NumOrden,VH.IdCia,VH.Item,VH.IdVehiculo,VH.TipoAfiVehic,VH.NitEmpresa,NomEmpresa,VH.nRemolque
,VH.IdConductor,NC.RazonSocial AS Conductor,IdCondAux,NA.RazonSocial AS CondAuxiliar,TipDocRef,VH.NumDocRef,CdCiaRef,VH.CdTipoOper,TipoOperacion
,VH.TipoIngreso,VH.TarifaIng,VH.VrIngreso
,V.NumVeh,V.IdMarca AS CdMarca,MV.Marca,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.Modelo,V.IdColor AS CdColor,NomColor,V.Config
,V.IdPoseedor AS CdPoseedor,T.RazonSocial AS Poseedor,V.NumSoat,VigSoat,V.SerieChasis,V.CarrCapac,V.UndCapc
,CDT.Licencia AS NumLicencia,CDT.CatLicencia,CDT.VigLicencia
,CDA.Licencia AS AuxNumLicencia,CDA.CatLicencia AS AuxCategLic,CDA.VigLicencia AS AuxVigLicencia
 FROM Trn_MudOrdenVeh AS VH INNER JOIN Vehiculos AS V ON VH.IdVehiculo=V.IdVehiculo
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN TiposCol AS C ON V.IdColor=C.IdColor
 INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Terceros AS T ON V.IdPoseedor=T.IdTercero
 INNER JOIN Terceros AS NC ON VH.IdConductor=NC.IdTercero
 LEFT JOIN Terceros AS NA ON VH.IdCondAux=NA.IdTercero
 LEFT JOIN TercCndtores AS CDT ON VH.IdConductor=CDT.IdConductor LEFT JOIN TercCndtores AS CDA ON VH.IdCondAux=CDA.IdConductor
 LEFT JOIN TiposOperac AS TP ON VH.CdTipoOper=TP.IdTipoOper
 WHERE VH.TipDoc=@pmTipDoc AND VH.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND VH.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryVehiculosSal] @pmIdVehiculo VARCHAR(10)
AS
 SELECT     IdVehiculo, NumVeh,VrSaldoAct,VrLmtCred,IdPropietario 
 FROM  Vehiculos WHERE   IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraManifInt_Cr] @pmTipDoc VARCHAR(3),@pmManifiestoIni INT,@pmManifiestoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT M.TipDoc,TipoDoc,M.Manifiesto,M.IdCia AS CdCia,Compania,M.Fecha,M.FecEmision,DPO.IdPais AS CdPaisOrig,POR.NombrePais AS PaisOrig,LO.IdDep AS CdDepOrig,DPO.Departamento AS DptoOrigen,M.IdOrigen,LO.Localidad AS DescOrigen
,DPD.IdPais AS CdPaisDest,PDE.NombrePais AS PaisDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino,M.IdDestino,LD.Localidad AS DescDestino
,M.IdVehiculo AS PlacaVeh,M.nRemolque,M.IdConductor AS CdConductor,NC.RazonSocial AS CondTitula,NC.Telefono AS TelCondTitu,NC.TelMovil AS MovCondTitu,M.IdCondRelev,NCA.RazonSocial AS CondAuxiliar,NCA.Telefono AS TelCondAuxiliar,NCA.TelMovil AS MovCondAuxiliar,NatCarga
,M.CdAduana,AF.NomAduana AS AduanaFront,AF.IdPais AS CdPaisAdu,PAF.NombrePais AS PaisAduFront,M.CdAduanaDest,AD.NomAduana AS AduanaDest,AD.IdPais AS CdPaisAduDest,PAD.NombrePais AS PaisAduDestino,M.IdMoneda AS CdMoneda,MN.Simbolo,MN.Mneda AS Moneda
,M.ValorTotal,M.PesoTotal,M.NumMuc,M.CdCiaMuc,M.Anulado,M.FecDev,M.Observacion AS Observ,M.IdEstado AS CdEstado,Estado,M.Negociacion,M.OrigenAdd,M.TimeSys AS FechaCrea,M.FecUpdate,M.IdCiaCrea AS CdCiaCrea,M.IdUsuario AS CdUsuario,Usuario
--detalles
,D.Item,D.TipOrd,D.NumOrden,D.IdCiaOrd,D.CdMercancia,D.DescripMcias,D.Cantidad,D.PesoNeto,D.PesoBruto,D.Volumen,D.ValorMcia,D.IdMoneda AS DetIdMoneda,MD.Mneda AS DetMoneda,MD.Simbolo AS DetMonSimb,D.IdUnd AS CdUnd,UM.Unidad
,D.IdEmp AS CdEmp,Empaque,D.IdNat AS CdNat,Natlzaprod,D.UndMed,UP.Unidad AS DesUndPeso,D.UndVol,UV.Unidad AS DesUndVol,D.Bultos,CP.DocRemitente,D.DescripUnid
--datos del vehículo
,V.IdMarca AS CdMarca,MV.Marca,V.Modelo,V.SerieChasis,V.NumSerie,V.IdPoseedor AS CdPoseedor,T.RazonSocial AS Poseedor
,V.NumSoat,VigSoat,TarjProp,VigTProp,RevTecMec,VigTecMec,V.TarjOper,VigTarjOper,V.Ulttramite,V.Descripcion AS VehDescripcion,V.CertMovilizacion,VigCertMovil
,V.CdLugTp,LTP.Localidad AS VehLugarTp,DTP.IdPais AS CdPaisTP,PTP.NombrePais AS PaisLugarTP
,RQ.IdMarca AS CdMarcaRmq,MR.Marca AS MarcaRmq,RQ.Modelo AS ModeloRmq,RQ.NumSerial AS NumSerieRmq,RQ.TarjetaProp,RQ.FecTarjProp,RQ.VigTarjProp
,RQ.CdLocTarj,LPR.Localidad AS RemLugarTp,DPR.IdPais AS RemCdPaisTp,PRP.NombrePais AS RemPaisTP
,NC.IdLugarCed AS CondIdLugar,LC.Localidad AS CondLugarCed,DLC.IdPais AS CdPaisLugar,PLC.NombrePais AS CondPaisLugar
,CDT.TipoSangre,CDT.FactorRh,CDT.Pasporte,CDT.VigPaspte,CDT.Licencia AS NumLicencia,CDT.CatLicencia,CDT.VigLicencia,CDT.NumTarjeta,CDT.VigTarjeta,CDT.PasSeguridad,CDT.VigPasSeg
,NCA.IdLugarCed AS AuxIdLugar,LCA.Localidad AS AuxLugarCed,DLA.IdPais AS AuxCdPais,PLA.NombrePais AS AuxPaisLugar
,CDA.TipoSangre AS AuxTipoSangre,CDA.FactorRh AS AuxFactorRh,CDA.Pasporte AS AuxPasaporte,CDA.VigPaspte AS AuxVigPaspte,CDA.Licencia AS AuxNumLicencia
,CDA.CatLicencia AS AuxCategLic,CDA.VigLicencia AS AuxVigLicencia,CDA.NumTarjeta AS AuxNumTarjeta,CDA.VigTarjeta AS AuxVigTarjeta,CDA.PasSeguridad AS AuxPasSeguridad,CDA.VigPasSeg AS AuxVigPasSeg
 FROM Trn_TraManifInt AS M INNER JOIN Companias AS C ON M.IdCia=C.IdCia INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo
 INNER JOIN Localidades AS LO ON M.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep INNER JOIN adm_Paises AS POR ON DPO.IdPais=POR.IdPais
 INNER JOIN Localidades AS LD ON M.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep INNER JOIN adm_Paises AS PDE ON DPD.IdPais=PDE.IdPais
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS E ON M.IdEstado=E.IdEstado INNER JOIN Sys_TiposDoc AS TD ON M.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS NC ON M.IdConductor=NC.IdTercero INNER JOIN TercCndtores AS CDT ON M.IdConductor=CDT.IdConductor 
 INNER JOIN Terceros AS T ON V.IdPoseedor=T.IdTercero  INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca
 INNER JOIN Trn_TraManifIntMcia AS D ON M.TipDoc=D.TipDoc AND M.Manifiesto=D.Manifiesto AND M.IdCia=D.IdCia
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN adm_monedas AS MD ON D.IdMoneda=MD.IdMneda
 INNER JOIN TiposNat AS NZ ON D.IdNat=NZ.IdNat 
 LEFT JOIN Trn_TraPorteInt AS CP ON D.TipOrd=CP.TipDoc AND D.NumOrden=CP.NumOrden AND D.IdCiaOrd=CP.IdCia
 LEFT JOIN VehRemolq AS RQ ON M.nRemolque=RQ.IdRemque LEFT JOIN Marcas AS MR ON RQ.IdMarca=MR.IdMarca
 LEFT JOIN Sys_Um AS UP ON D.UndMed=UP.UndMed LEFT JOIN Sys_Um AS UV ON D.UndVol=UV.UndMed LEFT JOIN Empaques AS EP ON D.IdEmp=EP.IdEmp
 LEFT JOIN Terceros AS NCA ON M.IdCondRelev=NCA.IdTercero LEFT JOIN TercCndtores AS CDA ON M.IdCondRelev=CDA.IdConductor
 LEFT JOIN Aduanas AS AF ON M.CdAduana=AF.IdAduana LEFT JOIN Aduanas AS AD ON M.CdAduanaDest=AD.IdAduana
 LEFT JOIN adm_Paises AS PAF ON AF.IdPais=PAF.IdPais LEFT JOIN adm_Paises AS PAD ON AD.IdPais=PAD.IdPais
 LEFT JOIN adm_monedas AS MN ON M.IdMoneda=MN.IdMneda 
 LEFT JOIN Localidades AS LC ON NC.IdLugarCed=LC.IdLocal LEFT JOIN Departamentos AS DLC ON LC.IdDep=DLC.IdDep LEFT JOIN adm_Paises AS PLC ON DLC.IdPais=PLC.IdPais
 LEFT JOIN Localidades AS LCA ON NCA.IdLugarCed=LCA.IdLocal LEFT JOIN Departamentos AS DLA ON LCA.IdDep=DLA.IdDep LEFT JOIN adm_Paises AS PLA ON DLA.IdPais=PLA.IdPais
 LEFT JOIN Localidades AS LTP ON V.CdLugTp=LTP.IdLocal LEFT JOIN Departamentos AS DTP ON LTP.IdDep=DTP.IdDep LEFT JOIN adm_Paises AS PTP ON DTP.IdPais=PTP.IdPais
 LEFT JOIN Localidades AS LPR ON RQ.CdLocTarj=LPR.IdLocal LEFT JOIN Departamentos AS DPR ON LPR.IdDep=DPR.IdDep LEFT JOIN adm_Paises AS PRP ON DPR.IdPais=PRP.IdPais
 WHERE M.TipDoc=@pmTipDoc AND M.Manifiesto BETWEEN @pmManifiestoIni AND @pmManifiestoFin AND M.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[paQryAcuPropietariosSalPro] @pmnAnno INT,@pmnMes INT, @pmIdPropietario VARCHAR(16)
,@pmIdAdmon VARCHAR(4)
AS
 SELECT V.IdVehiculo AS PlacaVeh,V.IdPoseedor 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.IdPoseedor=@pmIdPropietario
 AND V.IdAdmon<>@pmIdAdmon AND V.TipoAfil<>'PROPIO'
 GROUP BY  V.IdVehiculo,V.IdPoseedor,NumVeh,IdAdmon,TipoAfil,IdConductor,RazonSocial,IdGrupo
 ORDER BY  V.IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryVehLecturasDso] @pmTipDoc VARCHAR(3)=Null,@pmpVehiculo VARCHAR(10)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT TipDoc,L.Numero,L.IdCia AS CdCia,Compania,Item,Fecha,TipoUnidad,pVehiculo,TipoLectura,KmsAnterior,KmsActual
,Kms2Anterior,Kms2Actual,L.Observacion,L.TimeSys,L.IdUsuario AS CdUsuario,Usuario
--datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdColor AS CdColor,NomColor,V.Modelo,Config,VehArtic,NumMotor
,SerieChasis,NumSerie,V.CdRemque,TipoAfil,FecCompra,V.KmInicial,V.KmActual AS KmsOdometro,V.Km2Actual AS KmsManual,V.CentFinal AS KmsOtro,V.Descripcion
,V.Inactivo,V.IdEstado AS CdEstado,Estado
 FROM Trn_VehLecturas AS L INNER JOIN adm_Usuarios AS U ON L.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS C ON L.IdCia=C.IdCia
 LEFT JOIN Vehiculos AS V ON L.pVehiculo=V.IdVehiculo LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
 LEFT JOIN Marcas AS M ON V.IdMarca=M.IdMarca LEFT JOIN TiposCol AS CL ON V.IdColor=CL.IdColor
 LEFT JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
 WHERE pVehiculo LIKE ISNULL(@pmpVehiculo,'%') AND TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND (L.Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND L.Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraManifIntLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT M.Manifiesto,M.IdCia AS CdCia,Compania,M.Fecha,M.FecEmision,DPO.IdPais AS CdPaisOrig,POR.NombrePais AS PaisOrig,LO.IdDep AS CdDepOrig,DPO.Departamento AS DptoOrigen,M.IdOrigen,LO.Localidad AS DescOrigen
,DPD.IdPais AS CdPaisDest,PDE.NombrePais AS PaisDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino,M.IdDestino,LD.Localidad AS DescDestino
,M.IdVehiculo AS PlacaVeh,M.nRemolque,M.IdConductor AS CdConductor,NC.RazonSocial AS CondTitula,M.IdCondRelev,NCA.RazonSocial AS CondAuxiliar,NatCarga,M.CdAduana,AF.NomAduana AS AduanaFront,M.CdAduanaDest,AD.NomAduana AS AduanaDest
,M.IdMoneda AS CdMoneda,MN.Simbolo,MN.Mneda AS Moneda,M.ValorTotal,M.PesoTotal,M.NumMuc,M.CdCiaMuc,M.Anulado,M.FecDev
,M.Observacion AS Observ,M.IdEstado AS CdEstado,Estado,M.Negociacion,M.OrigenAdd,M.TimeSys AS FechaCrea,M.FecUpdate,M.IdCiaCrea AS CdCiaCrea,M.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraManifInt AS M INNER JOIN Companias AS C ON M.IdCia=C.IdCia INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo
 INNER JOIN Localidades AS LO ON M.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep INNER JOIN adm_Paises AS POR ON DPO.IdPais=POR.IdPais
 INNER JOIN Localidades AS LD ON M.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep INNER JOIN adm_Paises AS PDE ON DPD.IdPais=PDE.IdPais
 INNER JOIN Terceros AS NC ON M.IdConductor=NC.IdTercero
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS E ON M.IdEstado=E.IdEstado
 LEFT JOIN Terceros AS NCA ON M.IdCondRelev=NCA.IdTercero LEFT JOIN Aduanas AS AF ON M.CdAduana=AF.IdAduana LEFT JOIN Aduanas AS AD ON M.CdAduanaDest=AD.IdAduana
 LEFT JOIN adm_monedas AS MN ON M.IdMoneda=MN.IdMneda 
 WHERE M.TipDoc=@pmTipDoc AND M.FecEmision BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraRemCancelaOst] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT CR.Fecha,CR.TipDoc AS TipFac,CR.Factura,CR.IdCia AS CdCiaFac,CF.Compania AS CiaFactura,CR.TipRem AS TipOds,CR.Remesa AS NumOrden,CR.IdCiaRem AS CdCiaOrden,CN.Compania AS CiaOrden
 ,O.FecDespacho,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia,O.IdVehiculo AS PlacaVeh,NumVeh,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor
,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,O.TipoAfiVehic,O.Modalidad,O.VrTotal,O.VrCosto,O.Cantidad AS Cant_Total,VrAnticipo
,O.IdOrigen,LO.Localidad AS Origen,O.IdDestino AS CdDestino,LD.Localidad AS NomDestino,TipoOrden
,TipoResiduo,O.RemCliente,O.NomRecibido,O.NumRemesa,O.CdCiaRem,O.EstCumplido,O.NumCump,O.CdCiaCum,O.EstFactura,O.EstOrden,O.TipLiq,O.NumLiquida,O.CdCiaLiq
,O.Observacion AS OrdenObserv,CR.Observacion,CR.IdUsuario AS CdUsuario,Usuario,CR.FechaCrea,CR.Id
FROM Trn_TraRemCancela AS CR INNER JOIN Companias AS CN ON CR.IdCiaRem=CN.IdCia INNER JOIN adm_Usuarios AS U ON CR.IdUsuario=U.IdUsuario
INNER JOIN Trn_TraOrdenServ AS O ON CR.TipRem=O.TipDoc AND CR.Remesa=O.NumOrden AND CR.IdCiaRem=O.IdCia
INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero
INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo
LEFT JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal LEFT JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal LEFT JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
LEFT JOIN Companias AS CF ON CR.IdCia=CF.IdCia LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia
WHERE CR.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND CR.IdCiaRem LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[paQryAcuPropietariosSalCue] @pmnAnno INT,@pmnMes INT
,@pmSaldoActual MONEY,@pmIdAdmon VARCHAR(4)
AS
 SELECT V.IdPoseedor 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.IdPoseedor
 HAVING  SUM(A.SaldoAnterior+A.TotalDebitos-A.TotalCreditos)<=@pmSaldoActual
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraManifiestoNov]  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
, @pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
SELECT M.TipDoc AS CdTipDoc,M.Manifiesto AS NumManif,M.IdCia AS CdCia,Compania,Fecha,FecDespacho,FecEntrega,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta
,M.IdVehiculo AS PlacaVeh,M.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,TipoAfiVehic,M.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,M.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,VrFletes,VrRetencion,VrReteIca,VrDescuento,VrAnticipo,VrAntAdic,VrNeto,VrPagos,VrCargos,VrDctos,TarifaFlete,M.Cantidad AS CantTotal,PesoTotal,BaseRet,TarifaRet,TarifaIca
,FechaPago,PagoCargue,PagoDescargue,NumMintrans,EdoMintrans,M.Cumplido AS NumCumplido,M.IdCiaCump AS CdCiaCump,M.FechaCump AS FecCumplido,EstCumplido
,M.TipOdp AS TipoOdp,M.OrdPago AS NumOrdPago,M.IdCiaOdp AS CdCiaOdp,FechaOdp,EstOrden,Anulado,FecDev,M.Observacion AS Observ,M.IdEstado AS CdEstado,Estado
,NomRemite,NomDestino,LugarFletes,NumAnticipo,NumCheque,TipoMintrans,MucMintrans,ContIntegral,VolumenCarga
--Datos de novedades
,Item,C.Descripcion AS Descrpcion,Tarifa,TipoConc,RubroConcep,C.Cantidad AS CantConc,VrUnitario,TarifIva,C.IdConcepto AS CdConcepto,Concepto,CdCuenta,NomCuenta
,NitTercero,TC.RazonSocial AS NomTercero,CdTipoEsc,TipoEscolta,FechaNov,VrBase,TipoTarif,RefConc
--datos del propietario
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,Modelo,Config,V.Descripcion AS VehDescripcion
 FROM Trn_TraManifiesto AS M INNER JOIN  Trn_TraManifAnexo AS MA ON M.TipDoc=MA.TipDoc AND  M.Manifiesto=MA.Manifiesto AND  M.IdCia=MA.IdCia
 INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia  INNER JOIN EstadoDoc AS ED ON M.IdEstado=ED.IdEstado
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta  INNER JOIN Terceros AS  CDT ON M.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS NP ON M.IdPropietario=NP.IdTercero INNER JOIN Terceros AS T ON M.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca
 INNER JOIN  Trn_TraConceptos AS C ON M.TipDoc=C.TipDoc AND M.Manifiesto=C.Documento AND M.IdCia=C.IdCia
 LEFT JOIN ConcDiversos AS CDV ON C.IdConcepto=CDV.IdConcepto
 LEFT JOIN TiposEsc AS TE ON C.CdTipoEsc=TE.IdTipoEsc LEFT JOIN Terceros AS TC ON C.NitTercero=TC.IdTercero
 LEFT JOIN Puc AS P ON C.CdCuenta=P.IdCuenta
 WHERE M.TipDoc='MUC'  AND  M.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND M.IdVehiculo  LIKE ISNULL(@pmIdVehiculo ,'%') AND M.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraManifiestoDet] @pmTipDoc VARCHAR(3),@pmManifiestoIni INT,@pmManifiestoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT M.TipDoc AS CdTipMuc,M.Manifiesto AS NumManif,M.IdCia AS CdCia,M.Fecha AS FechaMuc,M.FecDespacho,M.IdRuta AS CdRuta,RT.Ruta AS DescRuta
,M.IdVehiculo AS PlacaVeh,M.nRemolque,M.TipoAfiVehic,M.EstCumplido,M.Cumplido AS NumCumplido,M.IdCiaCump AS CdCiaCump,M.FechaCump AS FecCumplido
,M.TipOdp AS TipoOdp,M.OrdPago AS NumOrdPago,M.IdCiaOdp AS CdCiaOdp,FechaOdp,EstOrden,M.Anulado,M.FecDev,M.Observacion AS Observ
,M.VrFletes,VrRetencion,VrReteIca,VrAnticipo,VrAntAdic,VrNeto,TarifaFlete
,MR.Item AS ItemMuc,TipRem,MR.Remesa AS NumRemesa,MR.IdCiaRem AS CdCiaRem,ItemRem,D.IdMercancia AS CdMercancia,DescripMcias,CodigoMcia,D.Cantidad AS Cant,PesoNeto,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol
,Cases,Cajas,Palets,NitRemite,Remitente,DirOrigen,D.IdOrigen AS RemCodOrigen,LO.Localidad AS RemCiudadOrigen,LO.IdDep AS RemCdepOrigen,DOR.Departamento AS RemDptoOrigen,NitDestntario,Destinatario,DirDestino
,D.IdDestino AS RemCodDestino,LD.Localidad AS RemCiudadDestino,LD.IdDep AS RemCdepDestino,DDN.Departamento AS RemDptoDestino,TarifClie,MR.TarifTabla AS TarifaTabla,MR.TarifPago AS TarifaPago,D.VrDeclarado AS DetVrDeclarado,D.VrSeguro AS DetVrSeguro
,TarifSeguro,UndTarifa,UndTarifPago,Remision,DocCliente,Referencia1,Referencia2,Referencia3,Contenedor1,Contenedor2,D.CdAgencia AS DetCdAgencia,DA.Agencia AS DetAgencia
,D.Cumplido AS RemCumplido,D.IdCiaCump AS RemCiaCump,D.FechaCump AS RemFechaCum,DetalleCump,CantidadCump,PesoCump,VolCump,CasesCump,CajasCump,PaletsCump,TarifCump,PagoCump,UndTarCump,UndTarPagoCump
,TipFac,Factura,IdCiaFac,FechaFac,TarifClieFac,D.TipOdp AS RemTipoOdp,D.NumeroOdp AS RemNumOdp,D.IdCiaOdp AS RemCiaOdp,TarifOdp,PesoCont,D.IdUnd AS CdUnd,UM.Unidad AS UndMedida
,IdCliente,CL.RazonSocial AS RemNomCliente,R.IdClieFact AS RemIdPropMcia,PM.RazonSocial AS RemPropMcia
 FROM Trn_TraManifiesto AS M INNER JOIN Trn_TraManifAnexo AS MA ON M.TipDoc=MA.TipDoc AND M.Manifiesto=MA.Manifiesto AND M.IdCia=MA.IdCia
 INNER JOIN Trn_TraManifRem AS MR ON M.TipDoc=MR.TipDoc AND M.Manifiesto=MR.Manifiesto AND M.IdCia=MR.IdCia
 INNER JOIN Trn_TraRemMcias AS D ON MR.TipRem=D.TipDoc  AND MR.Remesa=D.NumOrden AND MR.IdCiaRem=D.IdCia AND MR.ItemRem=D.Item
 INNER JOIN Trn_TraRemesa AS R ON MR.TipRem=R.TipDoc AND MR.Remesa=R.NumOrden AND MR.IdCiaRem=R.IdCia
 INNER JOIN Rutas AS RT ON M.IdRuta=RT.IdRuta 
 INNER JOIN Mercancias AS MCA ON D.IdMercancia=MCA.IdMercancia
 INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DOR ON LO.IdDep=DOR.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DDN ON LD.IdDep=DDN.IdDep
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo 
 INNER JOIN Terceros AS CL ON R.IdCliente=CL.IdTercero LEFT JOIN Terceros AS PM ON R.IdClieFact=PM.IdTercero
 LEFT JOIN Agencias AS DA ON D.CdAgencia=DA.IdAgencia LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed 
 WHERE M.TipDoc=@pmTipDoc AND M.Manifiesto BETWEEN @pmManifiestoIni AND @pmManifiestoFin AND M.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTercCndReferenciasVeh] 
 AS 
 SELECT V.IdVehiculo,V.IdConductor,C.RazonSocial AS NomConductor,R.Item,TipoRef,EmpresaRef,NitRef,DireccionRef,TelefonoRef
,ContactoRef,EmailRef,CargoRef,CuentaBanco,FechaInicial,FechaFinal,Verificado,ObservRef
,C.Telefono,C.TelMovil,C.e_mail
 FROM Vehiculos AS V INNER JOIN TercCndReferencias AS R ON V.IdConductor=R.IdConductor
 INNER JOIN Terceros AS C ON V.IdConductor=C.IdTercero
 WHERE V.Inactivo=0
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,TipoInf,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,TipoInf,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,FC.IdCuentaIva,FC.IdCuentaRet,FC.IdCuentaIca,SUM(ValorFactura) AS SvalFac
,SUM(VC.BaseImpu) AS BaseIva,SUM(VC.VrIva) AS TotalIva,SUM(VC.VrRetFte) AS TotalRet,SUM(VC.VrRetIca) AS TotalIca
 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,FC.IdCuentaIva,FC.IdCuentaRet,FC.IdCuentaIca
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryVehPartesLta] @pmIdVehiculo VARCHAR(10)=Null,@pmIdSistema VARCHAR(4)=Null
 AS 
 SELECT TipoUnidad,P.IdVehiculo AS PlacaVeh,NumParte,DescripParte,P.IdSistema AS CdSistema,Sistema,TipoParte,TipoControl
,Intervalo_km,Intervalo_dias,Intervalo_Horas,P.kmActual,kmManual,kmSat,P.kmCambio,kmCambioMan,kmCambioSat,FechaCambio,HorasTrab,HsUltCambio,CdProdRep,DescripProd
,P.IdEstado AS CdEstado,E.Estado,E.NColor,P.Inactivo,FechaRetiro,MarcaParte,P.ModeloParte,P.NumSerie,P.FechaCrea,P.IdUsuario AS CdUsuario,Usuario
--datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipo,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdColor AS CdColor,CL.NomColor AS Color
,V.IdLinea AS CdLinea,V.Modelo,FecRep,SerieChasis,CdRemque,V.IdPropietario,NP.RazonSocial AS Propietario,FecIngreso,V.KmInicial,V.KmActual AS kmsActual
,V.Km2Actual,NumSoat,FecSoat,VigSoat,V.Descripcion,V.IdEstado AS VehCodEstado,EV.Estado AS VehEstado,EV.NColor AS VehEdoColor,V.Inactivo AS VehInactivo
 FROM VehPartes AS P INNER JOIN MttoSistemas AS S ON P.IdSistema=S.IdSistema
 INNER JOIN EstadoSer AS E ON P.IdEstado=E.IdEstado INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 LEFT JOIN ProdMcias AS M ON P.CdProdRep=M.IdProducto
 LEFT JOIN Vehiculos AS V ON P.IdVehiculo=V.IdVehiculo LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
 LEFT JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca LEFT JOIN TiposCol AS CL ON V.IdColor=CL.IdColor
 LEFT JOIN Terceros AS NP ON V.IdPropietario=NP.IdTercero LEFT JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
 WHERE P.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND P.IdSistema LIKE ISNULL(@pmIdSistema,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpVehiculosSal] @pmIdVehiculo VARCHAR(10),@pmSaldoActual MONEY
AS
  UPDATE Vehiculos  SET VrSaldoAct=@pmSaldoActual
  WHERE  IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[paQryVehiculosSalPro] @pmIdPropietario VARCHAR(16),@pmIdAdmon VARCHAR(4)
AS
 SELECT IdPoseedor AS IdPropietario,IdVehiculo,NumVeh,IdAdmon,TipoAfil,IdConductor,RazonSocial,IdGrupo,VrSaldoAct
 FROM  Vehiculos INNER JOIN Terceros ON Vehiculos.IdConductor=Terceros.IdTercero
 WHERE IdPoseedor=@pmIdPropietario AND IdAdmon<>@pmIdAdmon AND TipoAfil<>'PROPIO'
 ORDER  BY IdPoseedor,IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraLiqManifFmt] @pmTipDoc VARCHAR(3),@pmLiquidacionIni INT,@pmLiquidacionFin INT,@pmIdCia CHAR(2)
 AS 
SELECT L.TipDoc,L.Liquidacion,L.IdCia AS CdCia,L.Item,L.TipMuc,L.Manifiesto,L.IdCiaMuc,M.Fecha AS FechaMuc,M.FecDespacho
,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,M.IdRuta AS CdRuta,RT.Ruta AS DescRuta,M.IdVehiculo AS PlacaVeh,M.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,M.nRemolque,M.TipoAfiVehic,M.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,M.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,M.VrFletes,VrAnticipo,VrAntAdic,VrNeto,TarifaFlete,M.Cantidad AS CantTotal,M.PesoTotal,M.FechaPago,NumMintrans,EdoMintrans,M.EstCumplido,M.Cumplido AS NumCumplido,M.IdCiaCump AS CdCiaCump,M.FechaCump AS FecCumplido
,M.TipOdp AS TipoOdp,M.OrdPago AS NumOrdPago,M.IdCiaOdp AS CdCiaOdp,FechaOdp,EstOrden,M.VrFleteNeto,M.Anulado,M.FecDev,M.Observacion AS Observ
,MA.VrFleteTon,MA.TipoRuta AS MucTipoRuta,MA.NomRemite AS NomRmtente,MA.NomDestino AS NomDestnatario,NumAnticipo,NumCheque,MA.TipoMintrans AS TipoMucMintrans,MucMintrans,VolumenCarga
 FROM Trn_TraLiqManif AS L INNER JOIN Trn_TraManifiesto AS M ON L.TipMuc=M.TipDoc AND L.Manifiesto=M.Manifiesto AND L.IdCiaMuc=M.IdCia
 INNER JOIN Trn_TraManifAnexo AS MA ON L.TipMuc=MA.TipDoc AND L.Manifiesto=MA.Manifiesto AND L.IdCiaMuc=MA.IdCia
 INNER JOIN Localidades AS CO ON M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS RT ON M.IdRuta=RT.IdRuta INNER JOIN Terceros AS CDT ON M.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS NP ON M.IdPropietario=NP.IdTercero INNER JOIN Terceros AS T ON M.IdPoseedor=T.IdTercero
 INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo  
 WHERE L.TipDoc=@pmTipDoc AND L.Liquidacion BETWEEN @pmLiquidacionIni AND @pmLiquidacionFin AND L.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpVehiculosCup] @pmIdVehiculo VARCHAR(10),@pmVrLmtCred MONEY,@pmVrSaldoAct MONEY
 AS 
 UPDATE Vehiculos SET  VrLmtCred=@pmVrLmtCred,VrSaldoAct=@pmVrSaldoAct
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CdCuentaIva,CdCuentaRet,CdCuentaIca,SUM(ValorFactura) AS SvalFac,SUM(VC.BaseImpu) AS BaseIva,SUM(VC.VrIva) AS TotalIva
,SUM(VC.VrRetFte) AS TotalRet,SUM(VC.VrRetIca) AS TotalIca,MAX(VC.Tarifa_Iva) AS TarifIva,MAX(VC.Tarifa_Ret) AS TarifRet,MAX(VC.Tarifa_Ica) AS TarifIca
 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,CdCuentaIva,CdCuentaRet,CdCuentaIca
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmDeclaracImp VARCHAR(50),@pmTipoIngreso VARCHAR(4),@pmIdOrgTra VARCHAR(8),@pmGPSoperador VARCHAR(250),@pmGPSUsuario VARCHAR(50),@pmGPSClave VARCHAR(50),@pmCantFiltros DECIMAL(14,4),@pmGPSIdOper VARCHAR(16)
 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,DeclaracImp,TipoIngreso,IdOrgTra,GPSoperador,GPSUsuario,GPSClave,CantFiltros,GPSIdOper)
 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,@pmDeclaracImp,@pmTipoIngreso,@pmIdOrgTra,@pmGPSoperador,@pmGPSUsuario,@pmGPSClave,@pmCantFiltros,@pmGPSIdOper)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[paQryTraTrasladosRel] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdVehiculo VARCHAR(10)=Null
,@pmIdConductor VARCHAR(16)=Null
AS 
 SELECT T.TipDoc AS CdTipDoc,TipoDoc,T.Traslado AS NumTraslado,T.IdCia AS CdCia,Compania,T.Fecha AS FecTraslado,T.FecDespacho AS FecDespTrasl,TipoTraslado,T.IdBodega AS CdBodega,BG.Bodega AS BodegaOrig,IdBodDtno,BD.Bodega AS BodegaDtno
,T.IdVehiculo AS PlacaVeh,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,T.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,Manifiesto,CdCiaMuc,EstadoRec,FechaRec,CedRecibido,NomRecibido
,T.Observacion AS Observ,T.OrigenAdd,T.TimeSys,T.IdCiaCrea,T.IdUsuario AS CdUsuario,Usuario
,DR.Item AS DetItem,TipRem,Remesa,IdCiaRem,ItemRem,EdoRecibido,Comentarios,IdMercancia,DescripMcias,D.Cantidad AS Cant,PesoNeto,UndMed,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol
,Cases,Cajas,Palets,NitRemite,Remitente,DirOrigen,IdOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,NitDestntario,Destinatario,DirDestino,IdDestino,LD.Localidad AS CiudadDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,TarifClie,D.VrDeclarado AS VlrDeclarado,D.VrSeguro AS VlrSeguro,TarifSeguro,UndTarifa,UndTarifPago,Remision,DocCliente,Referencia1,Referencia2,Referencia3,CdNovedad,Novedad
,R.FecDespacho AS FecDespRemesa,CL.TipoId AS CliTipoId,R.IdCliente AS NitCliente,CL.Dv AS CliDv,CL.RazonSocial AS NomCliente,R.IdAgencia AS CdAgencia,TipoAfiVehic,Modalidad,NumPedido,IdCiaPed,FechaPed,TipDcm,NumDocmto,IdCiaDcm,FechaDcm
,NumManif,IdCiaManif,EstCumplido,EstFactura,SerieGuia,NumGuia,R.CdBodega AS CodBodega,BR.Bodega AS BodActual
 FROM Trn_TraTraslados AS T INNER JOIN Companias AS CN ON T.IdCia=CN.IdCia INNER JOIN Terceros AS CDT ON T.IdConductor=CDT.IdTercero 
 INNER JOIN adm_Usuarios AS U ON T.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON T.TipDoc=TD.IdDoc
 INNER JOIN AlmBodegas AS BG ON T.IdBodega=BG.IdBodega INNER JOIN AlmBodegas AS BD ON T.IdBodDtno=BD.IdBodega
 INNER JOIN Trn_TraTrasladoRem AS DR ON T.TipDoc=DR.TipDoc AND T.Traslado=DR.Traslado AND T.IdCia=DR.IdCia
 INNER JOIN Trn_TraRemesa AS R ON DR.TipRem=R.TipDoc AND DR.Remesa=R.NumOrden AND DR.IdCiaRem=R.IdCia
 INNER JOIN Terceros AS CL ON R.IdCliente=CL.IdTercero  
 LEFT JOIN Trn_TraRemMcias AS D ON DR.TipRem=D.TipDoc AND DR.Remesa=D.NumOrden AND DR.IdCiaRem=D.IdCia AND DR.ItemRem=D.Item
 LEFT JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal LEFT JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 LEFT JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal LEFT JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 LEFT JOIN NovedadTra AS NV ON DR.CdNovedad=NV.IdNovedad
 LEFT JOIN AlmBodegas AS BR ON R.CdBodega=BR.IdBodega 
 LEFT JOIN Vehiculos AS V ON T.IdVehiculo=V.IdVehiculo LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 WHERE  T.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND T.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND T.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND T.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 AND R.Anulado=0
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryVehiculosCau] @pmIdVehiculo VARCHAR(10),@pmIdEstado VARCHAR(4)
AS
 SELECT IdVehiculo,NumVeh,CdTarifa,IdTipoVeh,PasjerosPie,PasjerosSen,IdPropietario,IdPoseedor,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[paQryVehiculosSalCue] @pmSaldoActual MONEY,@pmIdAdmon VARCHAR(4)
AS
 SELECT  IdPoseedor AS IdPropietario, SUM(VrSaldoAct) AS StotSal
 FROM  Vehiculos
 WHERE IdAdmon<>@pmIdAdmon AND TipoAfil<>'PROPIO'
 GROUP BY IdPoseedor
 HAVING  SUM(VrSaldoAct)<=@pmSaldoActual
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraCumRadicaFmt] @pmNumRadicaIni INT,@pmNumRadicaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT NumRadica,R.IdCia AS CdCia,CN.Compania AS NombreCia,R.Fecha AS FecRadica,R.TipMuc AS TipManif,R.Manifiesto AS NumManif,R.IdCiaMuc AS CdCiaManif,CU.IdVehiculo AS PlacaVeh
,CdCiaRad,CR.Compania AS CiaRadica,DocAnexos,CantDoc,FormaEnvio,NomRadica,CedRadica,Funcionario,R.Observacion AS Observ,NumeroCum,IdCiaCum,CU.Fecha AS FecCump,CU.Observacion AS ObservCump
--datos del manifiesto
,M.Fecha AS FecManif,FecDespacho,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,RT.Ruta AS DescRuta
,M.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,TipoAfiVehic,M.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario
,M.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,R.TimeSys AS FechaCrea,R.IdUsuario AS CdUsuario,Usuario
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.Descripcion AS VehDescripcion
 FROM Trn_TraCumRadica AS R  INNER JOIN Companias AS CN ON R.IdCia=CN.IdCia INNER JOIN Companias AS CR ON R.CdCiaRad=CR.IdCia
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 INNER JOIN Trn_TraManifiesto AS M ON R.TipMuc=M.TipDoc AND R.Manifiesto=M.Manifiesto AND R.IdCiaMuc=M.IdCia
 INNER JOIN Terceros AS T ON M.IdPoseedor=T.IdTercero INNER JOIN Terceros AS NP ON M.IdPropietario=NP.IdTercero
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS RT ON M.IdRuta=RT.IdRuta  INNER JOIN Terceros AS  CDT ON M.IdConductor=CDT.IdTercero 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 LEFT JOIN Trn_TraCumplido AS CU ON R.NumeroCum=CU.Cumplido AND R.IdCiaCum=CU.IdCia
 LEFT JOIN Vehiculos AS V ON CU.IdVehiculo=V.IdVehiculo  LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 LEFT JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 WHERE NumRadica BETWEEN @pmNumRadicaIni AND @pmNumRadicaFin AND R.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpVehiculosMuc] @pmIdVehiculo VARCHAR(10),@pmCdLocal VARCHAR(8),@pmUbicacion VARCHAR(100)
,@pmFecMuc SMALLDATETIME,@pmCdRutaHab VARCHAR(4)
 AS 
 UPDATE Vehiculos SET CdLocal=@pmCdLocal,Ubicacion=@pmUbicacion,FecUltServ=@pmFecMuc
,FecPriServ=ISNULL(FecPriServ,@pmFecMuc),CdRutaHab=@pmCdRutaHab
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraLiquidaDedFmt] @pmTipDoc VARCHAR(3),@pmLiquidacionIni INT,@pmLiquidacionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT D.TipDoc AS TipoLiq,Liquidacion,D.IdCia AS CdCia,Compania,D.Item AS nItem,TipCau,D.Causacion AS NumCausac,IdCiaCau,ItemCau,FechaCau
,D.IdConcepto AS CdConcCausac,D.Descripcion AS DetDescripcion,VrAbono,NitPoseedor,N.RazonSocial AS NomPoseedor,D.pVehiculo AS PlacaVeh,EsVence
,TipOdp,OrdenPago,IdCiaOdp,TipMuc,Manifiesto,IdCiaMuc,VrSaldoOrden,D.CdCuenta AS CodCuenta,CdCuePagar,NumFactura,D.CdCCosto AS CodCentro,CCosto,D.CdSubCos AS CodSubCosto,SubCosto
--Datos del detalle
,DC.Descripcion AS CauDescripcion,DC.VrDebito AS VrTotalDed,EstadoReg,DC.VrAbonado AS VrTotAbonado
--datos del poseedor
,N.TipoId AS TercTipId,N.Dv AS TercDv,N.Codigo AS TercCodigo,N.Direccion AS TercDireccion,N.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad
,N.Telefono AS TercTelefono,N.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdColor AS CdColor,NomColor,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,TipoAfil,V.Descripcion AS VehDescripcion
 FROM Trn_TraLiquidaDed AS D INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia 
 LEFT JOIN Terceros AS N ON D.NitPoseedor=N.IdTercero LEFT JOIN Localidades AS L ON N.IdLocal=L.IdLocal 
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos 
 LEFT JOIN Vehiculos AS V ON D.pVehiculo=V.IdVehiculo  LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 LEFT JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca  LEFT JOIN TiposCol AS CL ON V.IdColor=CL.IdColor
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero 
 LEFT JOIN Trn_TraCauDetalle AS DC ON D.TipCau=DC.TipDoc AND D.Causacion=DC.Causacion AND D.IdCiaCau=DC.IdCia AND D.ItemCau=DC.Item
 WHERE D.TipDoc=@pmTipDoc AND D.Liquidacion BETWEEN @pmLiquidacionIni AND @pmLiquidacionFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoControlesLta] @pmNumUnidad VARCHAR(10)=Null,@pmIdConc VARCHAR(4)=Null
 AS 
 SELECT M.TipoUnidad,M.NumUnidad AS IdVehiculo,M.IdConc AS CodConc,Concepto,C.IdSistema AS CdSistema,Sistema,C.IdMtto AS CdMtto,TipoMtto
,M.TipoControl,M.Intervalo_km,M.Intervalo_dias,M.Intervalo_Horas,M.kmUltMtto,M.kmUltMttoRuta,M.kmUltMttoSat,M.FecUltMtto,HsUltMtto
,C.TipoControl AS ConcControl,C.Intervalo_km AS ConcKm,C.Intervalo_dias AS ConcDias,C.Intervalo_Horas AS ConcHoras,C.Req_Parte,C.CantHoras,C.TarifaCosto
,M.Comentarios,M.Inactivo,M.FechaCrea,M.IdUsuario AS CdUsuario,Usuario
--campos de subconsulta vehiculos
,CdTipoVeh,TipoVehiculo,CdMarca,Marca,CdColor,NomColor,Modelo,Config,CdComb,TipoComb,SerieChasis,IdPropietario,NP.RazonSocial AS Propietario
,VigSoat,VigRCivil,VigRegNal,VigTecMec,KmInicial,VehKmActual,VehKmRuta,VehKmOtro,VH.Descripcion,VehInactivo
,VehEstado,Estado,EV.NColor AS EdoNumColor,EV.OutDemand AS EdoDisponible,HR.CantHoras AS AcumHoras,VH.IdGrupo AS CdGrupo,GrupoProp
FROM MttoControles AS M INNER JOIN MttoConceptos AS C ON M.IdConc=C.IdConc INNER JOIN MttoSistemas AS S ON C.IdSistema=S.IdSistema
INNER JOIN MttoTipos AS TM ON C.IdMtto=TM.IdMtto INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
--subconsulta vehiculos
LEFT JOIN (SELECT IdVehiculo AS VehPlaca,NumVeh,IdTipoVeh AS CdTipoVeh,IdMarca AS CdMarca,IdLinea AS CdLinea,IdColor AS CdColor,Modelo,Config,IdCom AS CdComb
,NumMotor,SerieChasis,CdRemque,NitEmpresa,IdPropietario,IdPoseedor,IdConductor,TipoAfil,FecCompra,FecIngreso,NumSoat,VigSoat,VigRCivil,VigRegNal,VigTecMec
,KmInicial,KmActual AS VehKmActual,Km2Actual AS VehKmRuta,CentFinal AS VehKmOtro,Descripcion,Inactivo AS VehInactivo,IdEstado AS VehEstado,IdGrupo
FROM Vehiculos UNION ALL SELECT IdRemque,'0','0',IdMarca,'0',IdColor,Modelo,ConfEjes,'0','',NumSerial,'',NitProv,IdPropietario,IdPropietario,'0','',FecCompra
,FecAdd,'',Null,Null,Null,VigPruebaHid,0,Kilmetraje,kmRuta,kmOtro,Descripcion,Inactivo,IdEstado,'0' FROM VehRemolq) AS VH ON M.NumUnidad=VH.VehPlaca
LEFT JOIN TiposVeh AS TV ON VH.CdTipoVeh=TV.IdTipoVeh LEFT JOIN Marcas AS MV ON VH.CdMarca=MV.IdMarca
LEFT JOIN TiposCol AS CL ON VH.CdColor=CL.IdColor LEFT JOIN TiposFuel AS TC ON VH.CdComb=TC.IdCom
LEFT JOIN Terceros AS NP ON VH.IdPropietario=NP.IdTercero LEFT JOIN EstadoVeh AS EV ON VH.VehEstado=EV.IdEstado
LEFT JOIN VehHoras AS HR ON M.TipoUnidad=HR.TipoUnidad AND M.NumUnidad=HR.NumUnidad
LEFT JOIN GruposPro AS GP ON VH.IdGrupo=GP.IdGrupo
WHERE M.NumUnidad LIKE ISNULL(@pmNumUnidad,'%') AND M.IdConc LIKE ISNULL(@pmIdConc,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpVehiculosCen] @pmIdVehiculo VARCHAR(10),@pmCentInicial INT,@pmCentFinal INT
AS
 UPDATE  Vehiculos  SET  CentInicial=@pmCentInicial,CentFinal=@pmCentFinal
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmNCuota
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,DeclaracImp,TipoIngreso,IdOrgTra,GPSoperador,GPSUsuario,GPSClave,CantFiltros,GPSIdOper
 FROM Vehiculos
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,DeclaracImp,TipoIngreso,IdOrgTra,GPSoperador,GPSUsuario,GPSClave,CantFiltros,GPSIdOper)
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,DeclaracImp,TipoIngreso,IdOrgTra,GPSoperador,GPSUsuario,GPSClave,CantFiltros,GPSIdOper
 FROM Vehiculos WHERE  IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraCumRadicaLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmCdCiaRad CHAR(2)=Null
 AS 
 SELECT NumRadica,R.IdCia AS CdCia,CN.Compania AS NombreCia,R.Fecha AS FecRadica,R.TipMuc AS TipManif,R.Manifiesto AS NumManif,R.IdCiaMuc AS CdCiaManif
,CdCiaRad,CR.Compania AS CiaRadica,DocAnexos,CantDoc,FormaEnvio,NomRadica,CedRadica,Funcionario,R.Observacion AS Observ,NumeroCum,IdCiaCum,CU.Fecha AS FecCump,CU.Observacion AS ObservCump
--datos del manifiesto
,M.Fecha AS FecManif,FecDespacho,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,RT.Ruta AS DescRuta
,M.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,M.IdVehiculo AS PlacaVeh,nRemolque,TipoAfiVehic,M.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario
,M.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,R.TimeSys AS FechaCrea,R.IdUsuario AS CdUsuario,Usuario
,M.FechaPago AS ManFechaPago,M.Remesa AS NumRemesa,M.IdCiaRem AS CiaRemesa,RM.Fecha AS FecRemesa,RM.IdCliente AS NitCliente,TC.RazonSocial AS NomCliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.Descripcion AS VehDescripcion
 FROM Trn_TraCumRadica AS R INNER JOIN Companias AS CN ON R.IdCia=CN.IdCia INNER JOIN Companias AS CR ON R.CdCiaRad=CR.IdCia
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 INNER JOIN Trn_TraManifiesto AS M ON R.TipMuc=M.TipDoc AND R.Manifiesto=M.Manifiesto AND R.IdCiaMuc=M.IdCia
 INNER JOIN Terceros AS T ON M.IdPoseedor=T.IdTercero INNER JOIN Terceros AS NP ON M.IdPropietario=NP.IdTercero
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS RT ON M.IdRuta=RT.IdRuta  INNER JOIN Terceros AS  CDT ON M.IdConductor=CDT.IdTercero 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 LEFT JOIN Trn_TraCumplido AS CU ON R.NumeroCum=CU.Cumplido AND R.IdCiaCum=CU.IdCia
 LEFT JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo  LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 LEFT JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca LEFT JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 LEFT JOIN (SELECT NumOrden,IdCia,Fecha,IdCliente,IdRemitente FROM Trn_TraRemesa  WHERE TipDoc ='RMT') AS RM ON M.Remesa=RM.NumOrden AND M.IdCiaRem=RM.IdCia 
 LEFT JOIN Terceros AS TC ON RM.IdCliente=TC.IdTercero
 WHERE R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND R.CdCiaRad LIKE ISNULL(@pmCdCiaRad,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmDeclaracImp VARCHAR(50),@pmTipoIngreso VARCHAR(4),@pmIdOrgTra VARCHAR(8),@pmGPSoperador VARCHAR(250),@pmGPSUsuario VARCHAR(50),@pmGPSClave VARCHAR(50),@pmCantFiltros DECIMAL(14,4),@pmGPSIdOper VARCHAR(16)
 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,DeclaracImp=@pmDeclaracImp,TipoIngreso=@pmTipoIngreso,IdOrgTra=@pmIdOrgTra,GPSoperador=@pmGPSoperador,GPSUsuario=@pmGPSUsuario,GPSClave=@pmGPSClave,CantFiltros=@pmCantFiltros,GPSIdOper=@pmGPSIdOper
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevCumFmt] @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT D.TipDev,D.Devolucion,D.IdCia AS CdCia,Compania,D.Fecha,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc,D.Cumplido,D.IdCiaDoc,D.FecDoc
,C.TipMuc,C.Manifiesto,C.IdCiaMuc,C.IdVehiculo AS PlacaVeh,C.Modalidad,C.DiasPlazo,C.FecPago,C.Observacion AS CumObserv
,C.TipoCumpMT,C.MotivoSusp,C.ConsecSusp,C.VrAdicCargue,C.VrAdicDescargue,C.VrAdicFlete,C.MotivoVrAdic
,C.VrDctoFlete,C.MotivoVrDcto,C.VrAdicAnticipo,C.FecEntregaDoc,C.NumRadicaMT,C.MvoAnulaCump,C.ObservAnulado,C.NumViajesCum,C.PesoLiqPago,C.PesoLiqFact
,C.CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,C.CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,C.CdRuta,R.Ruta AS DescRuta
,M.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,M.nRemolque,M.TipoAfiVehic,M.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,M.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,M.VrFletes,M.VrRetencion,M.VrReteIca,M.VrDescuento,M.VrAnticipo,M.VrAntAdic,M.VrNeto,M.VrPagos,M.VrCargos,M.VrDctos,M.TarifaFlete,M.Cantidad AS CantTotal,M.PesoTotal
,M.TipOdp AS TipoOdp,M.OrdPago AS NumOrdPago,M.IdCiaOdp AS CdCiaOdp,M.FechaOdp,M.EstOrden
,D.ModdDev,D.OrigenAdd,D.TipCom,D.Comprobante,D.IdCiaCom,D.Observacion AS Observ,D.TimeSys AS FechaCrea,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraDevCum AS D INNER JOIN Trn_TraCumplido AS C ON D.TipDoc=C.TipDoc AND D.Cumplido=C.Cumplido AND D.IdCiaDoc=C.IdCia
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Trn_TraManifiesto AS M ON C.TipMuc=M.TipDoc AND C.Manifiesto=M.Manifiesto AND C.IdCiaMuc=M.IdCia
 INNER JOIN Terceros AS CDT ON M.IdConductor=CDT.IdTercero INNER JOIN Terceros AS NP ON M.IdPropietario=NP.IdTercero
 INNER JOIN Terceros AS T ON M.IdPoseedor=T.IdTercero INNER JOIN Vehiculos AS V ON C.IdVehiculo=V.IdVehiculo
 INNER JOIN Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 LEFT JOIN Localidades AS CO ON C.CdOrigen=CO.IdLocal LEFT JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 LEFT JOIN Localidades AS CD ON C.CdDestino=CD.IdLocal LEFT JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 LEFT JOIN Rutas AS R ON C.CdRuta=R.IdRuta  
 WHERE D.TipDev=@pmTipDev AND D.Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraManifIntLtd] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT M.Manifiesto,M.IdCia AS CdCia,Compania,M.Fecha,M.FecEmision,DPO.IdPais AS CdPaisOrig,POR.NombrePais AS PaisOrig,LO.IdDep AS CdDepOrig,DPO.Departamento AS DptoOrigen,M.IdOrigen,LO.Localidad AS DescOrigen
,DPD.IdPais AS CdPaisDest,PDE.NombrePais AS PaisDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino,M.IdDestino,LD.Localidad AS DescDestino
,M.IdVehiculo AS PlacaVeh,M.nRemolque,M.IdConductor AS CdConductor,NC.RazonSocial AS CondTitula,M.IdCondRelev,NCA.RazonSocial AS CondAuxiliar,NatCarga,M.CdAduana,AF.NomAduana AS AduanaFront,M.CdAduanaDest,AD.NomAduana AS AduanaDest
,M.IdMoneda AS CdMoneda,MN.Simbolo,MN.Mneda AS Moneda,M.ValorTotal,M.PesoTotal,M.NumMuc,M.CdCiaMuc,M.Anulado,M.FecDev
,M.Observacion AS Observ,M.IdEstado AS CdEstado,Estado,M.Negociacion,M.OrigenAdd,M.TimeSys AS FechaCrea,M.FecUpdate,M.IdCiaCrea AS CdCiaCrea,M.IdUsuario AS CdUsuario,Usuario
,D.Item,D.TipOrd,D.NumOrden,D.IdCiaOrd,D.CdMercancia,D.DescripMcias,D.Cantidad,D.PesoNeto,D.PesoBruto,D.Volumen,D.ValorMcia,D.IdMoneda AS DetIdMoneda,MD.Mneda AS DetMoneda,MD.Simbolo AS DetMonSimb,D.IdUnd AS CdUnd,UM.Unidad
,D.IdEmp AS CdEmp,Empaque,D.IdNat AS CdNat,Natlzaprod,D.UndMed,UP.Unidad AS DesUndPeso,D.UndVol,UV.Unidad AS DesUndVol,D.Bultos,D.DescripUnid,CP.DocRemitente
 FROM Trn_TraManifInt AS M INNER JOIN Companias AS C ON M.IdCia=C.IdCia INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo
 INNER JOIN Localidades AS LO ON M.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep INNER JOIN adm_Paises AS POR ON DPO.IdPais=POR.IdPais
 INNER JOIN Localidades AS LD ON M.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep INNER JOIN adm_Paises AS PDE ON DPD.IdPais=PDE.IdPais
 INNER JOIN Terceros AS NC ON M.IdConductor=NC.IdTercero
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS E ON M.IdEstado=E.IdEstado
 INNER JOIN Trn_TraManifIntMcia AS D ON M.TipDoc=D.TipDoc AND M.Manifiesto=D.Manifiesto AND M.IdCia=D.IdCia
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN adm_monedas AS MD ON D.IdMoneda=MD.IdMneda
 INNER JOIN TiposNat AS NZ ON D.IdNat=NZ.IdNat 
 LEFT JOIN Terceros AS NCA ON M.IdCondRelev=NCA.IdTercero LEFT JOIN Aduanas AS AF ON M.CdAduana=AF.IdAduana LEFT JOIN Aduanas AS AD ON M.CdAduanaDest=AD.IdAduana
 LEFT JOIN adm_monedas AS MN ON M.IdMoneda=MN.IdMneda 
 LEFT JOIN Trn_TraPorteInt AS CP ON D.TipOrd=CP.TipDoc AND D.NumOrden=CP.NumOrden AND D.IdCiaOrd=CP.IdCia
 LEFT JOIN Sys_Um AS UP ON D.UndMed=UP.UndMed LEFT JOIN Sys_Um AS UV ON D.UndVol=UV.UndMed LEFT JOIN Empaques AS EP ON D.IdEmp=EP.IdEmp
 WHERE M.TipDoc=@pmTipDoc AND M.FecEmision BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraManifIntMcia] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmCdMercancia VARCHAR(16),@pmDescripMcias VARCHAR(500),@pmCantidad DECIMAL(14,4)
,@pmPesoNeto DECIMAL(14,4),@pmPesoBruto DECIMAL(14,4),@pmVolumen DECIMAL(14,4),@pmValorMcia MONEY,@pmIdMoneda VARCHAR(5),@pmIdUnd VARCHAR(4),@pmIdEmp VARCHAR(4),@pmIdNat VARCHAR(4),@pmUndMed VARCHAR(10),@pmUndVol VARCHAR(10),@pmBultos VARCHAR(150),@pmDescripUnid VARCHAR(500)
 AS 
 INSERT INTO Trn_TraManifIntMcia (TipDoc,Manifiesto,IdCia,Item,TipOrd,NumOrden,IdCiaOrd,CdMercancia,DescripMcias,Cantidad,PesoNeto,PesoBruto,Volumen,ValorMcia,IdMoneda,IdUnd,IdEmp,IdNat,UndMed,UndVol,Bultos,DescripUnid)
 VALUES (@pmTipDoc,@pmManifiesto,@pmIdCia,@pmItem,@pmTipOrd,@pmNumOrden,@pmIdCiaOrd,@pmCdMercancia,@pmDescripMcias,@pmCantidad,@pmPesoNeto,@pmPesoBruto,@pmVolumen,@pmValorMcia,@pmIdMoneda,@pmIdUnd,@pmIdEmp,@pmIdNat,@pmUndMed,@pmUndVol,@pmBultos,@pmDescripUnid)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpOilCompra] @pmTipDoc VARCHAR(3),@pmNumCompra INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdProveedor VARCHAR(16),@pmNumFactura VARCHAR(15),@pmFechaFac SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmCxPagar BIT,@pmSubTotal DECIMAL(16,6),@pmDescuento DECIMAL(16,6),@pmImpuesto DECIMAL(16,6),@pmRetencion DECIMAL(16,6),@pmReteIca DECIMAL(16,6),@pmReteIva DECIMAL(16,6),@pmIvaIngProd DECIMAL(16,6),@pmOtrosCargos DECIMAL(16,6),@pmOtrosDctos DECIMAL(16,6),@pmFletes DECIMAL(16,6),@pmVrNeto DECIMAL(16,6),@pmCantidad DECIMAL(16,6),@pmBaseImp DECIMAL(16,6),@pmBaseRet DECIMAL(16,6),@pmBaseIvaIgp DECIMAL(16,6),@pmTarifaIva DECIMAL(16,6),@pmTarifaRet DECIMAL(16,6),@pmTarifaIca DECIMAL(16,6),@pmTarifaRiv DECIMAL(16,6),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4)
,@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmModalidad VARCHAR(10),@pmFechaInicio SMALLDATETIME,@pmFechaFinal SMALLDATETIME,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmNumDev INT,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmEstRegCosto INT,@pmNumRegCosto INT,@pmCiaRegCosto CHAR(2)
 AS 
 UPDATE Trn_OilCompra SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdProveedor=@pmIdProveedor,NumFactura=@pmNumFactura,FechaFac=@pmFechaFac,FechaVence=@pmFechaVence,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,CxPagar=@pmCxPagar,SubTotal=@pmSubTotal,Descuento=@pmDescuento,Impuesto=@pmImpuesto,Retencion=@pmRetencion,ReteIca=@pmReteIca,ReteIva=@pmReteIva,IvaIngProd=@pmIvaIngProd,OtrosCargos=@pmOtrosCargos,OtrosDctos=@pmOtrosDctos,Fletes=@pmFletes,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,BaseIvaIgp=@pmBaseIvaIgp,TarifaIva=@pmTarifaIva,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv,CodTarIva=@pmCodTarIva,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos,Modalidad=@pmModalidad,FechaInicio=@pmFechaInicio,FechaFinal=@pmFechaFinal,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom
,Anulado=@pmAnulado,FecDev=@pmFecDev,NumDev=@pmNumDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,EstRegCosto=@pmEstRegCosto,NumRegCosto=@pmNumRegCosto,CiaRegCosto=@pmCiaRegCosto
 WHERE TipDoc=@pmTipDoc AND NumCompra=@pmNumCompra AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelOilCompra] @pmTipDoc VARCHAR(3),@pmNumCompra INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_OilCompra
 WHERE TipDoc=@pmTipDoc AND NumCompra=@pmNumCompra AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilCompra] @pmTipDoc VARCHAR(3),@pmNumCompra INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumCompra,IdCia,Fecha,IdConcepto,IdProveedor,NumFactura,FechaFac,FechaVence,MulPlazos,IdPlazo,CxPagar,SubTotal,Descuento,Impuesto,Retencion,ReteIca,ReteIva,IvaIngProd,OtrosCargos,OtrosDctos,Fletes,VrNeto,Cantidad,BaseImp,BaseRet,BaseIvaIgp,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,CdCCosto,CdSubCos,Modalidad
,FechaInicio,FechaFinal,TipCom,Comprobante,IdCiaCom,Anulado,FecDev,NumDev,Observacion,IdEstado,EstRegCosto,NumRegCosto,CiaRegCosto,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario
 FROM Trn_OilCompra
 WHERE TipDoc=@pmTipDoc AND NumCompra=@pmNumCompra AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilCompraFmt] @pmTipDoc VARCHAR(3),@pmNumCompraIni INT,@pmNumCompraFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipDoc,TipoDoc,C.NumCompra,C.IdCia,Compania,C.Fecha,C.IdProveedor,T.RazonSocial,C.NumFactura,C.FechaFac,C.FechaVence,C.Modalidad,C.SubTotal,C.Descuento,C.Impuesto,C.Retencion,C.ReteIca
,C.ReteIva,C.IvaIngProd,C.OtrosCargos,C.OtrosDctos,C.Fletes,C.VrNeto,C.Cantidad,C.BaseImp,C.BaseRet,C.BaseIvaIgp,C.TarifaIva,C.TarifaRet,C.TarifaIca,C.TarifaRiv
,C.MulPlazos,C.IdPlazo,Plazo,NVmto,DiasPago,C.CxPagar,C.IdConcepto,Concepto,C.CdCCosto,CC.CCosto AS CentCosto,C.CdSubCos,SC.SubCosto AS SubcCosto,C.FechaInicio,C.FechaFinal,C.EstRegCosto,C.NumRegCosto,C.CiaRegCosto,C.TipCom,C.Comprobante,C.IdCiaCom
,C.CodTarIva,C.CodTarRet,C.CodTarIca,C.CodTarRiv,C.Anulado,C.FecDev,C.NumDev,C.Observacion,C.IdEstado,ED.Estado,C.FechaCrea,C.IdCiaCrea,C.OrigenAdd,C.IdUsuario,Usuario
,D.Item,D.IdProducto,P.DescripProd,D.Cantidad,D.UndMed,UM.Unidad,D.VrUnitario,D.CostoUnit,D.VrTotal,D.VrDcto,D.VrIva,D.TarifaIva,D.TarifaDct,D.CodTarIva,D.CodTarDct,D.Descripcion,D.CdMoneda,Mneda
,D.CantBruto,D.CantNeto,D.CantEntrada,D.UMCargue,D.UMEntrada
,P.IdSubgrupo,P.TipoRef,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea
,T.TipoId,T.Dv,T.Codigo AS CodTercero,T.NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,T.Telefono,Fax,T.e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte
 FROM Trn_OilCompra AS C 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 Terceros AS T ON C.IdProveedor=T.IdTercero INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=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 Trn_OilCompraProd AS D ON C.TipDoc=D.TipDoc AND C.NumCompra=D.NumCompra AND C.IdCia=D.IdCia
 INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed
 INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea LEFT JOIN adm_monedas AS MN ON D.CdMoneda=MN.IdMneda
 LEFT JOIN Plazos AS PZ ON C.IdPlazo=PZ.IdPlazo LEFT JOIN TercProvee AS TP ON C.IdProveedor=TP.IdProv
 LEFT JOIN CentroCosto AS CC ON C.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON C.CdSubCos=SC.IdSubCos
 WHERE C.TipDoc=@pmTipDoc AND C.NumCompra BETWEEN @pmNumCompraIni AND @pmNumCompraFin AND C.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilCostosFmt] @pmTipProc VARCHAR(3),@pmNumProcesoIni INT,@pmNumProcesoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT PC.TipProc,TipoDoc,PC.NumProceso,PC.IdCia,CI.Compania,PC.Fecha,PC.FechaInicio,PC.FechaFinal,PC.IdConcepto,Concepto,PC.NitProveedor AS NitTercero,N.RazonSocial,PC.CodCiaFac,CC.Compania AS CiaCompras
,PC.Cantidad,PC.SubTotal,PC.VrTransporte,PC.VrTraVacio,PC.TipCom,PC.Comprobante,PC.IdCiaCom,PC.Anulado,PC.FecDev,PC.NumDev,PC.Observacion,PC.FechaCrea,PC.IdCiaCrea,PC.OrigenAdd,PC.IdUsuario,Usuario
--detalles
,D.Item,D.TipCom,D.NumCompra,D.IdCiaCom,D.ItemCom,D.IdProducto,P.DescripProd,D.Cantidad,D.UndMed,UM.Unidad,D.VrUnitario,D.VrTotal,D.CostoUnit,D.CantNeto,D.UMCargue,UMC.Unidad AS UM_Cargue
,C.Fecha AS FecCompra,C.IdProveedor,T.RazonSocial AS NomProveedor,C.NumFactura,C.FechaFac,C.FechaVence,C.Modalidad,C.FechaInicio AS FecIniPdo,C.FechaFinal AS FecFinPdo,C.Observacion AS ObservCompra
FROM Trn_OilCostos AS PC INNER JOIN Trn_OilCostosProd AS D ON PC.TipProc=D.TipProc AND PC.NumProceso=D.NumProceso AND PC.IdCia=D.IdCia
INNER JOIN Companias AS CI ON PC.IdCia=CI.IdCia INNER JOIN Conceptos AS CN ON PC.IdConcepto=CN.IdConcepto
INNER JOIN adm_Usuarios AS U ON PC.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON PC.TipProc=TD.IdDoc
INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed
INNER JOIN Trn_OilCompra AS C ON D.TipCom=C.TipDoc AND D.NumCompra=C.NumCompra AND D.IdCiaCom=C.IdCia
INNER JOIN Terceros AS T ON C.IdProveedor=T.IdTercero
LEFT JOIN Terceros AS N ON PC.NitProveedor=N.IdTercero LEFT JOIN Companias AS CC ON PC.CodCiaFac=CC.IdCia
LEFT JOIN Sys_Um AS UMC ON D.UMCargue=UMC.UndMed
WHERE PC.TipProc=@pmTipProc AND PC.NumProceso BETWEEN @pmNumProcesoIni AND @pmNumProcesoFin AND PC.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpOilCompraDev] @pmTipDoc VARCHAR(3),@pmNumCompra INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmNumDev INT,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_OilCompra SET Anulado=@pmAnulado,FecDev=@pmFecDev,NumDev=@pmNumDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND NumCompra=@pmNumCompra AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOilCompra] @pmTipDoc VARCHAR(3),@pmNumCompra INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdProveedor VARCHAR(16),@pmNumFactura VARCHAR(15),@pmFechaFac SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmCxPagar BIT,@pmSubTotal DECIMAL(16,6),@pmDescuento DECIMAL(16,6),@pmImpuesto DECIMAL(16,6),@pmRetencion DECIMAL(16,6),@pmReteIca DECIMAL(16,6),@pmReteIva DECIMAL(16,6),@pmIvaIngProd DECIMAL(16,6),@pmOtrosCargos DECIMAL(16,6),@pmOtrosDctos DECIMAL(16,6),@pmFletes DECIMAL(16,6),@pmVrNeto DECIMAL(16,6),@pmCantidad DECIMAL(16,6),@pmBaseImp DECIMAL(16,6),@pmBaseRet DECIMAL(16,6),@pmBaseIvaIgp DECIMAL(16,6)
,@pmTarifaIva DECIMAL(16,6),@pmTarifaRet DECIMAL(16,6),@pmTarifaIca DECIMAL(16,6),@pmTarifaRiv DECIMAL(16,6),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmModalidad VARCHAR(10),@pmFechaInicio SMALLDATETIME,@pmFechaFinal SMALLDATETIME,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmNumDev INT,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmEstRegCosto INT,@pmNumRegCosto INT,@pmCiaRegCosto CHAR(2)
,@pmFechaCrea SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmOrigenAdd VARCHAR(10),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_OilCompra (TipDoc,NumCompra,IdCia,Fecha,IdConcepto,IdProveedor,NumFactura,FechaFac,FechaVence,MulPlazos,IdPlazo,CxPagar,SubTotal,Descuento,Impuesto,Retencion,ReteIca,ReteIva,IvaIngProd,OtrosCargos,OtrosDctos,Fletes,VrNeto,Cantidad,BaseImp,BaseRet,BaseIvaIgp,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,CdCCosto,CdSubCos,Modalidad,FechaInicio,FechaFinal,TipCom,Comprobante,IdCiaCom,Anulado,FecDev,NumDev,Observacion,IdEstado,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario,EstRegCosto,NumRegCosto,CiaRegCosto)
 VALUES (@pmTipDoc,@pmNumCompra,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdProveedor,@pmNumFactura,@pmFechaFac,@pmFechaVence,@pmMulPlazos,@pmIdPlazo,@pmCxPagar,@pmSubTotal,@pmDescuento,@pmImpuesto,@pmRetencion,@pmReteIca,@pmReteIva,@pmIvaIngProd,@pmOtrosCargos,@pmOtrosDctos,@pmFletes,@pmVrNeto,@pmCantidad,@pmBaseImp,@pmBaseRet,@pmBaseIvaIgp,@pmTarifaIva,@pmTarifaRet,@pmTarifaIca,@pmTarifaRiv,@pmCodTarIva,@pmCodTarRet,@pmCodTarIca,@pmCodTarRiv,@pmCdCCosto,@pmCdSubCos,@pmModalidad,@pmFechaInicio,@pmFechaFinal,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmFecDev,@pmNumDev,@pmObservacion,@pmIdEstado,@pmFechaCrea,@pmIdCiaCrea,@pmOrigenAdd,@pmIdUsuario,@pmEstRegCosto,@pmNumRegCosto,@pmCiaRegCosto)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilDevCompraFmt] @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
SELECT DV.TipDev,TipoDoc,DV.Devolucion,DV.IdCia,Compania,DV.Fecha,DV.IdConcepto,Concepto,DV.TipDoc,DV.NumCompra,DV.IdCiaDoc,DV.FecDoc,DV.IdProveedor,T.RazonSocial
,DV.NumFactura,C.FechaFac,C.FechaVence,DV.CxPagar,C.IdPlazo,Plazo,NVmto,DiasPago,DV.SubTotal,DV.Descuento,DV.Impuesto,DV.Retencion,DV.ReteIca,DV.ReteIva,DV.IvaIngProd,DV.OtrosCargos,DV.OtrosDctos,DV.Fletes,DV.VrNeto
,DV.Cantidad,DV.BaseImp,DV.BaseRet,DV.BaseIvaIgp,DV.TarifaIva,DV.TarifaRet,DV.TarifaIca,DV.TarifaRiv,DV.CodTarIva,DV.CodTarRet,DV.CodTarIca,DV.CodTarRiv
,DV.CdCCosto,CC.CCosto,DV.CdSubCos,SC.SubCosto,DV.Modalidad,C.FechaInicio,C.FechaFinal,DV.TipCom,DV.Comprobante,DV.IdCiaCom,DV.ModdDev,DV.Observacion,DV.FechaCrea,DV.IdCiaCrea,DV.OrigenAdd,DV.IdUsuario,Usuario
,D.Item,D.IdProducto,P.DescripProd,D.Cantidad,D.UndMed,UM.Unidad,D.VrUnitario,D.CostoUnit,D.VrTotal,D.VrDcto,D.VrIva,D.TarifaIva,D.TarifaDct,D.CodTarIva,D.CodTarDct,D.Descripcion,D.CdMoneda,Mneda,D.CantBruto,D.CantNeto,D.CantEntrada,D.UMCargue,D.UMEntrada
,P.IdSubgrupo,P.TipoRef,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea
,T.TipoId,T.Dv,T.Codigo AS CodTercero,T.NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,T.Telefono,Fax,T.e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte
FROM Trn_OilDevCompra AS DV INNER JOIN Trn_OilCompra AS C ON DV.TipDoc=C.TipDoc AND DV.NumCompra=C.NumCompra AND DV.IdCiaDoc=C.IdCia
INNER JOIN Trn_OilDevComProd AS D ON DV.TipDev=D.TipDev AND DV.Devolucion=D.Devolucion AND DV.IdCia=D.IdCia
INNER JOIN Companias AS CI ON DV.IdCia=CI.IdCia INNER JOIN Conceptos AS CN ON DV.IdConcepto=CN.IdConcepto
INNER JOIN adm_Usuarios AS U ON DV.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON DV.TipDev=TD.IdDoc 
INNER JOIN Terceros AS T ON DV.IdProveedor=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 ProdMcias AS P ON D.IdProducto=P.IdProducto INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed
INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
LEFT JOIN Plazos AS PZ ON C.IdPlazo=PZ.IdPlazo LEFT JOIN TercProvee AS TP ON DV.IdProveedor=TP.IdProv
LEFT JOIN CentroCosto AS CC ON DV.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON DV.CdSubCos=SC.IdSubCos
LEFT JOIN adm_monedas AS MN ON D.CdMoneda=MN.IdMneda
WHERE DV.TipDev=@pmTipDev AND DV.Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND DV.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmCdGrupo,tmTipoContab)
 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,CdGrupoDif,TipoContable
 FROM Trn_AfAmtDetalle AS DT INNER JOIN Diferidos AS D ON DT.IdDiferido=D.IdDiferido
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,TipoInvers,TipoContable,CodNiifDif,CodNiifAmtza,TipComp,NumComp,CdCiaComp
 FROM Diferidos
 WHERE IdDiferido=@pmIdDiferido
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelDiferidos] @pmIdDiferido VARCHAR(30)
 AS 
 DELETE FROM Diferidos
 WHERE IdDiferido=@pmIdDiferido
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,TipoInvers,TipoContable,CodNiifDif,CodNiifAmtza
 FROM Diferidos
 WHERE FecInicio<=@pmFechaFin
  AND (IdEstado<>'9999'  OR (IdEstado='9999' AND FecRetiro>=@pmFechaIni))
 ORDER BY IdDiferido
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,IdGrupo
 FROM tm_DifIngreso AS T INNER JOIN Diferidos AS D ON T.tmIdDiferido=D.IdDiferido
 WHERE tmNumero=@pmtmNumero
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 DATETIME
,@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),@pmTipoInvers VARCHAR(10),@pmTipoContable VARCHAR(10),@pmCodNiifDif VARCHAR(16),@pmCodNiifAmtza VARCHAR(16)
,@pmTipComp VARCHAR(3),@pmNumComp INT,@pmCdCiaComp CHAR(2),@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,TipoInvers,TipoContable,CodNiifDif,CodNiifAmtza,TipComp,NumComp,CdCiaComp)
 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,@pmTipoInvers,@pmTipoContable,@pmCodNiifDif,@pmCodNiifAmtza,@pmTipComp,@pmNumComp,@pmCdCiaComp)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 DATETIME,@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),@pmTipoInvers VARCHAR(10)
,@pmTipoContable VARCHAR(10),@pmCodNiifDif VARCHAR(16),@pmCodNiifAmtza VARCHAR(16),@pmTipComp VARCHAR(3),@pmNumComp INT,@pmCdCiaComp CHAR(2)
 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,TipoInvers=@pmTipoInvers,TipoContable=@pmTipoContable,CodNiifDif=@pmCodNiifDif,CodNiifAmtza=@pmCodNiifAmtza,TipComp=@pmTipComp,NumComp=@pmNumComp,CdCiaComp=@pmCdCiaComp
 WHERE IdDiferido=@pmIdDiferido
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmNumParte)
 SELECT  @pmtmNumero,Item,R.IdDiferido,R.IdCausal,'0','',VrCostoIni,R.VrAdiciones,R.VrDisminucion,VrAcuAjustes,VrAmortizaAcum,VrAjuAmtzaAcum,0,FechaRet
,'','',Concepto,R.IdCia,IdCCosto,IdSubCos,IdRespons,0
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_AfAmtLta] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdDiferido,NomDiferido,tmTipoContab,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,tmCdGrupo
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelProdCostos] @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2)
 AS 
 DELETE FROM ProdCostos
 WHERE IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTipoProd INT,@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,TipoProd=@pmTipoProd,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoProd,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNotaProdOrd]  @pmNumOrden INT,@pmIdCia CHAR(2),@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT NumNota,IdCia,Fecha,IdConcepto,IdTercero,VrSubTotal,VrCostos,CantEnt,CantSal,NumOrden,TipoProd,Modalidad
 FROM Trn_NotaProd
 WHERE  NumOrden=@pmNumOrden  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND  Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY IdCia,NumNota
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoProd,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoProd,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoProd
,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTipoProd INT
,@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,TipoProd,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,@pmTipoProd,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev
,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
  ,SUM(tmBasExceso) AS STOTBEXC,SUM(CASE WHEN tmIdConcepto='IGE' AND tmBasExceso>0 THEN tmBasExceso ELSE tmVrBaseLiq END) AS SBASEIGE 
  FROM tm_Nomina 
  WHERE tmNumero=@pmtmNumero AND tmIdEmpleado=@pmtmIdEmpleado AND tmIdConcepto NOT IN ('BAS','LIC','IGE','IRP','HHD','HHN','DSR','VAC','DCP','DCS','FSP','FSU','RET')
  AND tmClaseLiq LIKE ISNULL(@pmtmClaseLiq,'%')
  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
  ,SUM(tmBasExceso) AS STOTBEXC,SUM(CASE WHEN tmIdConcepto='IGE' AND tmBasExceso>0 THEN tmBasExceso ELSE tmVrBaseLiq END) AS SBASEIGE 
  FROM tm_Nomina 
  WHERE tmNumero=@pmtmNumero AND tmIdEmpleado=@pmtmIdEmpleado AND tmClaseLiq IN ('NOMINA','NOVEDAD')
  AND tmIdConcepto NOT IN ('DCP','DCS','FSP','FSU','RET')
  GROUP BY tmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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(tmDiasCalc) AS SDIASCAL
,SUM(CASE  WHEN tmIdConcepto IN ('DNR','SAN','STC') THEN tmCantDed*tmVrUnitario WHEN tmIdConcepto IN ('IGE','IRP') THEN tmVrBaseLiq  ELSE tmVrBaseLiq END) AS SBASLIQ
 FROM tm_Nomina 
 WHERE  tmNumero=@pmtmNumero AND  tmIdEmpleado=@pmtmIdEmpleado 
 AND tmIdConcepto IN ('BAS','AXT','LIC','IGE','IRP','HHD','HHN','DSR','VAC','DNR','SAN','STC')
 AND (tmVrBaseLiq>0 OR tmIdConcepto='DNR' OR tmIdConcepto='SAN' OR tmIdConcepto='STC')
 AND tmClaseLiq LIKE ISNULL(@pmtmClaseLiq,'%')
 GROUP BY tmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,SUM(CASE WHEN tmIdConcepto='IGE' AND tmDiasCalc>0 AND tmBasExceso>0 THEN tmBasExceso ELSE 0 END) AS BASEIGECON
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmHoraInicial,tmHoraFinal,tmTipoInc
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,@pmtmBasExceso MONEY,@pmtmNitTercero VARCHAR(16),@pmtmHoraInicial DATETIME,@pmtmHoraFinal DATETIME,@pmtmTipoInc VARCHAR(20)
 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,tmBasExceso,tmNitTercero,tmHoraInicial,tmHoraFinal,tmTipoInc)
 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,@pmtmBasExceso,@pmtmNitTercero,@pmtmHoraInicial,@pmtmHoraFinal,@pmtmTipoInc)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmBasExceso,tmNitTercero,tmHoraInicial,tmHoraFinal,tmTipoInc
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmNitTercero
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero AND tmClaseLiq IN ('NOMINA','PROVISION','APORTE','NOVEDAD',@pmClaseLiq)
 AND  tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmBasExceso,tmNitTercero,tmHoraInicial,tmHoraFinal,tmTipoInc)
 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,0,'',Null,Null,''
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,tmHoraInicial,tmHoraFinal,tmTipoInc
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero  AND tmClaseLiq IN ('NOMINA','NOVEDAD')
 AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
ORDER BY tmIdEmpleado,tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,@pmtmBasExceso MONEY,@pmtmNitTercero VARCHAR(16),@pmtmHoraInicial DATETIME,@pmtmHoraFinal DATETIME,@pmtmTipoInc VARCHAR(20)
 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,tmBasExceso=@pmtmBasExceso,tmNitTercero=@pmtmNitTercero
,tmHoraInicial=@pmtmHoraInicial,tmHoraFinal=@pmtmHoraFinal,tmTipoInc=@pmtmTipoInc
 WHERE tmNumero=@pmtmNumero AND tmIdEmpleado=@pmtmIdEmpleado AND tmItem=@pmtmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmBasExceso,tmNitTercero,tmHoraInicial,tmHoraFinal,tmTipoInc)
 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,0,'',Null,Null,''
 FROM Trn_NomVacDet  AS D INNER JOIN NomConceptos AS C ON D.IdConcepto=C.IdConcepto
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraOrdenAbo] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME
,@pmTipAnt VARCHAR(3),@pmAnticipo INT,@pmIdCiaAnt CHAR(2),@pmVrAbono MONEY,@pmTipoAplica VARCHAR(10),@pmObservacion VARCHAR(250),@pmCodCuenta VARCHAR(16)
 AS 
 INSERT INTO Trn_TraOrdenAbo (TipDoc,Documento,IdCia,Item,Fecha,TipAnt,Anticipo,IdCiaAnt,VrAbono,TipoAplica,Observacion,CodCuenta)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmItem,@pmFecha,@pmTipAnt,@pmAnticipo,@pmIdCiaAnt,@pmVrAbono,@pmTipoAplica,@pmObservacion,@pmCodCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenAboFmt] @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT Documento,AP.IdCia AS CdCia,Item,AP.Fecha AS FechaDoc,TipAnt,AP.Anticipo AS NumAntcpo,IdCiaAnt,VrAbono,TipoAplica,AP.Observacion,CodCuenta,A.Fecha AS FechaAnt
,A.TipOds,A.NumOrden,IdCiaOds,O.IdVehiculo AS PlacaVeh,O.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,A.VrAnticipo,A.VrAbonado,TipoPago,NumCheque,FecCheque,A.IdCta AS CdCta,NumeroCta,CTA.IdBanco,Banco,A.IdCuenta AS CdCuenta,NomCuenta
,Beneficiario,FechaVence,A.TipCom,A.Comprobante,A.IdCiaCom,A.Anulado,A.NumDev,A.FecDev,A.Observacion AS AntObserv,AP.TipDoc AS Tip_Doc
 FROM Trn_TraOrdenAbo AS AP  INNER JOIN  Trn_TraOrdenAnt AS A ON AP.TipAnt=A.TipDoc  AND AP.Anticipo=A.Anticipo  AND AP.IdCiaAnt=A.IdCia
 INNER JOIN Trn_TraOrdenServ AS O ON A.TipOds=O.TipDoc AND A.NumOrden=O.NumOrden AND A.IdCiaOds=O.IdCia
 INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN Terceros AS T ON O.IdPoseedor=T.IdTercero
 LEFT JOIN CtasCorrientes AS CTA ON A.IdCta=CTA.IdCta LEFT JOIN Bancos AS B ON CTA.IdBanco=B.IdBanco
 LEFT JOIN Puc AS P ON A.IdCuenta=P.IdCuenta
 WHERE AP.TipDoc=@pmTipDoc AND AP.Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin AND AP.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[paInstm_TraAnt_Aos] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraAnt (tmNumero,tmItem,tmTipAnt,tmAnticipo,tmIdCia,tmValor,tmVrAnticipo,tmTotalAbonos,tmDetalle,tmCdCueAnt)
 SELECT @pmtmNumero,Item,TipAnt,A.Anticipo,IdCiaAnt,A.VrAbono,AN.VrAnticipo,AN.VrAbonado,A.Observacion,CodCuenta
 FROM Trn_TraOrdenAbo AS A INNER JOIN Trn_TraOrdenAnt AS AN ON A.TipAnt=AN.TipDoc AND A.Anticipo=AN.Anticipo AND A.IdCiaAnt=AN.IdCia
 WHERE  A.TipDoc=@pmTipDoc AND A.Documento=@pmDocumento AND A.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpRecBusesCeroVeh] @pmIdVehiculo VARCHAR(10)
AS
  UPDATE  Trn_RecBuses  SET  ValorAbono=0
  WHERE ValorAnticipo>0 AND  IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetalle_Ocv] @pmTipDoc VARCHAR(3),@pmNumViaje INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraDetalle (tmNumero,tmItem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed,tmdmsAlto,tmdmsAncho,tmdmsLargo,tmVolumen,tmUndVol,tmIdUnd,tmIdEmp,tmIdNat
,tmIdMnjo,tmIdTmcia,tmCdRango,tmCases,tmCajas,tmPalets,tmRemision,tmNumOrden,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen,tmNitDestntario,tmDestinatario,tmDirDestino,tmIdDestino
,tmCdAgencia,tmTipoVehic,tmTarifClie,tmTarifPago,tmTarifTabla,tmVrDeclarado,tmVrSeguro,tmTarifSeguro,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmContenedor1,tmContenedor2,tmUndTarifa
,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmNumMintrans,tmPesoCont,tmUndTarPago,tmCodBodega,tmTipoTraslado,tmTieCargue,tmTieDesc,tmFecIniCargue,tmFecFinCargue,tmFecInidesc,tmFecFindesc,tmCdTipoVeh,tmTipoServicio
,tmSedeRem,tmSedeDest,tmFecLlegaCargue,tmFecLlegaDesc,tmMinCargue,tmMinDescargue)
SELECT @pmtmNumero,RM.Item,IdMercancia,DescripMcias,Cantidad,PesoNeto,UndMed,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol,IdUnd,IdEmp,IdNat
,IdMnjo,IdTmcia,CdRango,Cases,Cajas,Palets,Remision,Factura,NitRemite,Remitente,DirOrigen,IdOrigen,NitDestntario,Destinatario,DirDestino,IdDestino
,CdAgencia,'TERCEROS',TarifClie,TarifPago,TarifTabla,VrDeclarado,VrSeguro,TarifSeguro,DocCliente,Referencia1,Referencia2,Referencia3,Contenedor1,Contenedor2,UndTarifa
,TipOrden,RM.NumOrden,IdCiaOrd,ItemOrd,0,PesoCont,UndTarifPago,'','',0,0,Null,Null,Null,Null,'','',SedeRem,SedeDest,Null,Null,0,0
 FROM Trn_TraInfViajesOrd AS RM INNER JOIN  Trn_TraRemMcias  AS D ON  RM.TipOrden=D.TipDoc AND RM.NumOrden=D.NumOrden AND RM.IdCiaOrd=D.IdCia AND RM.ItemOrd=D.Item
 WHERE RM.TipDoc=@pmTipDoc AND RM.NumViaje=@pmNumViaje AND RM.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CedContparte, 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelJurEstados] @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM JurEstados
 WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CedContparte, 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTanqAforo] @pmIdTanque VARCHAR(4),@pmUndAforo DECIMAL(14,4)
 AS 
 SELECT IdTanque,UndAforo,Cantidad
 FROM TanqAforo
 WHERE IdTanque=@pmIdTanque AND UndAforo=@pmUndAforo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpGruposUsu] @pmIdGrupo INT,@pmGrupo VARCHAR(50),@pmDescripcion VARCHAR(100)
 AS 
 UPDATE Sys_GruposUsu SET Grupo=@pmGrupo,Descripcion=@pmDescripcion
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsGruposUsu] @pmIdGrupo INT,@pmGrupo VARCHAR(50),@pmDescripcion VARCHAR(100)
 AS 
 INSERT INTO Sys_GruposUsu (IdGrupo,Grupo,Descripcion)
 VALUES (@pmIdGrupo,@pmGrupo,@pmDescripcion)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelGruposUsu] @pmIdGrupo INT
 AS 
 DELETE FROM Sys_GruposUsu
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomNovedadPen] @pmIdEmpleado VARCHAR(16),@pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME
,@pmIdConcepto VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null,@pmRemnrado BIT=Null,@pmCantDias INT=0
 AS 
 SELECT N.IdEmpleado,N.Numero,N.IdConcepto,N.FecInicio,N.FecFinal,N.CantDias,N.DiaInicio,N.Modalidad,N.IdNov,N.CodInstla
,CodClsRie,N.NumAutza,CodOldFon,CodNewFon,N.VrCotVolAfi,N.VrCotVolApo,N.VrNoRetenido,N.TipoIncap
,Remnrado,FechaDcto,VrDescuento,N.TipoBaseLiq,N.Observacion,N.IdUsuario,N.Inactivo
,C.Concepto,C.Tarifa AS ConcTarif,C.BaseTrfa AS ConcBase,C.DiasEmp,C.TarifaEmp
 FROM Trn_NomNovedad AS N INNER JOIN NomConceptos AS C ON N.IdConcepto=C.IdConcepto
 WHERE N.IdEmpleado=@pmIdEmpleado AND N.Inactivo=0 AND N.CantDias>@pmCantDias AND N.FecFinal>=@pmFecIni AND N.FecInicio<=@pmFecFin
 AND N.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND N.Modalidad LIKE ISNULL(@pmModalidad,'%') 
 AND (Remnrado=ISNULL(@pmRemnrado,0) or Remnrado=ISNULL(@pmRemnrado,1))
 ORDER BY N.Numero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsNomConceptos] @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(100),@pmClaseCon VARCHAR(10),@pmUnidad VARCHAR(10),@pmTarifa DECIMAL(10,4)
,@pmBaseTrfa CHAR(2),@pmPdtermnado BIT,@pmInactivo BIT,@pmTarifaEmp DECIMAL(14,4),@pmDiasEmp INT,@pmIdConcNE VARCHAR(50),@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO NomConceptos (IdConcepto,Concepto,ClaseCon,Unidad,Tarifa,BaseTrfa,Pdtermnado,Inactivo,FechaAdd,IdUsuario,TarifaEmp,DiasEmp,IdConcNE)
 VALUES (@pmIdConcepto,@pmConcepto,@pmClaseCon,@pmUnidad,@pmTarifa,@pmBaseTrfa,@pmPdtermnado,@pmInactivo,@pmFechaAdd,@pmIdUsuario,@pmTarifaEmp,@pmDiasEmp,@pmIdConcNE)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ((Q.TipoLiq='CES' AND Semstre=0) OR Q.TipoLiq<>'CES')
 AND Q.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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpNomConceptos] @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(100),@pmClaseCon VARCHAR(10),@pmUnidad VARCHAR(10),@pmTarifa DECIMAL(10,4)
,@pmBaseTrfa CHAR(2),@pmPdtermnado BIT,@pmInactivo BIT,@pmTarifaEmp DECIMAL(14,4),@pmDiasEmp INT,@pmIdConcNE VARCHAR(50),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE NomConceptos SET Concepto=@pmConcepto,ClaseCon=@pmClaseCon,Unidad=@pmUnidad,Tarifa=@pmTarifa,BaseTrfa=@pmBaseTrfa,Pdtermnado=@pmPdtermnado,Inactivo=@pmInactivo
,TarifaEmp=@pmTarifaEmp,DiasEmp=@pmDiasEmp,IdConcNE=@pmIdConcNE,FechaUpdate=@pmFechaUpdate
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NumPerMes,A.Observacion AS Observ,A.Inactivo AS Inactvo
,A.NitTercero,RazonSocial,AplicaPrima,AplicaVac,PdoAplicaEsp,VrAplicaEsp,A.FecAdd AS Fecha_Add,A.FecUpdate AS FecUpd,A.IdUsuario AS IdUsuari,Usuario,E.Inactivo AS Emp_Inactivo
 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 LEFT JOIN Terceros AS T ON A.NitTercero=T.IdTercero
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNomConceptos] @pmIdConcepto VARCHAR(4)
 AS 
 SELECT IdConcepto,Concepto,ClaseCon,Unidad,Tarifa,BaseTrfa,Pdtermnado,Inactivo,TarifaEmp,DiasEmp,IdConcNE,FechaAdd,FechaUpdate,IdUsuario
 FROM NomConceptos
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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.Anulado=0
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomConceptosLta] @pmClaseCon VARCHAR(10)=Null,@pmPdtermnado BIT=Null,@pmInactivo BIT=Null
 AS 
 SELECT C.IdConcepto,C.Concepto,C.ClaseCon,C.Unidad,C.Tarifa,C.BaseTrfa,C.Pdtermnado,C.TarifaEmp,C.DiasEmp
,C.IdConcNE,ConceptoNE,C.Inactivo,C.FechaAdd,C.FechaUpdate,C.IdUsuario
 FROM NomConceptos AS C LEFT JOIN NomConcDian AS CD ON C.IdConcNE=CD.IdConcNE
 WHERE C.ClaseCon LIKE ISNULL(@pmClaseCon,'%')
  AND  (Pdtermnado=ISNULL(@pmPdtermnado,0) or Pdtermnado=ISNULL(@pmPdtermnado,1))
  AND  (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY C.Concepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelNomConceptos] @pmIdConcepto VARCHAR(4)
 AS 
 DELETE FROM NomConceptos
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryFondosCla] @pmIdClase VARCHAR(4)
 AS 
  SELECT IdFondo,Fondo,IdClase,IdTercero,CodigoEps,CodigoPen,CodigoArp,Inactivo
  FROM Fondos
  WHERE IdClase=@pmIdClase
  ORDER BY IdFondo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 VARCHAR(5)=Null
,@pmTallaPan VARCHAR(5)=Null,@pmTallaZap VARCHAR(5)=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,FecUltParc,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo,DecRenta,NivelEduc,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFondos] @pmIdFondo VARCHAR(8)
 AS 
  SELECT IdFondo,Fondo,IdClase,IdTercero,CodigoEps,CodigoPen,CodigoArp,Inactivo
  FROM Fondos
  WHERE IdFondo=@pmIdFondo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryEmpleadosLab] @pmIdEmpleado VARCHAR(16),@pmCodigo VARCHAR(16)
 AS 
 SELECT IdEmpleado,Apellidos,Nombres,TipoId,Dv,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,FecUltParc,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo,DecRenta,NivelEduc
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_BalComparaGCue] @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCuenta,COUNT(tmItem) AS SCANT
 FROM tm_BalCompara
 WHERE tmEst=@pmtmEst
 GROUP BY tmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_BalCompara] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_BalCompara
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,NumPedido,CdCiaPed,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,NumPedido,CdCiaPed,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,NumPedido,CdCiaPed,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Cotizacion
 WHERE TipDoc=@pmTipDoc AND Cotizacion=@pmCotizacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmNumPedido INT,@pmCdCiaPed CHAR(2),@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,NumPedido,CdCiaPed,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,@pmNumPedido,@pmCdCiaPed,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmNumPedido INT,@pmCdCiaPed CHAR(2),@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,NumPedido=@pmNumPedido,CdCiaPed=@pmCdCiaPed,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Cotizacion=@pmCotizacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposMat] @pmIdMat VARCHAR(4)
 AS 
 DELETE FROM TiposMat
 WHERE IdMat=@pmIdMat
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposMat] @pmIdMat VARCHAR(4),@pmMaterial VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposMat (IdMat,Material,Inactivo)
 VALUES (@pmIdMat,@pmMaterial,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposMat] @pmIdMat VARCHAR(4),@pmMaterial VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposMat SET Material=@pmMaterial,Inactivo=@pmInactivo
 WHERE IdMat=@pmIdMat
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomConAuto] @pmIdReg INT
 AS 
 SELECT IdReg,IdConcepto,FecInicio,VrAplicar,Modalidad,OrigenBase,FrecAplica,CedEmpleado,CodNom,CodDep,CodCcto
,CodSubCos,CritSelect,FecFinal,Exceptuar,Observacion,Inactivo,NumPerMes,NitTercero,AplicaPrima,AplicaVac,PdoAplicaEsp,VrAplicaEsp,FecAdd,FecUpdate,IdUsuario
 FROM Trn_NomConAuto
 WHERE IdReg=@pmIdReg
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmNitTercero VARCHAR(16),@pmAplicaPrima INT,@pmAplicaVac INT,@pmPdoAplicaEsp VARCHAR(8),@pmVrAplicaEsp MONEY,@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
,NitTercero=@pmNitTercero,AplicaPrima=@pmAplicaPrima,AplicaVac=@pmAplicaVac,PdoAplicaEsp=@pmPdoAplicaEsp,VrAplicaEsp=@pmVrAplicaEsp,FecUpdate=@pmFecUpdate
 WHERE IdReg=@pmIdReg
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmNitTercero VARCHAR(16),@pmAplicaPrima INT,@pmAplicaVac INT,@pmPdoAplicaEsp VARCHAR(8),@pmVrAplicaEsp MONEY,@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,NitTercero,AplicaPrima,AplicaVac,PdoAplicaEsp,VrAplicaEsp)
 VALUES (@pmIdReg,@pmIdConcepto,@pmFecInicio,@pmVrAplicar,@pmModalidad,@pmOrigenBase,@pmFrecAplica,@pmCedEmpleado,@pmCodNom,@pmCodDep,@pmCodCcto,@pmCritSelect,@pmFecFinal,@pmExceptuar
,@pmObservacion,@pmCodSubCos,@pmInactivo,@pmNumPerMes,@pmFecAdd,@pmIdUsuario,@pmNitTercero,@pmAplicaPrima,@pmAplicaVac,@pmPdoAplicaEsp,@pmVrAplicaEsp)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NitTercero
,AplicaPrima,AplicaVac,PdoAplicaEsp,VrAplicaEsp,FecAdd,FecUpdate,IdUsuario
 FROM Trn_NomConAuto
 WHERE FecInicio<=@pmFecFin AND Inactivo=0 AND FrecAplica<>'PRIMA' AND FrecAplica<>'VACACIONES'
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_NiifGyp] @pmtmEst CHAR(2) 
 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,tmTipoAfi,tmIdCCosto,CCosto,tmIdSubCos,SubCosto
,TipoId,Dv,Direccion,T.IdLocal AS CdCiudad,Localidad,L.IdDep AS CdDepto,Departamento,Telefono 
,CodAgencia,Referencia
,tmItem,tmEst,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12
 FROM tm_NiifGyp AS GP INNER JOIN PucNiif 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
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_NiifGyp] @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),@pmtmTipoAfi INT
,@pmtmSaldoAnterior MONEY,@pmtmTotalDebitos MONEY,@pmtmTotalCreditos MONEY,@pmtmNuevoSaldo MONEY,@pmtmSalAnt2 MONEY,@pmtmDebitos2 MONEY,@pmtmCreditos2 MONEY,@pmtmSaldo2 MONEY
 AS 
 INSERT INTO tm_NiifGyp (tmEst,tmIdCuenta,tmItem,tmIdCia,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmCodAgncia,tmTipo,tmTipoAfi,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2)
 VALUES (@pmtmEst,@pmtmIdCuenta,@pmtmItem,@pmtmIdCia,@pmtmIdTercero,@pmtmIdVehiculo,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmCodAgncia,@pmtmTipo,@pmtmTipoAfi,@pmtmSaldoAnterior,@pmtmTotalDebitos,@pmtmTotalCreditos,@pmtmNuevoSaldo,@pmtmSalAnt2,@pmtmDebitos2,@pmtmCreditos2,@pmtmSaldo2)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraReexpedido] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraReexpedido
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraReexpedido] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdEmpTrans VARCHAR(16),@pmNumFactura VARCHAR(15),@pmFecFactura SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmVrTotal MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteIca MONEY,@pmVrOtrosCargos MONEY,@pmVrOtrosDctos MONEY,@pmVrNeto MONEY,@pmPesoTotal DECIMAL(14,4)
,@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmTarifaImp DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(16,6),@pmCodTarImp VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmReferencia VARCHAR(50),@pmCxPagar BIT,@pmCdPlazo 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_TraReexpedido SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdEmpTrans=@pmIdEmpTrans,NumFactura=@pmNumFactura,FecFactura=@pmFecFactura,FecVence=@pmFecVence,VrTotal=@pmVrTotal,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteIca=@pmVrReteIca,VrOtrosCargos=@pmVrOtrosCargos,VrOtrosDctos=@pmVrOtrosDctos,VrNeto=@pmVrNeto,PesoTotal=@pmPesoTotal,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,TarifaImp=@pmTarifaImp
,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,CodTarImp=@pmCodTarImp,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,Referencia=@pmReferencia,CxPagar=@pmCxPagar,CdPlazo=@pmCdPlazo,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraReexpedido] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdEmpTrans VARCHAR(16),@pmNumFactura VARCHAR(15),@pmFecFactura SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmVrTotal MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteIca MONEY,@pmVrOtrosCargos MONEY,@pmVrOtrosDctos MONEY,@pmVrNeto MONEY,@pmPesoTotal DECIMAL(14,4),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY
,@pmTarifaImp DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(16,6),@pmCodTarImp VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmReferencia VARCHAR(50),@pmCxPagar BIT,@pmCdPlazo 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_TraReexpedido (TipDoc,Causacion,IdCia,Fecha,IdConcepto,IdEmpTrans,NumFactura,FecFactura,FecVence,VrTotal,VrImpuesto,VrRetencion,VrReteIca,VrOtrosCargos,VrOtrosDctos,VrNeto,PesoTotal,BaseImp,BaseRet,BaseIca,TarifaImp,TarifaRet,TarifaIca,CodTarImp,CodTarRet,CodTarIca,Referencia,CxPagar,CdPlazo,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmCausacion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdEmpTrans,@pmNumFactura,@pmFecFactura,@pmFecVence,@pmVrTotal,@pmVrImpuesto,@pmVrRetencion,@pmVrReteIca,@pmVrOtrosCargos,@pmVrOtrosDctos,@pmVrNeto,@pmPesoTotal,@pmBaseImp,@pmBaseRet,@pmBaseIca,@pmTarifaImp,@pmTarifaRet,@pmTarifaIca,@pmCodTarImp,@pmCodTarRet,@pmCodTarIca,@pmReferencia,@pmCxPagar,@pmCdPlazo
,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraReexpedido] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Causacion,IdCia,Fecha,IdConcepto,IdEmpTrans,NumFactura,FecFactura,FecVence,VrTotal,VrImpuesto,VrRetencion,VrReteIca,VrOtrosCargos,VrOtrosDctos,VrNeto
,PesoTotal,BaseImp,BaseRet,BaseIca,TarifaImp,TarifaRet,TarifaIca,CodTarImp,CodTarRet,CodTarIca,Referencia,CxPagar,CdPlazo,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraReexpedido
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraReexpedidoFmt] @pmTipDoc VARCHAR(3),@pmCausacionIni INT,@pmCausacionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.Causacion AS NumCausac,C.IdCia AS CdCia,Compania,Fecha,C.IdConcepto AS CdConcepto,Concepto,IdEmpTrans,T.RazonSocial AS NomEmpresa,NumFactura,FecFactura,FecVence,VrTotal,VrImpuesto
,VrRetencion,VrReteIca,VrOtrosCargos,VrOtrosDctos,VrNeto,PesoTotal,BaseImp,BaseRet,BaseIca,TarifaImp,TarifaRet,TarifaIca,CodTarImp
,CodTarRet,CodTarIca,Referencia,CxPagar,CdPlazo,Plazo,NVmto,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado
--detalles
,Item,TipRem,Remesa,IdCiaRem,FecRemesa,Descripcion,Unidades,PesoNeto,D.UndMed AS CdUmed,UMP.Unidad AS UmPeso,Volumen,UndVol,UndTarifa,VrUnitario,ValorTotal
,pVehiculo,CedConductor,CDT.RazonSocial AS NomConductor
,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS CdUsuario,Usuario,C.TipDoc AS CdTipDoc,TipoDoc,Leyenda
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
 FROM Trn_TraReexpedido AS C  INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia INNER JOIN Conceptos AS CN ON C.IdConcepto=CN.IdConcepto
 INNER JOIN Terceros AS T ON C.IdEmpTrans=T.IdTercero INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Trn_TraReexpedidoRem AS D ON C.TipDoc=D.TipDoc AND C.Causacion=D.Causacion  AND C.IdCia=D.IdCia
 LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed
 LEFT JOIN Plazos AS PZ ON C.CdPlazo=PZ.IdPlazo  LEFT JOIN TiposCom AS TCM ON C.TipCom=TCM.IdCom
 LEFT JOIN Terceros AS  CDT ON D.CedConductor=CDT.IdTercero 
 WHERE C.TipDoc=@pmTipDoc   AND C.Causacion BETWEEN @pmCausacionIni AND @pmCausacionFin  AND C.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraReexpedidoAnu] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4) 
 AS 
 UPDATE Trn_TraReexpedido SET  Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraReexpedidoLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdEmpTrans VARCHAR(16)=Null
 AS 
 SELECT  Causacion,C.IdCia AS CdCia,Compania,Fecha,C.IdConcepto AS CdConcepto,Concepto,IdEmpTrans,RazonSocial,NumFactura,FecFactura,FecVence,VrTotal,VrImpuesto
,VrRetencion,VrReteIca,VrOtrosCargos,VrOtrosDctos,VrNeto,PesoTotal,BaseImp,BaseRet,BaseIca,TarifaImp,TarifaRet,TarifaIca,CodTarImp
,CodTarRet,CodTarIca,Referencia,CxPagar,CdPlazo,Plazo,NVmto,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,C.Observacion AS Observ,C.IdEstado AS CdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraReexpedido AS C  INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia INNER JOIN Conceptos AS CN ON C.IdConcepto=CN.IdConcepto
 INNER JOIN Terceros AS T ON C.IdEmpTrans=T.IdTercero INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 LEFT JOIN Plazos AS PZ ON C.CdPlazo=PZ.IdPlazo  
 WHERE C.TipDoc=@pmTipDoc   AND C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND  C.IdEmpTrans LIKE ISNULL(@pmIdEmpTrans,'%')
 ORDER BY C.IdCia,C.Causacion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinFactSeguroFmt] @pmTipDoc VARCHAR(3), @pmIdPrestamoIni INT,@pmIdPrestamoFin INT,@pmIdCia CHAR(2)
 AS 
SELECT TipDoc,IdPrestamo,IdCia,Item,Fecha,VrSaldoIni,VrPagos,TarifSeguro,VrSeguro
FROM  Trn_FinFactSeguro
WHERE TipDoc=@pmTipDoc  AND IdPrestamo BETWEEN @pmIdPrestamoIni AND @pmIdPrestamoFin  AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraMovLiquidaCau] @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdPoseedor VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
 AS 
 INSERT INTO tm_TraMovLiquida (tmEst,tmTipo,tmNumero,tmIdCia,tmItem,tmFecha,tmFecVence,tmIdPoseedor,tmIdVehiculo,tmIdConcepto,tmDescripcion
,tmVrDebito,tmVrCredito,tmVrAbonado,tmTipoVehic,tmVrBase,tmTarifa,tmReferencia,tmTipDocRef,tmDocRef,tmEstado,tmTipoLiq,tmLiquida,tmCiaLiq,tmSaldoAnt,tmSaldoCia,tmSaldoAntVeh,tmSaldoVehCia)
 SELECT @pmtmEst,D.TipDoc,D.Causacion,D.IdCia,D.Item,C.Fecha,D.FecVence,D.NitTercero,D.pVehiculo,D.IdConcepto,D.Descripcion
,D.VrDebito,D.VrCredito,D.VrAbonado,D.TipoAfiVehic,VrBase,TarifBase,D.Referencia,TipDocRef,DocRef,EstadoReg,D.TipoLiq,D.NumLiquida,D.CiaLiquida,0,0,0,0
 FROM Trn_TraCauDetalle AS D INNER JOIN Trn_TraCausacion AS C ON D.TipDoc=C.TipDoc AND D.Causacion=C.Causacion AND D.IdCia=C.IdCia
 WHERE D.FecVence BETWEEN @pmFechaIni AND @pmFechaFin AND D.TipoConc='CTACOBRAR' AND D.VrDebito>0
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.NitTercero LIKE ISNULL(@pmIdPoseedor,'%') AND D.pVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraCausacionAnu] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_TraCausacion SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraCausacion] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdProv VARCHAR(16),@pmFactura VARCHAR(20),@pmVrTotal MONEY
,@pmVrOtros MONEY,@pmVrNeto 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_TraCausacion SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdProv=@pmIdProv,Factura=@pmFactura,VrTotal=@pmVrTotal,VrOtros=@pmVrOtros,VrNeto=@pmVrNeto,TipCom=@pmTipCom,Comprobante=@pmComprobante
,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraCausacionLta] @pmTipDoc VARCHAR(3), @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdProv VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null
 AS 
 SELECT Causacion,C.IdCia AS CdCia,Compania,Fecha,C.IdConcepto AS CdConcepto,Concepto,IdProv,RazonSocial,Factura,VrTotal,VrOtros,VrNeto
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraCausacion AS C INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Terceros AS T ON C.IdProv=T.IdTercero  INNER JOIN Conceptos AS CO ON C.IdConcepto=CO.IdConcepto
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND C.IdProv LIKE ISNULL(@pmIdProv,'%') AND C.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 ORDER BY C.IdCia,C.Causacion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraCausacion] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdProv VARCHAR(16),@pmFactura VARCHAR(20),@pmVrTotal MONEY,@pmVrOtros MONEY,@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_TraCausacion (TipDoc,Causacion,IdCia,Fecha,IdConcepto,IdProv,Factura,VrTotal,VrOtros,VrNeto,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmCausacion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdProv,@pmFactura,@pmVrTotal,@pmVrOtros,@pmVrNeto,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraCausacion] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Causacion,IdCia,Fecha,IdConcepto,IdProv,Factura,VrTotal,VrOtros,VrNeto,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraCausacion
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraCausacion] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraCausacion
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoPtmo] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
 SELECT @pmtmEst,@pmtmTipoRep,TipDoc,IdPrestamo,IdCia,0,FecPrestamo,IdCliente,IdAgencia,0,0,IdLinea,'CAPITAL',Observacion,NumCredito,TipoCredito,VrPrestamo,VrCuota
,PagosCapital,AcumCuotas,IdVend,TimeSys,FecCorte,-1,0,0,0,TipDoc,NumPrestRef,ISNULL(CiaPrestRef,''),TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,0,0,0,0,0,0,0,0
 FROM Trn_FinPrestamo
 WHERE FecPrestamo BETWEEN @pmFechaIni AND @pmFechaFin 
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoPri] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
 SELECT @pmtmEst,@pmtmTipoRep,C.TipDoc,C.IdPrestamo,C.IdCia,C.Item,C.Fecha,C.IdCliente,C.IdAgencia,C.VrTotal,0,ISNULL(CodLinCred,P.IdLinea),C.Concepto,C.Detalle,C.NumFactura,ISNULL(P.TipoCredito,'PRESTAMO'),P.VrPrestamo,P.VrCuota
,C.VrAbonado,NumCuota,P.IdVend,P.TimeSys,C.FechaVence,C.TipoCuota,DTF_EA,TasaNom,TasaEfe,ISNULL(TipFacInt,''),NumFacInt,ISNULL(CdCiaFacInt,''),TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,0,0,0,0,0,0,0,0
 FROM Trn_FinCuotas AS C LEFT 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.Concepto<>'CAPITAL' AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoDpa] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
SELECT @pmtmEst,@pmtmTipoRep,A.TipRec,A.Recibo,A.IdCia,A.Item,A.Fecha,A.IdCliente,A.IdAgencia,0,A.TotalAbono,ISNULL(C.CodLinCred,P.IdLinea),C.Concepto,A.Detalle,C.NumFactura,P.TipoCredito,P.VrPrestamo,P.VrCuota
,PagosCapital,AcumCuotas,P.IdVend,A.TimeSys,D.FecDoc,-1,0,0,0,A.TipDoc,A.IdPrestamo,A.IdCiaPre,D.TipCom,D.Comprobante,D.IdCiaCom,0,0,Null
,0,0,0,0,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
 INNER JOIN Trn_FinPrestamo AS P ON A.TipDoc=P.TipDoc AND A.IdPrestamo=P.IdPrestamo AND A.IdCiaPre=P.IdCia
 INNER JOIN Trn_FinDevPre AS D ON A.TipRec=D.TipDev AND A.Recibo=D.Devolucion AND A.IdCia=D.IdCia
 WHERE A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND A.TipRec='DP1' AND P.CauAnticipado=1
 AND A.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExto] @pmtmEst CHAR(2),@pmtmTipDoc VARCHAR(3),@pmtmNumero INT,@pmtmIdCia CHAR(2),@pmtmItem INT,@pmtmFecha SMALLDATETIME,@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16),@pmtmCargos MONEY,@pmtmAbonos MONEY,@pmtmIdLinea VARCHAR(4),@pmtmConcepto VARCHAR(10),@pmtmDetalle VARCHAR(250),@pmtmReferencia VARCHAR(50),@pmtmCredito VARCHAR(10),@pmtmVrPrestamo MONEY,@pmtmVrCuota MONEY,@pmtmTotAbonos MONEY,@pmtmNCuota INT
,@pmtmIdVend VARCHAR(16),@pmtmTimeSys SMALLDATETIME,@pmtmFecVence SMALLDATETIME,@pmtmTipoAbono INT,@pmtmDTF_EA DECIMAL(16,8),@pmtmTasaNom DECIMAL(16,8),@pmtmTasaEfe DECIMAL(16,8),@pmtmTipRef VARCHAR(3),@pmtmDocRef INT,@pmtmIdCiaRef CHAR(2),@pmtmTipCom VARCHAR(3),@pmtmComprobante INT,@pmtmIdCiaCom CHAR(2),@pmtmAnulado BIT,@pmtmNumDev INT,@pmtmFecDev SMALLDATETIME,@pmtmSanClie MONEY,@pmtmSanCia MONEY,@pmtmSanAgenc MONEY,@pmtmSanAgeCia MONEY
,@pmtmSanLinea MONEY,@pmtmSanLineaCia MONEY,@pmtmSanConc MONEY,@pmtmSanConcCia MONEY,@pmtmTipoRep INT
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
 VALUES (@pmtmEst,@pmtmTipoRep,@pmtmTipDoc,@pmtmNumero,@pmtmIdCia,@pmtmItem,@pmtmFecha,@pmtmIdCliente,@pmtmIdAgencia,@pmtmCargos,@pmtmAbonos,@pmtmIdLinea,@pmtmConcepto,@pmtmDetalle,@pmtmReferencia,@pmtmCredito,@pmtmVrPrestamo,@pmtmVrCuota,@pmtmTotAbonos,@pmtmNCuota,@pmtmIdVend,@pmtmTimeSys,@pmtmFecVence,@pmtmTipoAbono,@pmtmDTF_EA,@pmtmTasaNom,@pmtmTasaEfe,@pmtmTipRef,@pmtmDocRef,@pmtmIdCiaRef,@pmtmTipCom,@pmtmComprobante,@pmtmIdCiaCom,@pmtmAnulado,@pmtmNumDev
,@pmtmFecDev,@pmtmSanClie,@pmtmSanCia,@pmtmSanAgenc,@pmtmSanAgeCia,@pmtmSanLinea,@pmtmSanLineaCia,@pmtmSanConc,@pmtmSanConcCia)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoNca] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
--notas cred pendiente por aplicar
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
SELECT @pmtmEst,@pmtmTipoRep,N.TipDoc,NumNota,N.IdCia,0,N.Fecha,N.IdCliente,N.IdAgencia,0,N.VrNeto-ISNULL(RecTotAbonos,0),'0001','INTERESES',N.Observacion,N.Referencia,N.Modalidad,N.VrNeto,0
,ISNULL(RecTotAbonos,0),0,N.IdVend,N.TimeSys,N.Fecha,2,0,0,0,TipDcm,Documento,IdCiaDcm,N.TipCom,N.Comprobante,N.IdCiaCom,N.Anulado,0,Null
,0,0,0,0,0,0,0,0
 FROM Trn_FinNotas AS N LEFT JOIN (SELECT TipRec,Recibo,IdCia,SUM(TotalAbono) AS RecTotAbonos FROM Trn_FinAbonos WHERE TipRec='NC2'
 GROUP BY TipRec,Recibo,IdCia) AS A ON N.TipDoc=A.TipRec AND N.NumNota=A.Recibo AND N.IdCia=A.IdCia
 WHERE N.TipDoc='NC2' AND N.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (N.VrNeto-ISNULL(RecTotAbonos,0))>0
 AND N.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND N.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoOdb] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
SELECT @pmtmEst,@pmtmTipoRep,D.TipDev,D.Devolucion,D.IdCia,A.Item,D.Fecha,D.IdCliente,D.IdAgencia,A.TotalAbono*-1,0,ISNULL(CodLinCred,'0001'),C.Concepto,A.Detalle,D.Referencia,R.TipoCredito,D.VrNeto,C.VrTotal
,C.VrAbonado,C.NumCuota,A.IdVend,D.TimeSys,A.FecPago,A.TipoAplica,DTF_EA,TasaNom,TasaEfe,A.TipDoc,A.IdPrestamo,A.IdCiaPre,D.TipCom,D.Comprobante,D.IdCiaCom,0,0,FecDoc
,0,0,0,0,0,0,0,0
 FROM Trn_FinDevRec AS D INNER JOIN Trn_FinAbonos AS A ON D.TipDev=A.TipRec AND D.Devolucion=A.Recibo AND D.IdCia=A.IdCia
 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 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.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoPre] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
 SELECT @pmtmEst,@pmtmTipoRep,TipDoc,IdPrestamo,IdCia,0,FecPrestamo,IdCliente,IdAgencia,VrPrestamo,0,IdLinea,'CAPITAL',Observacion,NumCredito,TipoCredito,VrPrestamo,VrCuota
,PagosCapital,AcumCuotas,IdVend,TimeSys,FecCorte,-1,0,0,0,TipDoc,NumPrestRef,ISNULL(CiaPrestRef,''),TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,0,0,0,0,0,0,0,0
 FROM Trn_FinPrestamo
 WHERE FecPrestamo BETWEEN @pmFechaIni AND @pmFechaFin AND CauAnticipado=0
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoPra] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
 SELECT @pmtmEst,@pmtmTipoRep,C.TipDoc,C.IdPrestamo,C.IdCia,C.Item,P.FecPrestamo,C.IdCliente,C.IdAgencia,C.VrTotal,0,ISNULL(CodLinCred,P.IdLinea),C.Concepto,C.Detalle,C.NumFactura,ISNULL(P.TipoCredito,'PRESTAMO'),P.VrPrestamo,P.VrCuota
,C.VrAbonado,NumCuota,P.IdVend,P.TimeSys,C.FechaVence,C.TipoCuota,DTF_EA,TasaNom,TasaEfe,C.TipDoc,NumPrestRef,ISNULL(CiaPrestRef,''),TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,0,0,0,0,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 P.FecPrestamo BETWEEN @pmFechaIni AND @pmFechaFin AND C.Concepto='CAPITAL' AND P.CauAnticipado=1
 AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoDfi] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
SELECT @pmtmEst,@pmtmTipoRep,D.TipDev,D.Devolucion,D.IdCia,A.Item,D.Fecha,D.IdCliente,D.IdAgencia,0,A.TotalAbono,ISNULL(CodLinCred,'0001'),C.Concepto,A.Detalle,C.NumFactura,'FACTURAS',D.VrNeto,C.VrTotal
,C.VrAbonado,C.NumCuota,A.IdVend,D.TimeSys,A.FecPago,A.TipoAplica,DTF_EA,TasaNom,TasaEfe,A.TipDoc,A.IdPrestamo,A.IdCiaPre,D.TipCom,D.Comprobante,D.IdCiaCom,0,0,FecDoc
,0,0,0,0,0,0,0,0
 FROM Trn_FinDevFac AS D INNER JOIN Trn_FinAbonos AS A ON D.TipDev=A.TipRec AND D.Devolucion=A.Recibo AND D.IdCia=A.IdCia
 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 D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoNcr] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
SELECT @pmtmEst,@pmtmTipoRep,N.TipDoc,N.NumNota,N.IdCia,A.Item,N.Fecha,N.IdCliente,N.IdAgencia,0,A.TotalAbono,ISNULL(CodLinCred,'0001'),C.Concepto,A.Detalle,N.Referencia,N.Modalidad,N.VrNeto,C.VrTotal
,C.VrAbonado,C.NumCuota,A.IdVend,N.TimeSys,A.FecPago,A.TipoAplica,DTF_EA,TasaNom,TasaEfe,A.TipDoc,A.IdPrestamo,A.IdCiaPre,N.TipCom,N.Comprobante,N.IdCiaCom,N.Anulado,0,Null
,0,0,0,0,0,0,0,0
 FROM Trn_FinNotas AS N INNER JOIN Trn_FinAbonos AS A ON N.TipDoc=A.TipRec AND N.NumNota=A.Recibo AND N.IdCia=A.IdCia
 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 N.TipDoc='NC2' AND N.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND N.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND N.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoDpr] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
SELECT @pmtmEst,@pmtmTipoRep,TipDev,Devolucion,D.IdCia,0,D.Fecha,D.IdCliente,D.IdAgencia,0,D.VrPrestamo,P.IdLinea,'CAPITAL',D.Observacion,P.NumCredito,P.TipoCredito,D.VrPrestamo,P.VrCuota
,PagosCapital,AcumCuotas,P.IdVend,D.TimeSys,D.FecDoc,-1,0,0,0,D.TipDoc,D.IdPrestamo,D.IdCiaDoc,D.TipCom,D.Comprobante,D.IdCiaCom,0,0,Null
,0,0,0,0,0,0,0,0
 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
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND P.CauAnticipado=0
 AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoRca] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
--recibo de caja pendiente por aplicar
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
SELECT @pmtmEst,@pmtmTipoRep,R.TipDoc,R.Recibo,R.IdCia,0,R.Fecha,R.IdCliente,R.IdAgencia,0,R.VrNeto-ISNULL(RecTotAbonos,0),CASE R.TipoCredito WHEN 'TC' THEN '0005' WHEN 'FACTURAS' THEN '0004' ELSE '0001' END
,CASE R.TipoCredito WHEN 'FACTURAS' THEN 'INTERESES' ELSE 'CAPITAL' END,R.Observacion,R.Referencia,R.TipoCredito,R.VrNeto,0
,ISNULL(RecTotAbonos,0),0,R.IdVend,R.TimeSys,R.FecPago,2,0,0,0,TipDcm,Documento,IdCiaDcm,R.TipCom,R.Comprobante,R.IdCiaCom,R.Anulado,NumDev,FecDev
,0,0,0,0,0,0,0,0
 FROM Trn_FinRecibos AS R LEFT JOIN (SELECT TipRec,Recibo,IdCia,SUM(TotalAbono) AS RecTotAbonos FROM Trn_FinAbonos WHERE TipRec='RC2'
 GROUP BY TipRec,Recibo,IdCia) AS A ON R.TipDoc=A.TipRec AND R.Recibo=A.Recibo AND R.IdCia=A.IdCia
 WHERE R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (R.VrNeto-ISNULL(RecTotAbonos,0))>0
 AND R.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoCuo] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
 SELECT @pmtmEst,@pmtmTipoRep,C.TipDoc,C.IdPrestamo,C.IdCia,0,MAX(C.Fecha),C.IdCliente,C.IdAgencia,SUM(C.VrTotal),0,P.IdLinea,'CAPITAL','','',P.TipoCredito,P.VrPrestamo,P.VrCuota
,SUM(C.VrAbonado),MAX(NumCuota),P.IdVend,P.TimeSys,MAX(C.FechaVence),0,MAX(DTF_EA),MAX(TasaNom),MAX(TasaEfe),'PR1',0,'',TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,0,0,0,0,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.Concepto='CAPITAL' 
 AND NOT EXISTS (SELECT * FROM tm_PreExto WHERE tm_PreExto.tmTipDoc='PR1' AND tm_PreExto.tmNumero=C.IdPrestamo AND tm_PreExto.tmIdCia=C.IdCia
 AND tm_PreExto.tmConcepto='CAPITAL')
 AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  C.TipDoc,C.IdPrestamo,C.IdCia,C.IdCliente,C.IdAgencia,P.IdLinea,P.TipoCredito,P.VrPrestamo,P.VrCuota
,P.IdVend,P.TimeSys,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUptm_PreExtoCuo] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
 AS 
 UPDATE tm_PreExto SET tmCargos=ISNULL((SELECT SUM(VrTotal) FROM Trn_FinCuotas WHERE TipDoc='PR1' AND TipDoc=tmTipDoc AND IdPrestamo=tmNumero
 AND IdCia=tmIdCia AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Concepto='CAPITAL'),0)
--CUOTA
,tmNCuota=ISNULL((SELECT MAX(NumCuota) FROM Trn_FinCuotas WHERE TipDoc='PR1' AND TipDoc=tmTipDoc AND IdPrestamo=tmNumero
 AND IdCia=tmIdCia AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Concepto='CAPITAL'),0)
,tmDTF_EA=ISNULL((SELECT MAX(DTF_EA) FROM Trn_FinCuotas WHERE TipDoc='PR1' AND TipDoc=tmTipDoc AND IdPrestamo=tmNumero
 AND IdCia=tmIdCia AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Concepto='CAPITAL'),0)
,tmTasaNom=ISNULL((SELECT MAX(TasaNom) FROM Trn_FinCuotas WHERE TipDoc='PR1' AND TipDoc=tmTipDoc AND IdPrestamo=tmNumero
 AND IdCia=tmIdCia AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Concepto='CAPITAL'),0)
,tmTasaEfe=ISNULL((SELECT MAX(TasaEfe) FROM Trn_FinCuotas WHERE TipDoc='PR1' AND TipDoc=tmTipDoc AND IdPrestamo=tmNumero
 AND IdCia=tmIdCia AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Concepto='CAPITAL'),0)
 WHERE tmEst=@pmtmEst AND tmTipoRep=@pmtmTipoRep AND tmTipDoc='PR1' AND tmConcepto='CAPITAL'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoNdb] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
 SELECT @pmtmEst,@pmtmTipoRep,C.TipDoc,C.IdPrestamo,C.IdCia,C.Item,C.Fecha,C.IdCliente,C.IdAgencia,C.VrTotal,0,ISNULL(CodLinCred,'0001'),C.Concepto,C.Detalle,N.Referencia,N.Modalidad,0,0
,C.VrAbonado,NumCuota,N.IdVend,N.TimeSys,C.FechaVence,TipoCuota,DTF_EA,TasaNom,TasaEfe,'PR1',NumPreMora,IdCiaCau,TipCom,Comprobante,IdCiaCom,0,0,Null
,0,0,0,0,0,0,0,0
 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 C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoApr] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
SELECT @pmtmEst,@pmtmTipoRep,A.TipRec,A.Recibo,A.IdCia,A.Item,A.Fecha,A.IdCliente,A.IdAgencia,0,A.TotalAbono,ISNULL(CodLinCred,'0001'),C.Concepto,A.Detalle,C.NumFactura,'PRESTAMO',A.TotalAbono,C.VrTotal
,C.VrAbonado,C.NumCuota,A.IdVend,A.TimeSys,A.FecPago,A.TipoAplica,DTF_EA,TasaNom,TasaEfe,A.TipDoc,A.IdPrestamo,A.IdCiaPre,'0',0,'00',0,0,Null
,0,0,0,0,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.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoRec] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
SELECT @pmtmEst,@pmtmTipoRep,R.TipDoc,R.Recibo,R.IdCia,A.Item,R.Fecha,R.IdCliente,R.IdAgencia,0,A.TotalAbono,ISNULL(CodLinCred,'0001'),C.Concepto,A.Detalle,R.Referencia,R.TipoCredito,R.VrNeto,C.VrTotal
,C.VrAbonado,C.NumCuota,A.IdVend,R.TimeSys,A.FecPago,A.TipoAplica,DTF_EA,TasaNom,TasaEfe,A.TipDoc,A.IdPrestamo,A.IdCiaPre,R.TipCom,R.Comprobante,R.IdCiaCom,R.Anulado,NumDev,FecDev
,0,0,0,0,0,0,0,0
 FROM Trn_FinRecibos AS R INNER JOIN Trn_FinAbonos AS A ON R.TipDoc=A.TipRec AND R.Recibo=A.Recibo AND R.IdCia=A.IdCia
 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 R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND R.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoApf] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
--diferencia de cuotas no causadas prestamos refinanciados
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
SELECT @pmtmEst,@pmtmTipoRep,P.TipDoc,P.IdPrestamo,P.IdCia,0,P.FecPrestamo,P.IdCliente,P.IdAgencia,0,P.VrPrestamo-ISNULL(AB.VrAbono,0),P.IdLinea,'CAPITAL','PRESTAMO REFINANCIADO-CAPITAL NO CAUSADO',P.NumCredito,P.TipoCredito,P.VrPrestamo,P.VrCuota
,P.PagosCapital,P.AcumCuotas,P.IdVend,P.TimeSys,P.FecCorte,-1,0,0,0,P.TipDoc,P.NumPrestRef,ISNULL(P.CiaPrestRef,''),P.TipCom,P.Comprobante,P.IdCiaCom,P.Anulado,P.NumDev,P.FecDev,0,0,0,0,0,0,0,0
FROM Trn_FinPrestamo AS P LEFT JOIN (SELECT A.TipDoc,A.IdPrestamo,A.IdCiaPre,SUM(A.TotalAbono) AS VrAbono
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 C.Concepto='CAPITAL' AND A.IdCliente LIKE ISNULL(@pmIdCliente,'%')
GROUP BY A.TipDoc,A.IdPrestamo,A.IdCiaPre) AS AB ON P.TipDoc=AB.TipDoc AND P.IdPrestamo=AB.IdPrestamo AND P.IdCia=AB.IdCiaPre
WHERE ISNULL(P.FecCorte,P.FecPrestamo) BETWEEN @pmFechaIni AND @pmFechaFin AND P.CauAnticipado=0 AND P.Refinanciado=1 AND P.NumPrestRef>0
 AND P.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoOda] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
SELECT @pmtmEst,@pmtmTipoRep,D.TipDev,D.Devolucion,D.IdCia,0,D.Fecha,D.IdCliente,D.IdAgencia,D.VrNeto-(ISNULL(RecTotAbonos,0)*-1),0
,CASE R.TipoCredito WHEN 'TC' THEN '0005' WHEN 'FACTURAS' THEN '0004' ELSE '0001' END
,CASE R.TipoCredito WHEN 'FACTURAS' THEN 'INTERESES' ELSE 'CAPITAL' END
,D.Observacion,D.Referencia,R.TipoCredito,D.VrNeto,0,ISNULL(RecTotAbonos,0)*-1,0,D.IdVend,D.TimeSys,D.FecPago,2,0,0,0,D.TipDoc,D.Recibo,D.IdCiaDoc,D.TipCom,D.Comprobante,D.IdCiaCom
,0,0,D.FecDoc,0,0,0,0,0,0,0,0
 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
 LEFT JOIN (SELECT TipRec,Recibo,IdCia,SUM(TotalAbono) AS RecTotAbonos FROM Trn_FinAbonos WHERE TipRec='OD2'
 GROUP BY TipRec,Recibo,IdCia) AS A ON D.TipDev=A.TipRec AND D.Devolucion=A.Recibo AND D.IdCia=A.IdCia 
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (D.VrNeto-(ISNULL(RecTotAbonos,0)*-1))>0
 AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoDfa] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
SELECT @pmtmEst,@pmtmTipoRep,D.TipDev,D.Devolucion,D.IdCia,0,D.Fecha,D.IdCliente,D.IdAgencia,0,D.VrNeto-ISNULL(RecTotAbonos,0),'0004','INTERESES',D.Observacion,'','FACTURAS',D.VrNeto,0
,ISNULL(RecTotAbonos,0),0,D.IdVend,D.TimeSys,D.Fecha,2,0,0,0,D.TipDoc,D.Factura,D.IdCiaDoc,D.TipCom,D.Comprobante,D.IdCiaCom,0,0,D.FecDoc
,0,0,0,0,0,0,0,0
 FROM Trn_FinDevFac AS D INNER JOIN Trn_FinFactint AS F ON D.TipDoc=F.TipDoc AND D.Factura=F.Factura AND D.IdCiaDoc=F.IdCia
 LEFT JOIN (SELECT TipRec,Recibo,IdCia,SUM(TotalAbono) AS RecTotAbonos FROM Trn_FinAbonos WHERE TipRec='DFI'
 GROUP BY TipRec,Recibo,IdCia) AS A ON D.TipDev=A.TipRec AND D.Devolucion=A.Recibo AND D.IdCia=A.IdCia
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (D.VrNeto-ISNULL(RecTotAbonos,0))>0
 AND EXISTS (SELECT * FROM Trn_FinCuotas WHERE Trn_FinCuotas.TipDoc=D.TipDoc AND Trn_FinCuotas.IdPrestamo=D.Factura AND Trn_FinCuotas.IdCia=D.IdCiaDoc) 
 AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreExtoFif] @pmtmEst CHAR(2),@pmtmTipoRep INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreExto (tmEst,tmTipoRep,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmCargos,tmAbonos,tmIdLinea,tmConcepto,tmDetalle,tmReferencia,tmCredito,tmVrPrestamo,tmVrCuota
,tmTotAbonos,tmNCuota,tmIdVend,tmTimeSys,tmFecVence,tmTipoAbono,tmDTF_EA,tmTasaNom,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmSanClie,tmSanCia,tmSanAgenc,tmSanAgeCia,tmSanLinea,tmSanLineaCia,tmSanConc,tmSanConcCia)
 SELECT @pmtmEst,@pmtmTipoRep,C.TipDoc,C.IdPrestamo,C.IdCia,C.Item,C.Fecha,C.IdCliente,C.IdAgencia,C.VrTotal,0,ISNULL(CodLinCred,'0001'),C.Concepto,C.Detalle,C.NumFactura,F.TipoFactInt,0,0
,C.VrAbonado,NumCuota,F.IdVend,F.TimeSys,C.FechaVence,TipoCuota,DTF_EA,TasaNom,TasaEfe,'PR1',NumPreMora,IdCiaCau,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,0,0,0,0,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.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrySalidasNetDet] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS 
 SELECT S.TipDoc,S.Salida,S.IdCia AS CdCia,Compania,S.Fecha,S.IdConcepto AS CdConcepto,Concepto,S.IdTercero AS NitTercero,T.RazonSocial AS NomTercero
,S.VrSubTotal,S.VrImpuesto,S.VrCostos,S.VrNeto,S.BaseImp,S.TarifaIva,S.TipOrd,S.NumOrden,S.IdCiaOrd
,S.pVehiculo,S.CdConductor,CDT.RazonSocial AS NomConductor,S.NContrato AS NumContrato,S.IdCiaCont,S.NitCliente,CLI.RazonSocial AS NomCliente
,S.CdAgencia AS IdAgncia,Agencia,S.CdDep AS CdDepend,S.ModRequis,S.DirEntrega,S.IdLocEnt,LE.Localidad AS CiuEntrega,S.Modalidad,S.GuiaComb,S.CdCiaGuia
--Detalles
,K.Item,K.IdProducto AS CdProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,K.Entradas,K.Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed,K.VrUnitario
,VrCostoEnt,VrCostoSal,K.TarifaIva AS DetTarifIva,VrIvaEnt,VrIvaSal,K.VrCostProm,K.Unidades,K.Descripcion,K.Referencia,Referencia2
,K.TipOrd as DetTipOrd,K.NumOrden AS DetNumOrden,K.IdCiaOrd AS DetIdCiaOrd,K.TipDocDev,K.NumDocDev,K.VrBruto,K.VrBase,K.Servcios,K.EsCombo,K.EsProdBase,K.NumLote,K.FechLote,K.CdTanque,K.CdCCosto AS CdCentCosto,CCosto,K.CdSubCos AS CdSubCenCost,SubCosto
,S.TipCom,S.Comprobante,S.IdCiaCom,S.Anulado,S.NumDev,S.FecDev,S.Observacion AS Observ,S.IdEstado AS CdEstado,Estado,S.TimeSys,S.FecUpdate AS Fec_Update,S.IdCiaCrea,S.IdUsuario AS CdUsuario,Usuario
,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 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 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 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
 LEFT JOIN CentroCosto AS CC ON K.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=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 S.TipDoc=@pmTipDoc  AND S.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND S.IdCia LIKE ISNULL(@pmIdCia,'%%')
 UNION ALL
 SELECT D.TipDev,D.Devolucion,D.IdCia,Compania,D.Fecha,D.IdConcepto,Concepto,D.IdTercero,T.RazonSocial,D.VrSubTotal
,D.VrImpuesto,D.VrCostos,D.VrNeto,D.BaseImp,S.TarifaIva,D.TipOrd,D.NumOrden,D.IdCiaOrd
,D.pVehiculo,S.CdConductor,CDT.RazonSocial,D.NContrato,D.IdCiaCont
,D.NitCliente,CLI.RazonSocial,D.CdAgencia,Agencia,CdDep,D.ModRequis,DirEntrega,IdLocEnt,LE.Localidad,D.Modalidad,GuiaComb,CdCiaGuia
,K.Item,K.IdProducto,DescripProd,K.IdBodega,Bodega,K.Entradas,K.Salidas,K.IdUnd,UM.Unidad,K.VrUnitario
,VrCostoEnt,VrCostoSal,K.TarifaIva,VrIvaEnt,VrIvaSal,K.VrCostProm,K.Unidades,K.Descripcion,K.Referencia,Referencia2
,K.TipOrd,K.NumOrden,K.IdCiaOrd,TipDocDev,K.NumDocDev,K.VrBruto,K.VrBase,K.Servcios,K.EsCombo,K.EsProdBase,K.NumLote,K.FechLote,K.CdTanque,K.CdCCosto,CCosto,K.CdSubCos,SubCosto
,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Salida,D.FecDoc,D.Observacion,D.IdEstado,Estado,D.TimeSys,D.FecUpdate,D.IdCiaCrea,D.IdUsuario,Usuario
,T.TipoId,T.Dv,T.Codigo,T.NomCial,T.Direccion,T.IdLocal,L.Localidad,L.IdDep,Departamento
,T.Telefono,T.Fax,T.e_mail,T.IdSector,SectorEco,T.IdRegimen,Regimen,T.TipEnte
 FROM Trn_DevSal AS D INNER JOIN Trn_Salidas AS S ON D.TipDoc=S.TipDoc AND D.Salida=S.Salida AND D.IdCiaDoc=S.IdCia
 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 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 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
 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 CentroCosto AS CC ON K.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=SC.IdSubCos
 WHERE D.TipDoc=@pmTipDoc  AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdOrdenSalFmt] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
AS
SELECT PS.TipDoc,PS.NumOrden,PS.IdCia,Compania,PS.Item,PS.TipSal,PS.NumSalida,PS.IdCiaSal,D.Item AS DetItem,D.IdProducto,D.Descripcion,D.Entradas,D.Salidas,D.VrUnitario
,D.IdBodega,Bodega,D.Referencia AS SalReferencia,SA.Fecha,SA.TipOrd,SA.NumOrden AS Num_OrdenSA,SA.IdCiaOrd,SA.Modalidad,SA.Anulado,SA.Observacion,SA.TipCom,SA.Comprobante,SA.IdCiaCom
,P.DescripProd,P.CodBarras,P.Referencia,P.TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo
,P.IdMarca,Marca,P.IdUnd,UD.Unidad,P.Color,P.Tamano,P.Inactivo AS ProdInactivo,P.DescripLong
FROM Trn_ProdOrdenSal AS PS INNER JOIN Trn_Kardex AS D ON PS.TipSal=D.TipDoc AND PS.NumSalida=D.Documento AND PS.IdCiaSal=D.IdCia
INNER JOIN ProdMcias AS P ON D.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 UD ON P.IdUnd=UD.IdUnd
INNER JOIN Bodegas AS B ON D.IdBodega=B.IdBodega INNER JOIN Companias AS CN ON PS.IdCiaSal=CN.IdCia
LEFT JOIN (SELECT TipDoc,Salida,IdCia,Fecha,TipOrd,NumOrden,IdCiaOrd,Modalidad,TipCom,Comprobante,IdCiaCom,Anulado,Observacion,IdEstado FROM Trn_Salidas WHERE TipDoc='SAI'
UNION ALL SELECT TipDev,Devolucion,IdCia,Fecha,TipOrd,NumOrden,IdCiaOrd,Modalidad,TipCom,Comprobante,IdCiaCom,1,Observacion,IdEstado
FROM Trn_DevSal WHERE TipDev='DSI') AS SA ON PS.TipSal=SA.TipDoc AND PS.NumSalida=SA.Salida AND PS.IdCiaSal=SA.IdCia
WHERE PS.TipDoc=@pmTipDoc AND PS.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND PS.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraPedMcias] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdMercancia VARCHAR(16),@pmDescripMcias VARCHAR(250),@pmCantidad DECIMAL(14,4),@pmPesoNeto DECIMAL(14,4),@pmUndMed VARCHAR(10),@pmdmsAlto DECIMAL(14,4),@pmdmsAncho DECIMAL(14,4),@pmdmsLargo DECIMAL(14,4),@pmVolumen DECIMAL(14,4),@pmUndVol VARCHAR(10),@pmIdUnd VARCHAR(4)
,@pmIdEmp VARCHAR(4),@pmIdNat VARCHAR(4),@pmIdMnjo VARCHAR(4),@pmIdTmcia VARCHAR(4),@pmCdRango VARCHAR(4),@pmCases INT,@pmCajas INT,@pmPalets INT,@pmNitRemite VARCHAR(16),@pmRemitente VARCHAR(250),@pmDirOrigen VARCHAR(250),@pmIdOrigen VARCHAR(8),@pmNitDestntario VARCHAR(16),@pmDestinatario VARCHAR(250),@pmDirDestino VARCHAR(250),@pmIdDestino VARCHAR(8),@pmTarifClie MONEY,@pmTarifPago MONEY
,@pmTarifTabla MONEY,@pmVrDeclarado MONEY,@pmVrSeguro MONEY,@pmTarifSeguro DECIMAL(14,4),@pmReferencia1 VARCHAR(50),@pmReferencia2 VARCHAR(50),@pmContenedor1 VARCHAR(50),@pmContenedor2 VARCHAR(50),@pmUndTarifa VARCHAR(10),@pmUndTarifPago VARCHAR(10),@pmDocCliente VARCHAR(30),@pmReferencia3 VARCHAR(50),@pmCdTipoVehic VARCHAR(4),@pmTipo_Servicio VARCHAR(10),@pmSedeRem VARCHAR(10),@pmSedeDest VARCHAR(10)
 AS 
 INSERT INTO Trn_TraPedMcias (TipDoc,Pedido,IdCia,Item,IdMercancia,DescripMcias,Cantidad,PesoNeto,UndMed,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol,IdUnd,IdEmp,IdNat,IdMnjo,IdTmcia,CdRango,Cases,Cajas,Palets,NitRemite,Remitente,DirOrigen,IdOrigen,NitDestntario,Destinatario,DirDestino,IdDestino,TarifClie,TarifPago,TarifTabla,VrDeclarado,VrSeguro,TarifSeguro
,Referencia1,Referencia2,Contenedor1,Contenedor2,UndTarifa,UndTarifPago,DocCliente,Referencia3,CdTipoVehic,Tipo_Servicio,SedeRem,SedeDest)
 VALUES (@pmTipDoc,@pmPedido,@pmIdCia,@pmItem,@pmIdMercancia,@pmDescripMcias,@pmCantidad,@pmPesoNeto,@pmUndMed,@pmdmsAlto,@pmdmsAncho,@pmdmsLargo,@pmVolumen,@pmUndVol,@pmIdUnd,@pmIdEmp,@pmIdNat,@pmIdMnjo,@pmIdTmcia,@pmCdRango,@pmCases,@pmCajas,@pmPalets,@pmNitRemite,@pmRemitente,@pmDirOrigen,@pmIdOrigen,@pmNitDestntario,@pmDestinatario,@pmDirDestino
,@pmIdDestino,@pmTarifClie,@pmTarifPago,@pmTarifTabla,@pmVrDeclarado,@pmVrSeguro,@pmTarifSeguro,@pmReferencia1,@pmReferencia2,@pmContenedor1,@pmContenedor2,@pmUndTarifa,@pmUndTarifPago,@pmDocCliente,@pmReferencia3,@pmCdTipoVehic,@pmTipo_Servicio,@pmSedeRem,@pmSedeDest)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraPedMcias] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Pedido,IdCia,Item,IdMercancia,DescripMcias,Cantidad,PesoNeto,UndMed,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol,IdUnd,IdEmp,IdNat,IdMnjo,IdTmcia,CdRango,Cases,Cajas,Palets
,Remitente,DirOrigen,IdOrigen,Destinatario,DirDestino,IdDestino,TarifClie,TarifPago,TarifTabla,VrDeclarado,VrSeguro,TarifSeguro,Referencia1,Referencia2,Contenedor1,Contenedor2
,NitRemite,NitDestntario,UndTarifa,UndTarifPago,DocCliente,Referencia3,CdTipoVehic,Tipo_Servicio,SedeRem,SedeDest
 FROM Trn_TraPedMcias
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTraPedMcias] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraPedMcias
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetalle_Sel] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraDetalle (tmNumero,tmItem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed,tmdmsAlto,tmdmsAncho,tmdmsLargo,tmVolumen,tmUndVol,tmIdUnd,tmIdEmp,tmIdNat
,tmIdMnjo,tmIdTmcia,tmCdRango,tmCases,tmCajas,tmPalets,tmRemision,tmNumOrden,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen,tmNitDestntario,tmDestinatario,tmDirDestino,tmIdDestino
,tmCdAgencia,tmTipoVehic,tmTarifClie,tmTarifPago,tmTarifTabla,tmVrDeclarado,tmVrSeguro,tmTarifSeguro,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmContenedor1,tmContenedor2,tmUndTarifa
,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmNumMintrans,tmPesoCont,tmUndTarPago,tmCodBodega,tmTipoTraslado,tmTieCargue,tmTieDesc,tmFecIniCargue,tmFecFinCargue,tmFecInidesc,tmFecFindesc,tmCdTipoVeh
,tmTipoServicio,tmSedeRem,tmSedeDest,tmFecLlegaCargue,tmFecLlegaDesc,tmMinCargue,tmMinDescargue)
SELECT @pmtmNumero,Item,IdMercancia,DescripMcias,Cantidad,PesoNeto,UndMed,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol,IdUnd,IdEmp,IdNat
,IdMnjo,IdTmcia,CdRango,Cases,Cajas,Palets,0,0,NitRemite,Remitente,DirOrigen,IdOrigen,NitDestntario,Destinatario,DirDestino,IdDestino
,'0','TERCEROS',TarifClie,TarifPago,TarifTabla,VrDeclarado,VrSeguro,TarifSeguro,DocCliente,Referencia1,Referencia2,Referencia3,Contenedor1,Contenedor2,UndTarifa
,TipDoc,Pedido,IdCia,Item,0,0,UndTarifPago,'',0,0,0,Null,Null,Null,Null,CdTipoVehic,Tipo_Servicio,SedeRem,SedeDest,Null,Null,0,0
 FROM Trn_TraPedMcias
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraPedMciasDoc] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,IdMercancia,DescripMcias,Cantidad,PesoNeto,D.UndMed AS UndPeso,Cases,Cajas,Palets,TarifClie,UndTarifa,TarifTabla,TarifPago,UndTarifPago,VrDeclarado,VrSeguro,TarifSeguro
,D.IdUnd AS CdUnid,Unidad,Referencia1,Referencia2,Referencia3,DocCliente,NitRemite,Remitente,DirOrigen,IdOrigen,LO.Localidad AS CiudadOrigen
,NitDestntario,Destinatario,DirDestino,IdDestino,LD.Localidad AS CiudadDestino
,dmsLargo,dmsAncho,dmsAlto,Volumen,UndVol,Contenedor1,Contenedor2,CdRango,D.IdEmp AS CdEmpaque,Empaque
,IdNat,IdMnjo,IdTmcia,CdTipoVehic,Tipo_Servicio,SedeRem,SedeDest
 FROM Trn_TraPedMcias AS D INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal
 INNER JOIN UndMed AS U ON D.IdUnd=U.IdUnd INNER JOIN Empaques AS E ON D.IdEmp=E.IdEmp
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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','VCC')
 AND TipoRef<>'SERVICIO' AND Lotes=1  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND L.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY IdCia,L.IdProducto,L.IdBodega,NumLote
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposRespFis] @pmIdResFis VARCHAR(10)
 AS 
 IF @pmIdResFis IS NULL
  SELECT IdResFis,NomResFiscal
  FROM TiposRespFis
  WHERE Inactivo=0
 ELSE
  SELECT IdResFis,NomResFiscal,Inactivo
  FROM TiposRespFis
  WHERE IdResFis=@pmIdResFis
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposRespFis] @pmIdResFis VARCHAR(10),@pmNomResFiscal VARCHAR(250),@pmInactivo BIT
 AS 
 UPDATE TiposRespFis SET NomResFiscal=@pmNomResFiscal,Inactivo=@pmInactivo
 WHERE IdResFis=@pmIdResFis
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposRespFis] @pmIdResFis VARCHAR(10)
 AS 
 DELETE FROM TiposRespFis WHERE IdResFis=@pmIdResFis
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposRespFis] @pmIdResFis VARCHAR(10),@pmNomResFiscal VARCHAR(250),@pmInactivo BIT
 AS 
 INSERT INTO TiposRespFis (IdResFis,NomResFiscal,Inactivo)
 VALUES (@pmIdResFis,@pmNomResFiscal,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsPlazos] @pmIdPlazo VARCHAR(4),@pmPlazo VARCHAR(50),@pmNVmto INT
,@pmDiasPago VARCHAR(50),@pmCdDctoPp VARCHAR(4),@pmInactivo BIT
 AS 
 INSERT INTO Plazos (IdPlazo,Plazo,NVmto,DiasPago,Inactivo,CdDctoPp)
 VALUES (@pmIdPlazo,@pmPlazo,@pmNVmto,@pmDiasPago,@pmInactivo,@pmCdDctoPp)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,FechaFirma,ClaseContrato,EdoDocmtos,GraciaBonif,ActFechaIni,ContOperando,ContVigente
--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
,FecVigCond,TasaIntArranq,AgencOperando,DocFinanc
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryFinFactintLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT F.Factura,F.IdCia AS CdCia,Compania,FE.Prefijo,FE.NumFace,F.Fecha,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.IdConcepto AS CdConcepto,Concepto
,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
,TipoFactInt,TipoFE,FechaFE,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,CodTarRtc,TarifaRtc,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca
,F.TimeSys,F.FecUpdate AS FechaAct,IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario,F.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
 LEFT JOIN Trn_Face AS FE ON F.TipDoc=FE.TipDoc AND F.Factura=FE.Documento AND F.IdCia=FE.IdCia
 WHERE F.TipDoc=@pmTipDoc AND F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND F.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND F.IdCliente LIKE ISNULL(@pmIdCliente,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpPlazos] @pmIdPlazo VARCHAR(4),@pmPlazo VARCHAR(50),@pmNVmto INT
,@pmDiasPago VARCHAR(50),@pmCdDctoPp VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE Plazos SET Plazo=@pmPlazo,NVmto=@pmNVmto,DiasPago=@pmDiasPago,Inactivo=@pmInactivo,CdDctoPp=@pmCdDctoPp
 WHERE IdPlazo=@pmIdPlazo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryPlazos] @pmIdPlazo VARCHAR(4)
 AS 
 IF @pmIdPlazo IS NULL
  SELECT IdPlazo,Plazo,NVmto,DiasPago,CdDctoPp
  FROM Plazos
  WHERE Inactivo=0
  ORDER BY IdPlazo
 ELSE
  SELECT IdPlazo,Plazo,NVmto,DiasPago,CdDctoPp,Inactivo
  FROM Plazos
  WHERE IdPlazo=@pmIdPlazo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPlazosDso]
 AS 
 SELECT IdPlazo,Plazo,DiasPago,IdPlazo+' '+Plazo+' ('+DiasPago+')' AS DsPla
 FROM Plazos
 WHERE Inactivo=0
 ORDER BY IdPlazo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,N.IdConcFE,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelPlazos] @pmIdPlazo VARCHAR(4)
 AS 
 DELETE FROM Plazos
 WHERE IdPlazo=@pmIdPlazo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOpedidoRmsn] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS
SELECT O.TipDoc,O.Pedido,O.IdCia,Compania,O.Fecha,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia,Agencia,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,O.TarifaCom
,D.Item,D.IdProducto,DescripProd,D.IdBodega AS CdBodega,B.Bodega,D.Salidas,D.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,D.VrUnitario,D.VrPrecio,D.TarifaIva
,D.VrIvaSal,D.TarifaDct,D.VrDctoSal,D.Descripcion,D.Comptmntos AS DescTallas,D.galsneto AS CantFact,D.TipOrd AS DetTipDoc,D.NumOrden AS DetNumDoc,D.IdCiaOrd AS DetCiaDoc,D.ItemCombo AS ItemDoc
,D.Remision,D.IdCiaRem,D.galsbruto AS CantTotalRem,D.Cotizacion,D.IdCiaCot,D.EsProdBase,(CASE WHEN D.Unidades>0 AND D.TipDocDev='PPD' THEN D.Unidades ELSE D.Salidas END) AS Cant_Aprob,D.Referencia,D.Referencia2
,D.CdOperario,OP.RazonSocial AS NomOperario,D.ComisnOper,D.TipDocDev,D.Servcios
,O.DiasEntraga,O.NitContac AS NitContacto,O.NomContac AS NomContacto,O.TelContac AS TelContacto,O.emlContac AS EmailContacto,O.CargoContac,O.FechaVence
,O.MulPlazos,O.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,O.RefPedido,O.NumAutSicom AS NumOrdenCom,O.TipFac,O.Factura,O.IdCiaFac,O.FechaFact,O.NumAprob,O.IdCiaApr,O.FecAprob,O.DetalleAprob
,O.Modalidad,O.Vigencia,O.IdConcepto,Concepto,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.Anulado,O.TimeSys AS FechaCrea,O.IdUsuario AS CdUsuario,Usuario
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.SitioWeb AS TercSitioWeb
,P.Referencia AS Prod_Referencia,P.TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,P.IdMarca AS CdMarca,Marca,P.Tamano
,P.ExtciaMin,P.ExtciaMax,P.ExtciaAct,P.VrCostAnt,P.VrCosto,P.VrCostPmd,P.FecUltcom,P.FecUltVta,P.Seriales,P.Lotes,P.Combo,P.Tanques,P.Tallaje,P.DescripLong
,P.Precio1,P.Precio2,P.Precio3,P.Precio4,P.Precio5
--detalle de remisiones
,RM.NumRemison,RM.IdCia AS CiaRemision,RM.ItemRem,RM.Salidas AS CantRemision,RM.Referencia AS RefRemision,RM.Descripcion AS DescRemision,RM.DescTallas AS TallasRem
,RM.Fecha AS FecRemision,RM.FechaDesp,RM.FechaVence AS FecVenceRem,RM.DiasEntraga AS DiasEntregaRem,RM.Observacion AS ObservRemision
FROM Trn_Opedido AS O INNER JOIN Trn_Kardex AS D ON O.TipDoc=D.TipDoc AND O.Pedido=D.Documento AND O.IdCia=D.IdCia
INNER JOIN Companias AS CI ON O.IdCia=CI.IdCia INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie
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 Conceptos AS CN ON O.IdConcepto=CN.IdConcepto
INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto INNER JOIN Bodegas AS B ON D.IdBodega=B.IdBodega
INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN SubGrupos AS S ON P.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 P.IdMarca=M.IdMarca
LEFT JOIN Terceros AS OP ON D.CdOperario=OP.IdTercero
--remisiones
LEFT JOIN (SELECT K.Documento AS NumRemison,K.IdCia,K.Item AS ItemRem,K.IdProducto,K.IdBodega,K.Entradas,K.Salidas,K.Unidades,K.VrUnitario,K.VrPrecio,K.pVehiculo
,K.Referencia,K.Descripcion,K.Comptmntos AS DescTallas,K.TipOrd,K.NumOrden,K.IdCiaOrd,K.ItemCombo AS ItemOrden,K.Servcios
,R.Fecha,R.FechaDesp,R.FechaVence,R.DiasEntraga,R.IdLocEnv,R.DirEnvio,R.EmpTrans,R.NitEmpTrans,R.pVehiculo AS RemVehiculo,R.CdConductor,R.DetalleEnvio
,R.TipFac,R.Factura,R.IdCiaFac,R.Modalidad,R.Observacion
FROM Trn_Kardex AS K INNER JOIN Trn_Remision AS R ON K.TipDoc=R.TipDoc AND K.Documento=R.Remision AND K.IdCia=R.IdCia
WHERE K.TipDoc='REM' AND K.TipOrd='PED' AND K.NumOrden>0 AND R.Anulado=0) AS RM ON O.TipDoc=RM.TipOrd AND O.Pedido=RM.NumOrden AND O.IdCia=RM.IdCiaOrd AND D.Item=RM.ItemOrden
WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
AND (O.IdCia=@pmIdCia OR @pmIdCia IS NULL)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenServLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT O.NumOrden,O.IdCia AS CdCia,Compania,O.Fecha,O.FecDespacho,O.IdTipoServ AS CdTipoServ,TipoServicio,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia
,O.IdOrigen,LO.Localidad AS CiuOrigen,O.IdDestino,LD.Localidad AS CiuDestino,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest
,O.VrServicio,O.VrEmpaque,O.VrBodega,O.VrOtros,O.VrDescuento,O.VrImpuesto,O.VrNeto,O.VrDeclarado,O.VrSeguro,O.IdVend,VN.RazonSocial AS NomVendedor
,O.NomContacto,O.TelsContacto,O.emlContacto,O.ContactoDest,O.emlContDest,O.Servicios,O.FormaPago,O.Referencia,O.TipInv,O.NumInvent,O.IdCiaInv,O.TipRem,O.NumRemesa,O.IdCiaRem
,O.EstFactura,O.TipFact,O.NumFactura,O.CdCiaFact,O.TipCausac,O.Causacion,O.CdCiaCau,EstCumplido,FechaCump,TipoRuta,O.Volumen,EstadoBod,O.CdBodega,FechaIngBod,FechaRetBod
,O.NitRemitente,O.Remitente,O.NitDestinatario,O.Destinatario,O.Anulado,O.FecDev,O.Observacion AS Observ,O.Referencias,O.IdEstado AS CdEstado,Estado,O.NumAutCupo,O.NumAutMora
,O.OrigenAdd,O.TimeSys AS FechaCrea,O.FecUpdate,O.IdCiaCrea,O.IdUsuario AS CdUsuario,Usuario
 FROM Trn_MudOrdenServ AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero
 INNER JOIN Terceros AS VN ON O.IdVend=VN.IdTercero INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN MudServicios AS TS ON O.IdTipoServ=TS.IdTipoServ 
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal INNER JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia 
 WHERE O.TipDoc=@pmTipDoc AND O.FecDespacho BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudInventario_Cr] @pmTipDoc VARCHAR(3),@pmNumInventIni INT,@pmNumInventFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT M.TipDoc,TipoDoc,M.NumInvent,M.IdCia AS CdCia,Compania,M.Fecha,M.FecDespacho,M.IdTipoServ AS CdTipoServ,TipoServicio,M.IdCliente,T.RazonSocial AS NomCliente,M.IdAgencia AS CdAgencia,Agencia
,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest
,M.IdOrigen,LO.Localidad AS CiuOrigen,DPO.Departamento AS DptoOrigen,M.IdDestino,LD.Localidad AS CiuDestino,DPD.Departamento AS DptoDestino,M.pVehiculo,M.CdConductor,NC.RazonSocial AS Conductor
,M.nRemolque,M.TipoAfiVehic,M.NitEmpresa,M.NomEmpresa,CajasEmp,CajasClie,M.VrDeclarado,M.VrServicio,M.NumCotizac,M.NumPlanilla,NumActa,M.TipOsm,M.NumOrden,M.IdCiaOsm,M.NomContacto,M.TelsContacto
,M.emlContacto,M.Responsable,M.FormaPago,M.Remitente AS ContactoCargue,M.Destinatario AS ContactoDestino,M.ContactoDest,M.Anulado,M.FecDev,M.Observacion AS Observ,M.IdEstado AS CdEstado,ED.Estado
,M.OrigenAdd,M.TimeSys AS FechaCrea,M.FecUpdate,M.IdCiaCrea AS CdCiaCrea,M.IdUsuario AS CdUsuario,U.Usuario
,D.Item,D.IdArticulo AS CdArticulo,D.Descripcion,D.Cantidad,D.CantSalida,D.IdEstado AS CdEstadoArt,EA.Estado AS EstadoArt,D.Observacion AS DetObserv,Articulo,MA.IdSeccion AS CdSeccion,Seccion,EA.NColor AS NColorEst
,D.TipoIngreso,D.FechaReg,D.CdUsuario AS DetCdUsuario,UD.Usuario AS DetUsuario
FROM Trn_MudInventario AS M INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia INNER JOIN Terceros AS T ON M.IdCliente=T.IdTercero
INNER JOIN MudServicios AS TS ON M.IdTipoServ=TS.IdTipoServ INNER JOIN EstadoDoc AS ED ON M.IdEstado=ED.IdEstado
INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON M.TipDoc=TD.IdDoc
INNER JOIN Localidades AS LO ON M.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
INNER JOIN Localidades AS LD ON M.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
INNER JOIN Trn_MudInventDet AS D ON M.TipDoc=D.TipDoc AND M.NumInvent=D.NumInvent AND M.IdCia=D.IdCia
INNER JOIN MudArticulos AS MA ON D.IdArticulo=MA.IdArticulo INNER JOIN MudSecciones AS SC ON MA.IdSeccion=SC.IdSeccion
INNER JOIN MudEstados AS EA ON D.IdEstado=EA.IdEstado
LEFT JOIN Terceros AS NC ON M.CdConductor=NC.IdTercero LEFT JOIN Agencias AS A ON M.IdAgencia=A.IdAgencia 
LEFT JOIN adm_Usuarios AS UD ON D.CdUsuario=UD.IdUsuario 
WHERE M.TipDoc=@pmTipDoc AND M.NumInvent BETWEEN @pmNumInventIni AND @pmNumInventFin AND M.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMudServicios] @pmIdTipoServ VARCHAR(4)
 AS 
 DELETE FROM MudServicios
 WHERE IdTipoServ=@pmIdTipoServ
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudServiciosDso]  
 AS 
 SELECT IdTipoServ,TipoServicio,TipoServicio+' '+IdTipoServ AS DsServ
 FROM MudServicios
 WHERE Inactivo=0
 ORDER BY TipoServicio
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudInventarioLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT M.NumInvent,M.IdCia AS CdCia,Compania,M.Fecha,M.FecDespacho,M.IdTipoServ AS CdTipoServ,TipoServicio,M.IdCliente,T.RazonSocial AS NomCliente,M.IdAgencia AS CdAgencia,Agencia
,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest
,M.IdOrigen,LO.Localidad AS CiuOrigen,M.IdDestino,LD.Localidad AS CiuDestino,M.pVehiculo,M.CdConductor,NC.RazonSocial AS Conductor,M.nRemolque,M.TipoAfiVehic,M.NitEmpresa,M.NomEmpresa
,CajasEmp,CajasClie,M.VrDeclarado,M.VrServicio,M.NumCotizac,M.NumPlanilla,NumActa,M.TipOsm,M.NumOrden,M.IdCiaOsm,M.NomContacto,M.TelsContacto,M.emlContacto,M.Responsable,M.FormaPago
,M.Anulado,M.FecDev,M.Observacion AS Observ,M.IdEstado AS CdEstado,Estado,M.OrigenAdd,M.TimeSys AS FechaCrea,M.FecUpdate,M.IdCiaCrea AS CdCiaCrea,M.IdUsuario AS CdUsuario,Usuario
,M.IdRemitente,M.Remitente,M.IdDestinatario,M.Destinatario,M.IdVend,M.ContactoDest
FROM Trn_MudInventario AS M INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia INNER JOIN Terceros AS T ON M.IdCliente=T.IdTercero
INNER JOIN MudServicios AS TS ON M.IdTipoServ=TS.IdTipoServ INNER JOIN EstadoDoc AS ED ON M.IdEstado=ED.IdEstado
INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
INNER JOIN Localidades AS LO ON M.IdOrigen=LO.IdLocal INNER JOIN Localidades AS LD ON M.IdDestino=LD.IdLocal
LEFT JOIN Terceros AS NC ON M.CdConductor=NC.IdTercero LEFT JOIN Agencias AS A ON M.IdAgencia=A.IdAgencia 
WHERE M.TipDoc=@pmTipDoc AND M.FecDespacho BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMudServicios] @pmIdTipoServ VARCHAR(4),@pmTipoServicio VARCHAR(150),@pmTransporte BIT,@pmBodega BIT,@pmEmpaque BIT,@pmPersonal BIT,@pmInactivo BIT
 AS 
 INSERT INTO MudServicios (IdTipoServ,TipoServicio,Transporte,Bodega,Empaque,Personal,Inactivo)
 VALUES (@pmIdTipoServ,@pmTipoServicio,@pmTransporte,@pmBodega,@pmEmpaque,@pmPersonal,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudInventarioOsm] @pmTipOsm VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT M.TipDoc,TipoDoc,M.NumInvent,M.IdCia AS CdCia,Compania,M.Fecha,M.FecDespacho,M.IdTipoServ AS CdTipoServ,TipoServicio,M.IdCliente,T.RazonSocial AS NomCliente,M.IdAgencia AS CdAgencia,Agencia
,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest
,M.IdOrigen,LO.Localidad AS CiuOrigen,DPO.Departamento AS DptoOrigen,M.IdDestino,LD.Localidad AS CiuDestino,DPD.Departamento AS DptoDestino,M.pVehiculo,M.CdConductor,NC.RazonSocial AS Conductor
,M.nRemolque,M.TipoAfiVehic,M.NitEmpresa,M.NomEmpresa,CajasEmp,CajasClie,M.VrDeclarado,M.VrServicio,M.NumCotizac,M.NumPlanilla,NumActa,M.TipOsm,M.NumOrden,M.IdCiaOsm,M.NomContacto,M.TelsContacto
,M.emlContacto,M.Responsable,M.FormaPago,M.Remitente AS ContactoCargue,M.Destinatario AS ContactoDestino,M.Anulado,M.FecDev,M.Observacion AS Observ,M.IdEstado AS CdEstado,ED.Estado
,M.OrigenAdd,M.TimeSys AS FechaCrea,M.FecUpdate,M.IdCiaCrea AS CdCiaCrea,M.IdUsuario AS CdUsuario,U.Usuario
,D.Item,D.IdArticulo AS CdArticulo,D.Descripcion,D.Cantidad,D.CantSalida,D.IdEstado AS CdEstadoArt,EA.Estado AS EstadoArt,D.Observacion AS DetObserv,Articulo,MA.IdSeccion AS CdSeccion,Seccion,EA.NColor AS NColorEst
,D.TipoIngreso,D.FechaReg,D.CdUsuario AS DetCdUsuario,UD.Usuario AS DetUsuario
FROM Trn_MudInventario AS M INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia INNER JOIN Terceros AS T ON M.IdCliente=T.IdTercero
INNER JOIN MudServicios AS TS ON M.IdTipoServ=TS.IdTipoServ INNER JOIN EstadoDoc AS ED ON M.IdEstado=ED.IdEstado
INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON M.TipDoc=TD.IdDoc
INNER JOIN Localidades AS LO ON M.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
INNER JOIN Localidades AS LD ON M.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
INNER JOIN Trn_MudInventDet AS D ON M.TipDoc=D.TipDoc AND M.NumInvent=D.NumInvent AND M.IdCia=D.IdCia
INNER JOIN MudArticulos AS MA ON D.IdArticulo=MA.IdArticulo INNER JOIN MudSecciones AS SC ON MA.IdSeccion=SC.IdSeccion
INNER JOIN MudEstados AS EA ON D.IdEstado=EA.IdEstado
LEFT JOIN Terceros AS NC ON M.CdConductor=NC.IdTercero LEFT JOIN Agencias AS A ON M.IdAgencia=A.IdAgencia 
LEFT JOIN adm_Usuarios AS UD ON D.CdUsuario=UD.IdUsuario 
WHERE M.TipDoc='MUD' AND M.TipOsm=@pmTipOsm AND M.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND M.IdCiaOsm=@pmIdCia
AND M.Anulado=0
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMudServicios] @pmIdTipoServ VARCHAR(4),@pmTipoServicio VARCHAR(150),@pmTransporte BIT,@pmBodega BIT,@pmEmpaque BIT,@pmPersonal BIT,@pmInactivo BIT
 AS 
 UPDATE MudServicios SET TipoServicio=@pmTipoServicio,Transporte=@pmTransporte,Bodega=@pmBodega,Empaque=@pmEmpaque,Personal=@pmPersonal,Inactivo=@pmInactivo
 WHERE IdTipoServ=@pmIdTipoServ
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenServ_Cr] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT O.TipDoc,TipoDoc,O.NumOrden,O.IdCia AS CdCia,Compania,O.Fecha,O.FecDespacho,O.IdTipoServ AS CdTipoServ,TipoServicio,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia
,O.IdOrigen,LO.Localidad AS CiuOrigen,O.IdDestino,LD.Localidad AS CiuDestino,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest
,O.VrServicio,O.VrEmpaque,O.VrBodega,O.VrOtros,O.VrDescuento,O.VrImpuesto,O.VrNeto,O.VrDeclarado,O.VrSeguro,O.IdVend,VN.RazonSocial AS NomVendedor
,O.NomContacto,O.TelsContacto,O.emlContacto,O.ContactoDest,O.emlContDest,O.Servicios,O.FormaPago,O.Referencia,O.TipInv,O.NumInvent,O.IdCiaInv,O.TipRem,O.NumRemesa,O.IdCiaRem
,O.EstFactura,O.TipFact,O.NumFactura,O.CdCiaFact,O.TipCausac,O.Causacion,O.CdCiaCau,EstCumplido,FechaCump,TipoRuta,O.Volumen,EstadoBod,O.CdBodega,FechaIngBod,FechaRetBod
,O.NitRemitente,O.Remitente,O.NitDestinatario,O.Destinatario,O.Anulado,O.FecDev,O.Observacion AS Observ,O.Referencias,O.IdEstado AS CdEstado,Estado,O.NumAutCupo,O.NumAutMora
,D.Item,D.IdConcepto AS CdConcepto,D.Descripcion,CD.Concepto,D.ValorTotal,D.TarifaIva,D.TipoConc,D.TipoRubro,D.CdCuenta,D.NitTercero,NT.RazonSocial AS NomTercero
,D.Referencia AS DetReferencia,D.NumDocRef,D.NumDocFac,D.IndFactItem,D.TipoReg,D.TarifSeguro,D.CodTarSeg,D.PlacaVehic,D.TarifaPago,D.FechaReg,D.CdUsuario AS DetCdUsuario,UC.Usuario AS DetUsuario,TS.Transporte,TS.Bodega,TS.Empaque,TS.Personal
,O.OrigenAdd,O.TimeSys AS FechaCrea,O.FecUpdate,O.IdCiaCrea,O.IdUsuario AS CdUsuario,U.Usuario
,T.Codigo AS TercCod,T.TipoId,T.Dv,T.Direccion,T.IdLocal AS TercCdLocal,L.Localidad AS TercLocalidad,T.Telefono AS TercTelefono,T.TelMovil AS TercTerCelular,T.e_mail AS TercEmail
 FROM Trn_MudOrdenServ AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie
 INNER JOIN Terceros AS VN ON O.IdVend=VN.IdTercero INNER JOIN MudServicios AS TS ON O.IdTipoServ=TS.IdTipoServ
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal INNER JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Trn_MudOrdenConc AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
 INNER JOIN ConcDiversos AS CD ON D.IdConcepto=CD.IdConcepto 
 LEFT JOIN Terceros AS NT ON D.NitTercero=NT.IdTercero
 LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia LEFT JOIN adm_Usuarios AS UC ON D.CdUsuario=UC.IdUsuario
 WHERE O.TipDoc=@pmTipDoc AND O.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND O.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudServicios] @pmIdTipoServ VARCHAR(4)
 AS 
 SELECT IdTipoServ,TipoServicio,Transporte,Bodega,Empaque,Personal,Inactivo
 FROM MudServicios
 WHERE IdTipoServ=@pmIdTipoServ
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,@pmBaseImpu MONEY,@pmTarifa_Iva DECIMAL(14,4),@pmVrIva MONEY,@pmTarifa_Ret DECIMAL(14,4),@pmVrRetFte MONEY,@pmTarifa_Ica DECIMAL(14,4),@pmVrRetIca MONEY,@pmCodTarifIva VARCHAR(4),@pmCodTarifRet VARCHAR(4),@pmCodTarifIca VARCHAR(4),@pmTipDocFac VARCHAR(3),@pmNumDocFac INT,@pmCdCiaFac CHAR(2)
 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
,BaseImpu,Tarifa_Iva,VrIva,Tarifa_Ret,VrRetFte,Tarifa_Ica,VrRetIca,CodTarifIva,CodTarifRet,CodTarifIca,TipDocFac,NumDocFac,CdCiaFac)
 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,@pmBaseImpu,@pmTarifa_Iva,@pmVrIva,@pmTarifa_Ret,@pmVrRetFte,@pmTarifa_Ica,@pmVrRetIca,@pmCodTarifIva,@pmCodTarifRet,@pmCodTarifIca,@pmTipDocFac,@pmNumDocFac,@pmCdCiaFac)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,BaseImpu,Tarifa_Iva,VrIva,Tarifa_Ret,VrRetFte,Tarifa_Ica,VrRetIca,CodTarifIva,CodTarifRet,CodTarifIca,TipDocFac,NumDocFac,CdCiaFac
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmBaseImpu,tmTarifaIva,tmVrIva,tmTarifaRet,tmVrRetFte,tmTarifaIca,tmVrRetIca,tmCodTarifIva,tmCodTarifRet,tmCodTarifIca)
 SELECT @pmtmNumero,Item,IdVehiculo,VehPropio,IdCliente,F.IdCargo,Concepto,Referencia,Factura,ValorFactura,Detalle,IdVendedor,FecVence
 ,CASE WHEN (TipDoc='ADM' AND NumDocFac>0) THEN TipDocFac ELSE TipDoc END
 ,CASE WHEN (TipDoc='ADM' AND NumDocFac>0) THEN NumDocFac ELSE Documento END
 ,CASE WHEN (TipDoc='ADM' AND NumDocFac>0) THEN CdCiaFac ELSE IdCia END
,RazonSocial,ValorAtcpo,TipRec, Recibo, IdCiaRec,IdCuenta,CombCant,CombValor,BaseImpu,Tarifa_Iva,VrIva,Tarifa_Ret,VrRetFte,Tarifa_Ica,VrRetIca,CodTarifIva,CodTarifRet,CodTarifIca
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CdCuentaIva,CdCuentaRet,SUM(ValorFactura) AS SvalFac,SUM(V.BaseImpu) AS BaseIva,SUM(V.VrIva) AS TotalIva,SUM(V.VrRetFte) AS TotalRet
 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,CdCuentaIva,CdCuentaRet
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,@pmBaseImpu MONEY,@pmTarifa_Iva DECIMAL(14,4),@pmVrIva MONEY,@pmTarifa_Ret DECIMAL(14,4),@pmVrRetFte MONEY,@pmTarifa_Ica DECIMAL(14,4),@pmVrRetIca MONEY
,@pmCodTarifIva VARCHAR(4),@pmCodTarifRet VARCHAR(4),@pmCodTarifIca VARCHAR(4),@pmTipDocFac VARCHAR(3),@pmNumDocFac INT,@pmCdCiaFac CHAR(2)
 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
,BaseImpu=@pmBaseImpu,Tarifa_Iva=@pmTarifa_Iva,VrIva=@pmVrIva,Tarifa_Ret=@pmTarifa_Ret,VrRetFte=@pmVrRetFte,Tarifa_Ica=@pmTarifa_Ica,VrRetIca=@pmVrRetIca,CodTarifIva=@pmCodTarifIva,CodTarifRet=@pmCodTarifRet,CodTarifIca=@pmCodTarifIca,TipDocFac=@pmTipDocFac,NumDocFac=@pmNumDocFac,CdCiaFac=@pmCdCiaFac
WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,BaseImpu,Tarifa_Iva,VrIva,Tarifa_Ret,VrRetFte,Tarifa_Ica,VrRetIca,CodTarifIva,CodTarifRet,CodTarifIca,TipDocFac,NumDocFac,CdCiaFac
  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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,BaseImpu,Tarifa_Iva,VrIva,Tarifa_Ret,VrRetFte,Tarifa_Ica,VrRetIca,CodTarifIva,CodTarifRet,CodTarifIca,TipDocFac,NumDocFac,CdCiaFac
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ((L.TipoLiq='CES' AND Semstre=0) OR L.TipoLiq<>'CES') 
 AND L.Anulado=0 AND FPnomina='TRANS' 
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,VrAprobado,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelCargos] @pmIdCargo VARCHAR(4)
 AS 
 DELETE FROM Cargos
 WHERE IdCargo=@pmIdCargo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create FUNCTION [dbo].[FuncItemFinPag]  (@pmidprestamo INT,@pmIdCia CHAR(2))
RETURNS INT
 AS  
BEGIN 
 DECLARE @Npagos  INT
 SET  @Npagos=(SELECT COUNT(*) FROM Trn_FinPrestSeguro WHERE IdPrestamo =@pmidprestamo AND IdCia=@pmIdCia)
 RETURN @Npagos
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraDespProgram] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmItem INT,@pmCdRuta VARCHAR(4),@pmIdPunto INT,@pmHoraProg SMALLDATETIME
,@pmTipoProg INT,@pmObservProg VARCHAR(250)
 AS 
 UPDATE Trn_TraDespProgram SET CdRuta=@pmCdRuta,IdPunto=@pmIdPunto,HoraProg=@pmHoraProg,TipoProg=@pmTipoProg,ObservProg=@pmObservProg
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraDespProgram] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraDespProgram
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraDespProgram] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmItem INT,@pmCdRuta VARCHAR(4),@pmIdPunto INT
,@pmHoraProg SMALLDATETIME,@pmTipoProg INT,@pmIdUsuario VARCHAR(11),@pmFechaCrea SMALLDATETIME,@pmObservProg VARCHAR(250)
 AS 
 INSERT INTO Trn_TraDespProgram (TipDoc,Manifiesto,IdCia,Item,CdRuta,IdPunto,HoraProg,TipoProg,IdUsuario,FechaCrea,ObservProg)
 VALUES (@pmTipDoc,@pmManifiesto,@pmIdCia,@pmItem,@pmCdRuta,@pmIdPunto,@pmHoraProg,@pmTipoProg,@pmIdUsuario,@pmFechaCrea,@pmObservProg)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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.Anulado=0
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CdCtaPago,CauAnticipado,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[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,CASE WHEN C.Concepto='CAPITAL' AND ISNULL(P.CauAnticipado,0)=1 THEN P.FecPrestamo ELSE C.Fecha END
,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 CASE WHEN C.Concepto='CAPITAL' AND ISNULL(P.CauAnticipado,0)=1 THEN P.FecPrestamo ELSE C.Fecha END BETWEEN @pmFechaIni AND @pmFechaFin 
 AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND C.IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinPagosFmt] @pmTipDoc VARCHAR(3),@pmIdPrestamoIni INT,@pmIdPrestamoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  P.IdPrestamo,P.TipDoc,P.IdCia ,P.Item ,Numero ,P.Fecha,FecPago,P.VrCuota,EdoForma,CdBanco,Banco,CtaCheque,Detalle,TipRec,Recibo,IdCiaRec
,VrLiqMora,FecLiqMora,VrRecCaja,CLI.IncRetFte AS CalRteFte
 --Detalle facturas
 ,FF.VrInteres,FF.ItemPago ,TarifSeguro As PorcSeguro, TotalSeguro As VrSeguro
 FROM Trn_FinFacturas AS FF 
 inner join Trn_FinPrestamo FP ON FF.TipDoc =FP.TipDoc AND FF.IdPrestamo =FP.IdPrestamo AND FF.IdCia =FP.IdCia 
 INNER JOIN TercCliePrestamo CLI ON FP.IdCliente=CLI.IdClie AND FP.IdAgencia =CLI.IdAgencia 
 inner join   Trn_FinPagos  AS P ON FF.TipDoc =P.TipDoc and FF.IdPrestamo = P.IdPrestamo 
 and FF.IdCia =P.IdCia  and FF.ItemPago =P.Item 
 
  LEFT JOIN Bancos AS B ON P.CdBanco=B.IdBanco
 WHERE P.TipDoc=@pmTipDoc AND P.IdPrestamo BETWEEN @pmIdPrestamoIni AND @pmIdPrestamoFin AND P.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreCartera_Pre] @pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdLinea VARCHAR(4)=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,'%')
 AND C.CodLinCred LIKE ISNULL(@pmIdLinea,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[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,tmVrSeguro,tmVrProrroga,tmVrCustodia,tmTasaCustod,tmTarifIva,tmVrIva,tmTarifRet,tmVrRet
--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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCauAnticipado BIT,@pmCdCtaPago 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,CauAnticipado=@pmCauAnticipado,CdCtaPago=@pmCdCtaPago
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCauAnticipado BIT,@pmCdCtaPago 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,CauAnticipado,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,CdCtaPago)
 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,@pmCauAnticipado,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmCdCtaPago)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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  IdEstado='0001' AND Anulado=0 
 AND (VrPrestamo>AcumCapital OR (CauAnticipado=1
  AND ISNULL((SELECT SUM(VrTotal-VrAbonado) AS VrSaldo 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 Trn_FinCuotas.Concepto='CAPITAL'),0)>0))
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinPrestGarantLta] @pmIdCliente VARCHAR(16)=Null,@pmTipoGarant VARCHAR(20)=Null,@pmIdCia CHAR(2)=Null
AS 
 SELECT G.TipDoc AS TipPrest,G.IdPrestamo AS NumPrest,G.IdCia AS CdCia,Compania,Item,TipoGarant,DescGarant,CdInmueble,TipoInmueble,CdTipoVeh,TipoVehiculo,G.VrGarantia AS VrGarant,VrAvaluo
,G.PesoBruto AS Peso_Bruto,PesoNeto,Propietario,DirInmueble,CdCiudad,Localidad,Matricula,Escritura,NumNotaria,MarcaVeh,Modelo,CdBanco,Banco,FecCheque,NumCheque
,CedFmnte1,NomFmnte1,CedFmnte2,NomFmnte2,CedFmnte3,NomFmnte3,CedFmnte4,NomFmnte4,CedFmnte5,NomFmnte5,CedFmnte6,NomFmnte6,ArchivoImg,G.IdUsuario AS CdUsuario,Usuario
--datos del prestamo
,FecPrestamo,FecCorte,VrPrestamo,VrCuota,IdCliente,T.RazonSocial AS NomCliente,P.IdAgencia AS CdAgencia,NomAgencia AS Agencia
,NPlazos,NumCredito,P.Observacion AS Observ,P.IdLinea AS CdLinea,LinCredito,TipoCredito,Anulado
 FROM Trn_FinPrestGarant AS G INNER JOIN Trn_FinPrestamo AS P ON G.TipDoc=P.TipDoc AND G.IdPrestamo=P.IdPrestamo AND G.IdCia=P.IdCia
 INNER JOIN Companias AS CI ON G.IdCia=CI.IdCia INNER JOIN adm_Usuarios AS U ON G.IdUsuario=U.IdUsuario
 INNER JOIN TercCliePrestamo AS CLI ON P.IdCliente=CLI.IdClie AND P.IdAgencia=CLI.IdAgencia
 INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero
 LEFT JOIN LineasCred AS L ON P.IdLinea=L.IdLinea
 LEFT JOIN TiposInm AS IM ON G.CdInmueble=IM.IdTipoInm LEFT JOIN TiposVeh AS TV ON G.CdTipoVeh=TV.IdTipoVeh
 LEFT JOIN Bancos AS B ON G.CdBanco=B.IdBanco LEFT JOIN Localidades AS LC ON G.CdCiudad=LC.IdLocal
 WHERE G.IdCia LIKE ISNULL(@pmIdCia,'%%') AND TipoGarant LIKE ISNULL(@pmTipoGarant,'%')
 AND P.IdCliente LIKE ISNULL(@pmIdCliente,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CauAnticipado
,TimeSys,FecUpdate,IdCiaCrea,IdUsuario,CdCtaPago
 FROM Trn_FinPrestamo
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelEgrAntAbonos] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_EgrAntAbonos WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsEgrAntAbonos] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipCan VARCHAR(3),@pmnAnticipo INT,@pmIdCiaAnt CHAR(2)
,@pmItemAnt INT,@pmVrAbonado MONEY,@pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCiaFac CHAR(2),@pmItemFac INT,@pmIdProveedor VARCHAR(16),@pmFecFactura SMALLDATETIME,@pmItemCom INT
 AS 
 INSERT INTO Trn_EgrAntAbonos (TipCom,Comprobante,IdCia,Item,TipCan,nAnticipo,IdCiaAnt,ItemAnt,VrAbonado,TipFac,Factura,IdCiaFac,ItemFac,IdProveedor,FecFactura,ItemCom)
 VALUES (@pmTipCom,@pmComprobante,@pmIdCia,@pmItem,@pmTipCan,@pmnAnticipo,@pmIdCiaAnt,@pmItemAnt,@pmVrAbonado,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmItemFac,@pmIdProveedor,@pmFecFactura,@pmItemCom)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpEgrAnticipoApl] @pmIdTercero VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null,@pmTipCom VARCHAR(3)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
AS
 IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
  UPDATE Trn_EgrAnticipo SET Trn_EgrAnticipo.VrAplicado=(SELECT ISNULL(SUM(Trn_EgrAntAbonos.VrAbonado),0)
  FROM Trn_EgrAntAbonos WHERE Trn_EgrAnticipo.TipCom=Trn_EgrAntAbonos.TipCan AND Trn_EgrAnticipo.Comprobante=Trn_EgrAntAbonos.nAnticipo
  AND Trn_EgrAnticipo.IdCia=Trn_EgrAntAbonos.IdCiaAnt AND Trn_EgrAnticipo.Item=Trn_EgrAntAbonos.ItemAnt)
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdTercero LIKE ISNULL(@pmIdTercero,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
  AND  TipCom LIKE ISNULL(@pmTipCom,'%') 
 ELSE
  UPDATE Trn_EgrAnticipo SET Trn_EgrAnticipo.VrAplicado=(SELECT ISNULL(SUM(Trn_EgrAntAbonos.VrAbonado),0)
  FROM Trn_EgrAntAbonos WHERE Trn_EgrAnticipo.TipCom=Trn_EgrAntAbonos.TipCan AND Trn_EgrAnticipo.Comprobante=Trn_EgrAntAbonos.nAnticipo
  AND Trn_EgrAnticipo.IdCia=Trn_EgrAntAbonos.IdCiaAnt AND Trn_EgrAnticipo.Item=Trn_EgrAntAbonos.ItemAnt)
  WHERE IdTercero LIKE ISNULL(@pmIdTercero,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
  AND  TipCom LIKE ISNULL(@pmTipCom,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryJurPreguntas] @pmIdPregunta VARCHAR(4)
 AS 
 SELECT IdPregunta,PreguntaBas,IdTipoPro,TipoDato,Inactivo
 FROM JurPreguntas
 WHERE IdPregunta=@pmIdPregunta
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelJurPreguntas] @pmIdPregunta VARCHAR(4)
 AS 
 DELETE FROM JurPreguntas
 WHERE IdPregunta=@pmIdPregunta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenOper_Cr] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT O.TipDoc,O.NumOrden,O.IdCia,O.Item,O.IdOperario,NomOperario,O.IdTipoOper,TipoOperario
,O.CdConcepto,C.Concepto,C.IdCuenta,C.TipoConc,TarifaCargo,O.PlacaVehic,O.Descripcion,O.TipoReg,O.FechaReg,O.CdUsuario,Usuario
 FROM Trn_MudOrdenOper AS O INNER JOIN TiposOperarios AS TP ON O.IdTipoOper=TP.IdOper
 LEFT JOIN Terceros AS T ON O.IdOperario=T.IdTercero
 LEFT JOIN TraConcCausac AS C ON O.CdConcepto=C.IdConcepto
 LEFT JOIN adm_Usuarios AS U ON O.CdUsuario=U.IdUsuario
 WHERE O.TipDoc=@pmTipDoc AND O.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND O.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_LibOf] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_LibOf
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_LibOfGcue] @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCuenta,COUNT(tmItem) AS SCANT
 FROM tm_LibOf
 WHERE tmEst=@pmtmEst 
 GROUP BY tmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraTrasladosFmt] @pmTipDoc VARCHAR(3),@pmTrasladoIni INT,@pmTrasladoFin INT,@pmIdCia CHAR(2)
AS 
 SELECT T.TipDoc AS CdTipDoc,T.Traslado AS NumTraslado,T.IdCia AS CdCia,Compania,Fecha,FecDespacho,TipoTraslado,T.IdBodega AS CdBodega,BG.Bodega AS BodegaOrig,IdBodDtno,BD.Bodega AS BodegaDtno
,IdVehiculo,IdConductor,CDT.RazonSocial AS NomConductor,Manifiesto,CdCiaMuc,EstadoRec,FechaRec,CedRecibido,NomRecibido
,T.Observacion AS Observ,OrigenAdd,TimeSys,IdCiaCrea,T.IdUsuario AS CdUsuario,Usuario
,DR.Item AS DetItem,TipRem,Remesa,IdCiaRem,ItemRem,EdoRecibido,Comentarios,IdMercancia,DescripMcias,D.Cantidad AS Cant,PesoNeto,UndMed,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol
,Cases,Cajas,Palets,NitRemite,Remitente,DirOrigen,IdOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,NitDestntario,Destinatario,DirDestino,IdDestino,LD.Localidad AS CiudadDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,TarifClie,VrDeclarado,VrSeguro,TarifSeguro,UndTarifa,UndTarifPago,Remision,DocCliente,Referencia1,Referencia2,Referencia3,CdNovedad,Novedad
 FROM Trn_TraTraslados AS T INNER JOIN Companias AS CN ON T.IdCia=CN.IdCia INNER JOIN Terceros AS CDT ON T.IdConductor=CDT.IdTercero 
 INNER JOIN adm_Usuarios AS U ON T.IdUsuario=U.IdUsuario
 INNER JOIN AlmBodegas AS BG ON T.IdBodega=BG.IdBodega INNER JOIN AlmBodegas AS BD ON T.IdBodDtno=BD.IdBodega
 INNER JOIN Trn_TraTrasladoRem AS DR ON T.TipDoc=DR.TipDoc AND T.Traslado=DR.Traslado AND T.IdCia=DR.IdCia
 LEFT JOIN Trn_TraRemMcias AS D ON DR.TipRem=D.TipDoc AND DR.Remesa=D.NumOrden AND DR.IdCiaRem=D.IdCia AND DR.ItemRem=D.Item
 LEFT JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal LEFT JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 LEFT JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal LEFT JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 LEFT JOIN NovedadTra AS NV ON DR.CdNovedad=NV.IdNovedad
 WHERE T.TipDoc=@pmTipDoc AND T.Traslado BETWEEN @pmTrasladoIni AND @pmTrasladoFin AND T.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNovedadTra] @pmIdNovedad VARCHAR(4)
 AS 
 IF @pmIdNovedad IS NULL
  SELECT IdNovedad,Novedad
  FROM NovedadTra
  WHERE Inactivo=0
  ORDER BY IdNovedad
 ELSE
  SELECT IdNovedad,Novedad,Inactivo
  FROM NovedadTra
  WHERE IdNovedad=@pmIdNovedad
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsNovedadTra] @pmIdNovedad VARCHAR(4),@pmNovedad VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO NovedadTra (IdNovedad,Novedad,Inactivo)
 VALUES (@pmIdNovedad,@pmNovedad,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpNovedadTra] @pmIdNovedad VARCHAR(4),@pmNovedad VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE NovedadTra SET Novedad=@pmNovedad,Inactivo=@pmInactivo
 WHERE IdNovedad=@pmIdNovedad
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelNovedadTra] @pmIdNovedad VARCHAR(4)
 AS 
 DELETE FROM NovedadTra
 WHERE IdNovedad=@pmIdNovedad
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 DevMasivo<=0 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_MovClientes] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_MovClientes
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelOilGuia] @pmTipDoc VARCHAR(3),@pmNumGuia INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_OilGuia WHERE TipDoc=@pmTipDoc AND NumGuia=@pmNumGuia AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilGuia] @pmTipDoc VARCHAR(3),@pmNumGuia INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumGuia,IdCia,Fecha,FecDespacho,IdProveedor,IdVehiculo,IdConductor,IdEstacion,IdProducto,CantBruto,CantNeto,UndMed,Temperatura,UnidTemp,ValorAPI,ValorVCW,CantDescargue,APIDescargue,TempDescargue,FecDescargue,NitEmpTrans,NomEmpTrans,NumRemision,Remolque,TipoEnt,NumEntrada,IdCiaEnt
,Anulado,FecDev,Observacion,IdEstado,NumFactCom,CdCiaFco,NumFactTra,CdCiaFtr,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario
 FROM Trn_OilGuia
 WHERE TipDoc=@pmTipDoc AND NumGuia=@pmNumGuia AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpOilGuia] @pmTipDoc VARCHAR(3),@pmNumGuia INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmIdProveedor VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmIdEstacion VARCHAR(4),@pmIdProducto VARCHAR(16),@pmCantBruto DECIMAL(16,6),@pmCantNeto DECIMAL(16,6),@pmUndMed VARCHAR(10),@pmTemperatura DECIMAL(16,6),@pmUnidTemp CHAR(1),@pmValorAPI DECIMAL(16,6),@pmValorVCW DECIMAL(16,6),@pmCantDescargue DECIMAL(16,6),@pmAPIDescargue DECIMAL(16,6),@pmTempDescargue DECIMAL(16,6),@pmFecDescargue SMALLDATETIME
,@pmNitEmpTrans VARCHAR(16),@pmNomEmpTrans VARCHAR(150),@pmNumRemision VARCHAR(20),@pmRemolque VARCHAR(10),@pmTipoEnt VARCHAR(3),@pmNumEntrada INT,@pmIdCiaEnt CHAR(2),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmNumFactCom INT,@pmCdCiaFco CHAR(2),@pmNumFactTra INT,@pmCdCiaFtr CHAR(2)
 AS 
 UPDATE Trn_OilGuia SET Fecha=@pmFecha,FecDespacho=@pmFecDespacho,IdProveedor=@pmIdProveedor,IdVehiculo=@pmIdVehiculo,IdConductor=@pmIdConductor,IdEstacion=@pmIdEstacion,IdProducto=@pmIdProducto,CantBruto=@pmCantBruto,CantNeto=@pmCantNeto,UndMed=@pmUndMed,Temperatura=@pmTemperatura,UnidTemp=@pmUnidTemp,ValorAPI=@pmValorAPI,ValorVCW=@pmValorVCW,CantDescargue=@pmCantDescargue,APIDescargue=@pmAPIDescargue,TempDescargue=@pmTempDescargue,FecDescargue=@pmFecDescargue,NitEmpTrans=@pmNitEmpTrans,NomEmpTrans=@pmNomEmpTrans,NumRemision=@pmNumRemision,Remolque=@pmRemolque
,TipoEnt=@pmTipoEnt,NumEntrada=@pmNumEntrada,IdCiaEnt=@pmIdCiaEnt,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,NumFactCom=@pmNumFactCom,CdCiaFco=@pmCdCiaFco,NumFactTra=@pmNumFactTra,CdCiaFtr=@pmCdCiaFtr
 WHERE TipDoc=@pmTipDoc AND NumGuia=@pmNumGuia AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOilGuia] @pmTipDoc VARCHAR(3),@pmNumGuia INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmIdProveedor VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmIdEstacion VARCHAR(4),@pmIdProducto VARCHAR(16),@pmCantBruto DECIMAL(16,6),@pmCantNeto DECIMAL(16,6),@pmUndMed VARCHAR(10),@pmTemperatura DECIMAL(16,6),@pmUnidTemp CHAR(1),@pmValorAPI DECIMAL(16,6),@pmValorVCW DECIMAL(16,6),@pmCantDescargue DECIMAL(16,6),@pmAPIDescargue DECIMAL(16,6),@pmTempDescargue DECIMAL(16,6),@pmFecDescargue SMALLDATETIME
,@pmNitEmpTrans VARCHAR(16),@pmNomEmpTrans VARCHAR(150),@pmNumRemision VARCHAR(20),@pmRemolque VARCHAR(10),@pmTipoEnt VARCHAR(3),@pmNumEntrada INT,@pmIdCiaEnt CHAR(2),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmNumFactCom INT,@pmCdCiaFco CHAR(2),@pmNumFactTra INT,@pmCdCiaFtr CHAR(2)
,@pmFechaCrea SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmOrigenAdd VARCHAR(10),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_OilGuia (TipDoc,NumGuia,IdCia,Fecha,FecDespacho,IdProveedor,IdVehiculo,IdConductor,IdEstacion,IdProducto,CantBruto,CantNeto,UndMed,Temperatura,UnidTemp,ValorAPI,ValorVCW,CantDescargue,APIDescargue,TempDescargue,FecDescargue,NitEmpTrans,NomEmpTrans,NumRemision,Remolque,TipoEnt,NumEntrada,IdCiaEnt,Anulado,FecDev,Observacion,IdEstado,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario,NumFactCom,CdCiaFco,NumFactTra,CdCiaFtr)
 VALUES (@pmTipDoc,@pmNumGuia,@pmIdCia,@pmFecha,@pmFecDespacho,@pmIdProveedor,@pmIdVehiculo,@pmIdConductor,@pmIdEstacion,@pmIdProducto,@pmCantBruto,@pmCantNeto,@pmUndMed,@pmTemperatura,@pmUnidTemp,@pmValorAPI,@pmValorVCW,@pmCantDescargue,@pmAPIDescargue,@pmTempDescargue,@pmFecDescargue,@pmNitEmpTrans,@pmNomEmpTrans,@pmNumRemision,@pmRemolque,@pmTipoEnt,@pmNumEntrada,@pmIdCiaEnt,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado
,@pmFechaCrea,@pmIdCiaCrea,@pmOrigenAdd,@pmIdUsuario,@pmNumFactCom,@pmCdCiaFco,@pmNumFactTra,@pmCdCiaFtr)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilCompraDespFmt] @pmTipDoc VARCHAR(3),@pmNumCompraIni INT,@pmNumCompraFin INT,@pmIdCia CHAR(2)
 AS 
SELECT D.TipDoc,D.NumCompra,D.IdCia,D.Item,D.NumGuia,D.IdCiaGuia,G.FecDespacho,G.IdVehiculo,G.IdEstacion AS IdCampo,CM.EstServicio AS Campo,G.NumRemision
,D.TipEnt,D.NumEntrada,D.IdCiaEnt,D.ItemEnt,E.FecDescargue,E.IdProveedor,T.RazonSocial AS NomProveedor,E.TipoAju,E.NumAjuInv,E.IdCiaAju,E.CantAjuste,E.EstContable,E.TipCom,E.Comprobante,E.IdCiaCom
,D.IdProducto,P.DescripProd,D.CantBruto,D.CantNeto,D.UndMed,UM.Unidad,D.CantEntrada,D.UMCargue,UMC.Unidad AS Um_Cargue,D.VrUnitario,D.NumTasas,D.TasaBrent,D.TasaWti,D.TRMdolar,D.Puntos,D.PrecioUnd
,D.VrTotal,D.CdMoneda,D.UMprecio
FROM Trn_OilCompraDesp AS D INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed
INNER JOIN Trn_OilGuia AS G ON D.NumGuia=G.NumGuia AND D.IdCiaGuia=G.IdCia
INNER JOIN Trn_OilEntrada AS E ON D.TipEnt=E.TipEnt AND D.NumEntrada=E.NumEntrada AND D.IdCiaEnt=E.IdCia
INNER JOIN Terceros AS T ON E.IdProveedor=T.IdTercero
LEFT JOIN EdsServicio AS CM ON G.IdEstacion=CM.IdEstacion LEFT JOIN Sys_Um AS UMC ON D.UMCargue=UMC.UndMed 
WHERE D.TipDoc=@pmTipDoc AND D.NumCompra BETWEEN ISNULL(@pmNumCompraIni,0) AND ISNULL(@pmNumCompraFin,2147483647) 
AND (D.IdCia=@pmIdCia OR @pmIdCia IS NULL)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilGuiaFmt] @pmTipDoc VARCHAR(3),@pmNumGuiaIni INT,@pmNumGuiaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT G.TipDoc,G.NumGuia,G.IdCia,Compania,G.Fecha,G.FecDespacho,G.IdProveedor,T.RazonSocial AS NomProveedor,G.IdVehiculo,G.IdConductor,CD.RazonSocial AS NomConductor,G.IdEstacion AS IdCampo,CM.EstServicio AS Campo
,G.IdProducto,DescripProd,G.CantBruto,G.CantNeto,G.UndMed,UM.Unidad,G.Temperatura,G.UnidTemp,G.ValorAPI,G.ValorVCW,G.CantDescargue,G.APIDescargue,G.TempDescargue,G.FecDescargue
,G.NitEmpTrans,G.NomEmpTrans,G.NumRemision,G.Remolque,G.TipoEnt,G.NumEntrada,G.IdCiaEnt,G.NumFactCom,G.CdCiaFco,G.NumFactTra,G.CdCiaFtr,G.Anulado,G.FecDev
,G.Observacion,G.IdEstado,Estado,G.FechaCrea,G.IdCiaCrea,G.OrigenAdd,G.IdUsuario,Usuario,TipoDoc
--datos proveedor
,T.TipoId AS TercTipoId,T.Dv AS TercDv,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.TelMovil AS TercCelular,T.e_mail AS TercEmail
 FROM Trn_OilGuia AS G INNER JOIN Sys_TiposDoc AS TD ON G.TipDoc=TD.IdDoc INNER JOIN EstadoDoc AS ED ON G.IdEstado=ED.IdEstado
 INNER JOIN Companias AS CN ON G.IdCia=CN.IdCia INNER JOIN Terceros AS T ON G.IdProveedor=T.IdTercero
 INNER JOIN ProdMcias AS P ON G.IdProducto=P.IdProducto INNER JOIN Sys_Um AS UM ON G.UndMed=UM.UndMed
 INNER JOIN Terceros AS CD ON G.IdConductor=CD.IdTercero INNER JOIN adm_Usuarios AS U ON G.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN EdsServicio AS CM ON G.IdEstacion=CM.IdEstacion
 WHERE G.TipDoc=@pmTipDoc AND G.NumGuia BETWEEN @pmNumGuiaIni AND @pmNumGuiaFin AND G.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreCartera_Fac] @pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdLinea VARCHAR(4)=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,'%')
 AND C.CodLinCred LIKE ISNULL(@pmIdLinea,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreCartera_Ndb] @pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdLinea VARCHAR(4)=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,'%')
 AND C.CodLinCred LIKE ISNULL(@pmIdLinea,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNomProrrogasId] @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(NumReg)  AS MXITM
 FROM Trn_NomProrrogas
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraTurn] @pmNumero INT,@pmIdCia CHAR(2),@pmIdVehiculo VARCHAR(10),@pmIndTurno INT,@pmFecha SMALLDATETIME,@pmIdConductor VARCHAR(16)
,@pmnRemolque VARCHAR(10),@pmEstado INT,@pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmFecOrden SMALLDATETIME,@pmNitCliente VARCHAR(16),@pmSedeClie VARCHAR(10),@pmAutorizac INT
 AS 
 UPDATE Trn_TraTurn SET IdVehiculo=@pmIdVehiculo,IndTurno=@pmIndTurno,Fecha=@pmFecha,IdConductor=@pmIdConductor,nRemolque=@pmnRemolque,Estado=@pmEstado
,TipOrd=@pmTipOrd,NumOrden=@pmNumOrden,IdCiaOrd=@pmIdCiaOrd,FecOrden=@pmFecOrden,NitCliente=@pmNitCliente,SedeClie=@pmSedeClie,Autorizac=@pmAutorizac
 WHERE Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraTurn] @pmNumero INT,@pmIdCia CHAR(2),@pmIdVehiculo VARCHAR(10),@pmIndTurno INT,@pmFecha SMALLDATETIME
,@pmIdConductor VARCHAR(16),@pmnRemolque VARCHAR(10),@pmEstado INT,@pmTipOrd VARCHAR(3)
,@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmFecOrden SMALLDATETIME,@pmNitCliente VARCHAR(16),@pmSedeClie VARCHAR(10),@pmAutorizac INT
,@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraTurn (Numero,IdCia,IdVehiculo,IndTurno,Fecha,IdConductor,nRemolque,Estado,TipOrd,NumOrden,IdCiaOrd,FecOrden,IdUsuario,FechaCrea,NitCliente,SedeClie,Autorizac)
 VALUES (@pmNumero,@pmIdCia,@pmIdVehiculo,@pmIndTurno,@pmFecha,@pmIdConductor,@pmnRemolque,@pmEstado
,@pmTipOrd,@pmNumOrden,@pmIdCiaOrd,@pmFecOrden,@pmIdUsuario,@pmFechaCrea,@pmNitCliente,@pmSedeClie,@pmAutorizac)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposProceso] @pmIdTipProc VARCHAR(4),@pmTipoProceso VARCHAR(150),@pmDescripcion VARCHAR(1000),@pmIndOrden INT,@pmInactivo BIT
 AS 
 UPDATE TiposProceso SET TipoProceso=@pmTipoProceso,Descripcion=@pmDescripcion,IndOrden=@pmIndOrden,Inactivo=@pmInactivo
 WHERE IdTipProc=@pmIdTipProc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdOrdenEntFmt] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
AS 
SELECT O.TipDoc,O.NumEntrega,O.IdCia,Compania,O.Fecha,O.FecEntrega,O.TipoEntrega,O.NitTercero,RazonSocial,O.NomContacto,O.TelContacto
,O.TipOrden,O.NumOrden,O.IdCiaOrden,O.EdoOrden,O.Anulado,O.FecDev,O.Observacion,O.TimeSys AS FechaCrea,O.IdUsuario,Usuario
,D.Item,D.ItemOrden,D.CantOrden,D.Cantidad,D.Defectuoso,D.Rechazado,D.CostoUnit,D.VrDcto,D.TipPed,D.Pedido,D.IdCiaPed,D.ItemPed
,D.IdProducto,P.DescripProd,D.Descripcion,D.CdTipProc,TipoProceso,D.Tallas,D.ProcFinal,D.Observacion AS MotivoRchzo,P.Tallaje
FROM Trn_ProdEntrega AS O INNER JOIN Trn_ProdEntregaDet AS D ON O.TipDoc=D.TipDoc AND O.NumEntrega=D.NumEntrega AND O.IdCia=D.IdCia
INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
LEFT JOIN TiposProceso AS TP ON D.CdTipProc=TP.IdTipProc LEFT JOIN Terceros AS T ON O.NitTercero=T.IdTercero
WHERE O.TipOrden=@pmTipDoc AND O.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin  AND O.IdCia=@pmIdCia
AND O.Anulado=0
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdEntregaInsuFmt] @pmTipDoc VARCHAR(3),@pmNumEntregaIni INT,@pmNumEntregaFin INT,@pmIdCia CHAR(2)
AS 
SELECT D.TipDoc,D.NumEntrega,D.IdCia,D.Item,D.ItemCons,D.IdInsumo,IM.DescripProd AS Insumo,D.CantTraslado,D.CantSobrante
,D.CdBodOrig,B.Bodega,D.CdBodTdo,BD.Bodega AS BodegaDtno,D.TipTdo,D.Traslado,D.IdCiaTdo,D.CdTipProc,TP.TipoProceso
FROM Trn_ProdEntregaInsu AS D INNER JOIN ProdMcias AS IM ON D.IdInsumo=IM.IdProducto
INNER JOIN Bodegas AS B ON D.CdBodOrig=B.IdBodega
LEFT JOIN TiposProceso AS TP ON D.CdTipProc=TP.IdTipProc
LEFT JOIN Bodegas AS BD ON D.CdBodTdo=BD.IdBodega
WHERE D.TipDoc=@pmTipDoc AND D.NumEntrega BETWEEN @pmNumEntregaIni AND @pmNumEntregaFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdOrdenLtp] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS
SELECT O.TipDoc,O.Pedido,O.IdCia,O.IdCliente,O.IdAgencia,O.IdVend,D.Item,D.IdProducto,D.Salidas,D.Unidades
,C.Fecha AS FechaCons,CP.NumCons,CP.IdCia AS CdCiaCons,CP.Item AS ItemCons,CP.IdTipProc,TipoProceso,CP.ProcFinal,CP.Cantidad,CP.CantOrden AS CantTotOrden
,OD.NumOrden,OD.IdCia AS CiaOrden,OD.Item AS ItemOrden,OD.Cantidad AS Cant_Orden,CASE WHEN OP.Modalidad='ARREGLO' THEN OD.Cantidad ELSE 0 END AS Cant_Arreglos
,OD.CostoEst,OD.Descripcion,OD.Terminado+OD.Defectuoso AS CantTotEntrega,OD.Rechazado AS CantTotDev,OD.MotivoDev,OD.NumEntrada,OD.CdCiaEnt,OD.ItemKdxEnt,OD.CantEntrada
,OP.Fecha AS FecOrden,OP.Modalidad,OP.IdTaller,NomTaller,OP.ConPago,OP.EdoOrden,OP.FecEntrega AS FecEstEntrega,OP.NitTercero,T.RazonSocial,OP.NomContacto,OP.TelContacto,OP.Observacion
--detalle de entregas
,ED.NumEntrega,ED.IdCia AS CdCiaEnt,ED.Item AS ItemEnt,ED.Cantidad+ED.Defectuoso AS CantEntregas,ED.Rechazado AS CantDevEntregas,ED.CostoUnit,ED.VrDcto,ED.Observacion AS EntObservacion
,E.FecEntrega,E.TipoEntrega,E.EdoOrden AS EdoEntrega,E.TipCom,E.Comprobante,E.IdCiaCom
FROM Trn_Opedido AS O INNER JOIN Trn_Kardex AS D ON O.TipDoc=D.TipDoc AND O.Pedido=D.Documento AND O.IdCia=D.IdCia
INNER JOIN Trn_ProdConsProc AS CP ON D.TipDoc=CP.TipPed AND D.Documento=CP.Pedido AND D.IdCia=CP.IdCiaPed AND D.Item=CP.ItemPed
INNER JOIN Trn_ProdConsolida AS C ON CP.TipDoc=C.TipDoc AND CP.NumCons=C.NumCons AND CP.IdCia=C.IdCia
INNER JOIN Trn_ProdOrdenDet AS OD ON CP.TipDoc=OD.TipCons AND CP.NumCons=OD.NumCons AND CP.IdCia=OD.IdCiaCons AND CP.Item=OD.ItemCons
INNER JOIN Trn_ProdOrden AS OP ON OD.TipDoc=OP.TipDoc AND OD.NumOrden=OP.NumOrden AND OD.IdCia=OP.IdCia 
INNER JOIN Talleres AS TL ON OP.IdTaller=TL.IdTaller INNER JOIN TiposProceso AS TP ON CP.IdTipProc=TP.IdTipProc
LEFT JOIN Trn_ProdEntregaDet AS ED ON OD.TipDoc=ED.TipOrden AND OD.NumOrden=ED.NumOrden AND OD.IdCia=ED.IdCiaOrden AND OD.Item=ED.ItemOrden
LEFT JOIN Trn_ProdEntrega AS E ON ED.TipDoc=E.TipDoc AND ED.NumEntrega=E.NumEntrega AND ED.IdCia=E.IdCia
LEFT JOIN Terceros AS T ON OP.NitTercero=T.IdTercero
WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
AND (O.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND O.Anulado=0 AND C.Anulado=0 AND OP.Anulado=0 AND ISNULL(E.Anulado,0)=0
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdEntregaLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
SELECT E.TipDoc,TipoDoc,E.NumEntrega,E.IdCia,CI.Compania,E.Fecha,E.FecEntrega,E.TipOrden,E.NumOrden,E.IdCiaOrden,CO.Compania AS CiaOrden
,D.ItemOrden,D.Item,D.IdProducto AS CdProducto,D.Descripcion,D.CantOrden,D.Cantidad,D.Defectuoso,D.Rechazado,D.CostoUnit,D.VrDcto
,D.TipPed,D.Pedido,D.IdCiaPed,D.ItemPed,D.CdTipProc,TipoProceso,D.Tallas,D.ProcFinal,D.Observacion AS Detalle
,E.TipoEntrega,E.CodConcepto,Concepto,E.NitTercero,T.RazonSocial,E.NomContacto,E.TelContacto,E.CostoTotal,E.Descuento,E.CantTotal
,E.EdoOrden,E.IdEstado,ED.Estado,E.Observacion,E.TipCom,E.Comprobante,E.IdCiaCom,E.Anulado,E.NumDev,E.FecDev,E.TimeSys AS FechaCrea,E.IdUsuario,U.Usuario
--Inf. producto
,P.DescripProd,P.TipoRef,P.Referencia,P.CodBarras,G.IdLinea,Linea,SG.IdGrupo,Grupo,P.IdSubgrupo,Subgrupo,P.IdMarca,Marca,P.Tamano,P.Color
,P.UndMed,UM.Unidad AS DescUndMed,P.IdUnd,UM.Unidad,P.Tallaje,P.Inactivo
FROM Trn_ProdEntrega AS E INNER JOIN Trn_ProdEntregaDet AS D ON E.TipDoc=D.TipDoc AND E.NumEntrega=D.NumEntrega AND E.IdCia=D.IdCia
INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS ED ON E.IdEstado=ED.IdEstado
INNER JOIN Sys_TiposDoc AS TD ON E.TipDoc=TD.IdDoc INNER JOIN Companias AS CI ON E.IdCia=CI.IdCia
INNER JOIN Companias AS CO ON E.IdCiaOrden=CO.IdCia  
INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto 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 L ON G.IdLinea=L.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
LEFT JOIN Conceptos AS CN ON E.CodConcepto=CN.IdConcepto LEFT JOIN TiposProceso AS TP ON D.CdTipProc=TP.IdTipProc
LEFT JOIN Terceros AS T ON E.NitTercero=T.IdTercero
WHERE E.TipDoc=@pmTipDoc AND E.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
AND (E.IdCia=@pmIdCia OR @pmIdCia IS NULL)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdEntregaDev_Cr] @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT DV.TipDev,TipoDoc,DV.Devolucion,DV.IdCia,Compania,DV.Fecha,DV.TipDocEnt,DV.NumEntrega,DV.IdCiaEnt,E.FecEntrega,DV.CostoTotal,DV.Descuento
,DV.CantTotal,E.TipoEntrega,E.TipOrden,E.NumOrden,E.IdCiaOrden,E.EdoOrden AS EdoEntrega,DV.NitTercero,RazonSocial,E.NomContacto,E.TelContacto
,O.Fecha AS FecOrden,O.TipCons,O.NumCons,O.IdCiaCons,O.Modalidad,O.IdTipProc,TP.TipoProceso,TP.IndOrden,O.IdTaller,TL.NomTaller,TL.Telefono,TL.TelCelular,TL.TipoTaller 
,O.ConPago,O.ProcFinal,E.Observacion AS EntObservacion,O.Observacion AS ObservOrden
,DV.Observacion,DV.TipCom,DV.Comprobante,DV.IdCiaCom,DV.TimeSys AS FechaCrea,DV.IdCiaCrea,DV.OrigenAdd,DV.IdUsuario,Usuario
--detalles
,D.Item,D.ItemEnt,D.Cantidad AS CantDev,D.CostoUnit,D.VrDcto,D.Tallas,D.Observacion AS DetObservacion
,DE.Cantidad AS CantEntrega,DE.Defectuoso,DE.Rechazado AS CantTotDev,DE.ItemOrden,DE.CantOrden,DE.TipPed,DE.Pedido,DE.IdCiaPed,DE.ItemPed
,DE.IdProducto,P.DescripProd,DE.Descripcion,DE.CdTipProc AS DetCdProc,PS.TipoProceso AS DetProceso,DE.ProcFinal AS DetProcFinal
,P.CodBarras,P.Referencia,P.TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo
,P.IdMarca,Marca,P.IdUnd,UD.Unidad,P.Color,P.Tamano,P.Tallaje,P.Seriales,P.Lotes,P.Combo,P.Inactivo AS ProdInactivo,P.DescripLong
FROM Trn_ProdEntregaDev AS DV INNER JOIN Trn_ProdEntregaDei AS D ON DV.TipDev=D.TipDev AND DV.Devolucion=D.Devolucion AND DV.IdCia=D.IdCia
INNER JOIN Trn_ProdEntrega AS E ON DV.TipDocEnt=E.TipDoc AND DV.NumEntrega=E.NumEntrega AND DV.IdCiaEnt=E.IdCia
INNER JOIN Trn_ProdEntregaDet AS DE ON DV.TipDocEnt=DE.TipDoc AND DV.NumEntrega=DE.NumEntrega AND DV.IdCiaEnt=DE.IdCia AND D.ItemEnt=DE.Item
INNER JOIN Companias AS CN ON DV.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON DV.IdUsuario=U.IdUsuario
INNER JOIN Sys_TiposDoc AS TD ON DV.TipDev=TD.IdDoc
INNER JOIN Trn_ProdOrden AS O ON E.TipOrden=O.TipDoc AND E.NumOrden=O.NumOrden AND E.IdCiaOrden=O.IdCia
INNER JOIN TiposProceso AS TP ON O.IdTipProc=TP.IdTipProc
INNER JOIN Talleres AS TL ON O.IdTaller=TL.IdTaller
INNER JOIN ProdMcias AS P ON DE.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 UD ON P.IdUnd=UD.IdUnd
LEFT JOIN TiposProceso AS PS ON DE.CdTipProc=PS.IdTipProc 
LEFT JOIN Terceros AS T ON DV.NitTercero=T.IdTercero
WHERE DV.TipDev=@pmTipDev AND DV.Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND DV.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdConsInsuLpt] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS 
SELECT O.TipDoc,O.Pedido,O.IdCia,O.IdCliente,O.IdAgencia,O.IdVend,D.Item,D.IdProducto,P.DescripProd,D.Salidas,D.Unidades
,DM.NumCons,DM.IdCia AS CdCiaCons,DM.Item AS ItemInsu,DM.ItemPed,DM.IdInsumo,IM.DescripProd AS Insumo,DM.IdTipProc,TP.TipoProceso,TP.IndOrden
,DM.IdBodega,B.Bodega,CdBodTdo,BD.Bodega AS BodegaTdo,DM.Cantidad,DM.CantPed,DM.Existencias,DM.CantSaldo,DM.CantTdo,DM.CantOrden,DM.CantSalidas,DM.CostoUntSai
--datos del insumo
,IM.Referencia,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,IM.IdSubgrupo AS CdSubgpo,Subgrupo
,IM.IdMarca,Marca,IM.IdUnd,UM.Unidad AS UndadMed,IM.Color,IM.Tamano,IM.DescripLong AS InsDescripcion
FROM Trn_Opedido AS O INNER JOIN Trn_Kardex AS D ON O.TipDoc=D.TipDoc AND O.Pedido=D.Documento AND O.IdCia=D.IdCia
INNER JOIN Trn_ProdConsInsu AS DM ON D.TipDoc=DM.TipPed AND D.Documento=DM.Pedido AND D.IdCia=DM.IdCiaPed AND D.Item=DM.ItemPed
INNER JOIN Trn_ProdConsolida AS C ON DM.TipDoc=C.TipDoc AND DM.NumCons=C.NumCons AND DM.IdCia=C.IdCia
INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
INNER JOIN Bodegas AS B ON DM.IdBodega=B.IdBodega INNER JOIN TiposProceso AS TP ON DM.IdTipProc=TP.IdTipProc
INNER JOIN ProdMcias AS IM ON DM.IdInsumo=IM.IdProducto INNER JOIN SubGrupos AS S ON IM.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 IM.IdMarca=M.IdMarca INNER JOIN UndMed AS UM ON IM.IdUnd=UM.IdUnd
LEFT JOIN Bodegas AS BD ON DM.CdBodTdo=BD.IdBodega
WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
AND (O.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND C.Anulado=0
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposProceso] @pmIdTipProc VARCHAR(4)
 AS 
 SELECT IdTipProc,TipoProceso,Descripcion,IndOrden,Inactivo
 FROM TiposProceso WHERE IdTipProc=@pmIdTipProc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdEntrega_Cr] @pmTipDoc VARCHAR(3),@pmNumEntregaIni INT,@pmNumEntregaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT E.TipDoc,TipoDoc,E.NumEntrega,E.IdCia,Compania,E.Fecha,E.FecEntrega,E.TipOrden,E.NumOrden,E.IdCiaOrden,E.CostoTotal,E.Descuento,E.CantTotal
,E.TipoEntrega,E.NitTercero,RazonSocial,E.NomContacto,E.TelContacto,E.EdoOrden AS EdoEntrega
,O.Fecha AS FecOrden,O.TipCons,O.NumCons,O.IdCiaCons,O.Modalidad,O.IdTipProc,TP.TipoProceso,TP.IndOrden,O.IdTaller,TL.NomTaller,TL.Direccion,TL.IdLocal,LC.Localidad,TL.Telefono,TL.TelCelular,TL.TipoTaller 
,O.ConPago,O.ProcFinal,O.Observacion AS ObservOrden,O.EdoOrden
,E.Anulado,E.FecDev,E.NumDev,E.Observacion,E.CodConcepto,E.TipCom,E.Comprobante,E.IdCiaCom,E.IdEstado,Estado,E.OrigenAdd,E.TimeSys AS FecCrea,E.FecUpdate AS FecActualiza,E.IdCiaCrea,E.IdUsuario,Usuario
--detalles
,D.Item,D.ItemOrden,D.CantOrden,D.Cantidad,D.Defectuoso,D.Rechazado,D.CostoUnit,D.VrDcto,D.TipPed,D.Pedido,D.IdCiaPed,D.ItemPed,D.IdProducto,P.DescripProd,D.Descripcion
,D.CdTipProc AS DetCdProc,PS.TipoProceso AS DetProceso,D.Tallas,D.ProcFinal AS DetProcFinal,D.Observacion AS MotivoRechz
,P.CodBarras,P.Referencia,P.TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo
,P.IdMarca,Marca,P.IdUnd,UD.Unidad,P.Color,P.Tamano,P.Tallaje,P.Seriales,P.Lotes,P.Combo,P.Inactivo AS ProdInactivo,P.DescripLong
FROM Trn_ProdEntrega AS E INNER JOIN Trn_ProdEntregaDet AS D ON E.TipDoc=D.TipDoc AND E.NumEntrega=D.NumEntrega AND E.IdCia=D.IdCia
INNER JOIN Companias AS CN ON E.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON E.IdEstado=ED.IdEstado
INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON E.TipDoc=TD.IdDoc
INNER JOIN Trn_ProdOrden AS O ON E.TipOrden=O.TipDoc AND E.NumOrden=O.NumOrden AND E.IdCiaOrden=O.IdCia
INNER JOIN TiposProceso AS TP ON O.IdTipProc=TP.IdTipProc
INNER JOIN Talleres AS TL ON O.IdTaller=TL.IdTaller INNER JOIN Localidades AS LC ON TL.IdLocal=LC.IdLocal
INNER JOIN ProdMcias AS P ON D.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 UD ON P.IdUnd=UD.IdUnd
LEFT JOIN TiposProceso AS PS ON D.CdTipProc=PS.IdTipProc 
LEFT JOIN Terceros AS T ON E.NitTercero=T.IdTercero
WHERE E.TipDoc=@pmTipDoc AND E.NumEntrega BETWEEN @pmNumEntregaIni AND @pmNumEntregaFin AND E.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdOrden_Cr] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
AS 
SELECT O.TipDoc,TipoDoc,O.NumOrden,O.IdCia,Compania,O.Fecha,D.TipCons,D.NumCons,D.IdCiaCons,C.Fecha AS FecCons,O.Modalidad,O.IdTipProc,TP.TipoProceso,TP.IndOrden
,O.IdTaller,TL.NomTaller,TL.Direccion,TL.IdLocal,LC.Localidad,TL.Telefono,TL.TelCelular,TL.TipoTaller,O.NitTercero,RazonSocial,O.NomContacto,O.TelContacto
,O.CostoTotal,O.ConPago,O.EdoOrden,O.Num_Entrega,O.CdCiaEnt,O.FecEntrega,O.ProcFinal,O.Anulado,O.FecDev,O.Observacion
,O.IdEstado,Estado,O.OrigenAdd,O.TimeSys AS FecCrea,O.FecUpdate AS FecActualiza,O.IdCiaCrea,O.IdUsuario,Usuario
--datos detalle
,D.Item,DT.TipPed,DT.Pedido,DT.IdCiaPed,DT.ItemPed,D.ItemCons,D.IdProducto,P.DescripProd,D.Descripcion,D.Cantidad,D.CostoEst,D.Terminado,D.Defectuoso,D.Rechazado
,DT.IdBodega AS CodBodega,B.Bodega,DT.CantAprob,DT.Existencias,DT.CantSaldo,DT.CantTdo,DT.Cantidad AS CantProceso,DT.Tallas
,DT.IdTipProc AS DetCdTipProc,PS.TipoProceso AS DetTipoProceso,DT.ProcFinal AS DetProcFinal,D.MotivoDev,D.ItemEnt
,P.CodBarras,P.Referencia,P.TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo
,P.IdMarca,Marca,P.IdUnd,UD.Unidad,P.Color,P.Tamano,P.Tallaje,P.Seriales,P.Lotes,P.Combo,P.Inactivo AS ProdInactivo,P.DescripLong
FROM Trn_ProdOrden AS O INNER JOIN TiposProceso AS TP ON O.IdTipProc=TP.IdTipProc
INNER JOIN Talleres AS TL ON O.IdTaller=TL.IdTaller INNER JOIN Localidades AS LC ON TL.IdLocal=LC.IdLocal
INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
INNER JOIN Trn_ProdOrdenDet AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
INNER JOIN Trn_ProdConsolida AS C ON D.TipCons=C.TipDoc AND D.NumCons=C.NumCons AND D.IdCiaCons=C.IdCia
INNER JOIN Trn_ProdConsProc AS DT ON D.TipCons=DT.TipDoc AND D.NumCons=DT.NumCons AND D.IdCiaCons=DT.IdCia AND D.ItemCons=DT.Item
INNER JOIN ProdMcias AS P ON D.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 UD ON P.IdUnd=UD.IdUnd
INNER JOIN Bodegas AS B ON DT.IdBodega=B.IdBodega
LEFT JOIN TiposProceso AS PS ON DT.IdTipProc=PS.IdTipProc
LEFT JOIN Terceros AS T ON O.NitTercero=T.IdTercero
WHERE O.TipDoc=@pmTipDoc AND O.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND O.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryProdProcesosLta] 
AS
SELECT PS.IdProducto AS CodProducto,P.DescripProd,PS.Item,PS.IdTipProc,TipoProceso,PC.Descripcion AS DescProceso,PC.IndOrden
,PS.CostoEst,PS.UltCosto,PS.ProcFinal,PS.Id,PC.Inactivo AS ProcInactivo
,P.CodBarras,P.Referencia,P.TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo
,P.IdMarca,Marca,P.Color,P.Tamano,P.IdUnd,UM.Unidad AS UndadMed,P.Tallaje,P.Seriales,P.Lotes,P.Combo,P.Inactivo AS ProdInactivo,P.DescripLong
FROM ProdProcesos AS PS INNER JOIN TiposProceso AS PC ON PS.IdTipProc=PC.IdTipProc
INNER JOIN ProdMcias AS P ON PS.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
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposProceso] @pmIdTipProc VARCHAR(4),@pmTipoProceso VARCHAR(150),@pmDescripcion VARCHAR(1000),@pmIndOrden INT,@pmInactivo BIT
 AS 
 INSERT INTO TiposProceso (IdTipProc,TipoProceso,Descripcion,IndOrden,Inactivo)
 VALUES (@pmIdTipProc,@pmTipoProceso,@pmDescripcion,@pmIndOrden,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryProdInsumosLta] 
AS
SELECT PN.IdProducto AS CodProducto,P.DescripProd,PN.Item,PN.IdInsumo AS CodInsumo,IM.DescripProd AS DescInsumo
,PN.IdTipProc,TipoProceso,PC.Descripcion AS DescProceso,PC.IndOrden,PN.Cantidad,PN.Inactivo,PN.Id
,P.CodBarras,P.Referencia,P.TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo
,P.IdMarca,Marca,P.Color,P.Tamano,P.IdUnd,UM.Unidad AS UndadMed,P.Tallaje,P.Seriales,P.Lotes,P.Combo,P.Inactivo AS ProdInactivo,P.DescripLong
--inf. insumos
,IM.IdUnd AS InsIdUnd,UMI.Unidad AS InsUndad
FROM ProdInsumos AS PN INNER JOIN TiposProceso AS PC ON PN.IdTipProc=PC.IdTipProc
INNER JOIN ProdMcias AS P ON PN.IdProducto=P.IdProducto INNER JOIN ProdMcias AS IM ON PN.IdInsumo=IM.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 UMI ON IM.IdUnd=UMI.IdUnd
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdConsProcFmt] @pmTipDoc VARCHAR(3),@pmNumConsIni INT,@pmNumConsFin INT,@pmIdCia CHAR(2)
AS 
SELECT D.TipDoc,D.NumCons,D.IdCia,D.Item,D.TipPed,D.Pedido,D.IdCiaPed,D.ItemPed,D.IdProducto AS CodProducto,P.DescripProd,D.IdBodega,B.Bodega
,D.CdUbic,D.CantAprob,D.Existencias,D.CantSaldo,D.CantTdo,D.IdTipProc,TP.TipoProceso,TP.IndOrden,D.ProcFinal,D.Cantidad,D.Tallas
,D.VrCostoEst,D.EdoOrden,D.TipOrden,D.NumOrden,D.CdCiaOrden,D.CantOrden
--datos del producto
,P.CodBarras,P.Referencia,P.TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo
,P.IdMarca,Marca,P.Color,P.Tamano,P.IdUnd,UM.Unidad,P.Tallaje,P.Seriales,P.Lotes,P.Combo,P.Inactivo AS ProdInactivo,P.DescripLong AS ProComentarios
FROM Trn_ProdConsProc AS D INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
INNER JOIN Bodegas AS B ON D.IdBodega=B.IdBodega INNER JOIN TiposProceso AS TP ON D.IdTipProc=TP.IdTipProc
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
WHERE D.TipDoc=@pmTipDoc AND D.NumCons BETWEEN @pmNumConsIni AND @pmNumConsFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdPedidoInsuFmt] @pmTipDoc VARCHAR(3),@pmPedidoIni INT,@pmPedidoFin INT,@pmIdCia CHAR(2)
AS
SELECT D.TipDoc,D.Pedido,D.IdCia,D.Item,D.ItemDet,D.IdProducto,P.DescripProd,D.IdInsumo,IM.DescripProd AS DescInsumo
,D.IdTipProc,TP.TipoProceso,TP.IndOrden,D.Cantidad,D.CantPed,D.CantAprob,IM.IdSubgrupo AS CdSubgpo,Subgrupo
FROM Trn_ProdPedidoInsu AS D INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
INNER JOIN ProdMcias AS IM ON D.IdInsumo=IM.IdProducto INNER JOIN SubGrupos AS S ON IM.IdSubgrupo=S.IdSubgrupo
INNER JOIN TiposProceso AS TP ON D.IdTipProc=TP.IdTipProc
WHERE D.TipDoc=@pmTipDoc AND D.Pedido BETWEEN @pmPedidoIni AND @pmPedidoFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposProceso] @pmIdTipProc VARCHAR(4)
 AS 
 DELETE FROM TiposProceso WHERE IdTipProc=@pmIdTipProc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdConsInsuFmt] @pmTipDoc VARCHAR(3),@pmNumConsIni INT,@pmNumConsFin INT,@pmIdCia CHAR(2)
AS 
SELECT D.TipDoc,D.NumCons,D.IdCia,D.Item,D.TipPed,D.Pedido,D.IdCiaPed,D.ItemPed,D.IdProducto AS CodProducto,P.DescripProd
,D.IdInsumo,IM.DescripProd AS Insumo,D.IdTipProc,TP.TipoProceso,TP.IndOrden,D.IdBodega,B.Bodega,CdBodTdo,BD.Bodega AS BodegaTdo
,D.Cantidad,D.CantPed,D.Existencias,D.CantSaldo,D.CantTdo,D.CantOrden,D.CantSalidas,D.CostoUntSai
--datos del insumo
,IM.Referencia,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,IM.IdSubgrupo AS CdSubgpo,Subgrupo
,IM.IdMarca,Marca,IM.IdUnd,UM.Unidad AS UndadMed,IM.Color,IM.Tamano,IM.DescripLong AS InsDescripcion
FROM Trn_ProdConsInsu AS D INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
INNER JOIN Bodegas AS B ON D.IdBodega=B.IdBodega INNER JOIN TiposProceso AS TP ON D.IdTipProc=TP.IdTipProc
INNER JOIN ProdMcias AS IM ON D.IdInsumo=IM.IdProducto INNER JOIN SubGrupos AS S ON IM.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 IM.IdMarca=M.IdMarca INNER JOIN UndMed AS UM ON IM.IdUnd=UM.IdUnd
LEFT JOIN Bodegas AS BD ON D.CdBodTdo=BD.IdBodega
WHERE D.TipDoc=@pmTipDoc AND D.NumCons BETWEEN @pmNumConsIni AND @pmNumConsFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelClaseRie] @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseRie
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpClaseRie] @pmIdClase VARCHAR(4),@pmClaseRiesgo VARCHAR(50),@pmTarifa DECIMAL(10,4),@pmInactivo BIT
,@pmCodClase VARCHAR(4)
 AS 
 UPDATE ClaseRie SET ClaseRiesgo=@pmClaseRiesgo,Tarifa=@pmTarifa,Inactivo=@pmInactivo,CodClase=@pmCodClase
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryClaseRieDso]
 AS 
  SELECT IdClase,ClaseRiesgo,Tarifa,ClaseRiesgo+ ' ' + STR(Tarifa,5,3) AS DsRie
  FROM ClaseRie
  WHERE  Inactivo=0
  ORDER  BY IdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsClaseRie] @pmIdClase VARCHAR(4),@pmClaseRiesgo VARCHAR(50),@pmTarifa DECIMAL(10,4),@pmInactivo BIT
,@pmCodClase VARCHAR(4)
 AS 
 INSERT INTO ClaseRie (IdClase,ClaseRiesgo,Tarifa,Inactivo,CodClase)
 VALUES (@pmIdClase,@pmClaseRiesgo,@pmTarifa,@pmInactivo,@pmCodClase)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryClaseRie] @pmIdClase VARCHAR(4)
 AS 
 IF @pmIdClase IS NULL
  SELECT IdClase,ClaseRiesgo,Tarifa,CodClase
  FROM ClaseRie
  WHERE  Inactivo=0
  ORDER  BY IdClase
 ELSE
  SELECT IdClase,ClaseRiesgo,Tarifa,CodClase,Inactivo
  FROM ClaseRie
  WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdConsTallasFmt] @pmTipDoc VARCHAR(3),@pmNumConsIni INT,@pmNumConsFin INT,@pmIdCia CHAR(2)
,@pmTipoReg VARCHAR(3)=Null
AS 
SELECT D.TipDoc,D.NumCons,D.IdCia,D.Item,D.TipoReg,D.ItemDet,D.NumTalla,D.Cantidad,D.CantPed AS CantAprob
FROM Trn_ProdConsTallas AS D
WHERE D.TipDoc=@pmTipDoc AND D.NumCons BETWEEN @pmNumConsIni AND @pmNumConsFin AND D.IdCia=@pmIdCia
AND (D.TipoReg=@pmTipoReg OR @pmTipoReg IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmIdConcFE INT,@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,IdConcFE)
 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,@pmIdConcFE)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,IdConcFE,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_NotasBuses
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmIdConcFE INT,@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,IdConcFE=@pmIdConcFE,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposComCons] @pmIdCom VARCHAR(3),@pmIdCia CHAR(2)
 AS 
 DELETE FROM TiposComCons
 WHERE IdCom=@pmIdCom AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,ClaseNiif,T.Doc_Soporte,CodResol,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,ClaseNiif,Doc_Soporte,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCodResol VARCHAR(4),@pmFechaAdd SMALLDATETIME
 AS 
 INSERT INTO TiposComCons (IdCom,IdCia,LDesde,LHasta,Numero,NumManual,Formato,ConfigFecha
,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,FechaAdd,CodResol)
 VALUES (@pmIdCom,@pmIdCia,@pmLDesde,@pmLHasta,@pmNumero,@pmNumManual,@pmFormato,@pmConfigFecha
,@pmTipoPapel,@pmOrientacion,@pmVistaPrevia,@pmVerSetup,@pmNumCopias,@pmFechaAdd,@pmCodResol)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCodResol VARCHAR(4),@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,CodResol=@pmCodResol,FechaUpdate=@pmFechaUpdate
 WHERE IdCom=@pmIdCom AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposComCons] @pmIdCom VARCHAR(3),@pmIdCia CHAR(2)
 AS 
 SELECT IdCom,IdCia,LDesde,LHasta,Numero,NumManual,Formato,ConfigFecha,TipoPapel
,Orientacion,VistaPrevia,VerSetup,NumCopias,CodResol,FechaAdd,FechaUpdate
 FROM TiposComCons
 WHERE IdCom=@pmIdCom AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_ReqDetalleItm] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_ReqDetalle
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_ReqDetalle] @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmItem,tmCdProducto,tmDescripcion,tmIdSubgrupo,tmCdBodega,tmCantidad,tmIdUnd,tmVrUnitario
,tmTipOrd,tmNumOrden,tmIdCiaOrd,tmTipSal,tmNSalida,tmIdCiaSal,tmFechaSal
,tmCantSalida,tmIdOperario,tmCdCenServ,tmNitTercero,tmNumVehic,tmNumParte,tmEstado,tmObservacion
,tmCodConc,tmVrPrecio,tmCodCenCto,tmCodSubCto,tmCantOrden,tmNumero
 FROM tm_ReqDetalle
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,@pmtmCantSalida DECIMAL(14,4),@pmtmIdOperario VARCHAR(16),@pmtmCdCenServ VARCHAR(4),@pmtmNitTercero VARCHAR(16),@pmtmNumVehic VARCHAR(10),@pmtmNumParte VARCHAR(20)
,@pmtmEstado INT,@pmtmObservacion VARCHAR(1000),@pmtmCodConc VARCHAR(4),@pmtmVrPrecio MONEY,@pmtmCodCenCto VARCHAR(16),@pmtmCodSubCto VARCHAR(16),@pmtmCantOrden DECIMAL(14,4)
 AS 
 INSERT INTO tm_ReqDetalle (tmNumero,tmItem,tmCdProducto,tmDescripcion,tmIdSubgrupo,tmCdBodega,tmCantidad,tmIdUnd,tmVrUnitario,tmTipOrd,tmNumOrden,tmIdCiaOrd
,tmTipSal,tmNSalida,tmIdCiaSal,tmFechaSal,tmCantSalida,tmIdOperario,tmCdCenServ,tmNitTercero,tmNumVehic,tmNumParte,tmEstado,tmObservacion,tmCodConc,tmVrPrecio,tmCodCenCto,tmCodSubCto,tmCantOrden)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmCdProducto,@pmtmDescripcion,@pmtmIdSubgrupo,@pmtmCdBodega,@pmtmCantidad,@pmtmIdUnd,@pmtmVrUnitario
,@pmtmTipOrd,@pmtmNumOrden,@pmtmIdCiaOrd,@pmtmTipSal,@pmtmNSalida,@pmtmIdCiaSal,@pmtmFechaSal,@pmtmCantSalida,@pmtmIdOperario,@pmtmCdCenServ
,@pmtmNitTercero,@pmtmNumVehic,@pmtmNumParte,@pmtmEstado,@pmtmObservacion,@pmtmCodConc,@pmtmVrPrecio,@pmtmCodCenCto,@pmtmCodSubCto,@pmtmCantOrden)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_ReqDetalleDso] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmCdProducto,tmDescripcion,tmCantidad,tmIdUnd,Unidad,tmVrUnitario,tmCantidad*tmVrUnitario AS ValorTotal
,tmTipOrd,tmNumOrden,tmIdCiaOrd,tmCantOrden,tmTipSal,tmNSalida,tmIdCiaSal,tmFechaSal,tmCantSalida,tmIdSubgrupo,tmCdBodega,tmObservacion
,tmVrPrecio,tmCodCenCto,tmCodSubCto,tmNumVehic,tmNumero
 FROM tm_ReqDetalle AS D INNER JOIN UndMed AS U ON D.tmIdUnd=U.IdUnd
 WHERE tmNumero=@pmtmNumero
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,tmCantSalida,tmIdOperario,tmCdCenServ,tmNitTercero,tmNumVehic,tmNumParte,tmEstado,tmObservacion,tmCodConc,tmVrPrecio,tmCodCenCto,tmCodSubCto,tmCantOrden)
 SELECT  @pmtmNumero,Item,CdProducto,Descripcion,IdSubgrupo,CdBodega,Cantidad,IdUnd,VrUnitario,TipOrd,NumOrden,IdCiaOrd,TipSal,NSalida,IdCiaSal,FechaSal
,(CantSalida-ISNULL(CantDevSal,0)),'0','','0',PlacaVehDet,'',0,Referencia,'',0,CodCCtoDet,CodSCtoDet,Cant_Orden
 FROM Trn_ReqDetalle
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_ReqDetalle_Mtto] @pmTipReq 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
,tmCantSalida,tmIdOperario,tmCdCenServ,tmNitTercero,tmNumVehic,tmNumParte,tmEstado,tmObservacion,tmCodConc,tmVrPrecio,tmCodCenCto,tmCodSubCto,tmCantOrden)
 SELECT  @pmtmNumero,Item,R.IdProducto,R.Descripcion,ISNULL(P.IdSubgrupo,'0'),R.CdBodega,R.Cantidad,R.IdUnd,CAST (VrUnitario AS MONEY),TipOdt,NumOrden,IdCiaOdt
,TipSal,NumSalida,IdCiaSal,Null,CantSalida,IdOperario,CdCenServ,NitTercero,NumVehic,NumParte,R.EstadoReq,R.Observacion,R.CdConcServ,VrPrecioUnd,'','',0
 FROM Trn_MttoOrdenReq AS R LEFT JOIN ProdMcias AS P ON R.IdProducto=P.IdProducto
 WHERE TipReq=@pmTipReq AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetalle_Odp] @pmTipDoc VARCHAR(3),@pmOrdPago INT,@pmIdCia CHAR(2)
,@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraDetalle (tmNumero,tmItem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed,tmdmsAlto,tmdmsAncho,tmdmsLargo,tmVolumen,tmUndVol,tmIdUnd,tmIdEmp,tmIdNat,tmIdMnjo,tmIdTmcia,tmCdRango,tmCases,tmCajas,tmPalets
,tmRemision,tmNumOrden,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen,tmNitDestntario,tmDestinatario,tmDirDestino,tmIdDestino,tmCdAgencia,tmTipoVehic,tmTarifClie,tmTarifPago,tmTarifTabla,tmVrDeclarado,tmVrSeguro,tmTarifSeguro,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3
,tmContenedor1,tmContenedor2,tmUndTarifa,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmNumMintrans,tmPesoCont,tmUndTarPago,tmCodBodega,tmTipoTraslado,tmTieCargue,tmTieDesc,tmFecIniCargue,tmFecFinCargue,tmFecInidesc,tmFecFindesc,tmCdTipoVeh,tmTipoServicio
,tmSedeRem,tmSedeDest,tmFecLlegaCargue,tmFecLlegaDesc,tmMinCargue,tmMinDescargue)
 SELECT @pmtmNumero,R.Item,IdMercancia,Detalle,R.Cantidad,R.PesoNeto,R.UndMed,dmsAlto,dmsAncho,dmsLargo,R.Volumen,R.UndVol,IdUnd,IdEmp,IdNat,IdMnjo,IdTmcia,CdRango,R.Cases,R.Cajas,R.Palets,Remision,Cumplido
,NitRemite,Remitente,DirOrigen,IdOrigen,NitDestntario,Destinatario,DirDestino,IdDestino,CdAgencia,'TERCEROS',TarifClie,R.TarifPago,R.TarifTabla,VrDeclarado,VrSeguro,TarifSeguro,DocCliente,Referencia1,Referencia2,Referencia3
,Contenedor1,Contenedor2,D.UndTarifa,TipRem,Remesa,IdCiaRem,ItemRem,0,PesoCont,R.UndTarifa,'',0,0,0,Null,Null,Null,Null,'','',SedeRem,SedeDest,Null,Null,0,0
 FROM Trn_TraOrdenRemesas AS R INNER JOIN Trn_TraRemMcias AS D ON R.TipRem=D.TipDoc AND  R.Remesa=D.NumOrden AND R.IdCiaRem=D.IdCia AND R.ItemRem=D.Item
 WHERE R.TipDoc=@pmTipDoc AND R.OrdPago=@pmOrdPago  AND R.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTraOrdenRemesas] @pmTipDoc VARCHAR(3),@pmOrdPago INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraOrdenRemesas
 WHERE TipDoc=@pmTipDoc AND OrdPago=@pmOrdPago AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTraOrdenRemesas] @pmTipDoc VARCHAR(3),@pmOrdPago INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipMuc VARCHAR(3),@pmManifiesto INT,@pmIdCiaMuc CHAR(2),@pmTipRem VARCHAR(3),@pmRemesa INT,@pmIdCiaRem CHAR(2)
,@pmItemRem INT,@pmCantidad DECIMAL(14,4),@pmPesoNeto DECIMAL(14,4),@pmUndMed VARCHAR(10),@pmVolumen DECIMAL(14,4),@pmUndVol VARCHAR(10),@pmCases INT,@pmCajas INT,@pmPalets INT,@pmTarifPago MONEY,@pmTarifTabla MONEY
,@pmUndTarifa VARCHAR(10),@pmDetalle VARCHAR(250)
 AS 
 INSERT INTO Trn_TraOrdenRemesas (TipDoc,OrdPago,IdCia,Item,TipMuc,Manifiesto,IdCiaMuc,TipRem,Remesa,IdCiaRem,ItemRem,Cantidad,PesoNeto,UndMed,Volumen,UndVol,Cases,Cajas,Palets,TarifPago,TarifTabla,UndTarifa,Detalle)
 VALUES (@pmTipDoc,@pmOrdPago,@pmIdCia,@pmItem,@pmTipMuc,@pmManifiesto,@pmIdCiaMuc,@pmTipRem,@pmRemesa,@pmIdCiaRem,@pmItemRem,@pmCantidad,@pmPesoNeto,@pmUndMed,@pmVolumen,@pmUndVol
,@pmCases,@pmCajas,@pmPalets,@pmTarifPago,@pmTarifTabla,@pmUndTarifa,@pmDetalle)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilEntrada] @pmTipEnt VARCHAR(3),@pmNumEntrada INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipEnt,NumEntrada,IdCia,Fecha,NumGuia,IdCiaGuia,FecDespacho,FecDescargue,IdProveedor,IdVehiculo,IdConductor,NitEmpTrans,NomEmpTrans,Remolque,ValorTotal,CtaPagar,TipFactCxp,NumFactura,IdCiaCxp
,TipoAju,NumAjuInv,IdCiaAju,CantAjuste,NumFactTra,CdCiaFtr,EstContable,CdConcepto,TipCom,Comprobante,IdCiaCom,Anulado,FecDev,Observacion,IdEstado,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario
 FROM Trn_OilEntrada
 WHERE TipEnt=@pmTipEnt AND NumEntrada=@pmNumEntrada AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOilEntrada] @pmTipEnt VARCHAR(3),@pmNumEntrada INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmNumGuia INT,@pmIdCiaGuia CHAR(2),@pmFecDespacho SMALLDATETIME,@pmFecDescargue SMALLDATETIME,@pmIdProveedor VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmNitEmpTrans VARCHAR(16),@pmNomEmpTrans VARCHAR(150),@pmRemolque VARCHAR(10),@pmValorTotal DECIMAL(16,6),@pmCtaPagar BIT,@pmTipFactCxp VARCHAR(3),@pmNumFactura INT,@pmIdCiaCxp CHAR(2),@pmEstContable INT,@pmCdConcepto VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmTipoAju VARCHAR(3),@pmNumAjuInv INT,@pmIdCiaAju CHAR(2),@pmCantAjuste DECIMAL(16,6),@pmNumFactTra INT,@pmCdCiaFtr CHAR(2),@pmFechaCrea SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmOrigenAdd VARCHAR(10),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_OilEntrada (TipEnt,NumEntrada,IdCia,Fecha,NumGuia,IdCiaGuia,FecDespacho,FecDescargue,IdProveedor,IdVehiculo,IdConductor,NitEmpTrans,NomEmpTrans,Remolque,ValorTotal,CtaPagar,TipFactCxp,NumFactura,IdCiaCxp,TipoAju,NumAjuInv,IdCiaAju,CantAjuste,EstContable,CdConcepto,TipCom,Comprobante,IdCiaCom,Anulado,FecDev,Observacion,IdEstado,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario,NumFactTra,CdCiaFtr)
 VALUES (@pmTipEnt,@pmNumEntrada,@pmIdCia,@pmFecha,@pmNumGuia,@pmIdCiaGuia,@pmFecDespacho,@pmFecDescargue,@pmIdProveedor,@pmIdVehiculo,@pmIdConductor,@pmNitEmpTrans,@pmNomEmpTrans,@pmRemolque,@pmValorTotal,@pmCtaPagar,@pmTipFactCxp,@pmNumFactura,@pmIdCiaCxp,@pmTipoAju,@pmNumAjuInv,@pmIdCiaAju,@pmCantAjuste,@pmEstContable,@pmCdConcepto,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmFechaCrea,@pmIdCiaCrea,@pmOrigenAdd,@pmIdUsuario,@pmNumFactTra,@pmCdCiaFtr)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilEntradaFmt] @pmTipEnt VARCHAR(3),@pmNumEntradaIni INT,@pmNumEntradaFin INT,@pmIdCia CHAR(2)
AS 
 SELECT E.TipEnt,TipoDoc,E.NumEntrada,E.IdCia,Compania,E.Fecha,E.NumGuia,E.IdCiaGuia,E.FecDespacho,E.FecDescargue,E.IdProveedor,T.RazonSocial AS NomProveedor,E.IdVehiculo,E.IdConductor,CD.RazonSocial AS NomConductor
,D.IdTanque,D.IdProducto,P.DescripProd,D.Cantidad,D.UndMed,UM.Unidad,D.Temperatura,D.UnidTemp,D.ValorAPI,D.ValorVCW,D.API_Corregido,D.Densidad
,D.CantCargue,D.UMCargue,D.TempCargue,D.API_Cargue,D.VCW_Cargue,D.PesoNeto,D.VrUnitario
,E.NitEmpTrans,E.NomEmpTrans,E.Remolque,E.ValorTotal,E.CtaPagar,E.TipFactCxp,E.NumFactura,E.IdCiaCxp,E.TipoAju,E.NumAjuInv,E.IdCiaAju,E.CantAjuste,E.NumFactTra,E.CdCiaFtr,E.EstContable,E.CdConcepto,Concepto
,E.TipCom,E.Comprobante,E.IdCiaCom,E.Anulado,E.FecDev,E.Observacion,E.IdEstado,Estado,E.FechaCrea,E.IdCiaCrea,E.OrigenAdd,E.IdUsuario,Usuario
--Datos del proveedor
,T.TipoId AS TercTipoId,T.Dv AS TercDv,T.Direccion AS TercDireccion,T.IdLocal AS TercIdCiu,L.Localidad AS TercCiudad,L.IdDep AS TercIdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.TelMovil AS TercCelular,T.e_mail AS TercEmail
 FROM Trn_OilEntrada AS E INNER JOIN Trn_OilEntradaProd AS D ON E.TipEnt=D.TipEnt AND E.NumEntrada=D.NumEntrada AND E.IdCia=D.IdCia
 INNER JOIN Sys_TiposDoc AS TD ON E.TipEnt=TD.IdDoc INNER JOIN EstadoDoc AS ED ON E.IdEstado=ED.IdEstado
 INNER JOIN Companias AS CN ON E.IdCia=CN.IdCia INNER JOIN Terceros AS T ON E.IdProveedor=T.IdTercero
 INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed
 INNER JOIN Terceros AS CD ON E.IdConductor=CD.IdTercero INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 LEFT JOIN Conceptos AS CT ON E.CdConcepto=CT.IdConcepto
 WHERE E.TipEnt=@pmTipEnt AND E.NumEntrada BETWEEN @pmNumEntradaIni AND @pmNumEntradaFin AND E.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpOilEntrada] @pmTipEnt VARCHAR(3),@pmNumEntrada INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmNumGuia INT,@pmIdCiaGuia CHAR(2),@pmFecDespacho SMALLDATETIME,@pmFecDescargue SMALLDATETIME,@pmIdProveedor VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmNitEmpTrans VARCHAR(16),@pmNomEmpTrans VARCHAR(150),@pmRemolque VARCHAR(10),@pmValorTotal DECIMAL(16,6),@pmCtaPagar BIT,@pmTipFactCxp VARCHAR(3),@pmNumFactura INT,@pmIdCiaCxp CHAR(2),@pmEstContable INT,@pmCdConcepto VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmTipoAju VARCHAR(3),@pmNumAjuInv INT,@pmIdCiaAju CHAR(2),@pmCantAjuste DECIMAL(16,6),@pmNumFactTra INT,@pmCdCiaFtr CHAR(2)
 AS 
 UPDATE Trn_OilEntrada SET Fecha=@pmFecha,NumGuia=@pmNumGuia,IdCiaGuia=@pmIdCiaGuia,FecDespacho=@pmFecDespacho,FecDescargue=@pmFecDescargue,IdProveedor=@pmIdProveedor,IdVehiculo=@pmIdVehiculo,IdConductor=@pmIdConductor,NitEmpTrans=@pmNitEmpTrans,NomEmpTrans=@pmNomEmpTrans,Remolque=@pmRemolque,ValorTotal=@pmValorTotal,CtaPagar=@pmCtaPagar,TipFactCxp=@pmTipFactCxp,NumFactura=@pmNumFactura,IdCiaCxp=@pmIdCiaCxp,EstContable=@pmEstContable,CdConcepto=@pmCdConcepto,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion
,IdEstado=@pmIdEstado,TipoAju=@pmTipoAju,NumAjuInv=@pmNumAjuInv,IdCiaAju=@pmIdCiaAju,CantAjuste=@pmCantAjuste,NumFactTra=@pmNumFactTra,CdCiaFtr=@pmCdCiaFtr
 WHERE TipEnt=@pmTipEnt AND NumEntrada=@pmNumEntrada AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraDevOdp] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraDevOdp
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevOdp] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,OrdPago,IdCiaDoc,FecDoc,IdVehiculo,IdPoseedor,IdConductor,CxPagar,ModdDev
,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraDevOdp
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraMovLiquidaDop] @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdPoseedor VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
 AS 
 INSERT INTO tm_TraMovLiquida (tmEst,tmTipo,tmNumero,tmIdCia,tmItem,tmFecha,tmFecVence,tmIdPoseedor,tmIdVehiculo,tmIdConcepto,tmDescripcion
,tmVrDebito,tmVrCredito,tmVrAbonado,tmTipoVehic,tmVrBase,tmTarifa,tmReferencia,tmTipDocRef,tmDocRef,tmEstado,tmTipoLiq,tmLiquida,tmCiaLiq,tmSaldoAnt,tmSaldoCia,tmSaldoAntVeh,tmSaldoVehCia)
 SELECT @pmtmEst,D.TipDev,D.Devolucion,D.IdCia,M.Item,D.Fecha,D.FecDoc,M.IdPoseedor,M.IdVehiculo,'0','ORDENES DE PAGO'
,M.VrNeto,0,0,'TERCEROS',M.VrTotalFletes-M.VrDescuento,0,M.Referencia,M.TipMuc,M.Manifiesto,M.EdoLiqCausac,'0',0,'00',0,0,0,0
 FROM Trn_TraDevOdp AS D INNER JOIN Trn_TraOrdenManif AS M ON D.TipDoc=M.TipDoc AND D.OrdPago=M.OrdPago AND D.IdCiaDoc=M.IdCia
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND M.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%') AND M.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevOdpDoc] @pmTipDoc VARCHAR(3),@pmOrdPago INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,FecDoc,IdPoseedor,IdConductor,ModdDev
 FROM Trn_TraDevOdp
 WHERE  TipDoc=@pmTipDoc  AND OrdPago=@pmOrdPago AND IdCiaDoc=@pmIdCiaDoc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraDevOdp] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmOrdPago INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdVehiculo VARCHAR(10)
,@pmIdPoseedor VARCHAR(16),@pmIdConductor 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_TraDevOdp SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,OrdPago=@pmOrdPago,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdVehiculo=@pmIdVehiculo,IdPoseedor=@pmIdPoseedor,IdConductor=@pmIdConductor,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraDevOdp] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmOrdPago INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME
,@pmIdVehiculo VARCHAR(10),@pmIdPoseedor VARCHAR(16),@pmIdConductor 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_TraDevOdp (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,OrdPago,IdCiaDoc,FecDoc,IdVehiculo,IdPoseedor,IdConductor,CxPagar,ModdDev,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmOrdPago,@pmIdCiaDoc,@pmFecDoc,@pmIdVehiculo,@pmIdPoseedor,@pmIdConductor,@pmCxPagar,@pmModdDev,@pmTipCom,@pmComprobante,@pmIdCiaCom
,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,SL.SaldoCapital,SL.SaldoInteres,MaxCupoSmlv,TipoCausac,DiasVence,FecPreAprob,PlazoAprob,EtdoAprob,TipoAprob,DescAprob,Garantias,TipoGarantia,DescGarantia,VrGarantia
,ReqCheques,FecActCupo,EstadoAct,LiqSeguro,CdTarifSeguro,CdTasaConv,NitConvenio,CuotasConv,TasaIntLinea,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
 LEFT JOIN (SELECT IdClie,CdAgencia,IdLinea,SUM(VrSaldoCap) AS SaldoCapital,SUM(VrSaldoInt) AS SaldoInteres FROM FinClieSaldosLin
 GROUP BY IdClie,CdAgencia,IdLinea) AS SL ON LC.IdClie=SL.IdClie AND LC.IdAgencia=SL.CdAgencia AND LC.IdLinea=SL.IdLinea
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsFinClieSaldosLin_Uni] @pmIdClie VARCHAR(16),@pmIdClieDos VARCHAR(16),@pmNewClie VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdClie FROM FinClieSaldosLin WHERE IdClie=@pmIdClie OR IdClie=@pmIdClieDos)
  INSERT INTO FinClieSaldosLin (IdClie,CdAgencia,IdLinea,IdCia,VrSaldoCap,VrSaldoInt,VrSaldoOtro)
  SELECT @pmNewClie,CdAgencia,IdLinea,IdCia,SUM(VrSaldoCap),SUM(VrSaldoInt),SUM(VrSaldoOtro)
  FROM FinClieSaldosLin 
  WHERE  IdClie=@pmIdClie OR IdClie=@pmIdClieDos
  GROUP BY CdAgencia,IdLinea,IdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelEstadoSer] @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM EstadoSer
 WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmtmImpConsumo2 MONEY
 AS 
 UPDATE tm_VentasCom SET tmUnidades2=@pmtmUnidades2,tmVentaNeto2=@pmtmVentaNeto2,tmCostoNeto2=@pmtmCostoNeto2
,tmImpuestos2=@pmtmImpuestos2,tmImpGlobal2=@pmtmImpGlobal2,tmSoldicom2=@pmtmSoldicom2,tmVolumen2=@pmtmVolumen2,tmImpConsumo2=@pmtmImpConsumo2
 WHERE tmEst=@pmtmEst AND tmItem=@pmtmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmtmImpConsumo MONEY,@pmtmImpConsumo2 MONEY
 AS 
 INSERT INTO tm_VentasCom (tmEst,tmItem,tmUnidades,tmVentaNeto,tmCostoNeto,tmImpuestos,tmImpGlobal,tmSoldicom,tmVolumen,tmUnidades2,tmVentaNeto2,tmCostoNeto2,tmImpuestos2,tmImpGlobal2,tmSoldicom2,tmVolumen2,tmImpConsumo,tmImpConsumo2)
 VALUES (@pmtmEst,@pmtmItem,@pmtmUnidades,@pmtmVentaNeto,@pmtmCostoNeto,@pmtmImpuestos,@pmtmImpGlobal,@pmtmSoldicom,@pmtmVolumen,@pmtmUnidades2,@pmtmVentaNeto2,@pmtmCostoNeto2
,@pmtmImpuestos2,@pmtmImpGlobal2,@pmtmSoldicom2,@pmtmVolumen2,@pmtmImpConsumo,@pmtmImpConsumo2)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryInvDetalleTan] @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,D.IdProducto AS CdProducto,DescripProd,D.IdBodega AS CdBodega,CdTanque,UndAforo,UndAgua,D.Cantidad,CantAgua,CantExist
,CASE T.TnqUnido WHEN 1 THEN 0 ELSE (Cantidad-CantExist) END AS CantDif,VrUnitario
,CASE T.TnqVirtual WHEN 1 THEN 0 ELSE (Cantidad*VrUnitario) END AS CostoTotal
,D.Descripcion,D.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
 INNER JOIN Tanques AS T ON D.CdTanque=T.IdTanque
 WHERE D.TipDoc=@pmTipDoc AND D.nInventario=@pmnInventario AND D.IdCia=@pmIdCia 
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCdTerminal VARCHAR(4),@pmTipoProd VARCHAR(10),@pmAltura DECIMAL(16,6)
 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,CdTerminal=@pmCdTerminal,TipoProd=@pmTipoProd,Altura=@pmAltura
 WHERE IdTanque=@pmIdTanque
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmUndsEnt AS UnidadesEnt,tmUndsDve AS UnidadesSal
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryInvDetalleTco] @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 SELECT COUNT(CASE T.TnqVirtual WHEN 1 THEN 0 ELSE D.Item END) AS SITEMS
,SUM(CASE T.TnqVirtual WHEN 1 THEN 0 ELSE D.Cantidad END) AS SCANT
,SUM(CASE T.TnqVirtual WHEN 1 THEN 0 ELSE (D.VrUnitario*D.Cantidad) END) AS SCOSTOT
,SUM(CASE T.TnqUnido WHEN 1 THEN 0 ELSE D.CantExist END) AS SCANEXI
,SUM(CASE T.TnqUnido WHEN 1 THEN 0 ELSE (D.VrUnitario*D.CantExist) END) AS SEXITOT
,SUM(CASE T.TnqVirtual WHEN 1 THEN 0 ELSE D.CantAgua END) AS SCANTAGUA
,SUM(CASE T.TnqVirtual WHEN 1 THEN 0 ELSE D.Unidades END) AS STOTUNI
 FROM Trn_InvDetalle AS D INNER JOIN Tanques AS T ON D.CdTanque=T.IdTanque
 WHERE D.TipDoc=@pmTipDoc AND D.nInventario=@pmnInventario AND D.IdCia=@pmIdCia 
 AND D.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTanquesLta] @pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT IdTanque,T.IdProducto AS CdProd,DescripProd,Descripcion,CapTanq,NivAgua,CapNeta,SaldoActual
,TnqGas,TnqUnido,TnqVirtual,CdNueTan,NColor,TipoProd,Altura,T.FechaAdd AS Fec_Add,T.FechaUpdate AS Fec_Upd,IdCiaDef,CdTerminal
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,tmUndsEnt,tmUndsDve,tmUndsFac,tmUndsDfc,tmUndsSal,tmUndsDvs,tmUndsOtr,tmUndsDot
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCdTerminal VARCHAR(4),@pmTipoProd VARCHAR(10),@pmAltura DECIMAL(16,6)
 AS 
 INSERT INTO Tanques (IdTanque,IdProducto,Descripcion,CapTanq,NivAgua,CapNeta,SaldoActual,TnqGas,TnqUnido,TnqVirtual,CdNueTan,NColor,FechaAdd,Inactivo,IdCiaDef,CodigoApi,CdTerminal,TipoProd,Altura)
 VALUES (@pmIdTanque,@pmIdProducto,@pmDescripcion,@pmCapTanq,@pmNivAgua,@pmCapNeta,@pmSaldoActual,@pmTnqGas,@pmTnqUnido,@pmTnqVirtual,@pmCdNueTan,@pmNColor
,@pmFechaAdd,@pmInactivo,@pmIdCiaDef,@pmCodigoApi,@pmCdTerminal,@pmTipoProd,@pmAltura)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTanques] @pmIdTanque VARCHAR(4)
 AS 
 SELECT IdTanque,IdProducto,Descripcion,CapTanq,NivAgua,CapNeta,SaldoActual
 ,TnqGas,TnqUnido,TnqVirtual,CdNueTan,NColor,FechaAdd,FechaUpdate,Inactivo,IdCiaDef,CodigoApi,CdTerminal,TipoProd,Altura
 FROM Tanques
 WHERE IdTanque=@pmIdTanque
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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','VCC') 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','VCC') AND TipoRef<>'SERVICIO' AND Tanques<>0
  AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  GROUP BY CdTanque,IdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryInvDetalleFalTq] @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
 AS 
SELECT D.IdProducto AS CdProducto,DescripProd,D.CdTanque,D.Cantidad,D.CantExist
,CASE T.TnqUnido WHEN 1 THEN 0 ELSE (D.Cantidad-D.CantExist) END AS CantDif
,D.UndAforo,D.UndAgua,D.CantAgua
 FROM Trn_InvDetalle AS D INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
 LEFT JOIN Tanques AS T ON D.CdTanque=T.IdTanque
 WHERE D.TipDoc=@pmTipDoc AND D.nInventario=@pmnInventario AND D.IdCia=@pmIdCia 
 AND (D.Cantidad-D.CantExist)<0 AND T.TnqUnido=0
 ORDER BY DescripProd,D.CdTanque
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryInvDetalleSobTq] @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
 AS 
 SELECT D.IdProducto AS CdProducto,DescripProd,D.CdTanque,D.Cantidad,D.CantExist
,CASE T.TnqUnido WHEN 1 THEN 0 ELSE (D.Cantidad-D.CantExist) END AS CantDif
,D.UndAforo,D.UndAgua,D.CantAgua
 FROM Trn_InvDetalle AS D INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
 LEFT JOIN Tanques AS T ON D.CdTanque=T.IdTanque
 WHERE D.TipDoc=@pmTipDoc AND D.nInventario=@pmnInventario AND D.IdCia=@pmIdCia 
 AND (D.Cantidad-D.CantExist)>0 AND T.TnqUnido=0
 ORDER BY DescripProd,D.CdTanque
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTanquesSal] @pmIdProducto VARCHAR(16)=Null,@pmIdTanque VARCHAR(4)=Null
 AS 
 IF @pmIdProducto IS NULL AND @pmIdTanque IS NULL
  BEGIN 
   UPDATE Tanques SET SaldoActual=(SELECT ISNULL(SUM(SaldoActual),0) FROM TanqSaldos WHERE Tanques.IdTanque=TanqSaldos.IdTanque)
  END
 ELSE
  BEGIN
   IF @pmIdProducto IS NULL
    BEGIN
     UPDATE Tanques SET SaldoActual=(SELECT ISNULL(SUM(SaldoActual),0) FROM TanqSaldos WHERE Tanques.IdTanque=TanqSaldos.IdTanque)
     WHERE IdTanque=@pmIdTanque
    END
   ELSE
    BEGIN
     UPDATE Tanques SET SaldoActual=(SELECT ISNULL(SUM(SaldoActual),0) FROM TanqSaldos WHERE Tanques.IdTanque=TanqSaldos.IdTanque)
     WHERE IdProducto=@pmIdProducto AND (IdTanque=@pmIdTanque OR @pmIdTanque IS NULL)
    END
   END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraDevLgo] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmVrTotal MONEY,@pmVrCosto MONEY,@pmVrAnticipo MONEY,@pmVrGastos MONEY,@pmVrIva MONEY,@pmVrRetencion MONEY,@pmVrReteIca MONEY
,@pmVrReteCREE MONEY,@pmVrReteIva MONEY,@pmVrOtros MONEY,@pmVrDescuento MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmCxPagar BIT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmModdDev VARCHAR(10),@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraDevLgo SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Liquidacion=@pmLiquidacion,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,VrTotal=@pmVrTotal,VrCosto=@pmVrCosto,VrAnticipo=@pmVrAnticipo,VrGastos=@pmVrGastos,VrIva=@pmVrIva,VrRetencion=@pmVrRetencion,VrReteIca=@pmVrReteIca,VrReteCREE=@pmVrReteCREE,VrReteIva=@pmVrReteIva,VrOtros=@pmVrOtros
,VrDescuento=@pmVrDescuento,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,CxPagar=@pmCxPagar,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,ModdDev=@pmModdDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate 
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraDevLgo] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraDevLgo WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevLgoDoc] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Liquidacion,IdCiaDoc,FecDoc,ModdDev
 FROM Trn_TraDevLgo
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCiaDoc=@pmIdCiaDoc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevLgoLta] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc,D.Liquidacion AS NumLiquida,D.IdCiaDoc,FecDoc
,TipOds,L.NumOrden,IdCiaOds,L.IdVehiculo AS PlacaVeh,L.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,L.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,L.TipoAfiVehic
,D.VrTotal,D.VrCosto,D.VrAnticipo,D.VrGastos,D.VrIva,D.VrRetencion,D.VrReteIca,D.VrReteCREE,D.VrReteIva,D.VrOtros,D.VrDescuento,L.VrConcCargos,L.VrConcDed,D.VrNeto,D.Cantidad
,TarifaPago,BaseImp,BaseRet,BaseIca,TarifaImp,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,L.Referencia,CdPlazo,FechaVence,UndMed
,D.CxPagar,D.TipCom,D.Comprobante,D.IdCiaCom,D.ModdDev,D.Observacion AS Observ,D.IdEstado AS CdEstado,D.OrigenAdd,D.TimeSys AS FechaCrea
,D.FecUpdate AS FechaModif,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraDevLgo AS D INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia INNER JOIN Conceptos AS CT ON D.IdConcepto=CT.IdConcepto
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Trn_TraOrdenLiq AS L ON D.TipDoc=L.TipDoc AND D.Liquidacion=L.Liquidacion AND D.IdCiaDoc=L.IdCia
 INNER JOIN Terceros AS NP ON L.IdPoseedor=NP.IdTercero INNER JOIN Terceros AS CDT ON L.IdConductor=CDT.IdTercero
 WHERE D.TipDev=@pmTipDev AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevLgo] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Liquidacion,IdCiaDoc,FecDoc,VrTotal,VrCosto,VrAnticipo,VrGastos,VrIva,VrRetencion,VrReteIca,VrReteCREE,VrReteIva,VrOtros,VrDescuento,VrNeto,Cantidad,CxPagar,TipCom,Comprobante,IdCiaCom,ModdDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraDevLgo
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraDevLgo] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmVrTotal MONEY,@pmVrCosto MONEY,@pmVrAnticipo MONEY,@pmVrGastos MONEY,@pmVrIva MONEY,@pmVrRetencion MONEY,@pmVrReteIca MONEY
,@pmVrReteCREE MONEY,@pmVrReteIva MONEY,@pmVrOtros MONEY,@pmVrDescuento MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmCxPagar BIT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmModdDev VARCHAR(10),@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraDevLgo (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Liquidacion,IdCiaDoc,FecDoc,VrTotal,VrCosto,VrAnticipo,VrGastos,VrIva,VrRetencion,VrReteIca,VrReteCREE,VrReteIva,VrOtros,VrDescuento,VrNeto,Cantidad,CxPagar,TipCom,Comprobante,IdCiaCom,ModdDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmLiquidacion,@pmIdCiaDoc,@pmFecDoc,@pmVrTotal,@pmVrCosto,@pmVrAnticipo,@pmVrGastos,@pmVrIva,@pmVrRetencion,@pmVrReteIca,@pmVrReteCREE,@pmVrReteIva,@pmVrOtros,@pmVrDescuento,@pmVrNeto,@pmCantidad,@pmCxPagar,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmModdDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,IdCuentaIva,IdCuentaRet,IdCuentaIca 
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,@pmCdTarifaIva VARCHAR(4),@pmCdTarifaRet VARCHAR(4),@pmCdCuentaIva VARCHAR(16),@pmCdCuentaRet VARCHAR(16),@pmCdCuentaIca VARCHAR(16),@pmCodFEDian VARCHAR(20),@pmIvaIncluido BIT,@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
,CdTarifaIva=@pmCdTarifaIva,CdTarifaRet=@pmCdTarifaRet,CdCuentaIva=@pmCdCuentaIva,CdCuentaRet=@pmCdCuentaRet,CdCuentaIca=@pmCdCuentaIca,CodFEDian=@pmCodFEDian,IvaIncluido=@pmIvaIncluido
WHERE IdCargo=@pmIdCargo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,@pmCdTarifaIva VARCHAR(4),@pmCdTarifaRet VARCHAR(4),@pmCdCuentaIva VARCHAR(16),@pmCdCuentaRet VARCHAR(16),@pmCdCuentaIca VARCHAR(16),@pmCodFEDian VARCHAR(20),@pmIvaIncluido BIT,@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,CdTarifaIva,CdTarifaRet,CdCuentaIva,CdCuentaRet,CdCuentaIca,CodFEDian,IvaIncluido)
 VALUES (@pmIdCargo,@pmConcepto, @pmObligatorio, @pmTipoAplica, @pmDiasSemana, @pmDiasMes
, @pmValorMensual, @pmValorDiario, @pmIdCuentaDeb, @pmIdCuentaCre, @pmIdCueDebVp, @pmIdCueCreVp
,@pmNCuotas,@pmDiasVcmto,@pmOrdenAbono,@pmTipoDcto,@pmDctoPpago,@pmVrCargoAdic,@pmFecLimiteIni,@pmFecLimiteFin,@pmInactivo,@pmFechaAdd,@pmIdUsuario,@pmCdTarifaIva,@pmCdTarifaRet,@pmCdCuentaIva,@pmCdCuentaRet,@pmCdCuentaIca,@pmCodFEDian,@pmIvaIncluido)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CdTarifaIva,CdTarifaRet,CdCuentaIva,CdCuentaRet,CdCuentaIca,CodFEDian,IvaIncluido,FechaAdd,FechaUpdate,IdUsuario
 FROM Fijos
 WHERE IdCargo=@pmIdCargo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CdTarifaIva,IvaIncluido,CdTarifaRet,CdCuentaIva,CdCuentaRet,CdCuentaIca,CodFEDian,Inactivo,FechaAdd, FechaUpdate, IdUsuario
 FROM Fijos 
 WHERE  (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY IdCargo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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
,FC.IdCuentaIva,PV.NomCuenta AS NomCueIVA,FC.IdCuentaRet,PR.NomCuenta AS NomCueRet,FC.IdCuentaIca
,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
 LEFT JOIN Puc AS PV ON FC.IdCuentaIva=PV.IdCuenta LEFT JOIN Puc AS PR ON FC.IdCuentaRet=PR.IdCuenta
  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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmVrImvCosto,tmCantObseq,tmIvaObseq,tmIvaComb,tmImpCarb,tmTarifaIco,tmVrImpCon
,tmTarifaStc,tmSobtasaCons,tmCodTarIco,tmBaseIvp,tmTarifaIvp,tmIvaIngProd,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
 WHERE  tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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.TdOrden,N.NumOrden AS Num_Orden,N.CdCiaOrden,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexFcr] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmCdTanque,tmSalidas,tmVrPrecio,tmTarifaDct,tmVrDcto
,(tmVrPrecio*tmSalidas)-tmVrDcto AS SubTotal,tmTarifaIva,tmVrIva,tmTarifaIco,tmVrImpCon
,((tmVrPrecio*tmSalidas)-tmVrDcto)+tmVrIva AS ValorTotal,((tmVrPrecio*tmSalidas)-tmVrDcto)+tmVrIva+tmVrImpCon AS ValorNeto
,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,tmMgenCont,tmIvaComb,tmImpCarb
,tmCantObseq,tmIvaObseq,tmBaseIvp,tmTarifaIvp,tmIvaIngProd
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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmTarifaIco,tmVrImpCon,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,tmCantObseq,tmIvaObseq
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc,BaseIvaObsq,VrIvaObsequio,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca,TipoFE,FechaFE
,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,K.TarifaIco,K.VrImpCon AS VrImpuCon
--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,CantImp
,FE.EstadoFE,FE.Prefijo,FE.NumFace,FE.CUFE,FE.Resolucion,FE.RangoNum,FE.FecVigencia,FE.CUFE_QR AS QR,FE.FechaValidacion
 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
 LEFT JOIN Trn_DocMemo AS MM ON F.TipDoc=MM.TipDoc AND F.Factura=MM.Documento AND F.IdCia=MM.IdCia
 LEFT JOIN Trn_Face AS FE ON F.TipDoc=FE.TipDoc AND F.Factura=FE.Documento AND F.IdCia=FE.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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryOpedidoDet] @pmPedido INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,K.IdProducto AS CdProducto,Descripcion,K.IdBodega AS CdBodega,Bodega
,CASE WHEN O.Modalidad='PRODUCCION' THEN (CASE WHEN K.Unidades>0 THEN (K.Unidades-K.galsneto) ELSE (K.Salidas-K.galsneto) END) ELSE Salidas END AS Cantidad
,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
,Salidas,Unidades,galsneto,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
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,IvaIngProd,TipDoc,Documento,IdCia,Referencia2,TarifaIco,VrImpCon
--,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTallasMovPed] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS 
SELECT TM.TipDoc,TM.Documento,TM.IdCia,Compania,TM.Item,TM.Fecha,TM.ItemKdx,TM.IdProducto AS CdProducto,DescripProd
,TM.IdBodega AS CdBodega,Bodega,TM.IdUbic,TM.NumTalla,TM.Entradas,TM.Salidas,TM.CantPedido,TM.CantRem,TM.CantFactura
,TL.Cantidad AS CantSaldo,TL.Pedidos AS CantPed,O.Fecha,O.IdCliente
FROM Trn_TallasMov AS TM INNER JOIN ProdMcias AS P ON TM.IdProducto=P.IdProducto
INNER JOIN Bodegas AS B ON TM.IdBodega=B.IdBodega INNER JOIN Companias AS CN ON TM.IdCia=CN.IdCia
INNER JOIN Trn_Opedido AS O ON TM.TipDoc=O.TipDoc AND TM.Documento=O.Pedido AND TM.IdCia=O.IdCia
LEFT JOIN Trn_Tallajes AS TL ON TM.IdProducto=TL.IdProducto AND TM.IdCia=TL.IdCia AND TM.IdBodega=TL.IdBodega
AND TM.NumTalla=TL.NumTalla
WHERE TM.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (TM.IdCia=@pmIdCia OR @pmIdCia IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,IvaDetCombo,LtaBaseIva,NoAjustes,Tanques,ValesComb,FecUltcom,FecUltVta,CodMcia,DescripLong,Cmntarios,PathFoto,CantTpv,P.IdEstado AS CdEstdo,Estado,P.Inactivo AS Inactvo,TipoZonaFront,ExcluidoImp,Electrocomb,CodFEDian,Tallaje
,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.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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc,BaseIvaObsq,VrIvaObsequio,TarifaArf,VrAutRetFte,TarifaAri,VrAutRetIca,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado
,D.CdMotDev,MotivoDev,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
,FE.EstadoFE,FE.Prefijo,FE.NumFace,FE.CUFE,FE.Resolucion,FE.RangoNum,FE.FecVigencia,FE.CUFE_QR AS QR,FE.FechaValidacion
 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
 LEFT JOIN TiposMotdev AS MD ON D.CdMotDev=MD.IdMotDev
 LEFT JOIN Trn_Face AS FE ON D.TipDev=FE.TipDoc AND D.Devolucion=FE.Documento AND D.IdCia=FE.IdCia
 WHERE TipDev=@pmTipDev  AND Devolucion BETWEEN  @pmDevolucionIni AND @pmDevolucionFin  AND D.IdCia=@pmIdCia
 AND EsProdBase=0
 ORDER BY Devolucion,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmVrImvCosto,tmItemCbo,tmgalsbruto,tmReferencia2
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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.TarifaArf,F.VrAutRetFte,F.TarifaAri,F.VrAutRetIca,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,Tallaje
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmTarifaIco,tmVrImpCon
,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,tmCantObseq,tmIvaObseq,tmBaseIvp,tmTarifaIvp,tmIvaIngProd
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TI.Tarifa,TI.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
,CdTarifIco,TIC.Tarifa AS TarifIco,TIC.Simbolo AS SimbIco,PrecioVenta1,PrecioVenta2,PrecioVenta3,PrecioVenta4,PrecioVenta5
,ISNULL(NV.NivelMin,ExtciaMin) AS NivelMinCia,ISNULL(NV.NivelMax,ExtciaMax) AS NivelMaxCia
--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 LEFT JOIN Tablapor AS TIC ON S.CdTarifIco=TIC.IdTarifa
 LEFT JOIN ProdNiveles AS NV ON LT.IdProducto=NV.IdProducto AND LT.IdCia=NV.IdCia
 WHERE tmEst=@pmtmEst  
 ORDER BY DescripProd
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmImpCarb,tmOtroImpto,tmIvaComb,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTallasMovDoc] @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2)
AS 
SELECT TM.TipDoc,TM.Documento,TM.IdCia,Compania,TM.Item,TM.Fecha,TM.ItemKdx,TM.IdProducto AS CdProducto,DescripProd
,TM.IdBodega AS CdBodega,Bodega,TM.IdUbic,TM.NumTalla,TM.Entradas,TM.Salidas,TM.CantRem,TM.CantFactura
,TL.Cantidad AS CantSaldo,TL.Pedidos AS CantPed
FROM Trn_TallasMov AS TM INNER JOIN ProdMcias AS P ON TM.IdProducto=P.IdProducto
INNER JOIN Bodegas AS B ON TM.IdBodega=B.IdBodega INNER JOIN Companias AS CN ON TM.IdCia=CN.IdCia
LEFT JOIN Trn_Tallajes AS TL ON TM.IdProducto=TL.IdProducto AND TM.IdCia=TL.IdCia AND TM.IdBodega=TL.IdBodega
AND TM.NumTalla=TL.NumTalla
WHERE TM.TipDoc=@pmTipDoc AND TM.Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin AND TM.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryRequisicionOrd] @pmTipDoc VARCHAR(3),@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.Num_Vehic,R.Num_Trailer,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,D.Referencia
--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.TipDoc=@pmTipDoc AND 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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.TdOrden,N.NumOrden AS Num_Orden,N.CdCiaOrden,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmpVehiculo,tmCodTarDct,tmCodTarRet,tmCodTarIca,tmServcios,Tanques,tmTipDoc,tmDocumento,tmIdCia,tmItemCbo AS ItemReq,tmVrImvCosto
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmRemision,tmIdCiaRem
,tmVrImvCosto
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmVrImvCosto,tmCantObseq,tmIvaObseq,tmIvaComb,tmImpCarb
,tmTarifaIco,tmVrImpCon,tmTarifaStc,tmSobtasaCons,tmCodTarIco,tmBaseIvp,tmTarifaIvp,tmIvaIngProd
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryBodegas] @pmIdBodega VARCHAR(4)
 AS 
 SELECT IdBodega,Bodega,Descrip,LtaPre,TipoInv,Inactivo
 FROM Bodegas
 WHERE IdBodega=@pmIdBodega
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexFco] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmSalidas,tmVrPrecio,tmListaPrec,tmTarifaDct,tmVrDcto,tmTarifaIva,tmTarifaIco
,((tmVrPrecio*tmSalidas)-tmVrDcto)+tmVrIva+tmVrImpCon AS ValorTotal,((tmVrPrecio*tmSalidas)-tmVrDcto)+tmVrIva AS ValorNeto
,tmVrIva,tmVrImpCon,tmUnidades,tmIdUnd,Unidad,tmIdBodega,Bodega,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,tmRec_Costo,tmCantObseq,tmIvaObseq
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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmMgenCont
,tmIvaComb,tmImpCarb,tmCdOperario,tmComisnOper,tmCodTarCmc,tmpVehiculo,tmBaseIvp,tmTarifaIvp,tmIvaIngProd,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
 WHERE  tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmVrImvCosto
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,S.IdGrupo AS CdGrupo,G.IdLinea AS CdLinea
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmTipDoc,tmDocumento,tmIdCia,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 tmSalidas>0
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,tmCdOperario,tmComsOper,tmServcios,tmCantDev,tmCantFact
 FROM tm_ForItems AS F INNER JOIN Bodegas AS B ON F.tmIdBodega=B.IdBodega
 WHERE tmNumero=@pmtmNumero
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdConsolida_Cr] @pmTipDoc VARCHAR(3),@pmNumConsIni INT,@pmNumConsFin INT,@pmIdCia CHAR(2)
AS 
 SELECT C.TipDoc,TipoDoc,C.NumCons,C.IdCia,Compania,C.Fecha,C.IdBodega,B.Bodega,C.CdBodDest,BD.Bodega AS BodDestino,C.IdBodIns,BU.Bodega AS BodInsumo,C.CdBodInsDest,BDI.Bodega AS BodDestInsumo
,C.Anulado,C.FecDev,C.CritPedidos,PT.NumTdoProd,PT.NumTdoInsu,C.Observacion,C.IdEstado,Estado,C.TimeSys AS FecCrea,C.FecUpdate AS FecActualiza,C.OrigenAdd,C.IdCiaCrea,C.IdUsuario,Usuario
--Detalles
,D.Item,D.TipPed,D.Pedido,D.IdCiaPed,D.ItemPed,D.IdProducto AS CodProducto,P.DescripProd,D.IdBodega AS CodBodega,BG.Bodega AS DetBodega,D.CdUbic,D.CantPedido,D.CantAprob,D.Existencias
,D.CantSaldo,D.CantTdo,D.CdBodTdo,BT.Bodega AS DetBodegaDest,D.Tallas,D.IdUnd,UD.Unidad
,P.CodBarras,P.Referencia,P.TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo
,P.IdMarca,Marca,P.Color,P.Tamano,P.Tallaje,P.Seriales,P.Lotes,P.Combo,P.Inactivo AS ProdInactivo,P.DescripLong
FROM Trn_ProdConsolida AS C INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia 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 Bodegas AS B ON C.IdBodega=B.IdBodega
INNER JOIN Trn_ProdConsDet AS D ON C.TipDoc=D.TipDoc AND C.NumCons=D.NumCons AND C.IdCia=D.IdCia
INNER JOIN ProdMcias AS P ON D.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 Bodegas AS BG ON D.IdBodega=BG.IdBodega
INNER JOIN UndMed AS UD ON D.IdUnd=UD.IdUnd 
LEFT JOIN Bodegas AS BD ON C.CdBodDest=BD.IdBodega
LEFT JOIN Bodegas AS BU ON C.IdBodIns=BU.IdBodega LEFT JOIN Bodegas AS BDI ON C.CdBodInsDest=BDI.IdBodega
LEFT JOIN Bodegas AS BT ON D.CdBodTdo=BT.IdBodega
LEFT JOIN (SELECT TipDoc,NumCons,IdCia,MAX(Traslado) AS NumTdoProd,MAX(NumTdoIns) AS NumTdoInsu FROM Trn_ProdConsPed GROUP BY TipDoc,NumCons,IdCia)
AS PT ON C.TipDoc=PT.TipDoc AND C.NumCons=PT.NumCons AND C.IdCia=PT.IdCia
WHERE C.TipDoc=@pmTipDoc AND C.NumCons BETWEEN @pmNumConsIni AND @pmNumConsFin AND C.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NumDotac,DiaVigDot,FecVigDot
,EdoDocEnt,NumDocEnt,CiaDocEnt,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NumRef2,F.Observacion AS Observ,CantPuntos,TipDcm,Documento,IdCiaDcm,FechaDcm,AgencDcm
,Anulado,NumDev,FecDev,Planilla,F.IdCia AS CdCia,Compania,F.VrPrecio AS VrPrecioUnd,F.VrBase,F.VrPrecioEsp
--detalles
,D.Item AS nItem,D.IdProducto AS CdProducto,DescripProd,D.IdBodega AS CdBodega,Bodega,D.Cantidad AS Cant,D.VrPrecio,TarifaIva,VrIva,TarifaDct,VrDcto
,Servicios,Descripcion,ListaPrec,PM.IdUnd AS CdUnid,Unidad,CdCCosto,CdSubCos,D.CantDev,D.CantFact,D.CdOperario,OP.RazonSocial AS Operario,D.Comision
 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
 LEFT JOIN Terceros AS OP ON D.CdOperario=OP.IdTercero
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,FCodBodega,FB.Bodega AS InvBodega,FCodMarca,FM.Marca AS InvMarca,FCodSubGrupo,FS.Subgrupo AS InvSubgrupo
,F.TimeSys,F.FecUpdate,F.IdCiaCrea,F.IdUsuario AS CdUsuarioCrea,U.Usuario AS UsuarioCrea
--Detalles de inventario
,Item,D.IdProducto AS CodProducto,DescripProd,D.IdBodega AS CdBodega,B.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
,B.Descrip AS DesBodega,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,S.Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,M.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
 LEFT JOIN Marcas AS FM ON F.FCodMarca=FM.IdMarca LEFT JOIN Bodegas AS FB ON F.FCodBodega=FB.IdBodega
 LEFT JOIN SubGrupos AS FS ON F.FCodSubGrupo=FS.IdSubgrupo
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTallajesLta]
 AS  
SELECT T.IdProducto,DescripProd,T.IdCia,Compania,T.IdBodega,Bodega,T.IdUbic,T.NumTalla,T.Cantidad,T.Pedidos,S.SaldoActual 
,P.TipoRef,P.Referencia,P.CodBarras,G.IdLinea,Linea,SG.IdGrupo,Grupo,P.IdSubgrupo,Subgrupo,P.IdMarca,Marca,P.Tamano,P.Color
,P.UndMed,UM.Unidad AS DescUndMed,P.IdUnd,U.Unidad,P.Tallaje,P.Inactivo
FROM Trn_Tallajes AS T INNER JOIN ProdMcias AS P ON T.IdProducto=P.IdProducto
INNER JOIN Bodegas AS B ON T.IdBodega=B.IdBodega INNER JOIN Companias AS CI ON T.IdCia=CI.IdCia
INNER JOIN SubGrupos AS SG ON P.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.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 U ON P.IdUnd=U.IdUnd 
LEFT JOIN Sys_Um AS UM ON P.UndMed=UM.UndMed
LEFT JOIN ProdSaldos AS S ON T.IdProducto=S.IdProducto AND T.IdBodega=S.IdBodega AND T.IdCia=S.IdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CC.CCosto,R.IdSubCos AS CdSubCent,SC.SubCosto,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,NivelAprob,OrigenAdd,Anulado,NomContacto,TelsContacto,EmailContacto,Num_Vehic,Num_Trailer,TipoVigencia,FecDev,R.Observacion AS Observ
,R.IdEstado AS CdEstado,Estado,R.TimeSys,R.FecUpdate,R.IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario
,D.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,D.Referencia,D.CantSalida,D.CantDevSal,D.Cant_Orden,CodCCtoDet,CCD.CCosto AS DetCentro,CodSCtoDet,SCD.SubCosto AS DetSubCosto,D.PlacaVehDet
 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
 LEFT JOIN SubCentros AS SC ON R.IdSubCos=SC.IdSubCos
 LEFT JOIN CentroCosto AS CCD ON D.CodCCtoDet=CCD.IdCCosto
 LEFT JOIN SubCentros AS SCD ON D.CodSCtoDet=SCD.IdSubCos
 WHERE R.TipDoc=@pmTipDoc AND R.Requisicion BETWEEN  @pmRequisicionIni AND @pmRequisicionFin 
 AND R.IdCia=@pmIdCia 
 ORDER BY  R.Requisicion,Item
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryEstadoOper] @pmIdEstado VARCHAR(4)
 AS 
 IF @pmIdEstado IS NULL
  SELECT IdEstado,Estado,NColor
  FROM EstadoOper
  WHERE Inactivo=0
 ELSE
  SELECT IdEstado,Estado,NColor,Inactivo
  FROM EstadoOper
  WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpEstadoOper] @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 UPDATE EstadoOper SET Estado=@pmEstado,NColor=@pmNColor,Inactivo=@pmInactivo
 WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTercerosDso] @pmIdEstado 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,RegimenFE
,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,ET.Estado,T.Inactivo AS Inactvo,FechaAdd,FechaUpdate,T.IdUsuario AS IdUsario,Usuario,IniStgNom,ET.NColor,ImgFoto,ImgFirma,ImagenDoc1,ImagenDoc2,ImagenDoc3,FechaNac
,T.IdEstOper AS CdEstOper,EO.Estado AS EstOper,IdLocUbic,DescUbicac,CdGrupoTerc
 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 LEFT JOIN EstadoOper AS EO ON T.IdEstOper=EO.IdEstado
 WHERE (@pmIdEstado IS NULL OR T.IdEstado=@pmIdEstado)
ORDER BY RazonSocial
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTercerosUbi] @pmIdEst CHAR(2),@pmTipoRep INT,@pmFecInicio SMALLDATETIME,@pmIdEstOper VARCHAR(4)=Null
 AS 
 SELECT T.IdTercero,T.Dv,T.TipoId,T.RazonSocial,T.Codigo,T.Direccion AS Dreccion,T.IdLocal AS CodCiud,L.Localidad AS Ciudad,D.Departamento AS Departmto
,T.Telefono,T.Fax,T.TelMovil,T.SitioWeb,T.e_mail,T.EsVendedor,T.EsConductor,T.EsEmpleado,T.EsOperario,T.IdSector AS CodSector,SectorEco
,T.IdProf AS CodProf,Profesion,T.IdRegimen AS IdRegmen,Regimen,T.IdLugarCed,LC.Localidad AS LugarCedula,T.FecExpCed
,T.Observacion,T.IdEstOper AS CdEstOper,ET.Estado,ET.NColor,T.FechaAdd AS FechaCrea,T.FechaUpdate,T.FechaNac,T.IdLocUbic,LU.Localidad AS CiudadUbic,T.DescUbicac
,TM.TipoOrd,TM.NumOrden,TM.IdCiaOrd,TM.Remesa,TM.IdCiaRem,R.Fecha AS FecOrden,R.IdCliente,C.RazonSocial AS NomCliente,IdLocOrigen,CO.Localidad AS CiuOrigen,IdLocDestino,CD.Localidad AS CiuDestino
,TM.IdVehiculo,TM.nRemolque,R.IdConductor AS CedConductor,NC.RazonSocial AS Conductor
 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 RG ON T.IdRegimen=RG.IdRegimen
 INNER JOIN Localidades AS LC ON T.IdLugarCed=LC.IdLocal INNER JOIN Profesiones AS PF ON T.IdProf=PF.IdProf 
 LEFT JOIN EstadoOper AS ET ON T.IdEstOper=ET.IdEstado LEFT JOIN Localidades AS LU ON T.IdLocUbic=LU.IdLocal
 LEFT JOIN (SELECT IdOperario,IdVehiculo,nRemolque,TipoOrd,NumOrden,IdCiaOrd,Remesa,IdCiaRem FROM tm_TraInfOper WHERE IdEst=@pmIdEst AND TipoRep=@pmTipoRep) AS TM ON T.IdTercero=TM.IdOperario
 LEFT JOIN (SELECT TipDoc,NumOrden,IdCia,Fecha,FecDespacho,IdCliente,IdAgencia,IdLocOrigen,IdLocDestino,IdVehiculo,IdConductor,nRemolque
 FROM Trn_TraRemesa WHERE TipDoc='OCT' AND Fecha>=@pmFecInicio
 UNION ALL SELECT TipDoc,NumOrden,IdCia,Fecha,FecDespacho,IdCliente,IdAgencia,IdDestino,IdDestino AS CiuDstno,IdVehiculo,IdConductor,nRemolque
 FROM Trn_TraOrdenServ WHERE TipDoc='OSA' AND Fecha>=@pmFecInicio) AS R ON TM.TipoOrd=R.TipDoc AND TM.NumOrden=R.NumOrden AND TM.IdCiaOrd=R.IdCia
 LEFT JOIN Terceros AS C ON R.IdCliente=C.IdTercero LEFT JOIN Localidades AS CO ON R.IdLocOrigen=CO.IdLocal LEFT JOIN Localidades AS CD ON R.IdLocDestino=CD.IdLocal
 LEFT JOIN Terceros AS NC ON R.IdConductor=NC.IdTercero
WHERE T.Inactivo=0 AND (@pmIdEstOper IS NULL OR T.IdEstOper=@pmIdEstOper)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelEstadoOper] @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM EstadoOper WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsEstadoOper] @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 INSERT INTO EstadoOper (IdEstado,Estado,NColor,Inactivo)
 VALUES (@pmIdEstado,@pmEstado,@pmNColor,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMudOrdenServ] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmIdTipoServ VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVend VARCHAR(16),@pmDirCargue VARCHAR(500),@pmTipoInmRem VARCHAR(30),@pmPisoCargue VARCHAR(10),@pmTelsRemite VARCHAR(30),@pmCelRemite VARCHAR(30),@pmDirDescargue VARCHAR(500)
,@pmTipoInmDest VARCHAR(30),@pmPisoDescargue VARCHAR(10),@pmTelsDest VARCHAR(30),@pmCelDest VARCHAR(30),@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmVrServicio MONEY,@pmVrEmpaque MONEY,@pmVrBodega MONEY,@pmVrOtros MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrNeto MONEY,@pmVrDeclarado MONEY,@pmVrSeguro MONEY,@pmServicios VARCHAR(50),@pmFormaPago VARCHAR(50),@pmReferencia VARCHAR(50)
,@pmTipInv VARCHAR(3),@pmNumInvent INT,@pmIdCiaInv CHAR(2),@pmTipRem VARCHAR(3),@pmNumRemesa INT,@pmIdCiaRem CHAR(2),@pmEstFactura INT,@pmTipFact VARCHAR(3),@pmNumFactura INT,@pmCdCiaFact CHAR(2),@pmTipCausac VARCHAR(3),@pmCausacion INT,@pmCdCiaCau CHAR(2),@pmNomContacto VARCHAR(150),@pmTelsContacto VARCHAR(30),@pmemlContacto VARCHAR(100),@pmContactoDest VARCHAR(150),@pmemlContDest VARCHAR(100)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2500),@pmIdEstado VARCHAR(4),@pmEstCumplido INT,@pmFechaCump SMALLDATETIME,@pmTipoRuta VARCHAR(10),@pmVolumen VARCHAR(50),@pmEstadoBod INT,@pmCdBodega VARCHAR(4),@pmFechaIngBod SMALLDATETIME,@pmFechaRetBod SMALLDATETIME,@pmNitRemitente VARCHAR(16),@pmRemitente VARCHAR(150),@pmNitDestinatario VARCHAR(16),@pmDestinatario VARCHAR(150)
,@pmNumAutCupo INT,@pmNumAutMora INT,@pmReferencias VARCHAR(1000),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_MudOrdenServ SET Fecha=@pmFecha,FecDespacho=@pmFecDespacho,IdTipoServ=@pmIdTipoServ,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdVend=@pmIdVend,DirCargue=@pmDirCargue,TipoInmRem=@pmTipoInmRem,PisoCargue=@pmPisoCargue,TelsRemite=@pmTelsRemite,CelRemite=@pmCelRemite,DirDescargue=@pmDirDescargue,TipoInmDest=@pmTipoInmDest,PisoDescargue=@pmPisoDescargue,TelsDest=@pmTelsDest,CelDest=@pmCelDest
,IdOrigen=@pmIdOrigen,IdDestino=@pmIdDestino,VrServicio=@pmVrServicio,VrEmpaque=@pmVrEmpaque,VrBodega=@pmVrBodega,VrOtros=@pmVrOtros,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrNeto=@pmVrNeto,VrDeclarado=@pmVrDeclarado,VrSeguro=@pmVrSeguro,Servicios=@pmServicios,FormaPago=@pmFormaPago,Referencia=@pmReferencia,TipInv=@pmTipInv,NumInvent=@pmNumInvent,IdCiaInv=@pmIdCiaInv,TipRem=@pmTipRem,NumRemesa=@pmNumRemesa,IdCiaRem=@pmIdCiaRem
,EstFactura=@pmEstFactura,TipFact=@pmTipFact,NumFactura=@pmNumFactura,CdCiaFact=@pmCdCiaFact,TipCausac=@pmTipCausac,Causacion=@pmCausacion,CdCiaCau=@pmCdCiaCau,NomContacto=@pmNomContacto,TelsContacto=@pmTelsContacto,emlContacto=@pmemlContacto,ContactoDest=@pmContactoDest,emlContDest=@pmemlContDest,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,EstCumplido=@pmEstCumplido
,FechaCump=@pmFechaCump,TipoRuta=@pmTipoRuta,Volumen=@pmVolumen,EstadoBod=@pmEstadoBod,CdBodega=@pmCdBodega,FechaIngBod=@pmFechaIngBod,FechaRetBod=@pmFechaRetBod,NitRemitente=@pmNitRemitente,Remitente=@pmRemitente,NitDestinatario=@pmNitDestinatario,Destinatario=@pmDestinatario,NumAutCupo=@pmNumAutCupo,NumAutMora=@pmNumAutMora,Referencias=@pmReferencias,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenServ] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumOrden,IdCia,Fecha,FecDespacho,IdTipoServ,IdCliente,IdAgencia,IdVend,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest,IdOrigen,IdDestino,VrServicio,VrEmpaque,VrBodega,VrOtros,VrDescuento,VrImpuesto,VrNeto,VrDeclarado,VrSeguro,Servicios,FormaPago,Referencia,TipInv,NumInvent,IdCiaInv,TipRem,NumRemesa,IdCiaRem,EstFactura,TipFact,NumFactura,CdCiaFact
,TipCausac,Causacion,CdCiaCau,NomContacto,TelsContacto,emlContacto,ContactoDest,emlContDest,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,EstCumplido,FechaCump,TipoRuta,Volumen,EstadoBod,CdBodega,FechaIngBod,FechaRetBod,NitRemitente,Remitente,NitDestinatario,Destinatario,NumAutCupo,NumAutMora,Referencias,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_MudOrdenServ
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenConcLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT O.TipDoc,O.NumOrden,O.IdCia,O.FecDespacho,O.IdCliente,D.Item,D.IdConcepto AS CdConcepto,D.Descripcion,CD.Concepto,D.ValorTotal,D.TarifaIva,D.TipoConc,D.TipoRubro
,D.CdCuenta,D.NitTercero,NT.RazonSocial AS NomTercero,D.Referencia,D.NumDocRef,D.TipoReg,D.TarifSeguro,D.CodTarSeg,D.PlacaVehic,D.TarifaPago
,D.NumDocFac,D.IndFactItem,D.FechaReg,D.CdUsuario,U.Usuario,CD.RubroConc,CD.MudReajuste
--datos de facturas
,FC.FacNumero,FC.FacFecha,ISNULL(FC.FacValor,0) AS FacVrTotal,ISNULL(FC.FacCosto,0) AS FacVrCosto,ISNULL(FC.FacImpuesto,0) AS FacVrIva,ISNULL(FC.FacSeguro,0) AS FacVrSeguro
,FacBaseIngreso,DfcBaseIngreso,FacReajuste,DfcReajuste
 FROM Trn_MudOrdenServ AS O INNER JOIN Trn_MudOrdenConc AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
 INNER JOIN ConcDiversos AS CD ON D.IdConcepto=CD.IdConcepto
 LEFT JOIN Terceros AS NT ON D.NitTercero=NT.IdTercero LEFT JOIN adm_Usuarios AS U ON D.CdUsuario=U.IdUsuario
 --Facturas item por item
 LEFT JOIN (SELECT TipRem AS FacTipOrd,Remesa AS FacNumOrden,IdCiaRem AS FacIdCiaOrd,ItemRem AS FacItemOrd,MAX(FecRemesa) AS FacFecha
,SUM(CASE WHEN SUBSTRING(FR.TipDoc,1,2)='DF' THEN (Cantidad*VrUnitario)*-1 ELSE Cantidad*VrUnitario END) AS FacValor
,SUM(CASE WHEN SUBSTRING(FR.TipDoc,1,2)='DF' THEN (Cantidad*VrCosto)*-1 ELSE Cantidad*VrCosto END) AS FacCosto
,SUM(CASE WHEN SUBSTRING(FR.TipDoc,1,2)='DF' THEN VrImpuesto*-1 ELSE VrImpuesto END) AS FacImpuesto
,SUM(CASE WHEN SUBSTRING(FR.TipDoc,1,2)='DF' THEN VrSeguroRem*-1 ELSE VrSeguroRem END) AS FacSeguro
,SUM(CASE WHEN SUBSTRING(FR.TipDoc,1,2)='FC' THEN VrDeclMcia ELSE 0 END) AS FacDeclarado
,MAX(CASE WHEN SUBSTRING(FR.TipDoc,1,2)='FC' THEN FR.Factura ELSE 0 END) AS FacNumero
,SUM(CASE WHEN FR.Referencia3='TRANSPORTE' AND SUBSTRING(FR.TipDoc,1,2)='FC' THEN Cantidad*VrUnitario ELSE 0 END) AS FacBaseIngreso
,SUM(CASE WHEN FR.Referencia3='TRANSPORTE' AND SUBSTRING(FR.TipDoc,1,2)='DF' THEN (Cantidad*VrUnitario)*-1 ELSE 0 END) AS DfcBaseIngreso
,SUM(CASE WHEN FR.Referencia3='TRANSPORTE' AND C.MudReajuste=1 AND SUBSTRING(FR.TipDoc,1,2)='FC' THEN Cantidad*VrUnitario ELSE 0 END) AS FacReajuste
,SUM(CASE WHEN FR.Referencia3='TRANSPORTE' AND C.MudReajuste=1 AND SUBSTRING(FR.TipDoc,1,2)='DF' THEN Cantidad*VrUnitario ELSE 0 END) AS DfcReajuste
 FROM Trn_TraFacRemesas AS FR LEFT JOIN ConcDiversos AS C ON FR.CdConcepto=C.IdConcepto
 WHERE FR.TipRem='OSM' GROUP BY TipRem,Remesa,IdCiaRem,ItemRem) AS FC ON D.TipDoc=FC.FacTipOrd AND D.NumOrden=FC.FacNumOrden AND D.IdCia=FC.FacIdCiaOrd AND D.Item=FC.FacItemOrd
 WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudOrdenNovRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS 
SELECT O.TipDoc,O.NumOrden,O.IdCia,O.FecDespacho,O.IdCliente,N.Item,N.Fecha,N.TipoNov,N.Descripcion,N.EstadoBod,N.CdBodAnt,BA.Bodega AS BodAnterior,N.CdBodega,B.Bodega
,N.FechaIngBod,N.FechaRetBod,N.CdConcepto,CD.Concepto,N.ValorTotal,N.Referencia,N.IdUsuario,Usuario,N.FechaCrea
FROM Trn_MudOrdenServ AS O INNER JOIN Trn_MudOrdenNov AS N ON O.TipDoc=N.TipDoc AND O.NumOrden=N.NumOrden AND O.IdCia=N.IdCia
INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario 
LEFT JOIN ConcDiversos AS CD ON N.CdConcepto=CD.IdConcepto
LEFT JOIN AlmBodegas AS B ON N.CdBodega=B.IdBodega LEFT JOIN AlmBodegas AS BA ON N.CdBodAnt=BA.IdBodega
WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMudOrdenServ] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmIdTipoServ VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVend VARCHAR(16),@pmDirCargue VARCHAR(500),@pmTipoInmRem VARCHAR(30),@pmPisoCargue VARCHAR(10),@pmTelsRemite VARCHAR(30),@pmCelRemite VARCHAR(30),@pmDirDescargue VARCHAR(500)
,@pmTipoInmDest VARCHAR(30),@pmPisoDescargue VARCHAR(10),@pmTelsDest VARCHAR(30),@pmCelDest VARCHAR(30),@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmVrServicio MONEY,@pmVrEmpaque MONEY,@pmVrBodega MONEY,@pmVrOtros MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrNeto MONEY,@pmVrDeclarado MONEY,@pmVrSeguro MONEY,@pmServicios VARCHAR(50),@pmFormaPago VARCHAR(50),@pmReferencia VARCHAR(50)
,@pmTipInv VARCHAR(3),@pmNumInvent INT,@pmIdCiaInv CHAR(2),@pmTipRem VARCHAR(3),@pmNumRemesa INT,@pmIdCiaRem CHAR(2),@pmEstFactura INT,@pmTipFact VARCHAR(3),@pmNumFactura INT,@pmCdCiaFact CHAR(2),@pmTipCausac VARCHAR(3),@pmCausacion INT,@pmCdCiaCau CHAR(2),@pmNomContacto VARCHAR(150),@pmTelsContacto VARCHAR(30),@pmemlContacto VARCHAR(100),@pmContactoDest VARCHAR(150),@pmemlContDest VARCHAR(100)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2500),@pmIdEstado VARCHAR(4),@pmEstCumplido INT,@pmFechaCump SMALLDATETIME,@pmTipoRuta VARCHAR(10),@pmVolumen VARCHAR(50),@pmEstadoBod INT,@pmCdBodega VARCHAR(4),@pmFechaIngBod SMALLDATETIME,@pmFechaRetBod SMALLDATETIME,@pmNitRemitente VARCHAR(16),@pmRemitente VARCHAR(150),@pmNitDestinatario VARCHAR(16),@pmDestinatario VARCHAR(150)
,@pmNumAutCupo INT,@pmNumAutMora INT,@pmReferencias VARCHAR(1000),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_MudOrdenServ (TipDoc,NumOrden,IdCia,Fecha,FecDespacho,IdTipoServ,IdCliente,IdAgencia,IdVend,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest,IdOrigen,IdDestino,VrServicio,VrEmpaque,VrBodega,VrOtros,VrDescuento,VrImpuesto,VrNeto,VrDeclarado,VrSeguro,Servicios,FormaPago,Referencia,TipInv,NumInvent,IdCiaInv
,TipRem,NumRemesa,IdCiaRem,EstFactura,TipFact,NumFactura,CdCiaFact,TipCausac,Causacion,CdCiaCau,NomContacto,TelsContacto,emlContacto,ContactoDest,emlContDest,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,EstCumplido,FechaCump,TipoRuta,Volumen,EstadoBod,CdBodega,FechaIngBod,FechaRetBod,NitRemitente,Remitente,NitDestinatario,Destinatario,NumAutCupo,NumAutMora,Referencias)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmFecha,@pmFecDespacho,@pmIdTipoServ,@pmIdCliente,@pmIdAgencia,@pmIdVend,@pmDirCargue,@pmTipoInmRem,@pmPisoCargue,@pmTelsRemite,@pmCelRemite,@pmDirDescargue,@pmTipoInmDest,@pmPisoDescargue,@pmTelsDest,@pmCelDest,@pmIdOrigen,@pmIdDestino,@pmVrServicio,@pmVrEmpaque,@pmVrBodega,@pmVrOtros,@pmVrDescuento,@pmVrImpuesto,@pmVrNeto,@pmVrDeclarado,@pmVrSeguro
,@pmServicios,@pmFormaPago,@pmReferencia,@pmTipInv,@pmNumInvent,@pmIdCiaInv,@pmTipRem,@pmNumRemesa,@pmIdCiaRem,@pmEstFactura,@pmTipFact,@pmNumFactura,@pmCdCiaFact,@pmTipCausac,@pmCausacion,@pmCdCiaCau,@pmNomContacto,@pmTelsContacto,@pmemlContacto,@pmContactoDest,@pmemlContDest,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmEstCumplido,@pmFechaCump
,@pmTipoRuta,@pmVolumen,@pmEstadoBod,@pmCdBodega,@pmFechaIngBod,@pmFechaRetBod,@pmNitRemitente,@pmRemitente,@pmNitDestinatario,@pmDestinatario,@pmNumAutCupo,@pmNumAutMora,@pmReferencias)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_Caja_Aos] @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,A.TipDoc,A.Anticipo,A.IdCia,A.Fecha,A.IdConcepto,O.IdPoseedor,O.IdAgencia,A.VrAnticipo,0,0,0,0,0,0,0,0,A.VrAnticipo AS ValNeto,CASE A.TipoPago WHEN 'EFECTIVO' THEN A.VrAnticipo ELSE 0 END
,0,0,0,0,0,0,0,CASE A.TipoPago WHEN 'EFECTIVO' THEN 1 ELSE 0 END,CASE A.TipoPago WHEN 'EFECTIVO' THEN 'EFE' ELSE 'CHE' END,A.NumCheque,IdBanco,SUBSTRING(A.Beneficiario,1,150),A.IdCta,A.FecCheque,ISNULL(A.CedBenef,'0'),0,'0',A.IdUsuario,'0','ANTICIPOS',''
,'EGRESO',O.IdVehiculo,A.TipOds,A.NumOrden,A.IdCiaOds,A.TipCom,A.Comprobante,A.IdCiaCom,A.Anulado,A.NumDev,A.FecDev,A.Observacion,A.TimeSys
 FROM Trn_TraOrdenAnt AS A INNER JOIN Trn_TraOrdenServ AS O ON A.TipOds=O.TipDoc AND A.NumOrden=O.NumOrden AND A.IdCiaOds=O.IdCia
 LEFT JOIN CtasCorrientes AS CT ON A.IdCta=CT.IdCta
 WHERE A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND A.TipoPago='EFECTIVO'
 AND A.IdCia LIKE ISNULL(@pmIdCia,'%%') AND A.IdUsuario LIKE ISNULL(@pmIdCajero,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenAntTotCaj]  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
AS
SELECT TipDoc,SUM(VrAnticipo) AS TotalAnt
FROM Trn_TraOrdenAnt 
WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipoPago='EFECTIVO'
AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GROUP BY TipDoc
UNION ALL
SELECT D.TipDev,SUM(D.VrDevolucion)
FROM Trn_TraDevAos AS D INNER JOIN Trn_TraOrdenAnt AS A ON D.TipDoc=A.TipDoc AND D.Anticipo=A.Anticipo AND D.IdCiaDoc=A.IdCia
WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND A.TipoPago='EFECTIVO'
AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GROUP BY D.TipDev
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraOrdenAntAnu] @pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCia CHAR(2),@pmVrAbonado MONEY
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_TraOrdenAnt SET VrAbonado=@pmVrAbonado,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Anticipo=@pmAnticipo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_Caja_Dao] @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,D.TipDev,D.Devolucion,D.IdCia,D.Fecha,D.IdConcepto,O.IdPoseedor,O.IdAgencia,D.VrDevolucion,0,0,0,0,0,0,0,0,D.VrDevolucion AS ValNeto,CASE A.TipoPago WHEN 'EFECTIVO' THEN D.VrDevolucion ELSE 0 END
,0,0,0,0,0,0,0,CASE A.TipoPago WHEN 'EFECTIVO' THEN 1 ELSE 0 END,CASE A.TipoPago WHEN 'EFECTIVO' THEN 'EFE' ELSE 'CHE' END,A.NumCheque,IdBanco,SUBSTRING(A.Beneficiario,1,150),D.CdCta,A.FecCheque,ISNULL(A.CedBenef,'0'),0,'0',D.IdUsuario,'0','ANTICIPOS',''
,'EGRESO',O.IdVehiculo,A.TipOds,A.NumOrden,A.IdCiaOds,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Anticipo,D.FecDoc,D.Observacion,D.TimeSys
 FROM Trn_TraDevAos AS D INNER JOIN Trn_TraOrdenAnt AS A ON D.TipDoc=A.TipDoc AND D.Anticipo=A.Anticipo AND D.IdCiaDoc=A.IdCia
 INNER JOIN Trn_TraOrdenServ AS O ON A.TipOds=O.TipDoc AND A.NumOrden=O.NumOrden AND A.IdCiaOds=O.IdCia
 LEFT JOIN CtasCorrientes AS CT ON D.CdCta=CT.IdCta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND A.TipoPago='EFECTIVO'
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdUsuario LIKE ISNULL(@pmIdCajero,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncOsaListaAntcipos] (@pmNumOrden INT,@pmIdCia CHAR(2))
RETURNS VARCHAR(250)
 AS  
BEGIN 
 DECLARE @Anticipos VARCHAR(250)
 DECLARE @Anticipo INT
 DECLARE TraAnticipos CURSOR FOR 
 SELECT Anticipo FROM Trn_TraOrdenAnt WHERE NumOrden=@pmNumOrden AND IdCiaOds=@pmIdCia AND Anulado=0
 SET @Anticipos=''
 OPEN TraAnticipos
 FETCH NEXT FROM TraAnticipos  
 INTO @Anticipo
 WHILE @@FETCH_STATUS=0
  BEGIN
   SET @Anticipos=@Anticipos+CAST(@Anticipo AS VARCHAR(10))+','
   FETCH NEXT FROM TraAnticipos  
   INTO @Anticipo
  END
 CLOSE TraAnticipos
 DEALLOCATE TraAnticipos
 RETURN @Anticipos
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenAntLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT Anticipo,A.IdCia AS CdCia,Compania,A.Fecha AS FecAnt,A.IdConcepto AS CdConcepto,Concepto,TipOds,A.NumOrden,IdCiaOds,O.Fecha AS FecOrden,O.IdVehiculo AS PlacaVeh,nRemolque
,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,O.IdCliente AS NitCliente,T.RazonSocial AS NomCliente
,O.IdAgencia AS CdAgencia,Agencia,A.VrAnticipo,VrAbonado,VrTotal,O.Cantidad,TipoPago,NumCheque,FecCheque,A.IdCta AS CdCta,NumeroCta,CT.IdBanco,Banco
,A.IdCuenta AS CdCuenta,CedBenef,Beneficiario,FechaVence,TipoAncpo,NumPresAnt,A.Cantidad AS CantGalones,TipCom,Comprobante,IdCiaCom,A.Anulado AS Anuldo,A.NumDev,A.FecDev,A.Observacion AS Observ
,A.IdEstado AS CdEstado,Estado,A.OrigenAdd,A.TimeSys AS FechaCrea,A.FecUpdate AS FechaModif,A.IdCiaCrea AS CdCiaCrea,A.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraOrdenAnt AS A INNER JOIN Trn_TraOrdenServ AS O ON A.TipOds=O.TipDoc AND A.NumOrden=O.NumOrden AND A.IdCiaOds=O.IdCia
 INNER JOIN Companias AS CN ON A.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON A.IdConcepto=C.IdConcepto INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero
 INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero
 LEFT JOIN CtasCorrientes AS CT ON A.IdCta=CT.IdCta LEFT JOIN Bancos AS B ON CT.IdBanco=B.IdBanco
 LEFT JOIN Agencias AS AG ON O.IdAgencia=AG.IdAgencia 
 WHERE A.TipDoc=@pmTipDoc AND A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraOrdenAnt] @pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipOds VARCHAR(3),@pmNumOrden INT,@pmIdCiaOds CHAR(2),@pmVrAnticipo MONEY,@pmVrAbonado MONEY,@pmTipoPago VARCHAR(10),@pmNumCheque VARCHAR(20),@pmFecCheque SMALLDATETIME,@pmIdCta VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmBeneficiario VARCHAR(150),@pmCedBenef VARCHAR(16),@pmFechaVence SMALLDATETIME
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTipoAncpo VARCHAR(10),@pmNumPresAnt INT,@pmCantidad DECIMAL(14,4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraOrdenAnt (TipDoc,Anticipo,IdCia,Fecha,IdConcepto,TipOds,NumOrden,IdCiaOds,VrAnticipo,VrAbonado,TipoPago,NumCheque,FecCheque,IdCta,IdCuenta,Beneficiario,CedBenef,FechaVence,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario,TipoAncpo,NumPresAnt,Cantidad)
 VALUES (@pmTipDoc,@pmAnticipo,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipOds,@pmNumOrden,@pmIdCiaOds,@pmVrAnticipo,@pmVrAbonado,@pmTipoPago,@pmNumCheque,@pmFecCheque,@pmIdCta,@pmIdCuenta,@pmBeneficiario,@pmCedBenef,@pmFechaVence,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmTipoAncpo,@pmNumPresAnt,@pmCantidad)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraOrdenAnt] @pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraOrdenAnt
 WHERE TipDoc=@pmTipDoc AND Anticipo=@pmAnticipo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenAnt] @pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Anticipo,IdCia,Fecha,IdConcepto,TipOds,NumOrden,IdCiaOds,VrAnticipo,VrAbonado,TipoPago,NumCheque,FecCheque,IdCta,IdCuenta,Beneficiario,CedBenef,FechaVence,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion
,IdEstado,TipoAncpo,NumPresAnt,Cantidad,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraOrdenAnt
 WHERE TipDoc=@pmTipDoc AND Anticipo=@pmAnticipo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraOrdenAnt] @pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipOds VARCHAR(3),@pmNumOrden INT,@pmIdCiaOds CHAR(2),@pmVrAnticipo MONEY,@pmVrAbonado MONEY,@pmTipoPago VARCHAR(10),@pmNumCheque VARCHAR(20),@pmFecCheque SMALLDATETIME,@pmIdCta VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmBeneficiario VARCHAR(150),@pmCedBenef VARCHAR(16),@pmFechaVence SMALLDATETIME
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTipoAncpo VARCHAR(10),@pmNumPresAnt INT,@pmCantidad DECIMAL(14,4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraOrdenAnt SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipOds=@pmTipOds,NumOrden=@pmNumOrden,IdCiaOds=@pmIdCiaOds,VrAnticipo=@pmVrAnticipo,VrAbonado=@pmVrAbonado,TipoPago=@pmTipoPago,NumCheque=@pmNumCheque,FecCheque=@pmFecCheque,IdCta=@pmIdCta,IdCuenta=@pmIdCuenta,Beneficiario=@pmBeneficiario,CedBenef=@pmCedBenef,FechaVence=@pmFechaVence,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom
,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,TipoAncpo=@pmTipoAncpo,NumPresAnt=@pmNumPresAnt,Cantidad=@pmCantidad
 WHERE TipDoc=@pmTipDoc AND Anticipo=@pmAnticipo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevAosLta] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS 
 SELECT Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc,D.Anticipo AS NumAnticipo,IdCiaDoc,FecDoc
,VrDevolucion,A.VrAnticipo,A.VrAbonado,A.VrAnticipo-A.VrAbonado AS SaldoActual,TipoPago,NumCheque,FecCheque,CdCta,NumeroCta,CT.IdBanco,Banco,CdCuenta,A.Cantidad AS CantGalones
,TipOds,A.NumOrden,IdCiaOds,O.Fecha AS FecOrden,O.IdVehiculo AS PlacaVeh,nRemolque,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor
,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,O.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,O.VrTotal,O.Cantidad
,CedBenef,Beneficiario,FechaVence,D.ModdDev,D.OrigenAdd,D.TipCom,D.Comprobante AS NumComp,D.IdCiaCom,D.Observacion AS Observ
,D.IdEstado AS CdEstado,Estado,D.TimeSys AS FechaCrea,D.FecUpdate AS FechaModif,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraDevAos AS D INNER JOIN Trn_TraOrdenAnt AS A ON D.TipDoc=A.TipDoc AND D.Anticipo=A.Anticipo AND D.IdCiaDoc=A.IdCia
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN Trn_TraOrdenServ AS O ON A.TipOds=O.TipDoc AND A.NumOrden=O.NumOrden AND A.IdCiaOds=O.IdCia
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero
 INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero
 LEFT JOIN CtasCorrientes AS CT ON D.CdCta=CT.IdCta LEFT JOIN Bancos AS B ON CT.IdBanco=B.IdBanco
 WHERE D.TipDev=@pmTipDev AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraInfViajes] @pmTipDoc VARCHAR(3),@pmNumViaje INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmIdRuta VARCHAR(4),@pmIdVehiculo VARCHAR(10),@pmIdPoseedor VARCHAR(16),@pmIdConductor VARCHAR(16),@pmnRemolque VARCHAR(10),@pmTipoAfiVehic VARCHAR(10)
,@pmVrFletes MONEY,@pmPesoTotal DECIMAL(14,4),@pmNumManif INT,@pmIdCiaManif CHAR(2),@pmNumMintrans DECIMAL(14,2),@pmMvoAnulacion VARCHAR(5),@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_TraInfViajes (TipDoc,NumViaje,IdCia,Fecha,IdOrigen,IdDestino,IdRuta,IdVehiculo,IdPoseedor,IdConductor,nRemolque,TipoAfiVehic,VrFletes,PesoTotal,NumManif,IdCiaManif,NumMintrans,MvoAnulacion,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmNumViaje,@pmIdCia,@pmFecha,@pmIdOrigen,@pmIdDestino,@pmIdRuta,@pmIdVehiculo,@pmIdPoseedor,@pmIdConductor,@pmnRemolque,@pmTipoAfiVehic,@pmVrFletes,@pmPesoTotal,@pmNumManif,@pmIdCiaManif,@pmNumMintrans,@pmMvoAnulacion,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraInfViajes] @pmTipDoc VARCHAR(3),@pmNumViaje INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraInfViajes
 WHERE TipDoc=@pmTipDoc AND NumViaje=@pmNumViaje AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraInfViajes] @pmTipDoc VARCHAR(3),@pmNumViaje INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmIdRuta VARCHAR(4),@pmIdVehiculo VARCHAR(10),@pmIdPoseedor VARCHAR(16),@pmIdConductor VARCHAR(16),@pmnRemolque VARCHAR(10),@pmTipoAfiVehic VARCHAR(10)
,@pmVrFletes MONEY,@pmPesoTotal DECIMAL(14,4),@pmNumManif INT,@pmIdCiaManif CHAR(2),@pmNumMintrans DECIMAL(14,2),@pmMvoAnulacion VARCHAR(5),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraInfViajes SET Fecha=@pmFecha,IdOrigen=@pmIdOrigen,IdDestino=@pmIdDestino,IdRuta=@pmIdRuta,IdVehiculo=@pmIdVehiculo,IdPoseedor=@pmIdPoseedor,IdConductor=@pmIdConductor,nRemolque=@pmnRemolque,TipoAfiVehic=@pmTipoAfiVehic,VrFletes=@pmVrFletes,PesoTotal=@pmPesoTotal,NumManif=@pmNumManif,IdCiaManif=@pmIdCiaManif,NumMintrans=@pmNumMintrans
,MvoAnulacion=@pmMvoAnulacion,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumViaje=@pmNumViaje AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraInfViajes] @pmTipDoc VARCHAR(3),@pmNumViaje INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumViaje,IdCia,Fecha,IdOrigen,IdDestino,IdRuta,IdVehiculo,IdPoseedor,IdConductor,nRemolque,TipoAfiVehic,VrFletes,PesoTotal,NumManif,IdCiaManif,NumMintrans,MvoAnulacion,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraInfViajes
 WHERE TipDoc=@pmTipDoc AND NumViaje=@pmNumViaje AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[spFinCuposSML] @pmnAnno INT
AS
 SELECT   Item, nSmlvIni, nSmlvFin, Antig_Ini, Antig_Fin, MontoConGar, MontoSinGar 
 FROM   FinCuposEmp
 WHERE nAnno=@pmnAnno
 ORDER BY Item
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpJurTipos] @pmIdTipoPro VARCHAR(4),@pmTipoProceso VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE JurTipos SET TipoProceso=@pmTipoProceso,Inactivo=@pmInactivo
 WHERE IdTipoPro=@pmIdTipoPro
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelJurTipos] @pmIdTipoPro VARCHAR(4)
 AS 
 DELETE FROM JurTipos
 WHERE IdTipoPro=@pmIdTipoPro
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsJurTipos] @pmIdTipoPro VARCHAR(4),@pmTipoProceso VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO JurTipos (IdTipoPro,TipoProceso,Inactivo)
 VALUES (@pmIdTipoPro,@pmTipoProceso,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryJurCausalesLta] 
 AS 
 SELECT IdCausal,Causal,C.IdTipoPro AS CdTipoProc,TipoProceso
 FROM JurCausales AS C INNER JOIN JurTipos AS T ON C.IdTipoPro=T.IdTipoPro
 WHERE C.Inactivo=0
 ORDER BY Causal
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryJurCausalesDso] @pmIdTipoPro VARCHAR(4)=Null
 AS 
 SELECT IdCausal,Causal,Causal+' ('+ TipoProceso +') '+IdCausal AS DsCau
 FROM JurCausales AS C INNER JOIN JurTipos AS T ON C.IdTipoPro=T.IdTipoPro
 WHERE C.IdTipoPro LIKE ISNULL(@pmIdTipoPro,'%')  AND  C.Inactivo=0
 ORDER BY Causal
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraCumplidoAnu] @pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4)
,@pmMvoAnulaCump VARCHAR(5),@pmObservAnulado VARCHAR(250)
 AS 
 UPDATE Trn_TraCumplido SET Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
,MvoAnulaCump=@pmMvoAnulaCump,ObservAnulado=@pmObservAnulado
 WHERE TipDoc=@pmTipDoc AND Cumplido=@pmCumplido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncTraCumplidoCobro] (@pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2))
RETURNS DECIMAL(16,4)
 AS  
BEGIN 
 DECLARE @VrRemCobro DECIMAL(16,4)
 SET @VrRemCobro=(SELECT SUM(CASE WHEN (C.PesoLiqFact=2 OR C.PesoLiqFact<=0) THEN (CASE D.UndTarifClie WHEN 'UNIDADES' THEN D.Cantidad*D.TarifClie WHEN 'VOLUMEN' THEN D.Volumen*D.TarifClie ELSE D.PesoNeto*D.TarifClie END)
 WHEN C.PesoLiqFact=3 THEN (CASE D.UndTarifClie WHEN 'UNIDADES' THEN (CASE WHEN D.Cantidad>0 AND D.Cantidad<D.CantCargue THEN D.Cantidad*D.TarifClie ELSE (CASE WHEN D.CantCargue>0 THEN D.CantCargue*D.TarifClie ELSE D.Cantidad*D.TarifClie END) END)
 WHEN 'VOLUMEN'  THEN (CASE WHEN D.Volumen>0 AND D.Volumen<D.VolCargue THEN D.Volumen*D.TarifClie ELSE (CASE WHEN D.VolCargue>0 THEN D.VolCargue*D.TarifClie ELSE D.Volumen*D.TarifClie END) END)
 ELSE (CASE WHEN D.PesoNeto>0 AND D.PesoNeto<D.PesoCargue THEN D.PesoNeto*D.TarifClie ELSE (CASE WHEN D.PesoCargue>0 THEN D.PesoCargue*D.TarifClie ELSE D.PesoNeto*D.TarifClie END) END) END)
 WHEN C.PesoLiqFact=4 THEN (CASE D.UndTarifClie WHEN 'UNIDADES' THEN (CASE WHEN D.CantCargue>D.Cantidad THEN D.CantCargue*D.TarifClie ELSE D.Cantidad*D.TarifClie END)
 WHEN 'VOLUMEN' THEN (CASE WHEN D.VolCargue>D.Volumen THEN D.VolCargue*D.TarifClie ELSE D.Volumen*D.TarifClie END)
 ELSE (CASE WHEN D.PesoCargue>D.PesoNeto THEN D.PesoCargue*D.TarifClie ELSE D.PesoNeto*D.TarifClie END) END )
 ELSE (CASE D.UndTarifClie WHEN 'UNIDADES' THEN (CASE WHEN D.CantCargue>0 THEN D.CantCargue*D.TarifClie ELSE D.Cantidad*D.TarifClie END)
 WHEN 'VOLUMEN' THEN (CASE WHEN D.VolCargue>0 THEN D.VolCargue*D.TarifClie ELSE D.Volumen*D.TarifClie END)
 ELSE (CASE WHEN D.PesoCargue>0 THEN D.PesoCargue*D.TarifClie ELSE D.PesoNeto*D.TarifPago END) END) END)
 FROM Trn_TraCumplido AS C INNER JOIN Trn_TraCumRemesas AS D ON C.TipDoc=D.TipDoc AND C.Cumplido=D.Cumplido AND C.IdCia=D.IdCia
 WHERE C.TipDoc=@pmTipDoc AND C.Cumplido=@pmCumplido AND C.IdCia=@pmIdCia)
 RETURN @VrRemCobro
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraCumplidoLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT C.Cumplido AS NumCumplido,C.IdCia AS CdCia,Compania,C.Fecha AS FecCumplido,C.Manifiesto AS NumManif,IdCiaMuc,M.Fecha AS FecManif,C.IdVehiculo AS PlacaVeh,Modalidad
,M.IdPropietario AS NitPropietario,T.RazonSocial AS Propietario,M.IdPoseedor AS NitPoseedor,NP.RazonSocial AS Poseedor
,M.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,DiasPlazo,FecPago,TipoMargen,MargenFalt,UndCalcFalt,TarifFaltPago,TarifFaltCobro,NRadicaDoc,IdCiaRadic,CdCiaOfic,FecRadic
,C.Anulado AS CumAnulado,C.FecDev AS FechAnulado,TipoComp,NumComp,CodConcepto,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado
,CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,CdRuta,R.Ruta AS DescRuta
,IdLocFletes,LP.Localidad AS LugarPago,C.TimeSys AS FechaCrea,C.FecUpdate AS FechaAct,C.IdCiaCrea AS CdCiaCrea,C.IdUsuario AS CdUsuario,Usuario
,TipoCumpMT,MotivoSusp,ConsecSusp,VrAdicCargue,VrAdicDescargue,VrAdicFlete,MotivoVrAdic,VrDctoFlete,MotivoVrDcto,VrAdicAnticipo,FecEntregaDoc,NumRadicaMT
,MA.TipoRuta,MvoAnulaCump,ObservAnulado,NumViajesCum,PesoLiqPago,PesoLiqFact,MA.MucMintrans AS TipoViaje,C.CdPlazoPago
 FROM Trn_TraCumplido AS C INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Trn_TraManifiesto AS M ON C.TipMuc=M.TipDoc AND C.Manifiesto=M.Manifiesto AND C.IdCiaMuc=M.IdCia
 INNER JOIN Trn_TraManifAnexo AS MA ON C.TipMuc=MA.TipDoc AND C.Manifiesto=MA.Manifiesto AND C.IdCiaMuc=MA.IdCia
 INNER JOIN Terceros AS NC ON M.IdConductor=NC.IdTercero INNER JOIN Terceros AS T ON M.IdPropietario=T.IdTercero
 INNER JOIN Terceros AS NP ON M.IdPoseedor=NP.IdTercero 
 LEFT JOIN Localidades AS CO ON C.CdOrigen=CO.IdLocal LEFT JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 LEFT JOIN Localidades AS CD ON C.CdDestino=CD.IdLocal LEFT JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 LEFT JOIN Rutas AS R ON C.CdRuta=R.IdRuta LEFT JOIN Localidades AS LP ON M.IdLocFletes=LP.IdLocal 
 WHERE C.TipDoc=@pmTipDoc  AND C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND C.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')  AND M.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY C.IdCia,C.Cumplido
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraCumplido] @pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmTipMuc VARCHAR(3),@pmManifiesto INT,@pmIdCiaMuc CHAR(2),@pmIdVehiculo VARCHAR(10),@pmModalidad VARCHAR(10),@pmDiasPlazo INT
,@pmFecPago SMALLDATETIME,@pmTipoMargen VARCHAR(10),@pmMargenFalt DECIMAL(14,4),@pmUndCalcFalt VARCHAR(10),@pmTarifFaltPago MONEY,@pmTarifFaltCobro MONEY,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4)
,@pmNRadicaDoc INT,@pmIdCiaRadic CHAR(2),@pmCdCiaOfic CHAR(2),@pmFecRadic SMALLDATETIME,@pmCdRuta VARCHAR(4),@pmCdOrigen VARCHAR(8),@pmCdDestino VARCHAR(8),@pmTipoComp VARCHAR(3),@pmNumComp INT,@pmCodConcepto VARCHAR(4)
,@pmTipoCumpMT VARCHAR(3),@pmMotivoSusp VARCHAR(3),@pmConsecSusp VARCHAR(3),@pmVrAdicCargue DECIMAL(16,4),@pmVrAdicDescargue DECIMAL(16,4),@pmVrAdicFlete DECIMAL(16,4),@pmMotivoVrAdic VARCHAR(3)
,@pmVrDctoFlete DECIMAL(16,4),@pmMotivoVrDcto VARCHAR(3),@pmVrAdicAnticipo DECIMAL(16,4),@pmFecEntregaDoc SMALLDATETIME,@pmMvoAnulaCump VARCHAR(5),@pmObservAnulado VARCHAR(250),@pmNumViajesCum INT,@pmPesoLiqPago INT,@pmPesoLiqFact INT,@pmCdPlazoPago VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraCumplido SET Fecha=@pmFecha,TipMuc=@pmTipMuc,Manifiesto=@pmManifiesto,IdCiaMuc=@pmIdCiaMuc,IdVehiculo=@pmIdVehiculo,Modalidad=@pmModalidad,DiasPlazo=@pmDiasPlazo,FecPago=@pmFecPago,TipoMargen=@pmTipoMargen
,MargenFalt=@pmMargenFalt,UndCalcFalt=@pmUndCalcFalt,TarifFaltPago=@pmTarifFaltPago,TarifFaltCobro=@pmTarifFaltCobro,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
,NRadicaDoc=@pmNRadicaDoc,IdCiaRadic=@pmIdCiaRadic,CdCiaOfic=@pmCdCiaOfic,FecRadic=@pmFecRadic,CdRuta=@pmCdRuta,CdOrigen=@pmCdOrigen,CdDestino=@pmCdDestino,TipoComp=@pmTipoComp,NumComp=@pmNumComp,CodConcepto=@pmCodConcepto,FecUpdate=@pmFecUpdate
,TipoCumpMT=@pmTipoCumpMT,MotivoSusp=@pmMotivoSusp,ConsecSusp=@pmConsecSusp,VrAdicCargue=@pmVrAdicCargue,VrAdicDescargue=@pmVrAdicDescargue,VrAdicFlete=@pmVrAdicFlete,MotivoVrAdic=@pmMotivoVrAdic,VrDctoFlete=@pmVrDctoFlete
,MotivoVrDcto=@pmMotivoVrDcto,VrAdicAnticipo=@pmVrAdicAnticipo,FecEntregaDoc=@pmFecEntregaDoc,MvoAnulaCump=@pmMvoAnulaCump,ObservAnulado=@pmObservAnulado,NumViajesCum=@pmNumViajesCum,PesoLiqPago=@pmPesoLiqPago,PesoLiqFact=@pmPesoLiqFact,CdPlazoPago=@pmCdPlazoPago
 WHERE TipDoc=@pmTipDoc AND Cumplido=@pmCumplido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraCumplido] @pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Cumplido,IdCia,Fecha,TipMuc,Manifiesto,IdCiaMuc,IdVehiculo,Modalidad,DiasPlazo,FecPago
,TipoMargen,MargenFalt,UndCalcFalt,TarifFaltPago,TarifFaltCobro,Anulado,FecDev,Observacion,IdEstado
,NRadicaDoc,IdCiaRadic,CdCiaOfic,FecRadic,CdRuta,CdOrigen,CdDestino,TipoComp,NumComp,CodConcepto
,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario,TipoCumpMT,MotivoSusp,ConsecSusp,VrAdicCargue
,VrAdicDescargue,VrAdicFlete,MotivoVrAdic,VrDctoFlete,MotivoVrDcto,VrAdicAnticipo,FecEntregaDoc,NumRadicaMT
,MvoAnulaCump,ObservAnulado,NumViajesCum,PesoLiqPago,PesoLiqFact,CdPlazoPago
 FROM Trn_TraCumplido
 WHERE TipDoc=@pmTipDoc AND Cumplido=@pmCumplido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncTraCumplidoPago] (@pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2))
RETURNS DECIMAL(16,4)
 AS  
BEGIN 
 DECLARE @VrRemPago DECIMAL(16,4)
 SET @VrRemPago=(SELECT SUM(CASE WHEN (C.PesoLiqPago=2 OR C.PesoLiqPago<=0) THEN (CASE D.UndTarifPago WHEN 'UNIDADES' THEN D.Cantidad*D.TarifPago WHEN 'VOLUMEN' THEN D.Volumen*D.TarifPago ELSE D.PesoNeto*D.TarifPago END)
 WHEN C.PesoLiqPago=3 THEN (CASE D.UndTarifPago WHEN 'UNIDADES' THEN (CASE WHEN D.Cantidad>0 AND D.Cantidad<D.CantCargue THEN D.Cantidad*D.TarifPago ELSE (CASE WHEN D.CantCargue>0 THEN D.CantCargue*D.TarifPago ELSE D.Cantidad*D.TarifPago END) END)
 WHEN 'VOLUMEN'  THEN (CASE WHEN D.Volumen>0 AND D.Volumen<D.VolCargue THEN D.Volumen*D.TarifPago ELSE (CASE WHEN D.VolCargue>0 THEN D.VolCargue*D.TarifPago ELSE D.Volumen*D.TarifPago END) END)
 ELSE (CASE WHEN D.PesoNeto>0 AND D.PesoNeto<D.PesoCargue THEN D.PesoNeto*D.TarifPago ELSE (CASE WHEN D.PesoCargue>0 THEN D.PesoCargue*D.TarifPago ELSE D.PesoNeto*D.TarifPago END) END) END)
 WHEN C.PesoLiqPago=4 THEN (CASE D.UndTarifPago WHEN 'UNIDADES' THEN (CASE WHEN D.CantCargue>D.Cantidad THEN D.CantCargue*D.TarifPago ELSE D.Cantidad*D.TarifPago END)
 WHEN 'VOLUMEN' THEN (CASE WHEN D.VolCargue>D.Volumen THEN D.VolCargue*D.TarifPago ELSE D.Volumen*D.TarifPago END)
 ELSE (CASE WHEN D.PesoCargue>D.PesoNeto THEN D.PesoCargue*D.TarifPago ELSE D.PesoNeto*D.TarifPago END) END )
 ELSE (CASE D.UndTarifPago WHEN 'UNIDADES' THEN (CASE WHEN D.CantCargue>0 THEN D.CantCargue*D.TarifPago ELSE D.Cantidad*D.TarifPago END)
 WHEN 'VOLUMEN' THEN (CASE WHEN D.VolCargue>0 THEN D.VolCargue*D.TarifPago ELSE D.Volumen*D.TarifPago END)
 ELSE (CASE WHEN D.PesoCargue>0 THEN D.PesoCargue*D.TarifPago ELSE D.PesoNeto*D.TarifPago END) END) END)
 FROM Trn_TraCumplido AS C INNER JOIN Trn_TraCumRemesas AS D ON C.TipDoc=D.TipDoc AND C.Cumplido=D.Cumplido AND C.IdCia=D.IdCia
 WHERE C.TipDoc=@pmTipDoc AND C.Cumplido=@pmCumplido AND C.IdCia=@pmIdCia)
 RETURN @VrRemPago
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraCumplido] @pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmTipMuc VARCHAR(3),@pmManifiesto INT,@pmIdCiaMuc CHAR(2),@pmIdVehiculo VARCHAR(10),@pmModalidad VARCHAR(10),@pmDiasPlazo INT
,@pmFecPago SMALLDATETIME,@pmTipoMargen VARCHAR(10),@pmMargenFalt DECIMAL(14,4),@pmUndCalcFalt VARCHAR(10),@pmTarifFaltPago MONEY,@pmTarifFaltCobro MONEY,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4)
,@pmNRadicaDoc INT,@pmIdCiaRadic CHAR(2),@pmCdCiaOfic CHAR(2),@pmFecRadic SMALLDATETIME,@pmCdRuta VARCHAR(4),@pmCdOrigen VARCHAR(8),@pmCdDestino VARCHAR(8),@pmTipoComp VARCHAR(3),@pmNumComp INT,@pmCodConcepto VARCHAR(4)
,@pmTipoCumpMT VARCHAR(3),@pmMotivoSusp VARCHAR(3),@pmConsecSusp VARCHAR(3),@pmVrAdicCargue DECIMAL(16,4),@pmVrAdicDescargue DECIMAL(16,4),@pmVrAdicFlete DECIMAL(16,4),@pmMotivoVrAdic VARCHAR(3)
,@pmVrDctoFlete DECIMAL(16,4),@pmMotivoVrDcto VARCHAR(3),@pmVrAdicAnticipo DECIMAL(16,4),@pmFecEntregaDoc SMALLDATETIME,@pmMvoAnulaCump VARCHAR(5),@pmObservAnulado VARCHAR(250),@pmNumViajesCum INT,@pmPesoLiqPago INT,@pmPesoLiqFact INT,@pmCdPlazoPago VARCHAR(4)
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraCumplido (TipDoc,Cumplido,IdCia,Fecha,TipMuc,Manifiesto,IdCiaMuc,IdVehiculo,Modalidad,DiasPlazo,FecPago,TipoMargen,MargenFalt,UndCalcFalt,TarifFaltPago,TarifFaltCobro,NRadicaDoc,IdCiaRadic,CdCiaOfic,FecRadic,CdRuta,CdOrigen,CdDestino
,Anulado,FecDev,TipoComp,NumComp,CodConcepto,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario,TipoCumpMT,MotivoSusp,ConsecSusp,VrAdicCargue,VrAdicDescargue,VrAdicFlete,MotivoVrAdic,VrDctoFlete,MotivoVrDcto,VrAdicAnticipo,FecEntregaDoc,NumRadicaMT,MvoAnulaCump,ObservAnulado,NumViajesCum,PesoLiqPago,PesoLiqFact,CdPlazoPago)
 VALUES (@pmTipDoc,@pmCumplido,@pmIdCia,@pmFecha,@pmTipMuc,@pmManifiesto,@pmIdCiaMuc,@pmIdVehiculo,@pmModalidad,@pmDiasPlazo,@pmFecPago,@pmTipoMargen,@pmMargenFalt,@pmUndCalcFalt,@pmTarifFaltPago,@pmTarifFaltCobro,@pmNRadicaDoc,@pmIdCiaRadic,@pmCdCiaOfic,@pmFecRadic
,@pmCdRuta,@pmCdOrigen,@pmCdDestino,@pmAnulado,@pmFecDev,@pmTipoComp,@pmNumComp,@pmCodConcepto,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmTipoCumpMT,@pmMotivoSusp,@pmConsecSusp,@pmVrAdicCargue,@pmVrAdicDescargue,@pmVrAdicFlete
,@pmMotivoVrAdic,@pmVrDctoFlete,@pmMotivoVrDcto,@pmVrAdicAnticipo,@pmFecEntregaDoc,0,@pmMvoAnulaCump,@pmObservAnulado,@pmNumViajesCum,@pmPesoLiqPago,@pmPesoLiqFact,@pmCdPlazoPago)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMayPsptoCant] @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT COUNT(Item) AS SCANT
 FROM Trn_MayPspto
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMayPsptoLta] @pmNContratoIni INT,@pmNContratoFin 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 BETWEEN @pmNContratoIni AND @pmNContratoFin AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMayPsptoItm] @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT  MAX(Item) AS MXITM
 FROM Trn_MayPspto
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 [Id]=@pmnReg
 AND IdCuenta=@pmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuCuentasAux_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum VARCHAR(4),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuCuentasAux (nAnno,nMes,IdCia,nReg,IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,SubAuxiliar,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos,TipoAfiVehic)
 SELECT @pmnAnno,@pmnMes,S.IdCia,0,S.IdCuenta,S.IdTercero,S.CodAgncia,S.IdCCosto,S.IdSubCos,S.SubAuxiliar,S.TipoAcum,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0,S.TipoAfiVehic
 FROM AcuCuentasAux AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt AND S.TipoAcum=@pmTipoAcum
 AND NOT EXISTS (SELECT * FROM AcuCuentasAux AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta
 AND AC.IdTercero=S.IdTercero AND AC.CodAgncia=S.CodAgncia AND AC.IdCCosto=S.IdCCosto AND AC.IdSubCos=S.IdSubCos
 AND AC.SubAuxiliar=S.SubAuxiliar AND AC.TipoAcum=S.TipoAcum AND AC.TipoAfiVehic=S.TipoAfiVehic)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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','VCC')
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposOperarios] @pmIdOper VARCHAR(4),@pmTipoOperario VARCHAR(50),@pmTarifaCom DECIMAL(14,4),@pmGrupoOper VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE TiposOperarios SET TipoOperario=@pmTipoOperario,TarifaCom=@pmTarifaCom,Inactivo=@pmInactivo,GrupoOper=@pmGrupoOper
 WHERE IdOper=@pmIdOper
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposOperarios] @pmIdOper VARCHAR(4),@pmTipoOperario VARCHAR(50),@pmTarifaCom DECIMAL(14,4)
,@pmGrupoOper VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO TiposOperarios (IdOper,TipoOperario,TarifaCom,Inactivo,GrupoOper)
 VALUES (@pmIdOper,@pmTipoOperario,@pmTarifaCom,@pmInactivo,@pmGrupoOper)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposOperarios] @pmIdOper VARCHAR(4)
 AS 
 DELETE FROM TiposOperarios WHERE IdOper=@pmIdOper
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposOperarios] @pmIdOper VARCHAR(4)
 AS 
 IF @pmIdOper IS NULL
  BEGIN
  SELECT IdOper,TipoOperario,TarifaCom,GrupoOper FROM TiposOperarios
  WHERE Inactivo=0 ORDER BY TipoOperario
  END
 ELSE
  BEGIN
   SELECT IdOper,TipoOperario,TarifaCom,GrupoOper,Inactivo FROM TiposOperarios
   WHERE IdOper=@pmIdOper
  END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMttoInspecciones] @pmIdInspec VARCHAR(4),@pmInspeccion VARCHAR(150),@pmIdSistema VARCHAR(4),@pmIdMtto VARCHAR(4)
,@pmTipoDato INT,@pmUndMed VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO MttoInspecciones (IdInspec,Inspeccion,IdSistema,IdMtto,TipoDato,UndMed,Inactivo)
 VALUES (@pmIdInspec,@pmInspeccion,@pmIdSistema,@pmIdMtto,@pmTipoDato,@pmUndMed,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoInspecciones] @pmIdInspec VARCHAR(4)
 AS 
 SELECT IdInspec,Inspeccion,IdSistema,IdMtto,TipoDato,UndMed,Inactivo
 FROM MttoInspecciones
 WHERE IdInspec=@pmIdInspec
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoInspeccionesLta] @pmIdSistema VARCHAR(4)
 AS 
 SELECT I.IdInspec,Inspeccion,I.IdSistema AS CdSistema,Sistema,I.IdMtto AS CdMtto,TipoMtto,TipoDato,UndMed,I.Inactivo
,Item,Estado,ValorInicial,ValorFinal,NColor,ReqMtto,CdConc,Concepto
 FROM MttoInspecciones AS I INNER JOIN MttoSistemas AS S ON I.IdSistema=S.IdSistema
 INNER JOIN MttoTipos AS TM ON I.IdMtto=TM.IdMtto
 LEFT JOIN MttoInspecRangos AS R ON I.IdInspec=R.IdInspec 
 LEFT JOIN MttoConceptos AS C ON R.CdConc=C.IdConc
 WHERE I.IdSistema LIKE ISNULL(@pmIdSistema,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMttoInspecciones] @pmIdInspec VARCHAR(4),@pmInspeccion VARCHAR(150),@pmIdSistema VARCHAR(4),@pmIdMtto VARCHAR(4)
,@pmTipoDato INT,@pmUndMed VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE MttoInspecciones SET Inspeccion=@pmInspeccion,IdSistema=@pmIdSistema,IdMtto=@pmIdMtto,TipoDato=@pmTipoDato,UndMed=@pmUndMed,Inactivo=@pmInactivo
 WHERE IdInspec=@pmIdInspec
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_AfIngreso_Sel] @pmtmNumero VARCHAR(5),@pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 BEGIN
 INSERT INTO tm_AfIngreso (tmNumero,tmItem,tmIdActivo,tmFecha,tmVrMejoras,tmVrDisminucion,tmVrAjuInfAcum,tmVrDepAcuHis,tmVrDepAcuAju,tmVrAjuInfDep,tmNumMeses
,tmVidaUtil,tmBaseDep,tmMetodo,tmFecUltDep,tmConcepto,tmUnidades,tmSalvamento,tmDepPartes,tmNumParte,tmCantidad)
 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,UndsProd,VrSalvamento,DepPartes,NumParte,Cantidad
 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 AND A.DepPartes=0
--registros de partes
 INSERT INTO tm_AfIngreso (tmNumero,tmItem,tmIdActivo,tmFecha,tmVrMejoras,tmVrDisminucion,tmVrAjuInfAcum,tmVrDepAcuHis,tmVrDepAcuAju,tmVrAjuInfDep,tmNumMeses
,tmVidaUtil,tmBaseDep,tmMetodo,tmFecUltDep,tmConcepto,tmUnidades,tmSalvamento,tmDepPartes,tmNumParte,tmCantidad)
 SELECT  @pmtmNumero,I.Item,I.IdActivo,I.FecActivacion,I.VrMejoras,I.VrDisminucion,I.VrAjuInfAcum,I.VrDepAcuHis,I.VrDepAcuAju,I.VrAjuInfDep,I.NumMesesDep
,P.VidaUtil,P.VrBaseDep,P.MetDeprec,I.FecUltDep,Concepto,P.UndsProd,P.VrSalvamento,1,I.NumParte,I.Cantidad
 FROM Trn_AfMovIngreso AS I INNER JOIN Activos AS A ON I.IdActivo=A.IdActivo
 LEFT JOIN Activos_Partes AS P ON I.IdActivo=P.IdActivo AND I.NumParte=P.Item
 WHERE I.IdMov=@pmIdMov AND I.Numero=@pmNumero AND I.IdCia=@pmIdCia AND A.DepPartes=1
 END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmNumParte INT,@pmCantidad INT
 AS 
 INSERT INTO Trn_AfMovIngreso (IdMov,Numero,IdCia,Item,IdActivo,FecActivacion,VrMejoras,VrDisminucion,VrAjuInfAcum,VrDepAcuHis,VrDepAcuAju,VrAjuInfDep,NumMesesDep,FecUltDep,Concepto,NumParte,Cantidad)
 VALUES (@pmIdMov,@pmNumero,@pmIdCia,@pmItem,@pmIdActivo,@pmFecActivacion,@pmVrMejoras,@pmVrDisminucion,@pmVrAjuInfAcum,@pmVrDepAcuHis,@pmVrDepAcuAju
,@pmVrAjuInfDep,@pmNumMesesDep,@pmFecUltDep,@pmConcepto,@pmNumParte,@pmCantidad)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,NumParte,Cantidad
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomTablaRetItm] @pmnAnno INT
 AS 
 SELECT MAX(nIntrvalo) AS MXITM,MAX(VrFinal) AS MXFINAL
 FROM NomTablaRet
 WHERE nAnno=@pmnAnno
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomTablaRet] @pmnAnno INT,@pmnIntrvalo INT
 AS 
 DELETE FROM NomTablaRet
 WHERE nAnno=@pmnAnno 
 AND (nIntrvalo>=ISNULL(@pmnIntrvalo,0)  AND nIntrvalo<=ISNULL(@pmnIntrvalo,2147483647))
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsFmtFactura] @pmnFormato VARCHAR(10),@pmIdDoc VARCHAR(3),@pmCdForma CHAR(2)
 AS 
 INSERT INTO Sys_FmtFactura (nFormato,IdDoc,CdForma)
 VALUES (@pmnFormato,@pmIdDoc,@pmCdForma)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFmtFactura] @pmnFormato VARCHAR(10)
 AS 
 SELECT nFormato,IdDoc,CdForma
 FROM Sys_FmtFactura
 WHERE nFormato=@pmnFormato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpFmtFactura] @pmnFormato VARCHAR(10),@pmIdDoc VARCHAR(3),@pmCdForma CHAR(2)
 AS 
 UPDATE Sys_FmtFactura SET IdDoc=@pmIdDoc,CdForma=@pmCdForma
 WHERE nFormato=@pmnFormato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelFmtFactura] @pmnFormato VARCHAR(10)
 AS 
 DELETE FROM Sys_FmtFactura
 WHERE nFormato=@pmnFormato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpAfCausales] @pmIdCausal VARCHAR(4),@pmCausal VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE AfCausales SET Causal=@pmCausal,Inactivo=@pmInactivo
 WHERE IdCausal=@pmIdCausal
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAfCausales] @pmIdCausal VARCHAR(4),@pmCausal VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO AfCausales (IdCausal,Causal,Inactivo)
 VALUES (@pmIdCausal,@pmCausal,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_AfRetiroDso] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdActivo,NomActivo,DepPartes,tmNumParte,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelAfCausales] @pmIdCausal VARCHAR(4)
 AS 
 DELETE FROM AfCausales
 WHERE IdCausal=@pmIdCausal
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryProdCodigos]  @pmCodigo VARCHAR(20)
 AS 
 SELECT Codigo,IdProducto
 FROM ProdCodigos
 WHERE Codigo=@pmCodigo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmVleConsumo BIT
,@pmCodIntegrity VARCHAR(20),@pmCodValera VARCHAR(10),@pmCodIntCom VARCHAR(20),@pmRefProducto BIT,@pmFactTicket BIT,@pmCdMedPago VARCHAR(4),@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,VleConsumo=@pmVleConsumo
,CodIntegrity=@pmCodIntegrity,CodValera=@pmCodValera,CodIntCom=@pmCodIntCom,RefProducto=@pmRefProducto,FactTicket=@pmFactTicket,CdMedPago=@pmCdMedPago,Inactivo=@pmInactivo
 WHERE IdForma=@pmIdForma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmVrPrecio,tmVrPrecio*tmCantidad AS VrPublico,tmVrTotal-(tmVrPrecio*tmCantidad) AS Excedente
,tmVrBase,tmVrPresp
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CdProducto,FE_Ticket
,Cant_Gals,PrecioBase,PrecioUnit,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmVleConsumo BIT
,@pmCodIntegrity VARCHAR(20),@pmCodValera VARCHAR(10),@pmCodIntCom VARCHAR(20),@pmRefProducto BIT,@pmFactTicket BIT,@pmCdMedPago VARCHAR(4),@pmInactivo BIT
 AS 
 INSERT INTO Formaspago (IdForma,FormaPago,Consecutivo,CuadreCaja,RefDcmto,RefNit,RefBco,RefFech,RefAtza,RefOtro,VldRangos,VleConsumo,CodIntegrity,CodValera,Inactivo,CodIntCom,RefProducto,FactTicket,CdMedPago)
 VALUES (@pmIdForma,@pmFormaPago,@pmConsecutivo,@pmCuadreCaja,@pmRefDcmto,@pmRefNit,@pmRefBco,@pmRefFech,@pmRefAtza
,@pmRefOtro,@pmVldRangos,@pmVleConsumo,@pmCodIntegrity,@pmCodValera,@pmInactivo,@pmCodIntCom,@pmRefProducto,@pmFactTicket,@pmCdMedPago)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFormaspago] @pmIdForma VARCHAR(4)
 AS 
 IF @pmIdForma IS NULL
  SELECT IdForma,FormaPago,Consecutivo,CuadreCaja,RefDcmto,RefNit,RefBco,RefFech,RefAtza,RefOtro,VldRangos,VleConsumo
,CodIntegrity,CodValera,CodIntCom,RefProducto,FactTicket,CdMedPago
  FROM Formaspago
  WHERE Inactivo=0
  ORDER BY Consecutivo
 ELSE
  SELECT IdForma,FormaPago,Consecutivo,CuadreCaja,RefDcmto,RefNit,RefBco,RefFech,RefAtza,RefOtro,VldRangos,VleConsumo
,CodIntegrity,CodValera,CodIntCom,RefProducto,FactTicket,CdMedPago,Inactivo
  FROM Formaspago
  WHERE IdForma=@pmIdForma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEdsFormasLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT F.TipoRango,F.IdRango,F.NumSerie,F.NumForma,F.Planilla,F.IdCia AS CdCia,Compania,F.Item,F.Fecha,F.VrTotal,F.IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia
,F.IdCuenta AS CdCuenta,NomCuenta,F.pVehiculo,F.nVehiculo,F.IdCajero,Usuario,F.IdVend AS CdVend,V.RazonSocial AS Vendedor,F.CdProducto,DescripProd,F.Cantidad,F.IdForma AS CdForma,FormaPago
,F.Referencia AS Referncia,F.NumRef2,F.CantPuntos,F.Observacion AS Observ,F.TipDcm,F.Documento,F.IdCiaDcm,F.FechaDcm,AgencDcm,F.Anulado,F.NumDev,F.FecDev,F.CdCCosto,F.CdSubCos,F.VrPrecio,F.VrBase,F.VrPrecioEsp
,ISNULL(D.Cant_Neto,0) AS Cant_Neto,ISNULL(D.Cant_Dev,0) AS Cant_Dev,(ISNULL(D.STOTDEV,0)-ISNULL(D.SDCTDEV,0)+ISNULL(D.SIVADEV,0)) AS TotalDev
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  
 LEFT JOIN (SELECT TipoRango,IdRango,NumSerie,NumForma,SUM(Cantidad-CantDev) AS Cant_Neto,SUM(CantDev) AS Cant_Dev,SUM((Cantidad-CantDev)*VrPrecio) AS SUBTOTAL
,SUM(CASE WHEN (CantDev>0 AND TarifaDct>0 AND VrDcto>0) THEN (((Cantidad-CantDev)*VrPrecio)*TarifaDct)/100 ELSE VrDcto END) AS STOTDCT
,SUM(CASE WHEN (CantDev>0 AND TarifaIva>0 AND VrIva>0) THEN ((((Cantidad-CantDev)*VrPrecio)-((((Cantidad-CantDev)*VrPrecio)*TarifaDct)/100))*TarifaIva)/100 ELSE VrIva END) AS STOTIVA
,SUM(CantDev*VrPrecio) AS STOTDEV,SUM(((CantDev*VrPrecio)*TarifaDct)/100) AS SDCTDEV
,SUM((((CantDev*VrPrecio)-(((CantDev*VrPrecio)*TarifaDct)/100))*TarifaIva)/100) AS SIVADEV
FROM Trn_EdsForItems GROUP BY TipoRango,IdRango,NumSerie,NumForma) AS D ON F.TipoRango=D.TipoRango AND F.IdRango=D.IdRango AND F.NumSerie=D.NumSerie AND F.NumForma=D.NumForma
WHERE (F.Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND F.Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
AND (F.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (F.IdCliente=@pmIdCliente OR @pmIdCliente IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmReferncia2,tmObservacion,tmIdForma,FormaPago
,tmIdUsuario,tmIdVend,V.RazonSocial AS Vendedor,tmCantPuntos,tmCdCCosto,tmCdSubCos,tmVrPrecio,tmTipAplica,tmVrBase,tmVrPresp
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,FecConsig,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,FecConsig,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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.Cod_CCosto,R.Cod_SubCosto,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposMod] @pmIdModd VARCHAR(4),@pmModalidad VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposMod (IdModd,Modalidad,Inactivo)
 VALUES (@pmIdModd,@pmModalidad,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposMod] @pmIdModd VARCHAR(4),@pmModalidad VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposMod SET Modalidad=@pmModalidad,Inactivo=@pmInactivo
 WHERE IdModd=@pmIdModd
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposMod] @pmIdModd VARCHAR(4)
 AS 
 DELETE FROM TiposMod
 WHERE IdModd=@pmIdModd
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomVacDetEmp] @pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Trn_NomVacDet
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMttoOrden] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaOrden SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdTipoOdt VARCHAR(4),@pmIdCenSer VARCHAR(4),@pmIdVehiculo VARCHAR(10),@pmnRemolque VARCHAR(10)
,@pmIdConductor VARCHAR(16),@pmIdOperario VARCHAR(16),@pmIdSupervisor VARCHAR(16),@pmModalidad VARCHAR(10),@pmkmVehiculo INT,@pmkmRemolque INT,@pmVrServicios MONEY,@pmVrRepuestos MONEY,@pmVrManoObra MONEY,@pmVrOtros MONEY,@pmFechaCierre SMALLDATETIME
,@pmNitCenSer VARCHAR(16),@pmIdEstado VARCHAR(4),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2000),@pmCdEstadoVeh VARCHAR(4),@pmTipFact VARCHAR(3),@pmNumFactura INT,@pmIdCiaFact CHAR(2),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_MttoOrden (TipDoc,NumOrden,IdCia,Fecha,FechaOrden,FechaVence,IdTipoOdt,IdCenSer,IdVehiculo,nRemolque,IdConductor,IdOperario,IdSupervisor,Modalidad,kmVehiculo,kmRemolque,VrServicios,VrRepuestos,VrManoObra,VrOtros,FechaCierre,NitCenSer,CdEstadoVeh
,IdEstado,Anulado,NumDev,FecDev,Observacion,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario,TipFact,NumFactura,IdCiaFact)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmFecha,@pmFechaOrden,@pmFechaVence,@pmIdTipoOdt,@pmIdCenSer,@pmIdVehiculo,@pmnRemolque,@pmIdConductor,@pmIdOperario,@pmIdSupervisor,@pmModalidad,@pmkmVehiculo,@pmkmRemolque,@pmVrServicios,@pmVrRepuestos
,@pmVrManoObra,@pmVrOtros,@pmFechaCierre,@pmNitCenSer,@pmCdEstadoVeh,@pmIdEstado,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmTipFact,@pmNumFactura,@pmIdCiaFact)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMttoOrden] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaOrden SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdTipoOdt VARCHAR(4),@pmIdCenSer VARCHAR(4),@pmIdVehiculo VARCHAR(10),@pmnRemolque VARCHAR(10)
,@pmIdConductor VARCHAR(16),@pmIdOperario VARCHAR(16),@pmIdSupervisor VARCHAR(16),@pmModalidad VARCHAR(10),@pmkmVehiculo INT,@pmkmRemolque INT,@pmVrServicios MONEY,@pmVrRepuestos MONEY,@pmVrManoObra MONEY,@pmVrOtros MONEY,@pmFechaCierre SMALLDATETIME
,@pmNitCenSer VARCHAR(16),@pmIdEstado VARCHAR(4),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2000),@pmCdEstadoVeh VARCHAR(4),@pmTipFact VARCHAR(3),@pmNumFactura INT,@pmIdCiaFact CHAR(2),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_MttoOrden SET Fecha=@pmFecha,FechaOrden=@pmFechaOrden,FechaVence=@pmFechaVence,IdTipoOdt=@pmIdTipoOdt,IdCenSer=@pmIdCenSer,IdVehiculo=@pmIdVehiculo,nRemolque=@pmnRemolque,IdConductor=@pmIdConductor,IdOperario=@pmIdOperario,IdSupervisor=@pmIdSupervisor
,Modalidad=@pmModalidad,kmVehiculo=@pmkmVehiculo,kmRemolque=@pmkmRemolque,VrServicios=@pmVrServicios,VrRepuestos=@pmVrRepuestos,VrManoObra=@pmVrManoObra,VrOtros=@pmVrOtros,FechaCierre=@pmFechaCierre,NitCenSer=@pmNitCenSer,IdEstado=@pmIdEstado,CdEstadoVeh=@pmCdEstadoVeh
,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,TipFact=@pmTipFact,NumFactura=@pmNumFactura,IdCiaFact=@pmIdCiaFact,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoOrden] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumOrden,IdCia,Fecha,FechaOrden,FechaVence,IdTipoOdt,IdCenSer,IdVehiculo,nRemolque,IdConductor,IdOperario,IdSupervisor,Modalidad,kmVehiculo,kmRemolque,VrServicios,VrRepuestos,VrManoObra,VrOtros,FechaCierre,NitCenSer
,CdEstadoVeh,IdEstado,Anulado,NumDev,FecDev,Observacion,TipFact,NumFactura,IdCiaFact,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_MttoOrden
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMttoOrdenAnu] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
,@pmIdEstado VARCHAR(4),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2000)
 AS 
 UPDATE Trn_MttoOrden SET IdEstado=@pmIdEstado,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevAntDoc] @pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Anticipo,IdCiaDoc,FecDoc,IdVehiculo,IdPoseedor,ModdDev
 FROM Trn_TraDevAnt
 WHERE TipDoc=@pmTipDoc AND Anticipo=@pmAnticipo AND IdCiaDoc=@pmIdCiaDoc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_Caja_Dva] @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,D.TipDev,D.Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.IdPoseedor,'0',VrDevolucion,0,0,0,0,0,0,0,0,VrDevolucion AS ValNeto,CASE A.TipoPago WHEN 'EFECTIVO' THEN VrDevolucion ELSE 0 END
,0,0,0,0,0,0,0,CASE A.TipoPago WHEN 'EFECTIVO' THEN 1 ELSE 0 END,CASE A.TipoPago WHEN 'EFECTIVO' THEN 'EFE' ELSE 'CHE' END,NumCheque,IdBanco,SUBSTRING(A.Beneficiario,1,150),D.CdCta,A.FecCheque,D.IdConductor,0,'0',D.IdUsuario,'0','ANTICIPOS',''
,A.Modalidad,D.IdVehiculo,A.TipMuc,A.Manifiesto,A.IdCiaMuc,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Anticipo,D.FecDoc,D.Observacion,D.TimeSys
 FROM Trn_TraDevAnt AS D INNER JOIN Trn_TraAnticipos AS A ON D.TipDoc=A.TipDoc AND D.Anticipo=A.Anticipo AND D.IdCiaDoc=A.IdCia
 LEFT JOIN CtasCorrientes AS CT ON D.CdCta=CT.IdCta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND A.TipoPago='EFECTIVO'
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdUsuario LIKE ISNULL(@pmIdCajero,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraDevAnt] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME
,@pmIdVehiculo VARCHAR(10),@pmIdPoseedor VARCHAR(16),@pmIdConductor VARCHAR(16),@pmVrDevolucion MONEY,@pmCdCta VARCHAR(4),@pmCdCuenta VARCHAR(16),@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_TraDevAnt (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Anticipo,IdCiaDoc,FecDoc,IdVehiculo,IdPoseedor,IdConductor,VrDevolucion,CdCta,CdCuenta,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmAnticipo,@pmIdCiaDoc,@pmFecDoc,@pmIdVehiculo,@pmIdPoseedor,@pmIdConductor,@pmVrDevolucion,@pmCdCta,@pmCdCuenta,@pmModdDev,@pmOrigenAdd,@pmTipCom
,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevAntLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null
,@pmIdCta VARCHAR(4)=Null
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipoAnt,D.Anticipo AS NumAnticipo,IdCiaDoc,FecDoc,TipMuc,Manifiesto,IdCiaMuc, D.IdVehiculo AS PlacaVeh
,D.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,D.IdConductor AS CedConductor,CDT.RazonSocial AS Conductor,VrDevolucion,CdCta,NumeroCta,CT.IdBanco,Banco,CdCuenta
,TipoPago,NumCheque,FecCheque,Beneficiario,Modalidad,VrAnticipo,VrAbonado,VrAnticipo-VrAbonado AS SaldoActual
,ModdDev,D.OrigenAdd AS Origen_Add,D.TipCom AS TipoComp,D.Comprobante AS NumComp,D.IdCiaCom AS CdCiaComp,D.Observacion AS Observ,D.IdEstado AS CdEstado,D.TimeSys AS Time_Sys,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraDevAnt AS D INNER JOIN  Terceros AS T ON D.IdPoseedor=T.IdTercero INNER JOIN Terceros AS CDT ON D.IdConductor=CDT.IdTercero
 INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia
 INNER JOIN Trn_TraAnticipos AS A ON D.TipDoc=A.TipDoc AND D.Anticipo=A.Anticipo AND D.IdCiaDoc=A.IdCia
 LEFT JOIN CtasCorrientes AS CT ON D.CdCta=CT.IdCta LEFT JOIN Bancos AS B ON CT.IdBanco=B.IdBanco
 WHERE  D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdVehiculo  LIKE ISNULL(@pmIdVehiculo ,'%') AND D.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')   AND A.Modalidad LIKE ISNULL(@pmModalidad,'%') 
 AND D.CdCta LIKE ISNULL( @pmIdCta,'%') 
 ORDER BY D.IdCia,Devolucion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraAnticiposTotCaj]  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
AS
SELECT TipDoc,SUM(VrAnticipo) AS TotalAnt
FROM Trn_TraAnticipos 
WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipoPago='EFECTIVO'
AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GROUP BY TipDoc
UNION ALL
SELECT D.TipDev,SUM(VrDevolucion)
FROM Trn_TraDevAnt AS D INNER JOIN Trn_TraAnticipos AS A ON D.TipDoc=A.TipDoc AND D.Anticipo=A.Anticipo AND D.IdCiaDoc=A.IdCia
WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND A.TipoPago='EFECTIVO'
AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GROUP BY D.TipDev
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraDevAnt] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdVehiculo VARCHAR(10)
,@pmIdPoseedor VARCHAR(16),@pmIdConductor VARCHAR(16),@pmVrDevolucion MONEY,@pmCdCta VARCHAR(4),@pmCdCuenta VARCHAR(16),@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraDevAnt SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Anticipo=@pmAnticipo,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdVehiculo=@pmIdVehiculo,IdPoseedor=@pmIdPoseedor,IdConductor=@pmIdConductor,VrDevolucion=@pmVrDevolucion
,CdCta=@pmCdCta,CdCuenta=@pmCdCuenta,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraDevAnt] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraDevAnt
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevAnt] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Anticipo,IdCiaDoc,FecDoc,IdVehiculo,IdPoseedor,IdConductor,VrDevolucion,CdCta,CdCuenta
,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraDevAnt
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCantExcluido DECIMAL(14,4),@pmMgnNeto DECIMAL(14,4),@pmTipoMargen CHAR(1)
,@pmPorcIvaCom DECIMAL(14,4),@pmVrIvaCom DECIMAL(16,4),@pmIvaIncBonif BIT,@pmCdAgenc VARCHAR(16),@pmVrBaseIva DECIMAL(16,4),@pmVrLiquidaNeto 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,CantExcluido,MgnNeto,TipoMargen
,PorcIvaCom,VrIvaCom,IvaIncBonif,CdAgenc,VrBaseIva,VrLiquidaNeto)
 VALUES (@pmNLiquida,@pmIdCia,@pmNContrato,@pmIdCiaCon,@pmItem,@pmIdSubgrupo,@pmCombustible,@pmUnidades
,@pmUnidDev,@pmValorTotal,@pmValorDev,@pmIntervalo,@pmMargen,@pmTarifa,@pmTipoTarifa,@pmVrLiquida
,@pmLogDetalle,@pmSubsidio,@pmVenGalones,@pmVenTotal,@pmCantExcluido,@pmMgnNeto,@pmTipoMargen,@pmPorcIvaCom,@pmVrIvaCom,@pmIvaIncBonif,@pmCdAgenc,@pmVrBaseIva,@pmVrLiquidaNeto)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoTarifa,Tarifa,VrLiquida
,VrBaseIva,PorcIvaCom,VrIvaCom,IvaIncBonif,TipoMargen,Margen,Subsidio,MgnNeto,LogDetalle,CantExcluido,FechaInicio
,IdCliente,RazonSocial,D.CdAgenc 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 D.CdAgenc=A.IdAgencia
 WHERE NLiquida=@pmNLiquida AND D.IdCia=@pmIdCia 
 ORDER BY D.NContrato,IdCiaCon,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMayLiqDetalle] @pmNLiquida INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_MayLiqDetalle
 WHERE NLiquida=@pmNLiquida AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraTrasladosLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null
 AS 
 SELECT  Traslado,T.IdCia AS CdCia,Compania,Fecha,FecDespacho,TipoTraslado,T.IdBodega AS CdBodega,BG.Bodega AS BodegaOrig,IdBodDtno,BD.Bodega AS BodegaDtno,IdVehiculo
,IdConductor,CDT.RazonSocial AS NomConductor,Cantidad,PesoTotal,Manifiesto,CdCiaMuc,EstadoRec,FechaRec,CedRecibido,NomRecibido
,T.Observacion AS Observ,OrigenAdd,TimeSys,IdCiaCrea,T.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraTraslados AS T INNER JOIN Companias AS CN ON T.IdCia=CN.IdCia INNER JOIN Terceros AS CDT ON T.IdConductor=CDT.IdTercero 
 INNER JOIN adm_Usuarios AS U ON T.IdUsuario=U.IdUsuario
 INNER JOIN AlmBodegas AS BG ON T.IdBodega=BG.IdBodega INNER JOIN AlmBodegas AS BD ON T.IdBodDtno=BD.IdBodega
 WHERE TipDoc='TRR' AND T.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND T.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND T.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND T.IdConductor LIKE ISNULL(@pmIdConductor,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraTraslados] @pmTipDoc VARCHAR(3),@pmTraslado INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmTipoTraslado INT,@pmIdBodega VARCHAR(4),@pmIdBodDtno VARCHAR(4),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmCantidad INT,@pmPesoTotal DECIMAL(14,4)
,@pmManifiesto INT,@pmCdCiaMuc CHAR(2),@pmEstadoRec INT,@pmFechaRec SMALLDATETIME,@pmCedRecibido VARCHAR(16),@pmNomRecibido VARCHAR(150),@pmObservacion VARCHAR(1000),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraTraslados (TipDoc,Traslado,IdCia,Fecha,FecDespacho,TipoTraslado,IdBodega,IdBodDtno,IdVehiculo,IdConductor,Cantidad,PesoTotal,Manifiesto,CdCiaMuc,EstadoRec,FechaRec,CedRecibido,NomRecibido,Observacion,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmTraslado,@pmIdCia,@pmFecha,@pmFecDespacho,@pmTipoTraslado,@pmIdBodega,@pmIdBodDtno,@pmIdVehiculo,@pmIdConductor,@pmCantidad,@pmPesoTotal,@pmManifiesto,@pmCdCiaMuc,@pmEstadoRec,@pmFechaRec,@pmCedRecibido,@pmNomRecibido,@pmObservacion,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraTraslados] @pmTipDoc VARCHAR(3),@pmTraslado INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraTraslados
 WHERE TipDoc=@pmTipDoc AND Traslado=@pmTraslado AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraTrasladosAnu] @pmTipDoc VARCHAR(3),@pmTraslado INT,@pmIdCia CHAR(2)
,@pmEstadoRec INT,@pmFechaRec SMALLDATETIME,@pmObservacion VARCHAR(1000)
 AS 
 UPDATE Trn_TraTraslados SET EstadoRec=@pmEstadoRec,FechaRec=@pmFechaRec,Observacion=@pmObservacion
 WHERE TipDoc=@pmTipDoc AND Traslado=@pmTraslado AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraTraslados] @pmTipDoc VARCHAR(3),@pmTraslado INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Traslado,IdCia,Fecha,FecDespacho,TipoTraslado,IdBodega,IdBodDtno,IdVehiculo,IdConductor,Cantidad,PesoTotal,Manifiesto,CdCiaMuc,EstadoRec,FechaRec,CedRecibido,NomRecibido,Observacion,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario
 FROM Trn_TraTraslados
 WHERE TipDoc=@pmTipDoc AND Traslado=@pmTraslado AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraTraslados] @pmTipDoc VARCHAR(3),@pmTraslado INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmTipoTraslado INT,@pmIdBodega VARCHAR(4),@pmIdBodDtno VARCHAR(4),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmCantidad INT,@pmPesoTotal DECIMAL(14,4)
,@pmManifiesto INT,@pmCdCiaMuc CHAR(2),@pmEstadoRec INT,@pmFechaRec SMALLDATETIME,@pmCedRecibido VARCHAR(16),@pmNomRecibido VARCHAR(150),@pmObservacion VARCHAR(1000)
 AS 
 UPDATE Trn_TraTraslados SET Fecha=@pmFecha,FecDespacho=@pmFecDespacho,TipoTraslado=@pmTipoTraslado,IdBodega=@pmIdBodega,IdBodDtno=@pmIdBodDtno,IdVehiculo=@pmIdVehiculo,IdConductor=@pmIdConductor,Cantidad=@pmCantidad,PesoTotal=@pmPesoTotal,Manifiesto=@pmManifiesto
,CdCiaMuc=@pmCdCiaMuc,EstadoRec=@pmEstadoRec,FechaRec=@pmFechaRec,CedRecibido=@pmCedRecibido,NomRecibido=@pmNomRecibido,Observacion=@pmObservacion
 WHERE TipDoc=@pmTipDoc AND Traslado=@pmTraslado AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTalleres] @pmIdTaller VARCHAR(4)
 AS 
 DELETE FROM Talleres WHERE IdTaller=@pmIdTaller
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTalleres] @pmIdTaller VARCHAR(4),@pmNomTaller VARCHAR(100),@pmDireccion VARCHAR(250),@pmIdLocal VARCHAR(8),@pmTelefono VARCHAR(30),@pmTelCelular VARCHAR(30),@pmNitTercero VARCHAR(16),@pmTipoTaller VARCHAR(20),@pmInactivo BIT
 AS 
 INSERT INTO Talleres (IdTaller,NomTaller,Direccion,IdLocal,Telefono,TelCelular,NitTercero,TipoTaller,Inactivo)
 VALUES (@pmIdTaller,@pmNomTaller,@pmDireccion,@pmIdLocal,@pmTelefono,@pmTelCelular,@pmNitTercero,@pmTipoTaller,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTalleresLta] 
 AS 
 SELECT IdTaller,NomTaller,T.Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,T.Telefono,T.TelCelular
,T.NitTercero,RazonSocial AS NomTercero,TipoTaller,T.Inactivo
 FROM Talleres AS T INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 LEFT JOIN Terceros AS N ON T.NitTercero=N.IdTercero
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTalleres] @pmIdTaller VARCHAR(4)
 AS 
 SELECT IdTaller,NomTaller,Direccion,IdLocal,Telefono,TelCelular,NitTercero,TipoTaller,Inactivo
 FROM Talleres WHERE IdTaller=@pmIdTaller
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTalleres] @pmIdTaller VARCHAR(4),@pmNomTaller VARCHAR(100),@pmDireccion VARCHAR(250),@pmIdLocal VARCHAR(8),@pmTelefono VARCHAR(30),@pmTelCelular VARCHAR(30),@pmNitTercero VARCHAR(16),@pmTipoTaller VARCHAR(20),@pmInactivo BIT
 AS 
 UPDATE Talleres SET NomTaller=@pmNomTaller,Direccion=@pmDireccion,IdLocal=@pmIdLocal,Telefono=@pmTelefono,TelCelular=@pmTelCelular,NitTercero=@pmNitTercero,TipoTaller=@pmTipoTaller,Inactivo=@pmInactivo
 WHERE IdTaller=@pmIdTaller
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraManifMod] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmVrFleteTotal MONEY,@pmVrRetencion MONEY,@pmVrReteIca MONEY
,@pmVrAnticipo MONEY,@pmPesoTotal DECIMAL(14,4),@pmNumCump INT,@pmCdCiaCump CHAR(2),@pmIdLugarPago VARCHAR(8),@pmFechaPago SMALLDATETIME,@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraManifMod (TipDoc,Manifiesto,IdCia,Item,Fecha,VrFleteTotal,VrRetencion,VrReteIca,VrAnticipo,PesoTotal,NumCump,CdCiaCump,IdLugarPago,FechaPago,TimeSys,IdUsuario)
 VALUES (@pmTipDoc,@pmManifiesto,@pmIdCia,@pmItem,@pmFecha,@pmVrFleteTotal,@pmVrRetencion,@pmVrReteIca,@pmVrAnticipo,@pmPesoTotal,@pmNumCump,@pmCdCiaCump,@pmIdLugarPago,@pmFechaPago,@pmTimeSys,@pmIdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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','VCC')
 AND TipoRef<>'SERVICIO' AND Tanques=1 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY IdCia,K.IdProducto,CdTanque
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCdGrupoDif VARCHAR(4),@pmCantCuotasIni INT
 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,CdGrupoDif,CantCuotasIni)
 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,@pmCdGrupoDif,@pmCantCuotasIni)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpPicoPlaca] @pmFecha SMALLDATETIME,@pmUltimoDig VARCHAR(50)
 AS 
 UPDATE PicoPlaca SET UltimoDig=@pmUltimoDig
 WHERE Fecha=@pmFecha
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryPicoPlaca] @pmFecha SMALLDATETIME
 AS 
 IF @pmFecha IS NULL
  SELECT Fecha,UltimoDig
  FROM PicoPlaca
 ELSE
  SELECT Fecha,UltimoDig
  FROM PicoPlaca
  WHERE Fecha=@pmFecha
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsPicoPlaca] @pmFecha SMALLDATETIME,@pmUltimoDig VARCHAR(50)
 AS 
 INSERT INTO PicoPlaca (Fecha,UltimoDig)
 VALUES (@pmFecha,@pmUltimoDig)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelPicoPlaca] @pmFecha SMALLDATETIME
 AS 
 DELETE FROM PicoPlaca
 WHERE Fecha=@pmFecha
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmFecMatMerc SMALLDATETIME,@pmExonParaf INT,@pmExonCajas INT,@pmCondEspecial VARCHAR(250),@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,FecMatMerc=@pmFecMatMerc,ExonParaf=@pmExonParaf,ExonCajas=@pmExonCajas,CondEspecial=@pmCondEspecial
 WHERE IdAportnte=@pmIdAportnte
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmFecMatMerc SMALLDATETIME
,@pmExonParaf INT,@pmExonCajas INT,@pmCondEspecial VARCHAR(250),@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,FecMatMerc,ExonParaf,ExonCajas,CondEspecial)
 VALUES (@pmIdAportnte,@pmRazonSocial,@pmTipoId,@pmDvNit,@pmIdRepLegal,@pmDireccion,@pmIdLocal,@pmTelefono,@pmFax,@pme_mail,@pmClaseApo,@pmTipoEco,@pmTipoPersna,@pmCIIUDane,@pmFecIniCndato,@pmTipAccCndato
,@pmFecFinCndato,@pmCodOperInfo,@pmTipoApo,@pmFecAdd,@pmIdUsuario,@pmFecMatMerc,@pmExonParaf,@pmExonCajas,@pmCondEspecial)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,FecMatMerc,ExonParaf,ExonCajas,CondEspecial,FecAdd,FecUpdate,IdUsuario
 FROM NomAportante
 WHERE IdAportnte LIKE ISNULL(@pmIdAportnte,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,NumDotac,DiaVigDot,FecVigDot,EdoDocEnt,NumDocEnt,CiaDocEnt
,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,OrigenAdd,IdUsuario
 FROM Trn_Salidas
 WHERE TipDoc=@pmTipDoc AND Salida=@pmSalida AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NumDotac,DiaVigDot,FecVigDot,EdoDocEnt,NumDocEnt,CiaDocEnt,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,S.Observacion AS Observ,S.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,OrigenAdd
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmNumDotac INT,@pmDiaVigDot INT,@pmFecVigDot SMALLDATETIME,@pmEdoDocEnt INT,@pmNumDocEnt INT,@pmCiaDocEnt 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,NumDotac=@pmNumDotac,DiaVigDot=@pmDiaVigDot,FecVigDot=@pmFecVigDot
,EdoDocEnt=@pmEdoDocEnt,NumDocEnt=@pmNumDocEnt,CiaDocEnt=@pmCiaDocEnt,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Salida=@pmSalida AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmNumDotac INT,@pmDiaVigDot INT,@pmFecVigDot SMALLDATETIME,@pmEdoDocEnt INT,@pmNumDocEnt INT,@pmCiaDocEnt 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,NumDotac,DiaVigDot,FecVigDot,EdoDocEnt,NumDocEnt,CiaDocEnt)
 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,@pmNumDotac,@pmDiaVigDot,@pmFecVigDot,@pmEdoDocEnt,@pmNumDocEnt,@pmCiaDocEnt)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NumDotac,DiaVigDot,FecVigDot
,EdoDocEnt,NumDocEnt,CiaDocEnt,Modalidad,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,S.Observacion AS Observ,S.IdEstado AS CdEstado,Estado
,TimeSys,S.FecUpdate AS Fec_Update,IdCiaCrea,OrigenAdd,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_CxPagarID] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem)  AS MXITM
 FROM tm_CxPagar
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_CxPagarTot] @pmtmNumero VARCHAR(5)
 AS 
 SELECT Count(tmItem) AS SCANT,SUM(tmVrFactura) AS SVALTOT
 FROM tm_CxPagar
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpSubzonas] @pmIdSzona VARCHAR(4),@pmSubzona VARCHAR(50),@pmIdZona VARCHAR(4),@pmCdRuta VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE Subzonas SET Subzona=@pmSubzona,IdZona=@pmIdZona,Inactivo=@pmInactivo,Cd_Rut=@pmCdRuta
 WHERE IdSzona=@pmIdSzona
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrySubzonas] @pmIdSzona VARCHAR(4)
 AS 
 SELECT IdSzona,Subzona,IdZona,Cd_Rut,Inactivo
 FROM Subzonas
 WHERE IdSzona=@pmIdSzona
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsSubzonas] @pmIdSzona VARCHAR(4),@pmSubzona VARCHAR(50),@pmIdZona VARCHAR(4),@pmCdRuta VARCHAR(4),@pmInactivo BIT
 AS 
 INSERT INTO Subzonas (IdSzona,Subzona,IdZona,Inactivo,Cd_Rut)
 VALUES (@pmIdSzona,@pmSubzona,@pmIdZona,@pmInactivo,@pmCdRuta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrySubzonasLta] @pmIdZona VARCHAR(4)=Null
 AS 
  SELECT IdSzona,Subzona,S.IdZona AS CdZona,Zona,Cd_Rut,Ruta
  FROM Subzonas AS S INNER JOIN Zonas AS Z ON S.IdZona=Z.IdZona
  LEFT JOIN Rutas AS R ON S.Cd_Rut=R.IdRuta
  WHERE S.Inactivo=0 AND S.IdZona LIKE ISNULL(@pmIdZona,'%')
  ORDER BY Zona,Subzona
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelSubzonas] @pmIdSzona VARCHAR(4)
 AS 
 DELETE FROM Subzonas
 WHERE IdSzona=@pmIdSzona
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelAfClasesCue] @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM AfClasesCue
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelProdAditivos] @pmIdProducto VARCHAR(16),@pmIdAditivo VARCHAR(16)
 AS 
 DELETE FROM ProdAditivos
 WHERE IdProducto=@pmIdProducto AND IdAditivo LIKE ISNULL(@pmIdAditivo,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMttoTrabajos] @pmFecha SMALLDATETIME,@pmIdConc VARCHAR(4),@pmTipoMtto INT,@pmTipoUnidad CHAR(1),@pmIdVehiculo VARCHAR(10),@pmnRemolque VARCHAR(10),@pmTipDoc VARCHAR(3)
,@pmNumOrden INT,@pmIdCia CHAR(2),@pmNumParte VARCHAR(20),@pmCantHoras INT,@pmkmActual INT,@pmKmRuta INT,@pmkmOtro INT,@pmkmAntMtto INT,@pmKmAntMtto2 INT,@pmkmAntMtto3 INT,@pmFecAntMtto SMALLDATETIME
 AS 
 INSERT INTO Trn_MttoTrabajos (Fecha,IdConc,TipoMtto,TipoUnidad,IdVehiculo,nRemolque,TipDoc,NumOrden,IdCia,NumParte,CantHoras,kmActual,KmRuta,kmOtro,kmAntMtto,KmAntMtto2,kmAntMtto3,FecAntMtto)
 VALUES (@pmFecha,@pmIdConc,@pmTipoMtto,@pmTipoUnidad,@pmIdVehiculo,@pmnRemolque,@pmTipDoc,@pmNumOrden,@pmIdCia,@pmNumParte,@pmCantHoras,@pmkmActual,@pmKmRuta,@pmkmOtro,@pmkmAntMtto,@pmKmAntMtto2,@pmkmAntMtto3,@pmFecAntMtto)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmVrPrecio,tmVrBase,tmVrPresp
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero AND tmCredito=@pmtmCredito
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_EdsFormasCue] @pmtmNumero VARCHAR(5),@pmIdConcepto VARCHAR(4)
 AS 
 SELECT tmIdForma,IdConcepto,IdCuenta
 FROM tm_EdsFormas AS EF LEFT JOIN FormasCue AS F ON EF.tmIdForma=F.IdForma
 WHERE tmNumero=@pmtmNumero AND tmCredito=0
 AND (ISNULL(IdConcepto,'')=@pmIdConcepto OR tmTipoRango='TKT')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmtmVrPrecio MONEY,@pmtmVrBase MONEY,@pmtmVrPresp MONEY
 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,tmVrPrecio,tmVrBase,tmVrPresp)
 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,@pmtmVrPrecio,@pmtmVrBase,@pmtmVrPresp)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_EdsFormasItm] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 tmTipoRango<>'TKT' AND tmIdForma='EFE'
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,SUM(CASE WHEN tmVrPrecio>0 AND tmCantidad>0 AND tmVrTotal>(tmCantidad*tmVrPrecio) THEN tmVrTotal-(tmCantidad*tmVrPrecio) ELSE 0 END) AS STOTEXCD
,SUM(CASE WHEN tmVrPrecio>0 AND tmCantidad>0 AND (tmCantidad*tmVrPrecio)>tmVrTotal THEN (tmCantidad*tmVrPrecio)-tmVrTotal ELSE 0 END) AS STOTDCT
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero 
 AND (tmCredito=ISNULL(@pmtmCredito,0) or tmCredito=ISNULL(@pmtmCredito,1))
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_EdsFormas_Fac] @pmtmNumero VARCHAR(5),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm 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,tmVrPrecio,tmVrBase,tmVrPresp)
SELECT @pmtmNumero,ROW_NUMBER() OVER(ORDER BY Planilla),1,TipoRango,IdRango,IdForma,Observacion,0,'0','0',Fecha,'0','',NumSerie,NumForma,VrTotal,IdCliente,IdAgencia,IdCuenta,pVehiculo,nVehiculo
,IdVend,ISNULL(CdProducto,'0'),Cantidad,Referencia,NumRef2,CantPuntos,'0','',0,0,'B','0','',IdCajero,CdCCosto,CdSubCos,VrPrecio,VrBase,VrPrecioEsp
FROM Trn_EdsFormas
WHERE TipDcm=@pmTipDcm AND Documento=@pmDocumento AND IdCiaDcm=@pmIdCiaDcm
AND Planilla>0 AND IdCia<>'' AND IdCia<>'00'
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmVrPrecio,tmVrBase,tmVrPresp
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero 
  AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmVrPrecio,tmVrBase,tmVrPresp)
 SELECT @pmtmNumero,Item,1,TipoRango,IdRango,IdForma,Observacion,0,'0','0',Fecha,'0','',NumSerie,NumForma,VrTotal,IdCliente,IdAgencia,IdCuenta,pVehiculo,nVehiculo
,IdVend,ISNULL(CdProducto,'0'),Cantidad,Referencia,NumRef2,CantPuntos,'0','',0,0,'B','0','',IdCajero,CdCCosto,CdSubCos,VrPrecio,VrBase,VrPrecioEsp
 FROM Trn_EdsFormas
 WHERE Planilla=@pmPlanilla AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmVrPrecio,tmVrBase,tmVrPresp)
 SELECT @pmtmNumero,Item,0,CASE WHEN FE_Ticket=1 THEN 'TKT' ELSE '0' END,0,IdForma,Detalle,EsCaja,IdBanco,NumForma,FecForma,CtaForma,Beneficiario,'0',0
,VrPagado,NitCliente,CdAgencia,'0','0','0','0',ISNULL(CdProducto,'0'),Cant_Gals,Referncia1,Referncia2,0,CdLocal,NumAutoriza,VrDenom,Cantidad,TipDenom,CdCta,'',IdUsuario,'0','0',0,PrecioBase,PrecioUnit
 FROM Trn_Pagos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmCdCCosto,tmCdSubCos
--VARIABLES DE ITEM
,Item,IdProducto,IdBodega,Cantidad,VrPrecio,TarifaIva,VrIva,TarifaDct,VrDcto,Servicios,Descripcion,ListaPrec,CantDev
,CdOperario,Comision
 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 AND (I.Cantidad-I.CantDev)>0
 ORDER BY tmItem,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsRoles] @pmIdObj VARCHAR(10),@pmIdRole VARCHAR(3),@pmFuncion VARCHAR(100)
 AS 
 INSERT INTO Sys_Roles (IdObj,IdRole,Funcion)
 VALUES (@pmIdObj,@pmIdRole,@pmFuncion)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelRoles] @pmIdObj VARCHAR(10),@pmIdRole VARCHAR(3)=Null
 AS 
 DELETE FROM Sys_Roles
 WHERE IdObj=@pmIdObj AND IdRole LIKE ISNULL(@pmIdRole,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[paInsAcuOrdenesUni] @pmIdVehiculo VARCHAR(10),@pmIdVehiculoDos VARCHAR(10),@pmNewVehiculo VARCHAR(10)
AS
IF EXISTS (SELECT * FROM AcuOrdenes WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos)
 INSERT INTO AcuOrdenes (nAnno,nMes,IdCia,IdPoseedor,IdVehiculo,SaldoAnt,VrIngresos,VrCausac)
 SELECT nAnno,nMes,IdCia,IdPoseedor,@pmNewVehiculo,SUM(SaldoAnt),SUM(VrIngresos),SUM(VrCausac)
 FROM AcuOrdenes WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos
 GROUP BY nAnno,nMes,IdCia,IdPoseedor
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuOrdenes_Nit] @pmIdPoseedor VARCHAR(16),@pmIdPoseedorDos VARCHAR(16),@pmNewIdPoseedor VARCHAR(16)
 AS 
 IF EXISTS (SELECT * FROM AcuOrdenes WHERE IdPoseedor=@pmIdPoseedor OR IdPoseedor=@pmIdPoseedorDos)
  INSERT INTO AcuOrdenes (nAnno,nMes,IdCia,IdPoseedor,IdVehiculo,SaldoAnt,VrIngresos,VrCausac)
  SELECT nAnno,nMes,IdCia,@pmNewIdPoseedor,IdVehiculo,SUM(SaldoAnt),SUM(VrIngresos),SUM(VrCausac)
  FROM AcuOrdenes WHERE IdPoseedor=@pmIdPoseedor OR IdPoseedor=@pmIdPoseedorDos
  GROUP BY nAnno,nMes,IdCia,IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuOrdenes_Uni] @pmIdVehiculo VARCHAR(10),@pmIdVehiculoDos VARCHAR(10),@pmNewVehiculo VARCHAR(10)
 AS 
 IF EXISTS (SELECT * FROM AcuOrdenes WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos)
  INSERT INTO AcuOrdenes (nAnno,nMes,IdCia,IdPoseedor,IdVehiculo,SaldoAnt,VrIngresos,VrCausac)
  SELECT nAnno,nMes,IdCia,IdPoseedor,@pmNewVehiculo,SUM(SaldoAnt),SUM(VrIngresos),SUM(VrCausac)
  FROM AcuOrdenes WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos
  GROUP BY nAnno,nMes,IdCia,IdPoseedor
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmtmNitTercero VARCHAR(16)
 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,tmNitTercero=@pmtmNitTercero
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmtmNitTercero VARCHAR(16)
 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,tmNitTercero)
 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,@pmtmNitTercero)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmNitTercero
 FROM tm_Liquida
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmNitTercero)
 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,NitTercero
 FROM Trn_NomLiqDet
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero AND IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmNitTercero
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_LiquidaId] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITEM
 FROM tm_Liquida
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTurnos] @pmIdTurno VARCHAR(4),@pmTurno VARCHAR(50),@pmHoraInicial VARCHAR(10),@pmHoraFinal VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE Turnos SET Turno=@pmTurno,HoraInicial=@pmHoraInicial,HoraFinal=@pmHoraFinal,Inactivo=@pmInactivo
 WHERE IdTurno=@pmIdTurno
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTurnos] @pmIdTurno VARCHAR(4),@pmTurno VARCHAR(50),@pmHoraInicial VARCHAR(10),@pmHoraFinal VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO Turnos (IdTurno,Turno,HoraInicial,HoraFinal,Inactivo)
 VALUES (@pmIdTurno,@pmTurno,@pmHoraInicial,@pmHoraFinal,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTurnos] @pmIdTurno VARCHAR(4)
 AS 
 SELECT IdTurno,Turno,HoraInicial,HoraFinal,Inactivo
 FROM Turnos
 WHERE IdTurno=@pmIdTurno
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMayCesion] @pmNumero INT,@pmFecha SMALLDATETIME,@pmNContrato INT,@pmIdCia CHAR(2),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdNueClie VARCHAR(16),@pmIdNueAgenc VARCHAR(16)
,@pmFecInicio SMALLDATETIME,@pmClaseCont VARCHAR(10),@pmCdTipoCont VARCHAR(4),@pmCuotaBase DECIMAL(14,4),@pmCuotaMin DECIMAL(14,4),@pmAcumGals DECIMAL(14,4),@pmObservacion VARCHAR(250),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_MayCesion (Numero,Fecha,NContrato,IdCia,IdCliente,IdAgencia,IdNueClie,IdNueAgenc,FecInicio,ClaseCont,CdTipoCont,CuotaBase,CuotaMin,AcumGals,Observacion,TimeSys,IdUsuario)
 VALUES (@pmNumero,@pmFecha,@pmNContrato,@pmIdCia,@pmIdCliente,@pmIdAgencia,@pmIdNueClie,@pmIdNueAgenc,@pmFecInicio,@pmClaseCont,@pmCdTipoCont,@pmCuotaBase,@pmCuotaMin,@pmAcumGals,@pmObservacion,@pmTimeSys,@pmIdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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.TarifaArf,D.VrAutRetFte,D.TarifaAri,D.VrAutRetIca,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[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,D.TipDev,D.Devolucion,D.IdCia,0,D.Fecha,D.IdCliente,D.IdAgencia,D.IdVend,0,D.VrNeto,0,D.TimeSys,D.Fecha,D.IdConcepto,'',D.Observacion,'','0',0,0,0,0,D.TipDoc,D.Factura,D.IdCiaDoc
,'','0','','',0,0,0,D.TipCom,D.Comprobante,D.IdCiaCom,0,0,D.Fecha,D.IdUsuario,0,0,0,0
 FROM Trn_FinDevFac AS D INNER JOIN Trn_FinFactint 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 EXISTS (SELECT * FROM Trn_FinCuotas WHERE Trn_FinCuotas.TipDoc=D.TipDoc AND Trn_FinCuotas.IdPrestamo=D.Factura AND Trn_FinCuotas.IdCia=D.IdCiaDoc) 
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND D.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TarifaArf,VrAutRetFte,TarifaAri,VrAutRetIca,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY,@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,TarifaArf,VrAutRetFte,TarifaAri,VrAutRetIca)
 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,@pmTarifaArf,@pmVrAutRetFte,@pmTarifaAri,@pmVrAutRetIca)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY,@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,TarifaArf=@pmTarifaArf,VrAutRetFte=@pmVrAutRetFte,TarifaAri=@pmTarifaAri,VrAutRetIca=@pmVrAutRetIca,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoProp
  FROM   CtasCorrientes AS C INNER JOIN Bancos ON C.IdBanco=Bancos.IdBanco
  WHERE  IdCuenta=@pmIdCuenta
  ORDER BY IdCta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CN.Concepto,Beneficiario,VrDivisa1,VrDivisa2,VrDivisa3
,PermEditar,C.Integrado AS EsIntegracion,C.EsEgreso AS ComEsEgreso,Anticipo,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,C.DocSoporte,C.CodRes,C.DescServicio
,Propddes,C.TimeSys AS Fec_Add,C.FecUpdate AS Fec_Update,C.IdCiaCrea AS CdCiaCrea,C.IdUsuario AS CdUsuario,Usuario,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut
--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,CdConcTrib,TB.Concepto AS DescConcTrib,CdTarifTrib,NumEstablec,PtoEmision,Num_Autoriza,FechAutoriza
--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
,RS.Resolucion,RS.Prefijo,RS.NumInicial,RS.NumFinal,RS.FechaExpRes,RS.FechaVigencia,RS.Establecimiento
 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 LEFT JOIN ConceptosTrib AS TB ON D.CdConcTrib=TB.IdConcepto
 LEFT JOIN TiposComRes AS RS ON C.CodRes=RS.IdRes
 WHERE C.Comprobante BETWEEN @pmComprobanteIni AND @pmComprobanteFin AND C.IdCia=@pmIdCia
 AND  C.TipCom LIKE ISNULL(@pmTipCom,'%') 
 ORDER BY C.Comprobante,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTercerosCta] @pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT T.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,ImgFoto,ImgFirma,ImagenDoc1,ImagenDoc2,ImagenDoc3,FechaNac
,Item,TC.IdClase AS CdClase,ClaseCuenta,NumCuenta,TC.IdBanco AS CdBanco,Banco,CdLugarCue,CC.Localidad AS LugarCuenta
 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 
 LEFT JOIN TercCuentas AS TC ON T.IdTercero=TC.IdTercero
 LEFT JOIN ClaseCta AS CL ON TC.IdClase=CL.IdClase LEFT JOIN Bancos AS B ON TC.IdBanco=B.IdBanco
 LEFT JOIN Localidades AS CC ON TC.CdLugarCue=CC.IdLocal
 WHERE (@pmIdEstado IS NULL OR T.IdEstado=@pmIdEstado)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,FecConsig,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,D.TipDocRef,D.DocRef,D.IdCiaRef,D.DocSop,D.TipSop,D.NumSoporte,D.IdCiaSop,D.TipoImp,D.CodTarImp,D.ItemBase
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoProp,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,NiifTipo,NiifNumero,FisTipo,FisNumero,C.Observacion AS Observ,C.IdEstado AS CodEstado,Estado,Propddes
,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS IdUsuari,Usuario,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut,DocSoporte,CodRes,DescServicio
,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,TCA.NitAutoriza,NC.RazonSocial AS NomAutorizado
 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
 LEFT JOIN (SELECT IdTercero,NumCuenta,MAX(NitTercAut) AS NitAutoriza FROM TercCuentas GROUP BY IdTercero,NumCuenta) AS TCA ON C.IdTercero=TCA.IdTercero AND C.Establec=TCA.NumCuenta
 LEFT JOIN Terceros AS NC ON TCA.NitAutoriza=NC.IdTercero
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNiifDetalleLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmTipCom VARCHAR(3)=Null
,@pmIdCia CHAR(2)=Null,@pmTipoNiif VARCHAR(10)=Null
 AS 
 SELECT C.TipCom AS IdTipoComp,TipoCom,C.Comprobante AS NumComp,C.IdCia AS CdCia,Compania,C.Fecha AS FecComp,C.TipoNiif,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,FisTipo,FisNumero,pVehiculo
--detalles de comprobantes
,Item,D.IdCuenta AS CdCuenta,P.NomCuenta,CodCuentaCG,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_NiifComp AS C INNER JOIN Trn_NiifDetalle 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 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 PucNiif 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 CtasCorrientes AS CTE ON C.IdCta=CTE.IdCta LEFT JOIN Bancos AS B ON CTE.IdBanco=B.IdBanco
 LEFT JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 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 (@pmTipCom IS NULL OR C.TipCom=@pmTipCom)
 AND (@pmIdCia IS NULL OR C.IdCia=@pmIdCia)
 AND (@pmTipoNiif IS NULL OR C.TipoNiif=@pmTipoNiif)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryComDetalleDmto] @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin 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,T.RazonSocial AS NomTercero
,IdVehiculo,D.IdCCosto AS CdCCosto,CCosto,D.IdSubCos AS CdSubCosto,SubCosto,VehPropio,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,Concepto
,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumeroCta,CTA.IdBanco AS CdBanco,Banco,NumCheque,Integrado,TipoAplica,Consolida,CodCargo
,NitOtros,N.RazonSocial AS NomTerceroOtros,CodSubgpo,CiuOrigen,Localidad,CodAgncia,A.Agencia AS NomAgencia,Referncia,NitDoc,TipDocRef,DocRef,IdCiaRef
,TimeSys,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
 FROM Trn_ComDetalle AS D INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 LEFT JOIN CentroCosto AS O ON D.IdCCosto=O.IdCCosto  LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 LEFT JOIN Agencias AS A ON D.CodAgncia=A.IdAgencia LEFT JOIN Conceptos AS CN ON D.CodConce=CN.IdConcepto
 LEFT JOIN CtasCorrientes AS CTA ON D.CodCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON CTA.IdBanco=BCT.IdBanco
 LEFT JOIN Terceros AS N ON D.NitOtros=N.IdTercero LEFT JOIN Localidades AS DL ON D.CiuOrigen=DL.IdLocal
 WHERE TipDoc=@pmTipDoc AND Documento  BETWEEN @pmDocumentoIni AND @pmDocumentoFin AND D.IdCia=@pmIdCia
 AND Integrado=1
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNiifCompLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmTipCom VARCHAR(3)=Null
,@pmIdCia CHAR(2)=Null,@pmTipoNiif VARCHAR(10)=Null
 AS 
 SELECT TipCom,TipoCom,Comprobante,C.IdCia AS CdCia,Compania,Fecha,C.TipoNiif,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,CtbTipo,CtbNumero,FisTipo,FisNumero,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,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut,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,TCA.NitAutoriza,NC.RazonSocial AS NomAutorizado
 FROM Trn_NiifComp 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 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 Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 LEFT JOIN CtasCorrientes AS CTE ON C.IdCta=CTE.IdCta LEFT JOIN Bancos AS B ON CTE.IdBanco=B.IdBanco
 LEFT JOIN Terceros AS CT ON C.CedCondtor=CT.IdTercero
 LEFT JOIN Conceptos AS CN ON C.CodConce=CN.IdConcepto
 LEFT JOIN (SELECT IdTercero,NumCuenta,MAX(NitTercAut) AS NitAutoriza FROM TercCuentas GROUP BY IdTercero,NumCuenta) AS TCA ON C.IdTercero=TCA.IdTercero AND C.Establec=TCA.NumCuenta
 LEFT JOIN Terceros AS NC ON TCA.NitAutoriza=NC.IdTercero
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND (@pmTipCom IS NULL OR TipCom=@pmTipCom)
 AND (@pmIdCia IS NULL OR C.IdCia=@pmIdCia)
 AND (@pmTipoNiif IS NULL OR C.TipoNiif=@pmTipoNiif)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryBancosEnt] @pmCodEntidad VARCHAR(20)
 AS 
  SELECT IdBanco,Banco,IdClase,CodEntidad,Direccion,Contacto,Comision,Inactivo
  FROM Bancos
  WHERE CodEntidad=@pmCodEntidad
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTercClieGarantLta] @pmIdClie VARCHAR(16),@pmIdLinea VARCHAR(4)
 AS 
 SELECT CG.IdClie AS NitClie,RazonSocial,CG.IdAgencia AS CdAgencia,NomAgencia,CG.IdLinea AS CdLinea,LinCredito,Item,TipoGarant,CdInmueble,TipoInmueble
,CdTipoVeh,TipoVehiculo,VrGarant,VrAvaluo,Propietario,DirInmueble,CdCiudad,Localidad,Matricula,Escritura
,NumNotaria,MarcaVeh,Modelo,CdBanco,Banco,FecCheque,NumCheque,OtroTipoVehic,OtroInmueble,CedFmnte1,NomFmnte1,CedFmnte2,NomFmnte2
,CedFmnte3,NomFmnte3,CedFmnte4,NomFmnte4,CedFmnte5,NomFmnte5,CedFmnte6,NomFmnte6
,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero
 FROM TercClieGarant AS CG INNER JOIN TercCliePrestamo AS CLI ON CG.IdClie=CLI.IdClie AND CG.IdAgencia=CLI.IdAgencia
 INNER JOIN Terceros AS T ON CG.IdClie=T.IdTercero INNER JOIN LineasCred AS L ON CG.IdLinea=L.IdLinea
 INNER JOIN TiposTerc AS TC ON CLI.IdTipoTerc=TC.IdTipoTerc
 LEFT JOIN TiposInm AS IM ON CG.CdInmueble=IM.IdTipoInm LEFT JOIN TiposVeh AS TV ON CG.CdTipoVeh=TV.IdTipoVeh
 LEFT JOIN Bancos AS B ON CG.CdBanco=B.IdBanco LEFT JOIN Localidades AS LC ON CG.CdCiudad=LC.IdLocal
 WHERE CG.IdClie LIKE ISNULL(@pmIdClie,'%') AND CG.IdLinea LIKE ISNULL(@pmIdLinea,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryComprobantesEgo] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdTercero VARCHAR(16)=Null
 AS 
 SELECT C.TipCom AS CdTipEgr,TipoCom,C.Comprobante AS NumEgreso,C.IdCia AS CdCiaEgr,Compania,C.Fecha AS FecEgreso,T.TipoId AS TercTipo,C.IdTercero AS NitTercero,T.Dv AS TercDv
,T.RazonSocial AS NomTercero,VrTotal,C.IdCta AS CdCtaCte,NumeroCta,CTA.IdBanco AS CdBanco,Banco,EnEfectivo,C.NumCheque AS NoCheque,FecCheque
,TipDoc,Documento,IdCiaDoc,Anulado,NumDev,FecDev,pVehiculo,C.VehPropio AS VehEsPropio,CedCondtor,CD.RazonSocial AS NomConductor,Beneficiario,Anticipo,C.Observacion AS Observ
 FROM Trn_Comprobantes AS C 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
 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 
 WHERE C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.EsEgreso=1 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdTercero LIKE ISNULL(@pmIdTercero,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoProp
  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,TipoProp
  FROM   CtasCorrientes AS C INNER JOIN Bancos ON C.IdBanco=Bancos.IdBanco
   WHERE  NumeroCta=@pmNumeroCta
  ORDER BY IdCta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryBancosDso]
AS
 SELECT  IdBanco,Banco,IdBanco+'  '+ Banco AS DsBan  
 FROM   Bancos  
 WHERE Inactivo=0   
 ORDER BY  IdBanco
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNiifRevelacLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmTipCom VARCHAR(3),@pmIdCia CHAR(2)
 AS 
 SELECT R.TipCom,TipoCom,R.Comprobante AS NumComp,R.IdCia AS CdCia,Compania,C.Fecha,C.IdTercero AS NitTercero,T.RazonSocial,C.VrTotal,C.IdCta AS CdCta,NumeroCta
,CTA.IdBanco AS CdBanco,Banco,EnEfectivo,C.NumCheque AS ComNumCheque,FecCheque,Anulado,NumDev,FecDev,pVehiculo,C.VehPropio,CedCondtor,CD.RazonSocial AS NomConductor
,C.CodConce,CN.Concepto,Beneficiario,C.Integrado AS EsIntegracion,C.EsEgreso AS ComEsEgreso,C.Observacion AS Observ,C.IdEstado AS CdEstado
,TipoNiif,CtbTipo,CtbNumero,FisTipo,FisNumero,R.Item,R.TipoRev,R.Titulo,R.Comentario,R.CdCuenta,NomCuenta,R.FechaCrea,R.IdUsuario AS CdUsuario,Usuario
 FROM Trn_NiifRevelac AS R INNER JOIN Trn_NiifComp AS C ON R.TipCom=C.TipCom AND R.Comprobante=C.Comprobante AND R.IdCia=C.IdCia
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia  INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 INNER JOIN TiposCom AS TC ON R.TipCom=TC.IdCom INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero
 LEFT JOIN Conceptos AS CN ON C.CodConce=CN.IdConcepto LEFT JOIN Terceros AS CD ON C.CedCondtor=CD.IdTercero 
 LEFT JOIN CtasCorrientes AS CTA ON C.IdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON CTA.IdBanco=BCT.IdBanco
 LEFT JOIN PucNiif AS P ON R.CdCuenta=P.IdCuenta
 WHERE C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND R.TipCom LIKE ISNULL(@pmTipCom,'%')
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraAnticiposLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null
,@pmIdCta VARCHAR(4)=Null
 AS 
 SELECT  Anticipo,A.IdCia AS CdCia,Compania,Fecha,TipMuc,Manifiesto,IdCiaMuc,A.IdVehiculo AS PlacaVeh,IdPoseedor,T.RazonSocial AS Poseedor,IdConductor,CDT.RazonSocial AS Conductor,VrAnticipo,VrAbonado,Modalidad
,A.IdConcepto AS CdConcepto,Concepto,TipoPago,NumCheque,FecCheque,A.IdCta AS CdCta,NumeroCta,CT.IdBanco,Banco,A.IdCuenta AS CdCuenta,CedBenef,Beneficiario,VrDeduccion,Cantidad,FechaVence,NumPresAnt,PreAnticipo,NumCargue,CiaCargue
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,A.Observacion AS Observ,A.IdEstado AS CdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,A.IdUsuario AS Cdusuario,Usuario
 FROM Trn_TraAnticipos AS A INNER JOIN Terceros AS T ON A.IdPoseedor=T.IdTercero INNER JOIN Terceros AS CDT ON A.IdConductor=CDT.IdTercero
 INNER JOIN Conceptos AS C ON A.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia
 LEFT JOIN CtasCorrientes AS CT ON A.IdCta=CT.IdCta LEFT JOIN Bancos AS B ON CT.IdBanco=B.IdBanco
 WHERE   Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdVehiculo  LIKE ISNULL(@pmIdVehiculo ,'%') AND A.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 AND A.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')   AND A.Modalidad LIKE ISNULL(@pmModalidad,'%') 
 AND A.IdCta LIKE ISNULL( @pmIdCta,'%') 
 ORDER BY A.IdCia,Anticipo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelBancos] @pmIdBanco VARCHAR(4)
 AS 
 DELETE FROM Bancos
 WHERE IdBanco=@pmIdBanco
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNiifRevelacLtc] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmTipCom VARCHAR(3),@pmIdCia CHAR(2)
 AS 
 SELECT R.TipCom,TipoCom,R.Comprobante AS NumComp,R.IdCia AS CdCia,Compania,C.Fecha,C.IdTercero AS NitTercero,T.RazonSocial,C.VrTotal,C.IdCta AS CdCta,NumeroCta
,CTA.IdBanco AS CdBanco,Banco,EnEfectivo,C.NumCheque AS ComNumCheque,FecCheque,Anulado,NumDev,FecDev,pVehiculo,C.VehPropio,CedCondtor,CD.RazonSocial AS NomConductor
,C.CodConce,CN.Concepto,Beneficiario,C.Integrado AS EsIntegracion,C.EsEgreso AS ComEsEgreso,C.Observacion AS Observ,C.IdEstado AS CdEstado,NiifTipo,NiifNumero,FisTipo,FisNumero
,R.Item,R.TipoRev,R.Titulo,R.Comentario,R.CdCuenta,NomCuenta,R.FechaCrea,R.IdUsuario AS CdUsuario,Usuario
 FROM Trn_NiifRevelac AS R INNER JOIN Trn_Comprobantes AS C ON R.TipCom=C.TipCom AND R.Comprobante=C.Comprobante AND R.IdCia=C.IdCia
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia  INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 INNER JOIN TiposCom AS TC ON R.TipCom=TC.IdCom INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero
 LEFT JOIN Conceptos AS CN ON C.CodConce=CN.IdConcepto LEFT JOIN Terceros AS CD ON C.CedCondtor=CD.IdTercero 
 LEFT JOIN CtasCorrientes AS CTA ON C.IdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON CTA.IdBanco=BCT.IdBanco
 LEFT JOIN Puc AS P ON R.CdCuenta=P.IdCuenta
 WHERE C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND R.TipCom LIKE ISNULL(@pmTipCom,'%')
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NiifTipo,NiifNumero,FisTipo,FisNumero,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,CdCuentaNiif
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNiifComp_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,CN.Concepto,Beneficiario,VrDivisa1,VrDivisa2,VrDivisa3
,PermEditar,C.Integrado AS EsIntegracion,C.EsEgreso AS ComEsEgreso,Anticipo,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,TipoNiif
,Propddes,C.TimeSys AS Fec_Add,C.FecUpdate AS Fec_Update,C.IdCiaCrea AS CdCiaCrea,C.IdUsuario AS CdUsuario,Usuario,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut
,CtbTipo,CtbNumero,FisTipo,FisNumero
--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,CdConcTrib,TB.Concepto AS DescConcTrib,CdTarifTrib,NumEstablec,PtoEmision,Num_Autoriza,FechAutoriza,CodCuentaCG
--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_NiifComp AS C INNER JOIN Trn_NiifDetalle 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 PucNiif 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 LEFT JOIN ConceptosTrib AS TB ON D.CdConcTrib=TB.IdConcepto
 WHERE C.TipCom=@pmTipCom AND C.Comprobante BETWEEN @pmComprobanteIni AND @pmComprobanteFin AND C.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_NiifMov] @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,CodCuentaCG,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_NiifMov 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 PucNiif 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraAntAbonosFmt] @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT Documento,AP.IdCia AS CdCia,Item,AP.Fecha AS FechaDoc,TipAnt,AP.Anticipo AS NumAntcpo,IdCiaAnt,VrAbono,TipoAplica,Detalle,CdCuentaAnt,A.Fecha AS FechaAnt,TipMuc,A.Manifiesto AS NumManif,IdCiaMuc,A.IdVehiculo AS PlacaVeh
 ,A.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,A.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,A.VrAnticipo AS ValAnticipo,VrAbonado,Modalidad,TipoPago,NumCheque,FecCheque
,A.IdCta AS CdCta,NumeroCta,CTA.IdBanco,Banco,A.IdCuenta AS CdCuenta,NomCuenta,Beneficiario,A.Cantidad AS CantGals,FechaVence,NumPresAnt,TipCom,Comprobante,IdCiaCom
,A.Anulado AS Anuldo,NumDev,A.FecDev AS FechaDev,A.Observacion AS Observ,AP.TipDoc AS Tip_Doc
 FROM Trn_TraAntAbonos AS AP  INNER JOIN  Trn_TraAnticipos AS A ON AP.TipAnt=A.TipDoc  AND AP.Anticipo=A.Anticipo  AND AP.IdCiaAnt=A.IdCia
 INNER JOIN Terceros AS  CDT ON A.IdConductor=CDT.IdTercero  INNER JOIN Terceros AS T ON A.IdPoseedor=T.IdTercero
 LEFT JOIN CtasCorrientes AS CTA ON A.IdCta=CTA.IdCta LEFT JOIN Bancos AS B ON CTA.IdBanco=B.IdBanco
 LEFT JOIN Puc AS P ON A.IdCuenta=P.IdCuenta
 WHERE AP.TipDoc=@pmTipDoc AND AP.Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin AND AP.IdCia=@pmIdCia
 ORDER BY  AP.Documento
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,D.TipDocRef,D.DocRef,D.IdCiaRef,D.DocSop,D.TipSop,D.NumSoporte,D.IdCiaSop,D.TipoImp,D.CodTarImp,D.ItemBase
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTercCndCapacLta] 
 AS 
 SELECT IdConductor,RazonSocial,Item,C.IdClase AS CdClase,NomClase,C.Fecha,C.FecVence,Habilitado
 FROM TercCndCapac AS C INNER JOIN ClaseCapac AS CL ON C.IdClase=CL.IdClase
 INNER JOIN Terceros AS T ON C.IdConductor=T.IdTercero
 WHERE CL.Inactivo=0
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryClaseCapac] @pmIdClase VARCHAR(4)
 AS 
 IF @pmIdClase IS NULL
  SELECT IdClase,NomClase FROM ClaseCapac WHERE Inactivo=0
 ELSE
  SELECT IdClase,NomClase,Inactivo FROM ClaseCapac WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsClaseCapac] @pmIdClase VARCHAR(4),@pmNomClase VARCHAR(150),@pmInactivo BIT
 AS 
 INSERT INTO ClaseCapac (IdClase,NomClase,Inactivo)
 VALUES (@pmIdClase,@pmNomClase,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelClaseCapac] @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseCapac WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpClaseCapac] @pmIdClase VARCHAR(4),@pmNomClase VARCHAR(150),@pmInactivo BIT
 AS 
 UPDATE ClaseCapac SET NomClase=@pmNomClase,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEmp_EstudiosDso] @pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT ER.IdEmpleado AS IdEmpldo,Apellidos,Nombres,Item,Estblecmto,Titulo,Fecha,FecVence,ER.IdClase,NomClase,Habilitado
 FROM Emp_Estudios AS ER INNER JOIN Empleados AS E ON ER.IdEmpleado=E.IdEmpleado
 LEFT JOIN ClaseCapac AS C ON ER.IdClase=C.IdClase
 WHERE ER.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 ORDER BY ER.IdEmpleado,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentas_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuFisCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,0,SUM(D.VrDebito),SUM(D.VrCredito)
 FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
 INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='FISCAL' AND P.Movimiento<>0
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuFisCuentasSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuFisCuentas SET AcuFisCuentas.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuFisCuentas INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuFisCuentas WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt) AS SA ON AcuFisCuentas.IdCia=SA.IdCia AND AcuFisCuentas.IdCuenta=SA.IdCuenta
 WHERE AcuFisCuentas.nAnno=@pmnAnno AND AcuFisCuentas.nMes=@pmnMes
 AND (@pmIdCia IS NULL OR AcuFisCuentas.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuFisCuentas.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentas] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCuenta VARCHAR(16),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuFisCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentas_Uni] @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuFisCuentas WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
  INSERT INTO AcuFisCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT nAnno,nMes,IdCia,@pmNewCuenta,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  FROM AcuFisCuentas
  WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
  GROUP BY nAnno,nMes,IdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryAcuFisCuentasMay] @pmnAnno INT,@pmnMes INT,@pmNiv INT,@pmIdCia CHAR(2)=Null
 AS 
 SELECT SUBSTRING(A.IdCuenta,1,@pmNiv) AS CdCuenta,NomCuenta,Naturaleza
,SUM(A.SaldoAnterior) AS tSaldoAnterior,SUM(A.TotalDebitos) AS tTotalDebitos,SUM(A.TotalCreditos) AS tTotalCreditos
,SUM(A.SaldoAnterior+A.TotalDebitos-A.TotalCreditos) AS NuevoSaldo
 FROM AcuFisCuentas AS A INNER JOIN PucNiif AS P ON SUBSTRING(A.IdCuenta,1,@pmNiv)=P.IdCuenta
 WHERE A.nAnno=@pmnAnno AND A.nMes=@pmnMes AND (@pmIdCia IS NULL OR A.IdCia=@pmIdCia)
 GROUP BY SUBSTRING(A.IdCuenta,1,@pmNiv),NomCuenta,Naturaleza
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentas_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuFisCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuFisCuentas AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt 
 AND NOT EXISTS (SELECT * FROM AcuFisCuentas AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes  
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryAcuFisCuentasMayCia] @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(A.SaldoAnterior) AS tSaldoAnterior,SUM(A.TotalDebitos) AS tTotalDebitos,SUM(A.TotalCreditos) AS tTotalCreditos
,SUM(A.SaldoAnterior+A.TotalDebitos-A.TotalCreditos) AS NuevoSaldo
 FROM AcuFisCuentas AS A INNER JOIN PucNiif AS P ON SUBSTRING(A.IdCuenta,1,@pmNiv)=P.IdCuenta
 INNER JOIN Companias AS C ON A.IdCia=C.IdCia
 WHERE A.nAnno=@pmnAnno AND A.nMes=@pmnMes AND (@pmIdCia IS NULL OR A.IdCia=@pmIdCia)
 GROUP BY SUBSTRING(A.IdCuenta,1,@pmNiv),NomCuenta,Naturaleza,A.IdCia,Compania
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraTalonariosLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
 AS 
 SELECT IdTalonario,Fecha,NumSerie,NumInicial,NumFinal,NumAuto,NumActual,IdRespons,NomRespons
,Anulado,TimeSys,T.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraTalonarios AS T INNER JOIN adm_Usuarios AS U ON T.IdUsuario=U.IdUsuario
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 ORDER BY IdTalonario
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraTalonarios] @pmIdTalonario INT,@pmFecha SMALLDATETIME,@pmNumSerie CHAR(2),@pmNumInicial INT,@pmNumFinal INT,@pmNumAuto BIT,@pmNumActual INT,@pmIdRespons VARCHAR(16)
,@pmNomRespons VARCHAR(150),@pmAnulado BIT,@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraTalonarios (IdTalonario,Fecha,NumSerie,NumInicial,NumFinal,NumAuto,NumActual,IdRespons,NomRespons,Anulado,TimeSys,IdUsuario)
 VALUES (@pmIdTalonario,@pmFecha,@pmNumSerie,@pmNumInicial,@pmNumFinal,@pmNumAuto,@pmNumActual,@pmIdRespons,@pmNomRespons,@pmAnulado,@pmTimeSys,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraTalonariosRmt] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
AS 
 SELECT IdTalonario,T.Fecha AS FecTalon,NumSerie,NumInicial,NumFinal,NumAuto,NumActual,IdRespons,NomRespons
,T.Anulado,T.TimeSys AS FechaCrea,T.IdUsuario AS CdUsuario,Usuario
--datos de la remesa
,R.NumOrden AS NumRemesa,R.IdCia AS CdCia,Compania,SerieGuia,NumGuia,R.Fecha AS FecRemesa,FecDespacho,IdCliente,NC.RazonSocial AS NomCliente
,IdRemitente,NomRemite,IdDestinatario,NomDestino,IdLocOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,IdLocDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,IdVehiculo,R.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,TipoAfiVehic,Modalidad
,NumPedido,IdCiaPed,FechaPed,TipDcm,NumDocmto,IdCiaDcm,FechaDcm,NumManif,IdCiaManif,RA.IdVend AS CdVend,VN.RazonSocial AS Vendedor,R.Observacion AS Observ
FROM Trn_TraTalonarios AS T INNER JOIN adm_Usuarios AS U ON T.IdUsuario=U.IdUsuario
LEFT JOIN Trn_TraRemesa AS R ON T.NumSerie=R.SerieGuia AND R.NumGuia BETWEEN T.NumInicial AND T.NumFinal
LEFT JOIN Trn_TraRemAnexo AS RA ON R.TipDoc=RA.TipDoc  AND R.NumOrden=RA.NumOrden AND R.IdCia=RA.IdCia 
LEFT JOIN Companias AS CN ON R.IdCia=CN.IdCia LEFT JOIN Terceros AS NC ON R.IdCliente=NC.IdTercero 
LEFT JOIN Localidades AS CO ON R.IdLocOrigen=CO.IdLocal LEFT JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
LEFT JOIN Localidades AS CD ON R.IdLocDestino=CD.IdLocal LEFT JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
LEFT JOIN Terceros AS  CDT ON R.IdConductor=CDT.IdTercero LEFT JOIN Terceros AS VN ON RA.IdVend=VN.IdTercero 
WHERE T.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
ORDER BY IdTalonario
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraTalonarios] @pmIdTalonario INT,@pmFecha SMALLDATETIME,@pmNumSerie CHAR(2),@pmNumInicial INT,@pmNumFinal INT,@pmNumAuto BIT,@pmNumActual INT
,@pmIdRespons VARCHAR(16),@pmNomRespons VARCHAR(150),@pmAnulado BIT
 AS 
 UPDATE Trn_TraTalonarios SET Fecha=@pmFecha,NumSerie=@pmNumSerie,NumInicial=@pmNumInicial,NumFinal=@pmNumFinal,NumAuto=@pmNumAuto
,NumActual=@pmNumActual,IdRespons=@pmIdRespons,NomRespons=@pmNomRespons,Anulado=@pmAnulado
 WHERE IdTalonario=@pmIdTalonario
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTarifBuses] @pmIdTarifa VARCHAR(4)
 AS 
 DELETE FROM TarifBuses
 WHERE IdTarifa=@pmIdTarifa
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[paInstm_TraLiqCausac_Sel] @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_TraLiqCausac (tmNumero,tmItem,tmTipCau,tmCausacion,tmIdCiaCau,tmItemCau,tmFecha,tmIdConcepto,tmDescripcion,tmVrAbono,tmNit,tmVehiculo,tmTipOdp,tmOrdenPago,tmIdCiaOdp
,tmTipMuc,tmManifiesto,tmIdCiaMuc,tmItemFact,tmVrSaldo,tmEsVence,tmCdCuenta,tmCuePagar,tmFactura,tmConcValor,tmConcAbono,tmCdCCosto,tmCdSubCos,tmTipoFac)
 SELECT @pmtmNumero,Item,TipCau,Causacion,IdCiaCau,ItemCau,FechaCau,IdConcepto,Descripcion,VrAbono,NitPoseedor,pVehiculo,TipOdp,OrdenPago,IdCiaOdp
,TipMuc,Manifiesto,IdCiaMuc,1,VrSaldoOrden,EsVence,CdCuenta,CdCuePagar,NumFactura,0,0,CdCCosto,CdSubCos,TipOdp
 FROM Trn_TraLiquidaDed
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraLiquidaDed] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipCau VARCHAR(3),@pmCausacion INT,@pmIdCiaCau CHAR(2),@pmItemCau INT,@pmFechaCau SMALLDATETIME,@pmIdConcepto VARCHAR(4)
,@pmDescripcion VARCHAR(250),@pmVrAbono MONEY,@pmNitPoseedor VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmEsVence BIT,@pmTipOdp VARCHAR(3),@pmOrdenPago INT,@pmIdCiaOdp CHAR(2),@pmTipMuc VARCHAR(3),@pmManifiesto INT,@pmIdCiaMuc CHAR(2)
,@pmVrSaldoOrden MONEY,@pmCdCuenta VARCHAR(16),@pmCdCuePagar VARCHAR(16),@pmNumFactura VARCHAR(20),@pmCdCCosto  VARCHAR(16),@pmCdSubCos VARCHAR(16)
 AS 
 INSERT INTO Trn_TraLiquidaDed (TipDoc,Liquidacion,IdCia,Item,TipCau,Causacion,IdCiaCau,ItemCau,FechaCau,IdConcepto,Descripcion,VrAbono,NitPoseedor,pVehiculo,EsVence,TipOdp,OrdenPago,IdCiaOdp
,TipMuc,Manifiesto,IdCiaMuc,VrSaldoOrden,CdCuenta,CdCuePagar,NumFactura,CdCCosto,CdSubCos)
 VALUES (@pmTipDoc,@pmLiquidacion,@pmIdCia,@pmItem,@pmTipCau,@pmCausacion,@pmIdCiaCau,@pmItemCau,@pmFechaCau,@pmIdConcepto,@pmDescripcion,@pmVrAbono,@pmNitPoseedor,@pmpVehiculo,@pmEsVence,@pmTipOdp,@pmOrdenPago,@pmIdCiaOdp
,@pmTipMuc,@pmManifiesto,@pmIdCiaMuc,@pmVrSaldoOrden,@pmCdCuenta,@pmCdCuePagar,@pmNumFactura,@pmCdCCosto,@pmCdSubCos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTercOperarios_Sel] @pmIdOperario VARCHAR(16),@pmNewOperario VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdOperario FROM TercOperarios WHERE IdOperario=@pmIdOperario)
  INSERT INTO TercOperarios (IdOperario,IdTipoOper,TipoSalario,VrSalario)
  SELECT @pmNewOperario,IdTipoOper,TipoSalario,VrSalario
  FROM TercOperarios WHERE IdOperario=@pmIdOperario
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelEgrAnticipo] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_EgrAnticipo WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpEgrAnticipoCero] @pmIdTercero VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
,@pmTipCom VARCHAR(3)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
AS
IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
 UPDATE Trn_EgrAnticipo  SET  VrAplicado=0
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdTercero LIKE ISNULL(@pmIdTercero,'%') 
  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')  AND  TipCom LIKE ISNULL(@pmTipCom,'%')
 ELSE
 UPDATE Trn_EgrAnticipo  SET  VrAplicado=0
 WHERE IdTercero LIKE ISNULL(@pmIdTercero,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%') AND TipCom LIKE ISNULL(@pmTipCom,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsEgrAnticipo] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmItem INT
,@pmFecha SMALLDATETIME,@pmIdCuenta VARCHAR(16),@pmIdTercero VARCHAR(16),@pmVrAnticipo MONEY,@pmVrAplicado MONEY
 AS 
 INSERT INTO Trn_EgrAnticipo (TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,IdTercero,VrAnticipo,VrAplicado)
 VALUES (@pmTipCom,@pmComprobante,@pmIdCia,@pmItem,@pmFecha,@pmIdCuenta,@pmIdTercero,@pmVrAnticipo,@pmVrAplicado)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CdCCBonif,CdSubCCBonif,BaseIvaBonif,VrIvaBonif
,CN.ClaseContrato,VrBonifNeto
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,BaseIvaBonif,VrIvaBonif
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMayLiqTotalesTot] @pmNLiquida INT,@pmIdCia CHAR(2)
 AS 
 SELECT SUM(Unidades) AS STOTUNI,SUM(VrBonif) AS STOTBON,SUM(VrAbono) AS STOTABO
,SUM(VrCuotaFija) AS STOTFIJ,SUM(VrIvaBonif) AS STOTIVA,SUM(BaseIvaBonif) AS STOTBASE
,SUM(VrBonifNeto) AS STOTNETO
 FROM  Trn_MayLiqTotales
 WHERE NLiquida=@pmNLiquida AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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)
,@pmVrIvaBonif DECIMAL(16,4),@pmBaseIvaBonif DECIMAL(16,4),@pmVrBonifNeto MONEY
 AS 
 INSERT INTO Trn_MayLiqTotales (NLiquida,IdCia,Item,NContrato,IdCiaCon,IdCliente,IdAgencia,Unidades,VrBonif,VrAbono
,VrCuotaFija,DiasGracia,TipoNcr,NotaCred,IdCiaNot,LogLiquida,VrIvaBonif,BaseIvaBonif,VrBonifNeto)
 VALUES (@pmNLiquida,@pmIdCia,@pmItem,@pmNContrato,@pmIdCiaCon,@pmIdCliente,@pmIdAgencia,@pmUnidades
,@pmVrBonif,@pmVrAbono,@pmVrCuotaFija,@pmDiasGracia,@pmTipoNcr,@pmNotaCred,@pmIdCiaNot,@pmLogLiquida,@pmVrIvaBonif,@pmBaseIvaBonif,@pmVrBonifNeto)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuAnticipos_Nit] @pmIdPoseedor VARCHAR(16),@pmIdPoseedorDos VARCHAR(16),@pmNewIdPoseedor VARCHAR(16)
 AS 
 IF EXISTS (SELECT * FROM AcuAnticipos WHERE IdPoseedor=@pmIdPoseedor OR IdPoseedor=@pmIdPoseedorDos)
  INSERT INTO AcuAnticipos (nAnno,nMes,IdCia,IdPoseedor,IdVehiculo,SaldoAnt,VrAnticipos,VrPagos)
  SELECT nAnno,nMes,IdCia,@pmNewIdPoseedor,IdVehiculo,SUM(SaldoAnt),SUM(VrAnticipos),SUM(VrPagos)
  FROM AcuAnticipos WHERE IdPoseedor=@pmIdPoseedor OR IdPoseedor=@pmIdPoseedorDos
  GROUP BY nAnno,nMes,IdCia,IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuAnticipos_Uni] @pmIdVehiculo VARCHAR(10),@pmIdVehiculoDos VARCHAR(10),@pmNewVehiculo VARCHAR(10)
 AS 
 IF EXISTS (SELECT * FROM AcuAnticipos WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos)
  INSERT INTO AcuAnticipos (nAnno,nMes,IdCia,IdPoseedor,IdVehiculo,SaldoAnt,VrAnticipos,VrPagos)
  SELECT nAnno,nMes,IdCia,IdPoseedor,@pmNewVehiculo,SUM(SaldoAnt),SUM(VrAnticipos),SUM(VrPagos)
  FROM AcuAnticipos WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos
  GROUP BY nAnno,nMes,IdCia,IdPoseedor
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraLiquidaLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
 SELECT  Liquidacion,L.IdCia AS CdCia,Compania,Fecha,L.IdConcepto AS CdConcepto,Concepto,IdVehiculo,IdPoseedor,T.RazonSocial AS Poseedor,IdConductor,CDT.RazonSocial AS Conductor
,VrTotal,CxPagar,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,L.Observacion AS Observ,L.IdEstado AS CdEstado,FecIniCau,FecFinCau,FecIniOdp,FecFinOdp,ModLiquida
,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,L.IdUsuario AS Cdusuario,Usuario
 FROM Trn_TraLiquida AS L  INNER JOIN Terceros AS T ON L.IdPoseedor=T.IdTercero INNER JOIN Terceros AS CDT ON  L.IdConductor=CDT.IdTercero
 INNER JOIN Conceptos AS C ON L.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON L.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CI ON L.IdCia=CI.IdCia
 WHERE TipDoc=@pmTipDoc  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND L.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND L.IdVehiculo  LIKE ISNULL(@pmIdVehiculo ,'%') AND L.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 ORDER BY L.IdCia,Liquidacion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevLegLta] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Liquidacion,IdCiaDoc,FecDoc,Modalidad,TipMuc,Manifiesto,IdCiaMuc
,IdVehiculo,IdConductor,CDT.RazonSocial AS Conductor,IdPropietario,TP.RazonSocial AS Propietario,IdPoseedor,T.RazonSocial AS Poseedor
,NitEmpresa,NE.RazonSocial AS NomEmpresa,VrAnticipos,VrGastos,VrCombustible,VrDiferencia,VrOtrosCred,galsComb,PesoTotal,Cantidad,Volumen
,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraDevLeg AS D  INNER JOIN Terceros AS T ON D.IdPoseedor=T.IdTercero INNER JOIN Terceros AS CDT ON D.IdConductor=CDT.IdTercero
 INNER JOIN Terceros AS TP ON D.IdPropietario=TP.IdTercero 
 INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia LEFT JOIN Terceros AS NE ON D.NitEmpresa=NE.IdTercero 
 WHERE TipDev=@pmTipDev AND  D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND D.IdVehiculo  LIKE ISNULL(@pmIdVehiculo ,'%') AND D.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 ORDER BY D.IdCia,Devolucion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraPorteIntLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT O.NumOrden,O.IdCia AS CdCia,Compania,O.Fecha,O.FecEmision,O.FecCargue,O.FecEntrega,DPO.IdPais AS CdPaisOrig,POR.NombrePais AS PaisOrig,LO.IdDep AS CdDepOrig,DPO.Departamento AS DptoOrigen,O.IdOrigen,LO.Localidad AS DescOrigen
,DPD.IdPais AS CdPaisDest,PDE.NombrePais AS PaisDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino,O.IdDestino,LD.Localidad AS DescDestino,O.IdNotificado,O.Notificado,O.IdRemitente,O.Remitente,O.DirOrigen
,O.IdDestinatario,O.Destinatario,O.DirDestino,O.IdConsig,O.Consignatario,O.DirConsig,O.InsTransporte,O.DocRemitente,O.ValorTotal,M.Simbolo,O.IdMoneda AS CdMoneda,M.Mneda AS Moneda,O.PesoTotal,O.Manifiesto,O.CdCiaManif
,O.Anulado,O.FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.Negociacion,O.Condic_Pago,O.TimeSys AS FechaCrea,O.FecUpdate,O.IdCiaCrea,O.OrigenAdd,O.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraPorteInt AS O INNER JOIN Companias AS C ON O.IdCia=C.IdCia
 INNER JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep INNER JOIN adm_Paises AS POR ON DPO.IdPais=POR.IdPais
 INNER JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep INNER JOIN adm_Paises AS PDE ON DPD.IdPais=PDE.IdPais
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS E ON O.IdEstado=E.IdEstado
 LEFT JOIN adm_monedas AS M ON O.IdMoneda=M.IdMneda 
 WHERE O.TipDoc=@pmTipDoc AND O.FecEmision BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_NiifLibDia] @pmtmEst CHAR(2)
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_NiifLib AS O INNER JOIN TiposCom AS TC ON O.tmTipCom=TC.IdCom
 INNER JOIN Diarios AS DR ON TC.IdDiario=DR.IdDiario
 INNER JOIN PucNiif 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
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_NiifLibOfi] @pmtmEst CHAR(2)
 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
,TipoId,Dv,NomCial,SiglaRaz,Direccion,T.IdLocal AS TercCdCiudad,Localidad,L.IdDep AS CdDep
,Departamento,Telefono,TipEnte
 FROM tm_NiifLib AS O INNER JOIN TiposCom AS TC ON O.tmTipCom=TC.IdCom
 INNER JOIN Diarios AS DR ON TC.IdDiario=DR.IdDiario
 INNER JOIN PucNiif 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
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_NiifCom] @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCia,Compania,BC.tmIdCuenta AS IdCuenta,NomCuenta,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos
,tmSaldoAnterior+tmTotalDebitos-tmTotalCreditos AS NuevoSaldo,tmSaldoAnterior2,tmTotalDebitos2,tmTotalCreditos2
,tmSaldoAnterior2+tmTotalDebitos2-tmTotalCreditos2 AS NuevoSaldo2,tmVrPresupto
,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
,EsAccnista,EsCliente,EsPropietario,CodAgencia,Referencia 
,tmItem,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12,P.Tercero,P.CentroCosto
 FROM tm_NiifCom AS BC INNER JOIN PucNiif 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 CN ON BC.tmIdCia=CN.IdCia
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelCompanias] @pmIdCia CHAR(2)
 AS 
 BEGIN
  DECLARE @TrnNombre VARCHAR(20)
  SET @TrnNombre = 'TrnCompanias'
  BEGIN TRANSACTION @TrnNombre
  DELETE FROM CompListas WHERE IdCia=@pmIdCia 
  DELETE FROM Companias WHERE IdCia=@pmIdCia
  COMMIT TRANSACTION  @TrnNombre
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CodBodega,FecModPrest,AutoIca,FechaAdd,FechaUpdate
 FROM Companias
 WHERE IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY IdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryComDetalleMud] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT D.TipCom,TipoCom,D.Comprobante,D.IdCia AS CdCia,Compania,D.Item,D.Fecha,D.IdCuenta AS CdCuenta,NomCuenta,D.Detalle,D.VrDebito,D.VrCredito
,D.IdTercero AS NitTercero,T.RazonSocial,D.IdVehiculo,D.IdCCosto,CCosto,D.IdSubCos,SubCosto,D.VehPropio,D.VrBase,D.TarifaBase,D.TipDoc,TipoDoc,D.Documento,D.IdCiaDoc
,CodConce,Concepto,D.TipFac,D.Factura,D.IdCiaFac,D.ItemFac,D.FecVence,D.CodCta,D.NumCheque,D.Integrado,D.TipoAplica,D.Consolida,D.CodCargo,D.NitOtros,NT.RazonSocial AS NomTercOtros
,D.CodSubgpo,D.CiuOrigen,LD.Localidad AS NomCiudad,D.CodAgncia,D.Referncia,D.NitDoc,ND.RazonSocial AS NomTercDoc,D.TipDocRef,D.DocRef,D.IdCiaRef,D.CdConcTrib,D.CdTarifTrib
,D.NumEstablec,D.PtoEmision,D.Num_Autoriza,D.FechAutoriza,D.CdCuentaNiif,D.TimeSys AS FechaCrea,D.FecUpdate AS FechaAct,D.IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
,P.Movimiento,P.Tercero,P.Vehiculo
 FROM Trn_ComDetalle AS D INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 LEFT JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 LEFT JOIN Sys_TiposDoc AS TD ON D.TipDoc=TD.IdDoc LEFT JOIN Conceptos AS CN ON D.CodConce=CN.IdConcepto
 LEFT JOIN Localidades AS LD ON D.CiuOrigen=LD.IdLocal LEFT JOIN Terceros AS ND ON D.NitDoc=ND.IdTercero
 LEFT JOIN Terceros AS NT ON D.NitOtros=NT.IdTercero
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 --AND (SUBSTRING(D.IdCuenta,1,1)='5' OR SUBSTRING(D.IdCuenta,1,1)='6' OR SUBSTRING(D.IdCuenta,1,1)='2')
 AND (@pmIdCia IS NULL OR D.IdCia=@pmIdCia)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,RT.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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,F.CdBodega,CantFisico,VrTotalFisico,Existencias,VrTotalCosto
,OrigenAdd,Procesado,FecProceso,NumAjuSob,NumAjuFal,Anulado,FecDev,F.Observacion,FCodBodega,FCodMarca,FCodSubGrupo
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryRequisicionSal] @pmTipDoc VARCHAR(3),@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,Num_Vehic,Num_Trailer
,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','SAL','DVS') AND TipOrd=@pmTipDoc 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.TipDoc=@pmTipDoc AND 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,Num_Vehic,Num_Trailer,NitCliente,CL.RazonSocial,CdAgencia,Modalidad,NumAprob,FecAprob
,R.Observacion,R.IdEstado,Estado
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdConsPedFmt] @pmTipDoc VARCHAR(3),@pmNumConsIni INT,@pmNumConsFin INT,@pmIdCia CHAR(2)
AS 
SELECT PD.TipDoc,PD.NumCons,PD.IdCia,PD.Item,PD.TipPed,PD.Pedido,PD.IdCiaPed,Compania,PD.TipTdo,PD.Traslado,PD.IdCiaTdo,PD.NumTdoIns,PD.CdCiaIns
,O.Fecha,FechaVence,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia,Agencia,VrSubTotal,VrDescuento,VrImpuesto,VrNeto,O.IdVend,VN.RazonSocial AS Vendedor
,O.IdLocEnv,LE.Localidad AS CiudadEnvio,DirEnvio,DiasEntraga,O.NomContac,O.TelContac,O.emlContac,O.RefPedido,O.Modalidad,O.Vigencia,NumAprob,IdCiaApr,FecAprob,DetalleAprob
,O.TipFac,O.Factura,O.IdCiaFac,TipRem,Remision,IdCiaRem,O.NumAutSicom AS NumOrdenComp,O.IdEstado,Estado,O.Anulado,O.Observacion
FROM Trn_ProdConsPed AS PD INNER JOIN dbo.Trn_Opedido AS O ON PD.TipPed=O.TipDoc AND PD.Pedido=O.Pedido AND PD.IdCiaPed=O.IdCia
INNER JOIN Companias AS CN ON PD.IdCiaPed=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 
LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia LEFT JOIN  Localidades AS LE ON O.IdLocEnv=LE.IdLocal
WHERE PD.TipDoc=@pmTipDoc AND PD.NumCons BETWEEN @pmNumConsIni AND @pmNumConsFin AND PD.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEdsFormasCos] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
SELECT F.Planilla,F.IdCia,Compania,F.Fecha,F.IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia,A.Agencia AS NomAgencia,F.CdProducto,DescripProd,F.Cantidad,F.VrTotal
,K.VrUnitario AS CostoUnid,K.VrUnitario*F.Cantidad AS CostoTotal,F.TipoRango,F.NumSerie,F.NumForma,F.Referencia,F.NumRef2,F.IdVend,V.RazonSocial AS Vendedor,F.pVehiculo,F.nVehiculo
FROM Trn_EdsFormas AS F LEFT JOIN (SELECT TipDoc,Documento,IdCia,Item,IdProducto,CdTanque,Entradas,Salidas,VrUnitario,VrPrecio,VrCostoEnt,VrCostoSal
FROM Trn_Kardex WHERE TipDoc='PLA' AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (IdCia=@pmIdCia OR @pmIdCia IS NULL)
) AS K ON F.Planilla=K.Documento AND F.IdCia=K.IdCia AND F.CdProducto=K.IdProducto
INNER JOIN Companias AS C ON F.IdCia=C.IdCia
INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero INNER JOIN Terceros AS V ON F.IdVend=V.IdTercero
INNER JOIN ProdMcias AS P ON F.CdProducto=P.IdProducto 
LEFT JOIN Agencias AS A ON F.IdAgencia=A.IdAgencia
WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin --F.TipoRango='VAL'
AND (F.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND F.Anulado=0
UNION ALL
SELECT P.Documento,P.IdCia,Compania,P.Fecha,P.NitCliente,T.RazonSocial,P.CdAgencia,A.Agencia,P.CdProducto,DescripProd,P.Cant_Gals,P.VrPagado
,K.VrUnitario AS CostoUnid,K.VrUnitario*P.Cant_Gals AS CostoTotal,P.IdForma,'',P.NumForma,P.Referncia1,P.Referncia2,'','',P.Referncia2,''
FROM Trn_Pagos AS P LEFT JOIN (SELECT TipDoc,Documento,IdCia,Item,IdProducto,CdTanque,Entradas,Salidas,VrUnitario,VrPrecio,VrCostoEnt,VrCostoSal
FROM Trn_Kardex WHERE TipDoc='PLA' AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (IdCia=@pmIdCia OR @pmIdCia IS NULL)
) AS K ON P.TipDoc=K.TipDoc AND P.Documento=K.Documento AND P.IdCia=K.IdCia AND P.CdProducto=K.IdProducto
INNER JOIN Companias AS C ON P.IdCia=C.IdCia INNER JOIN Terceros AS T ON P.NitCliente=T.IdTercero 
INNER JOIN ProdMcias AS PM ON P.CdProducto=PM.IdProducto 
LEFT JOIN Agencias AS A ON P.CdAgencia=A.IdAgencia
WHERE P.TipDoc='PLA' AND P.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
AND (P.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND P.FE_Ticket=1
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,SUM(K.NumFinal) AS RemCantFact
,MAX(CASE WHEN (K.TipOrd LIKE 'FC%' OR K.TipOrd LIKE 'FO%') THEN K.NumOrden ELSE 0 END) AS RemNumFac
,MAX(CASE WHEN (K.TipOrd LIKE 'FC%' OR K.TipOrd LIKE 'FO%') THEN K.IdCiaOrd ELSE 0 END) AS RemCiaFac
,TipoFact,ISNULL(NumFactura,0) AS NumFactura,ISNULL(CdCiaFac,'') AS 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevCauLta] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdProv VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Causacion,IdCiaDoc,FecDoc,IdProv,RazonSocial,Factura,VrTotal,VrOtros,VrNeto
,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraDevCau AS D  INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia
 INNER JOIN Terceros AS T ON D.IdProv=T.IdTercero  INNER JOIN Conceptos AS CO ON D.IdConcepto=CO.IdConcepto
 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.IdProv LIKE ISNULL(@pmIdProv,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 ORDER BY D.IdCia,D.Devolucion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,ISNULL(SA.SaldoAnterior,0) AS Saldo_Anterior,SUM(VrDebito-VrCredito) AS ValorTotal,SUM(VrBase) AS TotalBase,MAX(TarifaBase) AS TarifBase,SUM(VrDebito) AS TotalDeb,SUM(VrCredito) AS TotalCre
  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
  LEFT JOIN (SELECT IdCuenta,IdTercero,IdCia,SUM(SaldoAnterior+TotalDebitos-TotalCreditos) AS SaldoAnterior FROM AcuCuentasNit 
  WHERE  nAnno=YEAR(DATEADD (Month ,-1, @pmFechaIni))  AND nMes=MONTH(DATEADD (Month ,-1, @pmFechaIni))
   AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')   AND IdTercero LIKE ISNULL(@pmIdTercero,'%')
 GROUP BY IdCuenta,IdTercero,IdCia) AS SA ON D.IdCia=SA.IdCia AND D.IdCuenta=SA.IdCuenta AND D.IdTercero=SA.IdTercero
  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,SA.SaldoAnterior
 ELSE
  SELECT D.IdCuenta AS CdCuenta,NomCuenta,Tercero,Naturaleza,BaseGravable,Porcntaje,D.IdTercero AS NitTercero,RazonSocial,TipoId,Dv,D.IdCia AS CdCia,Compania
 ,ISNULL(SA.SaldoAnterior,0) AS Saldo_Anterior,SUM(VrDebito-VrCredito) AS ValorTotal,SUM(VrBase) AS TotalBase,MAX(TarifaBase) AS TarifBase,SUM(VrDebito) AS TotalDeb,SUM(VrCredito) AS TotalCre
  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
  LEFT JOIN (SELECT IdCuenta,IdTercero,IdCia,SUM(SaldoAnterior+TotalDebitos-TotalCreditos) AS SaldoAnterior FROM AcuCuentasNit 
  WHERE  nAnno=YEAR(DATEADD (Month ,-1, @pmFechaIni))  AND nMes=MONTH(DATEADD (Month ,-1, @pmFechaIni))
   AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND IdCuenta BETWEEN @pmIdCueIni AND @pmIdCueFin  AND IdTercero LIKE ISNULL(@pmIdTercero,'%')
 GROUP BY IdCuenta,IdTercero,IdCia) AS SA ON D.IdCia=SA.IdCia AND D.IdCuenta=SA.IdCuenta AND D.IdTercero=SA.IdTercero
  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,SA.SaldoAnterior
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryRequisicionLtm] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmModalidad VARCHAR(10)=Null
AS 
 SELECT R.Requisicion,R.IdCia AS CdCia,Compania,R.Fecha,FechaVence,R.NContrato AS NumOrden,R.IdCiaCont AS IdCiaOdt,R.Modalidad
,R.IdRespons AS CdOperario,T.RazonSocial AS Operario,Num_Vehic,Num_Trailer,R.NitCliente AS NitCentro,CL.RazonSocial AS NomCentroServ
,DirEntrega,IdLocEnt,Localidad,R.IdDep AS CdDep,Dependencia,VrSubTotal,Cantidad,NumAprob,FecAprob,CdUsuAprob
,TipSal,NumSalida,IdCiaSal,FechaSal,R.IdConcepto AS CdConcepto,Concepto,R.IdCCosto AS CdCCosto,CCosto,R.IdSubCos AS CdSubCos
,NomContacto,TelsContacto,EmailContacto,TipoVigencia,R.Anulado,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado
,OrigenAdd,R.TimeSys,R.FecUpdate,R.IdCiaCrea,R.IdUsuario AS CdUsuario,Usuario
 FROM Trn_Requisicion AS R INNER JOIN Companias AS CI ON R.IdCia=CI.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 Dependencias AS D ON R.IdDep=D.IdDep
 LEFT JOIN Terceros AS CL ON R.NitCliente =CL.IdTercero LEFT JOIN Localidades AS L ON R.IdLocEnt=L.IdLocal
 WHERE R.TipDoc=@pmTipDoc AND R.Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND R.Modalidad LIKE ISNULL(@pmModalidad,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,Tipo_Pago,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
,NiifTipo,NiifNumero,FisTipo,FisNumero,Propddes,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS IdUsuari,Usuario,CdSustento,Establec,PEmision,Autorizac,FechaAut,DocSoporte,CodRes,DescServicio
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryFinCuotasExto] @pmFechaIni SMALLDATETIME,@pmFecCorte SMALLDATETIME
 AS 
 SELECT TipDoc,IdPrestamo,C.IdCia AS CdCia,Compania,Item,NumCuota,Fecha,FechaVence,DATEDIFF(day,FechaVence,@pmFecCorte) AS DiasMora,VrTotal,VrAbonado
,IdCliente,RazonSocial AS NomCliente,C.IdAgencia AS CdAgencia,NomAgencia AS Agencia,Concepto,Detalle,TipoCuota,NumFactura,NumCausacion,IdCiaCau,FecUltCausac
,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,DiasLiquida,VrAcumCapital,Convenio,NitConvenio,FacturaInt,TipFacInt,NumFacInt,CdCiaFacInt,FechaFacInt
,FecUltPago,FecLiqMora
 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 TercCliePrestamo AS CLI ON  C.IdCliente=CLI.IdClie AND C.IdAgencia=CLI.IdAgencia
 WHERE Fecha<@pmFechaIni AND VrTotal>VrAbonado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryCompanias] @pmIdCia CHAR(2)
 AS 
 SELECT IdCia,Compania,DirInterface,FechaActual,PeriodoActual,IdCCosto,IdSubCos,IdTercero
,CodSucsal,DireccSuc,TelefSuc,FaxSuc,IdLocal,TipoEDS,CodPlanta,CodBodega,FecModPrest,AutoIca,FechaAdd,FechaUpdate,Inactivo
 FROM Companias
 WHERE IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY IdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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,MontoAprob
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraLiqDetalleFmt] @pmTipDoc VARCHAR(3),@pmLiquidacionIni INT,@pmLiquidacionFin INT,@pmIdCia CHAR(2)
AS 
 SELECT TipDoc,Liquidacion,D.IdCia,Compania,D.Item,D.IdConcepto AS CdConcepto,DescConcepto,D.IdCuenta AS CdCuenta,NomCuenta,VrDebito,VrCredito
,D.IdTercero AS NitTercero,RazonSocial,D.CodAgncia,CdCCosto,CCosto,CdSubCos,SubCosto,pVehiculo
,D.RubroConc,D.VrBase,D.TarifBase,D.TipFac,D.Factura,D.IdCiaFac,ItemFac,D.FecVence,RefConcep,Concepto,D.TipSop,D.NumSoporte,D.IdCiaSop
,D.DocSop,D.TipoImp,D.CodTarImp,D.ItemBase
 FROM Trn_TraLiqDetalle AS D INNER JOIN ConcDiversos AS C ON D.IdConcepto=C.IdConcepto 
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia
 INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta LEFT JOIN Terceros AS T ON D.IdTercero=T.IdTercero
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos 
 WHERE D.TipDoc=@pmTipDoc AND D.Liquidacion BETWEEN @pmLiquidacionIni AND @pmLiquidacionFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CodBodega,FecModPrest,AutoIca
,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[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,TarifaRet,VrRetFte, 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_NiifComIB] @pmtmEst CHAR(2)
 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
,tmItem,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12
 FROM tm_NiifCom AS BC INNER JOIN PucNiif 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
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenCumLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT Cumplido,C.IdCia AS CdCia,Compania,C.Fecha AS FechaCump,C.IdConcepto AS CdConcepto,Concepto,TipOds,C.NumOrden AS Num_Orden,IdCiaOds
,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia,O.IdVehiculo AS PlacaVeh,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor
,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,O.Modalidad,TipoOrden,O.VrAnticipo AS Vr_Anticipo,O.IdEstacion AS CdCampo,EstServicio AS NomCampo
,C.TipoAfiVehic,C.FecDespacho,C.FecRecibo,C.FecIngCertif,C.FecCertificado,DescServicio,C.Cantidad AS CumCantidad,C.VrTotal AS CumValor,C.VrCosto AS CumValTercero,C.RemCliente
,Funcionario,O.NumRemesa,O.CdCiaRem,O.kmtInicial,O.kmtFinal,C.TipCom,C.Comprobante AS NumComp,C.IdCiaCom,C.Anulado AS CumAnulado,C.NumDev,C.FecDev,C.Observacion AS Observ
,C.IdEstado AS CdEstado,Estado,C.OrigenAdd,C.TimeSys AS FechaCrea,C.FecUpdate AS FechaModif,C.IdCiaCrea AS CdCiaCrea,C.IdUsuario AS CdUsuario,Usuario
FROM Trn_TraOrdenCum AS C INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario INNER JOIN Conceptos AS CT ON C.IdConcepto=CT.IdConcepto
INNER JOIN Trn_TraOrdenServ AS O ON C.TipOds=O.TipDoc AND C.NumOrden=O.NumOrden  AND C.IdCiaOds=O.IdCia
INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero
INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion  INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero
LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia
WHERE C.TipDoc=@pmTipDoc AND C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraRemCancelaLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT CR.Fecha,CR.TipDoc AS TipFac,CR.Factura,CR.IdCia AS CdCiaFac,CF.Compania AS CiaFactura,CR.TipRem,CR.Remesa,CR.IdCiaRem,CN.Compania AS CiaRemesa,CR.Item
,D.IdMercancia,DescripMcias,D.Cantidad AS Cant,PesoNeto,D.UndMed AS CdUmPeso,Volumen,UndVol,NitRemite,Remitente,NitDestntario,Destinatario
,DirOrigen,IdOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,DirDestino,IdDestino,LD.Localidad AS CiudadDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,TarifClie,UndTarifa,TarifPago,UndTarifPago,D.Remision,DocCliente,Referencia1,Referencia2,Referencia3
,R.Fecha AS FecRemesa,R.FecDespacho,R.IdCliente,T.RazonSocial AS NombreCliente,R.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia
,R.IdVehiculo AS PlacaVeh,R.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,R.nRemolque,R.TipoAfiVehic,R.Modalidad
,NumPedido,IdCiaPed,FechaPed,TipDcm,NumDocmto,IdCiaDcm,FechaDcm,NumManif,IdCiaManif,EstCumplido,Cumplido,IdCiaCump,FechaCump,DetalleCump
,TipOdp,NumeroOdp,IdCiaOdp,EstFactura,Anulado,FecDev,R.Observacion AS RemObserv,CR.Observacion,CR.IdUsuario AS CdUsuario,Usuario,CR.FechaCrea,CR.Id
FROM Trn_TraRemCancela AS CR INNER JOIN Companias AS CN ON CR.IdCiaRem=CN.IdCia INNER JOIN adm_Usuarios AS U ON CR.IdUsuario=U.IdUsuario
INNER JOIN Trn_TraRemesa AS R ON CR.TipRem=R.TipDoc AND CR.Remesa=R.NumOrden AND CR.IdCiaRem=R.IdCia
INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero INNER JOIN Terceros AS CDT ON R.IdConductor=CDT.IdTercero
LEFT JOIN Trn_TraRemMcias AS D ON CR.TipRem=D.TipDoc AND CR.Remesa=D.NumOrden AND CR.IdCiaRem=D.IdCia AND CR.Item=D.Item
LEFT JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
LEFT JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
LEFT JOIN Companias AS CF ON CR.IdCia=CF.IdCia LEFT JOIN Agencias AS A ON R.IdAgencia=A.IdAgencia
WHERE CR.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND CR.IdCiaRem LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryAcuNiifCuentasMayCia] @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(A.SaldoAnterior) AS tSaldoAnterior,SUM(A.TotalDebitos) AS tTotalDebitos,SUM(A.TotalCreditos) AS tTotalCreditos
,SUM(A.SaldoAnterior+A.TotalDebitos-A.TotalCreditos) AS NuevoSaldo
 FROM AcuNiifCuentas AS A INNER JOIN PucNiif AS P ON SUBSTRING(A.IdCuenta,1,@pmNiv)=P.IdCuenta
 INNER JOIN Companias AS C ON A.IdCia=C.IdCia
 WHERE A.nAnno=@pmnAnno AND A.nMes=@pmnMes AND (@pmIdCia IS NULL OR A.IdCia=@pmIdCia)
 GROUP BY SUBSTRING(A.IdCuenta,1,@pmNiv),NomCuenta,Naturaleza,A.IdCia,Compania
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,PC.NomCuenta,D.IdCCosto AS CdCentro,CCosto
,D.IdSubCos AS CdSubcentro,SubCosto,VrPresupuesto,SaldoAnterior,TotalDebitos,TotalCreditos,PC.Movimiento,PC.CentroCosto,PC.SubCentro,PC.Naturaleza
,D.TipoNiif,D.CodCueNiif,D.VrPrespNiif,D.SAnteriorNiif,D.DebitosNiif,D.CreditosNiif,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,PN.NomCuenta AS NiifNomCuenta
 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
 LEFT 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
 LEFT JOIN PucNiif AS PN ON D.CodCueNiif=PN.IdCuenta
 WHERE P.Numero=@pmNumero AND P.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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.TipoNiif,D.CodCueNiif,D.VrPrespNiif,D.SAnteriorNiif,D.DebitosNiif,D.CreditosNiif,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
 LEFT 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_NiifGen] @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCia,Compania,BG.tmIdCuenta AS CdCuenta,NomCuenta,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo
,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12,tmEst
 FROM tm_NiifGen AS BG INNER JOIN PucNiif 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
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevReexLta] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdEmpTrans VARCHAR(16)=Null
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Causacion,IdCiaDoc,FecDoc,IdEmpTrans,RazonSocial,NumFactura,FecFactura
,VrTotal,VrImpuesto,VrRetencion,VrReteIca,VrOtrosCargos,VrOtrosDctos,VrNeto,PesoTotal,CxPagar,TipCom,Comprobante,IdCiaCom,ModdDev
,D.Observacion AS Observ,D.IdEstado AS CdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraDevReex AS D  INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN Terceros AS T ON D.IdEmpTrans=T.IdTercero INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 WHERE D.TipDev=@pmTipDev   AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND  D.IdEmpTrans LIKE ISNULL(@pmIdEmpTrans,'%')
 ORDER BY D.IdCia,D.Devolucion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevReexFmt] @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 CdTipDoc,D.Causacion AS NumCausac,IdCiaDoc,FecDoc,IdEmpTrans,T.RazonSocial AS NomEmpresa
,NumFactura,FecFactura,VrTotal,VrImpuesto,VrRetencion,VrReteIca,VrOtrosCargos,VrOtrosDctos,VrNeto,PesoTotal,CxPagar,TipCom,TipoCom,Comprobante,IdCiaCom,ModdDev,D.Observacion AS Observ,D.IdEstado AS CdEstado
--detalles
,Item,TipRem,Remesa,IdCiaRem,FecRemesa,Descripcion,Unidades,PesoNeto,R.UndMed AS CdUmed,UMP.Unidad AS UmPeso,Volumen,UndVol,UndTarifa,VrUnitario,ValorTotal
,pVehiculo,CedConductor,CDT.RazonSocial AS NomConductor
,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario,TipDev,TipoDoc,Leyenda
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
 FROM Trn_TraDevReex  AS D   INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN Terceros AS T ON D.IdEmpTrans=T.IdTercero INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Trn_TraReexpedidoRem AS R  ON D.TipDoc=R.TipDoc AND D.Causacion=R.Causacion  AND D.IdCiaDoc=R.IdCia
 LEFT JOIN Sys_Um AS UMP ON R.UndMed=UMP.UndMed  LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom
 LEFT JOIN Terceros AS  CDT ON R.CedConductor=CDT.IdTercero 
 WHERE D.TipDev=@pmTipDev AND D.Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_ResMensualCueN] @pmtmEst CHAR(2),@pmtmTipo INT
 AS 
 SELECT  tmCodigo,ISNULL(P.NomCuenta,PN.NomCuenta) AS NomCuenta,tmIdCia,Compania,tmIdTercero,RazonSocial,tmCdAgencia,A.Agencia AS NomAgencia
,tmCdCCosto,CCosto,tmCdSubCos,SubCosto,tmpVehiculo,tmTipoAfi
,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 LEFT JOIN Puc AS P ON TR.tmCodigo=P.IdCuenta LEFT JOIN PucNiif AS PN ON TR.tmCodigo=PN.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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrAboOtros,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,VrAboCapital,LiqAboCapital,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCodBodega VARCHAR(4),@pmFecModPrest SMALLDATETIME,@pmAutoIca BIT,@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,CodBodega=@pmCodBodega,FechaUpdate=@pmFechaUpdate,Inactivo=@pmInactivo,FecModPrest=@pmFecModPrest,AutoIca=@pmAutoIca
 WHERE IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCodBodega VARCHAR(4),@pmFecModPrest SMALLDATETIME,@pmAutoIca BIT,@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,CodBodega,FecModPrest,AutoIca)
 VALUES (@pmIdCia,@pmCompania,@pmDirInterface,@pmFechaActual,@pmPeriodoActual,@pmIdCCosto,@pmIdSubCos,@pmIdTercero
,@pmCodSucsal,@pmDireccSuc,@pmTelefSuc,@pmFaxSuc,@pmIdLocal,@pmTipoEDS,@pmCodPlanta,@pmFechaAdd,@pmInactivo,@pmCodBodega,@pmFecModPrest,@pmAutoIca)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDecAduanaLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT O.NumDeclara,O.IdCia AS CdCia,Compania,O.Fecha,O.FecEmision,DPO.IdPais AS CdPaisOrig,POR.NombrePais AS PaisOrig,LO.IdDep AS CdDepOrig,DPO.Departamento AS DptoOrigen,O.IdOrigen,LO.Localidad AS DescOrigen
,DPD.IdPais AS CdPaisDest,PDE.NombrePais AS PaisDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino,O.IdDestino,LD.Localidad AS DescDestino
,O.IdAduCarga,AC.NomAduana AS AduanaCargo,PAC.NombrePais AS PaisAduCargo,O.IdAduOrigen,AP.NomAduana AS AduanaOrigen,PAP.NombrePais AS PaisAduOrigen,O.IdAduDestino,AD.NomAduana AS AduanaDestino,PAD.NombrePais AS PaisAduDestino
,O.IdAduFrontera,AF.NomAduana AS AduanaFrontera,PAF.NombrePais AS PaisAduFront,O.IdDeclarante,Declarante,DirDeclara,CdLocalDec,LDC.Localidad AS CiudadDec,O.IdRemitente,O.Remitente,DirOrigen,CdLocalRem,LRM.Localidad AS CiudadRemite
,O.IdDestinatario,Destinatario,DirDestino,CdLocalDest,LDT.Localidad AS CiudadDest,O.IdConsig,Consignatario,DirConsig,CdLocalConsig,LCN.Localidad AS CiudadConsig,O.IdMoneda AS CdMoneda,M.Simbolo,M.Mneda AS Moneda
,O.ValorTotal,O.PesoTotal,O.DocAnexos,O.Vehiculos,O.Remolques,O.Manifiestos,O.Contenedores,O.Precintos,O.Anulado,O.FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.Negociacion,O.CondicPago
,O.OrigenAdd,O.TimeSys AS FechaCrea,O.FecUpdate,O.IdCiaCrea,O.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraDecAduana AS O INNER JOIN Companias AS C ON O.IdCia=C.IdCia
 INNER JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep INNER JOIN adm_Paises AS POR ON DPO.IdPais=POR.IdPais
 INNER JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep INNER JOIN adm_Paises AS PDE ON DPD.IdPais=PDE.IdPais
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS E ON O.IdEstado=E.IdEstado
 INNER JOIN Aduanas AS AC ON O.IdAduCarga=AC.IdAduana INNER JOIN adm_Paises AS PAC ON AC.IdPais=PAC.IdPais
 INNER JOIN Aduanas AS AP ON O.IdAduOrigen=AP.IdAduana INNER JOIN adm_Paises AS PAP ON AP.IdPais=PAP.IdPais
 INNER JOIN Aduanas AS AD ON O.IdAduDestino=AD.IdAduana INNER JOIN adm_Paises AS PAD ON AD.IdPais=PAD.IdPais
 LEFT JOIN adm_monedas AS M ON O.IdMoneda=M.IdMneda
 LEFT JOIN Aduanas AS AF ON O.IdAduFrontera=AF.IdAduana LEFT JOIN adm_Paises AS PAF ON AF.IdPais=PAF.IdPais
 LEFT JOIN Localidades AS LDC ON O.CdLocalDec=LDC.IdLocal LEFT JOIN Localidades AS LRM ON O.CdLocalRem=LRM.IdLocal
 LEFT JOIN Localidades AS LDT ON O.CdLocalDest=LDT.IdLocal LEFT JOIN Localidades AS LCN ON O.CdLocalConsig=LCN.IdLocal
 WHERE O.TipDoc=@pmTipDoc AND O.FecEmision BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenLiqLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT Liquidacion,L.IdCia AS CdCia,Compania,L.Fecha AS FechaLiq,L.IdConcepto AS CdConcepto,Concepto,TipOds,L.NumOrden,IdCiaOds,O.Fecha AS FecOrden
,L.IdVehiculo AS PlacaVeh,L.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,L.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,L.TipoAfiVehic
,O.IdCliente,T.RazonSocial AS NomCliente,O.IdDestino,Localidad,O.TipoOrden,O.IdEstacion AS CdCampo,EstServicio AS NomCampo,TipoResiduo,O.RemCliente,O.NumRemesa,O.CdCiaRem
,L.VrTotal,L.VrCosto,L.VrAnticipo,VrGastos,VrIva,VrRetencion,VrReteIca,VrReteCREE,VrReteIva,VrOtros,VrDescuento,VrConcCargos,VrConcDed,VrNeto,L.Cantidad
,TarifaPago,BaseImp,BaseRet,BaseIca,TarifaImp,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,CodTarRtc
,Referencia,CxPagar,CdPlazo,FechaVence,UndMed,L.TipCom,L.Comprobante,L.IdCiaCom,L.Anulado,L.NumDev,L.FecDev,L.Observacion AS Observ
,L.IdEstado AS CdEstado,Estado,TipEgr,Egreso,IdCiaEgr,NumCheque,L.CdCCosto,L.CdSubCos
,L.OrigenAdd,L.TimeSys AS FechaCrea,L.FecUpdate AS FechaModif,L.IdCiaCrea AS CdCiaCrea,L.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraOrdenLiq AS L INNER JOIN Companias AS CN ON L.IdCia=CN.IdCia INNER JOIN Conceptos AS CT ON L.IdConcepto=CT.IdConcepto
 INNER JOIN adm_Usuarios AS U ON L.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS ED ON L.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS NP ON L.IdPoseedor=NP.IdTercero INNER JOIN Terceros AS CDT ON L.IdConductor=CDT.IdTercero
 INNER JOIN Trn_TraOrdenServ AS O ON L.TipOds=O.TipDoc AND L.NumOrden=O.NumOrden AND L.IdCiaOds=O.IdCia
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion 
 LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
 WHERE L.TipDoc=@pmTipDoc AND L.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND L.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_TraMovLiquida] @pmtmEst CHAR(2)
 AS 
 SELECT tmTipo,TipoDoc,tmNumero,tmIdCia,Compania,tmItem,tmFecha,tmFecVence,tmIdPoseedor,RazonSocial,tmIdVehiculo
,tmIdConcepto,tmDescripcion,tmVrDebito,tmVrCredito,tmVrAbonado,tmTipoVehic,tmVrBase
,tmTarifa,tmReferencia,tmTipDocRef,tmDocRef,tmEstado,tmTipoLiq,tmLiquida,tmCiaLiq
,tmSaldoAnt,tmSaldoCia,tmSaldoAntVeh,tmSaldoVehCia
 FROM tm_TraMovLiquida AS M INNER JOIN Terceros AS T ON M.tmIdPoseedor=T.IdTercero
 INNER JOIN Companias  AS CN ON M.tmIdCia=CN.IdCia
 LEFT JOIN Sys_TiposDoc AS TD ON M.tmTipo=TD.IdDoc
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevLiqLta] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Liquidacion,IdCiaDoc,FecDoc,IdVehiculo,IdPoseedor,T.RazonSocial AS Poseedor,IdConductor,CDT.RazonSocial AS Conductor
,VrTotal,TipCom,Comprobante,IdCiaCom,ModdDev,D.Observacion AS Observ,D.IdEstado AS CdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS Cdusuario,Usuario
 FROM Trn_TraDevLiq AS D  INNER JOIN Terceros AS T ON D.IdPoseedor=T.IdTercero INNER JOIN Terceros AS CDT ON  D.IdConductor=CDT.IdTercero
 INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia
 WHERE  D.TipDev=@pmTipDev  AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdVehiculo  LIKE ISNULL(@pmIdVehiculo ,'%') AND D.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 ORDER BY D.IdCia,Devolucion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,TipoLiquida,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,EdoCausac,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.TipoProc=C.TipoCausac AND 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenServLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS 
 SELECT NumOrden,O.IdCia AS CdCia,Compania,Fecha,FecDespacho,FecRecibo,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS CdAgencia,Agencia
,O.IdVehiculo AS PlacaVeh,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,nRemolque,O.TipoAfiVehic,O.Modalidad,VrTotal,VrCosto,Cantidad,VrAnticipo,O.CdRuta,Ruta,O.IdOrigen,LO.Localidad AS Origen,O.IdDestino,LD.Localidad AS Destino
,TipoOrden,O.IdEstacion AS CdCampo,EstServicio AS NomCampo,NomEstacion,TipoResiduo,RemCliente,NomRecibido,FecIngCertif,FecCertificado,NumRemesa,CdCiaRem,EstCumplido,NumCump,CdCiaCum
,EstFactura,TipFact,NumFactura,CdCiaFact,EstOrden,TipLiq,NumLiquida,CdCiaLiq,NumAprob,IdCiaApr,FecAprob,UsuAprob,ObservAprob,OrigenAdd,O.Anulado,FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado
,O.TimeSys AS FechaCrea,O.FecUpdate AS FechaModif,kmtInicial,kmtFinal,Cantkms,O.IdCiaCrea AS CdCiaCrea,O.IdUsuario AS CdUsuario,Usuario
FROM Trn_TraOrdenServ AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero
INNER JOIN Terceros AS NP ON O.IdPoseedor=NP.IdTercero INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero
INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia  LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal
LEFT JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal LEFT JOIN Rutas AS RT ON O.CdRuta=RT.IdRuta
WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,TarifaArf,VrAutRetFte,TarifaAri,VrAutRetIca
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraLiqViajesLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null
 AS 
 SELECT  Liquidacion,L.IdCia AS CdCia,Compania,Fecha,L.IdConcepto AS CdConcepto,Concepto,Modalidad,TipMuc,Manifiesto,IdCiaMuc,FechaMuc,IdVehiculo,IdConductor,CDT.RazonSocial AS Conductor
,IdPropietario,TP.RazonSocial AS Propietario,IdPoseedor,T.RazonSocial AS Poseedor,NitEmpresa,NE.RazonSocial AS NomEmpresa,VrAnticipos,VrGastos,VrCombustible,VrOtrosCred,VrDiferencia
,galsComb,PesoTotal,Cantidad,Volumen,IdRuta,IdLocOrigen,LO.Localidad AS CiudadOrigen,IdLocDestino,LD.Localidad AS CiudadDestino,Referencia,DescMcias,LiqMixto,kmtInicial,kmtFinal
,EstFactura,TipDocRef,NumDocRef,IdCiaRef,FecDocRef,Num_Ppto,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,L.Observacion AS Observ,L.IdEstado AS CdEstado
,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,L.IdUsuario AS Cdusuario,Usuario
 FROM Trn_TraLiqViajes AS L  INNER JOIN Terceros AS T ON L.IdPoseedor=T.IdTercero INNER JOIN Terceros AS CDT ON L.IdConductor=CDT.IdTercero
 INNER JOIN Terceros AS TP ON L.IdPropietario=TP.IdTercero 
 INNER JOIN Conceptos AS C ON L.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON L.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CI ON L.IdCia=CI.IdCia LEFT JOIN Terceros AS NE ON L.NitEmpresa=NE.IdTercero 
 LEFT JOIN Localidades AS LO ON L.IdLocOrigen=LO.IdLocal LEFT JOIN Localidades AS LD ON L.IdLocDestino=LD.IdLocal
 WHERE TipDoc=@pmTipDoc   AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND L.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND L.IdVehiculo  LIKE ISNULL(@pmIdVehiculo ,'%') AND L.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
  AND L.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 ORDER BY L.IdCia,Liquidacion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryFinCuotasFmt] @pmTipDoc VARCHAR(3),@pmIdPrestamoIni INT,@pmIdPrestamoFin INT,@pmIdCia CHAR(2) 
 AS 
 SELECT TipDoc, 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
,TipoCausac,NumCausacion,IdCiaCau,Detalle,FecUltPago,FecLiqMora,C.FechaCrea AS Fecha_crea,CodLinCred,NumFactura
,VrInteresFact,DiasLiquida,FecUltCausac,VrAcumCapital,TipDoc,Convenio,NitConvenio,NumPreMora,FacturaInt,TipFacInt,NumFacInt,CdCiaFacInt,FechaFacInt
,TarifIvaInt,VrIvaInt,TarifRetInt,VrRetInt
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_ResMensualCue] @pmtmEst CHAR(2),@pmtmTipo INT
 AS 
 SELECT  tmCodigo,ISNULL(P.NomCuenta,PN.NomCuenta) AS NomCuenta,tmIdCia,Compania,tmIdTercero,RazonSocial,tmCdAgencia,A.Agencia AS NomAgencia
,tmCdCCosto,CCosto,tmCdSubCos,SubCosto,tmpVehiculo,tmTipoAfi
,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 LEFT JOIN Puc AS P ON TR.tmCodigo=P.IdCuenta LEFT JOIN PucNiif AS PN ON TR.tmCodigo=PN.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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmTipoAfi,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelFormasCue] @pmIdForma VARCHAR(4),@pmIdConcepto VARCHAR(4)
 AS 
 DELETE FROM FormasCue
 WHERE IdForma=@pmIdForma AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelFmtDoc] @pmIdDoc VARCHAR(3),@pmIdForma CHAR(2)
 AS 
 DELETE FROM Sys_FmtDoc
 WHERE IdDoc=@pmIdDoc AND IdForma LIKE ISNULL(@pmIdForma,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpFmtDocPre] @pmIdDoc VARCHAR(3),@pmIdForma CHAR(2),@pmPredtmnado BIT
 AS 
 UPDATE Sys_FmtDoc SET Predtmnado=@pmPredtmnado
 WHERE IdDoc=@pmIdDoc AND IdForma<>@pmIdForma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrAprobado,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmVrAprobado MONEY,@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
,VrAprobado=@pmVrAprobado,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipAut=@pmTipAut AND Autoriza=@pmAutoriza AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrAprobado,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrAprobado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Autoriza
 WHERE TipAut=@pmTipAut AND Autoriza=@pmAutoriza AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmVrAprobado MONEY,@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,VrAprobado)
 VALUES (@pmTipAut,@pmAutoriza,@pmIdCia,@pmFecha,@pmIdConcepto,@pmNumAutoriza,@pmIdUsuarioAut,@pmIdEstacion,@pmNitTercero,@pmpVehiculo,@pmTipoDoc,@pmDocumento,@pmIdCiaDoc,@pmFechaDoc,@pmDocRef,@pmOrigenAdd
,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmVrAprobado)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrAprobado,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraConceptos] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmDescripcion VARCHAR(250),@pmTarifa DECIMAL(14,4),@pmTipoConc VARCHAR(10),@pmRubroConcep VARCHAR(20)
,@pmCantidad DECIMAL(14,4),@pmVrUnitario MONEY,@pmTarifIva DECIMAL(14,4),@pmIdConcepto VARCHAR(4),@pmCdCuenta VARCHAR(16),@pmNitTercero VARCHAR(16),@pmCdTipoEsc VARCHAR(4),@pmFechaNov SMALLDATETIME
,@pmVrBase MONEY,@pmTipoTarif CHAR(1),@pmRefConc VARCHAR(50),@pmFijos BIT,@pmIncBaseRet INT,@pmReferencia2 VARCHAR(50),@pmReferencia3 VARCHAR(50)
 AS 
 INSERT INTO Trn_TraConceptos (TipDoc,Documento,IdCia,Item,Descripcion,Tarifa,TipoConc,RubroConcep,Cantidad,VrUnitario,TarifIva,IdConcepto,CdCuenta,NitTercero,CdTipoEsc,FechaNov,VrBase,TipoTarif,RefConc,Fijos,IncBaseRet,Referencia2,Referencia3)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmItem,@pmDescripcion,@pmTarifa,@pmTipoConc,@pmRubroConcep,@pmCantidad,@pmVrUnitario,@pmTarifIva,@pmIdConcepto,@pmCdCuenta,@pmNitTercero
,@pmCdTipoEsc,@pmFechaNov,@pmVrBase,@pmTipoTarif,@pmRefConc,@pmFijos,@pmIncBaseRet,@pmReferencia2,@pmReferencia3)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraConceptosItem] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmItem INT
 AS 
 SELECT TipDoc,Documento,IdCia,Item,Descripcion,Tarifa,TipoConc,RubroConcep,Cantidad,VrUnitario,TarifIva,IdConcepto
,CdCuenta,NitTercero,CdTipoEsc,FechaNov,VrBase,TipoTarif,RefConc,Fijos,IncBaseRet,Referencia2,Referencia3
 FROM Trn_TraConceptos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia   AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraConceptosFac] @pmTipDoc VARCHAR(3),@pmFacturaIni INT,@pmFacturaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT RF.Factura,RF.IdCia AS CdCiaFac,C.TipDoc AS TipRem,C.Documento AS NumRemesa,C.IdCia AS CdCia,C.Item,C.Descripcion,Tarifa,TipoConc
,RubroConcep,C.Cantidad,C.VrUnitario,C.TarifIva,C.IdConcepto AS CdConcepto,Concepto,C.CdCuenta,NomCuenta
,C.NitTercero,RazonSocial,CdTipoEsc,TipoEscolta,FechaNov,VrBase,TipoTarif,RefConc,C.Referencia2,C.Referencia3
 FROM Trn_TraFacRemesas AS RF INNER JOIN Trn_TraConceptos AS C ON RF.TipRem=C.TipDoc AND RF.Remesa=C.Documento AND RF.IdCiaRem=C.IdCia
 INNER JOIN ConcDiversos AS CD ON C.IdConcepto=CD.IdConcepto
 LEFT JOIN TiposEsc AS TE ON C.CdTipoEsc=TE.IdTipoEsc LEFT JOIN Terceros AS T ON C.NitTercero=T.IdTercero
 LEFT JOIN Puc AS P ON C.CdCuenta=P.IdCuenta
 WHERE RF.TipDoc=@pmTipDoc AND RF.Factura BETWEEN @pmFacturaIni AND @pmFacturaFin AND RF.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_TraConc_Sel] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraConc (tmNumero,tmItem,tmDescripcion,tmTarifa,tmTipoConc,tmRubro,tmCantidad,tmVrUnitario,tmTarifIva,tmIdConcepto,tmCdCuenta,tmNitTercero,tmVrBase,tmTipoTarif,tmRefConc,tmFijos,tmBaseRet)
 SELECT @pmtmNumero,Item,Descripcion,Tarifa,TipoConc,RubroConcep, Cantidad, VrUnitario, TarifIva, IdConcepto, CdCuenta, NitTercero,VrBase,TipoTarif,RefConc,Fijos,IncBaseRet
 FROM   Trn_TraConceptos
 WHERE TipDoc=@pmTipDoc  AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraConceptosFmt] @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Documento,IdCia,Item,Descripcion,Tarifa,TipoConc,RubroConcep,Cantidad,VrUnitario,TarifIva
,C.IdConcepto AS CdConcepto,Concepto,CdCuenta,NomCuenta,NitTercero,RazonSocial,CdTipoEsc,TipoEscolta
,FechaNov,VrBase,TipoTarif,RefConc,Referencia2,Referencia3
 FROM Trn_TraConceptos AS C INNER JOIN ConcDiversos AS CD ON C.IdConcepto=CD.IdConcepto
 LEFT JOIN TiposEsc AS TE ON C.CdTipoEsc=TE.IdTipoEsc LEFT JOIN Terceros AS T ON C.NitTercero=T.IdTercero
 LEFT JOIN Puc AS P ON C.CdCuenta=P.IdCuenta
 WHERE TipDoc=@pmTipDoc AND C.Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin AND C.IdCia=@pmIdCia 
 ORDER BY C.Documento
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTraConceptos] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraConceptos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraConceptos]  @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmDescripcion VARCHAR(250),@pmTarifa DECIMAL(14,4),@pmTipoConc VARCHAR(10),@pmRubroConcep VARCHAR(20),@pmCantidad DECIMAL(14,4)
,@pmVrUnitario MONEY,@pmTarifIva DECIMAL(14,4),@pmIdConcepto VARCHAR(4),@pmCdCuenta VARCHAR(16),@pmNitTercero VARCHAR(16),@pmCdTipoEsc VARCHAR(4),@pmFechaNov SMALLDATETIME,@pmVrBase MONEY,@pmTipoTarif CHAR(1),@pmRefConc VARCHAR(50)
,@pmFijos BIT,@pmIncBaseRet INT,@pmReferencia2 VARCHAR(50),@pmReferencia3 VARCHAR(50)
 AS 
 UPDATE Trn_TraConceptos SET Descripcion=@pmDescripcion,Tarifa=@pmTarifa,TipoConc=@pmTipoConc,RubroConcep=@pmRubroConcep,Cantidad=@pmCantidad,VrUnitario=@pmVrUnitario,TarifIva=@pmTarifIva,IdConcepto=@pmIdConcepto,CdCuenta=@pmCdCuenta
,NitTercero=@pmNitTercero,CdTipoEsc=@pmCdTipoEsc,FechaNov=@pmFechaNov,VrBase=@pmVrBase,TipoTarif=@pmTipoTarif,RefConc=@pmRefConc,Fijos=@pmFijos,IncBaseRet=@pmIncBaseRet,Referencia2=@pmReferencia2,Referencia3=@pmReferencia3
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraConceptos] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Documento,IdCia,Item,Descripcion,Tarifa,TipoConc,RubroConcep,Cantidad,VrUnitario,TarifIva
,IdConcepto,CdCuenta,NitTercero,CdTipoEsc,FechaNov,VrBase,TipoTarif,RefConc,Fijos,IncBaseRet,Referencia2,Referencia3
 FROM Trn_TraConceptos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_AfDepPartesLta] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdActivo,NomActivo,tmIdParte,DescParte,NumParte,tmCostoIni,tmMejoras,tmDisminucion,tmAcuAjustes,tmDepreciaHis
,tmDepreciaMes,tmAjustesInf,tmAjustesDep,tmDepreciaAcum,tmAjuDepAcum,tmDepMejoras,tmTarifa
,tmVidaUtil,tmMetodo,tmFechaIni,tmEstadoReg,tmObservacion,tmSalvamento,tmUnidades,tmCantidad
,P.IdEstado AS CdEstado,Estado,Fec_Retiro
 FROM tm_AfDepPartes AS T INNER JOIN Activos AS A ON T.tmIdActivo=A.IdActivo
 INNER JOIN Activos_Partes AS P ON T.tmIdActivo=P.IdActivo AND T.tmIdParte=P.Item
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryAfPartesLta] @pmNumeroIni INT,@pmNumeroFin INT
 AS 
 SELECT D.Numero AS NumDeprecia,D.Item,D.IdActivo AS CdActivo,NomActivo,IdParte,DescParte,NumParte,D.VrBaseDep,D.VrCostoIni,D.VrMejoras,D.VrDisminuc
,D.VrAcumAju,D.VrDepAcum,VrDeprecia,VrDepHis,VrAjusteInf,VrAjuDeprec,VrAcumAjuDep,VrDepMejoras,D.VrSalvamnto,D.Unidades,D.Cantidad
,D.FecIniDep,Vida_Util,D.MetDeprec,P.IdEstado AS CdEstado,ED.Estado AS NomEstado,Fec_Retiro
 FROM Trn_AfPartes AS D INNER JOIN Activos AS A ON D.IdActivo=A.IdActivo 
 INNER JOIN Activos_Partes AS P ON D.IdActivo=P.IdActivo AND D.IdParte=P.Item
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 WHERE D.Numero BETWEEN @pmNumeroIni AND @pmNumeroFin
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelAfEstados] @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM AfEstados
 WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelCalndarioMes] @pmMes INT,@pmAnno INT
 AS 
 DELETE FROM Calndario
 WHERE Month(FechaDia)=@pmMes AND Year(FechaDia)=@pmAnno
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpCalndario] @pmFechaDia SMALLDATETIME,@pmTipoFtvo VARCHAR(3)
 AS 
 UPDATE Calndario SET TipoFtvo=@pmTipoFtvo
 WHERE FechaDia=@pmFechaDia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryCalndarioMes] @pmMes INT,@pmAnno INT
 AS 
 SELECT FechaDia,TipoFtvo  FROM Calndario 
 WHERE MONTH(FechaDia)=@pmMes AND YEAR(FechaDia)=@pmAnno
 ORDER BY FechaDia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsCalndario] @pmFechaDia SMALLDATETIME,@pmTipoFtvo VARCHAR(3)
 AS 
 INSERT INTO Calndario (FechaDia,TipoFtvo)
 VALUES (@pmFechaDia,@pmTipoFtvo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelCalndario] @pmFechaDia SMALLDATETIME
 AS 
 DELETE FROM Calndario
 WHERE FechaDia=@pmFechaDia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTanqDispens] @pmIdMngra VARCHAR(10)
 AS 
 DELETE FROM TanqDispens
 WHERE IdMngra=@pmIdMngra
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmDiasSan,tmDiasDnrPen,tmDiasDnrSal,tmNovDsr,tmFecNovIng,tmFecNovRet,tmFecNovVsp,tmFecIniSln,tmFecFinSln,tmFecIniIge,tmFecFinIge,tmFecIniLma,tmFecFinLma,tmFecIniVac,tmFecFinVac,tmFecIniVct,tmFecFinVct,tmFecIniIrl,tmFecFinIrl,tmVrIBCparaf,tmCantHoras,tmTipSalario)
 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,0,0,0
,0,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,VrIBCparaf,CantHoras,TipoSalario
 FROM Trn_NomAlqDet
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmVrIBCparaf MONEY,@pmCantHoras INT,@pmTipoSalario VARCHAR(10)
 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,VrIBCparaf,CantHoras,TipoSalario)
 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,@pmVrIBCparaf,@pmCantHoras,@pmTipoSalario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrIBCparaf,CantHoras,TipoSalario
 FROM Trn_NomAlqDet
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmVrIBCparaf MONEY,@pmCantHoras INT,@pmTipoSalario VARCHAR(10)
 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,VrIBCparaf=@pmVrIBCparaf,CantHoras=@pmCantHoras,TipoSalario=@pmTipoSalario
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,R.Num_Vehic,R.Num_Trailer,Modalidad,DirEntrega,NumAprob,FecAprob,CdUsuAprob
,R.Observacion AS Observ,TipOrd,NumOrden,IdCiaOrd,NomContacto,TelsContacto,EmailContacto,D.Referencia
 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 R.TipDoc='REQ' AND R.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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_TraDetalleLta] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdMercancia,tmDescripMcias,tmCantidad,tmCases,tmCajas,tmPalets,tmPesoNeto,tmUndMed,tmTarifClie,tmUndTarifa,tmTarifTabla,tmTarifPago,tmUndTarPago
,tmVrDeclarado,tmTarifSeguro,tmVrSeguro,tmIdOrigen,LO.Localidad AS LugarOrigen,tmIdDestino,LD.Localidad AS LugarDestino,tmDirOrigen,tmDirDestino,tmNitRemite,tmRemitente,tmSedeRem
,tmNitDestntario,tmDestinatario,tmSedeDest,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmdmsLargo,tmdmsAncho,tmdmsAlto,tmVolumen,tmUndVol,tmIdUnd,U.Unidad AS UndPresenta,tmIdEmp,Empaque 
,tmIdNat,Natlzaprod,tmIdMnjo,ManejoMcia,tmIdTmcia,TipoMcia,tmCdRango,DescripRango,tmContenedor1,tmContenedor2,tmPesoCont,tmCodBodega,tmTipoTraslado,tmCdTipoVeh,tmTipoServicio
 FROM tm_TraDetalle AS T INNER JOIN Localidades AS LO ON T.tmIdOrigen=LO.IdLocal
 INNER JOIN Localidades AS LD ON T.tmIdDestino=LD.IdLocal
 INNER JOIN UndMed AS U ON T.tmIdUnd=U.IdUnd  LEFT JOIN Empaques AS E ON T.tmIdEmp=E.IdEmp
 LEFT JOIN TiposNat AS TN ON T.tmIdNat=TN.IdNat LEFT JOIN TiposMnjo AS MJ ON T.tmIdMnjo=MJ.IdMnjo
 LEFT JOIN TiposMcia AS TM ON T.tmIdTmcia=TM.IdTmcia 
 LEFT JOIN RangosPeso AS RP ON T.tmCdRango=RP.IdRango
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_TraDetalleOct] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed,tmIdEmp,Empaque,tmIdNat,Natlzaprod
,tmIdOrigen,LO.Localidad AS LugarOrigen,tmIdDestino,LD.Localidad AS LugarDestino,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3
,tmDirOrigen,tmDirDestino,tmNitRemite,tmRemitente,tmSedeRem,tmNitDestntario,tmDestinatario,tmSedeDest,tmCases,tmCajas,tmPalets
,tmdmsLargo,tmdmsAncho,tmdmsAlto,tmVolumen,tmUndVol,tmIdUnd,U.Unidad AS UndPresenta,tmIdMnjo,ManejoMcia,tmIdTmcia,TipoMcia,tmCdRango
,DescripRango,tmContenedor1,tmContenedor2,tmPesoCont
--,tmTarifClie,tmTarifTabla,tmTarifPago,tmUndTarifa,tmVrDeclarado,tmTarifSeguro,tmVrSeguro,tmUndTarPago
 FROM tm_TraDetalle AS T INNER JOIN Localidades AS LO ON T.tmIdOrigen=LO.IdLocal
 INNER JOIN Localidades AS LD ON T.tmIdDestino=LD.IdLocal
 INNER JOIN UndMed AS U ON T.tmIdUnd=U.IdUnd  LEFT JOIN Empaques AS E ON T.tmIdEmp=E.IdEmp
 LEFT JOIN TiposNat AS TN ON T.tmIdNat=TN.IdNat LEFT JOIN TiposMnjo AS MJ ON T.tmIdMnjo=MJ.IdMnjo
 LEFT JOIN TiposMcia AS TM ON T.tmIdTmcia=TM.IdTmcia 
 LEFT JOIN RangosPeso AS RP ON T.tmCdRango=RP.IdRango
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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.IdEmp AS CdEmp,Empaque,EstadoMcia,UmCapac,CodigoUN,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 LEFT JOIN Empaques AS E ON M.IdEmp=E.IdEmp
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_TraDetalleMuc] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed
,tmIdEmp,Empaque,tmIdNat,Natlzaprod,tmReferencia1,tmReferencia2,tmTarifTabla,tmTarifPago,tmIdOrigen,LO.Localidad AS LugarOrigen
,tmIdDestino,LD.Localidad AS LugarDestino,tmNitRemite,tmRemitente,tmSedeRem,tmDirOrigen,tmNitDestntario,tmDestinatario,tmSedeDest,tmDirDestino
,tmdmsLargo,tmdmsAncho,tmdmsAlto,tmVolumen,tmCases,tmCajas,tmPalets,tmRemision,tmReferencia3,tmDocCliente,tmPesoCont,tmUndTarPago,tmCodBodega,tmTipoTraslado
,tmTieCargue,tmMinCargue,tmFecLlegaCargue,tmFecIniCargue,tmFecFinCargue,tmTieDesc,tmMinDescargue,tmFecLlegaDesc,tmFecInidesc,tmFecFindesc
--datos pendientes
--,tmTarifClie,tmUndTarifa,tmVrDeclarado,tmTarifSeguro,tmVrSeguro,tmUndVol,tmIdUnd,U.Unidad AS UndPresenta
--,tmIdMnjo,ManejoMcia,tmIdTmcia,TipoMcia,tmCdRango,DescripRango,tmContenedor1,tmContenedor2, 
 FROM tm_TraDetalle AS T INNER JOIN Localidades AS LO ON T.tmIdOrigen=LO.IdLocal
 INNER JOIN Localidades AS LD ON T.tmIdDestino=LD.IdLocal
 INNER JOIN UndMed AS U ON T.tmIdUnd=U.IdUnd  LEFT JOIN Empaques AS E ON T.tmIdEmp=E.IdEmp
 LEFT JOIN TiposNat AS TN ON T.tmIdNat=TN.IdNat 
 --LEFT JOIN TiposMnjo AS MJ ON T.tmIdMnjo=MJ.IdMnjo
-- LEFT JOIN TiposMcia AS TM ON T.tmIdTmcia=TM.IdTmcia 
-- LEFT JOIN RangosPeso AS RP ON T.tmCdRango=RP.IdRango
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_TraDetalleRmt] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed,tmIdEmp,Empaque,tmIdNat,Natlzaprod
,tmIdOrigen,LO.Localidad AS LugarOrigen,tmIdDestino,LD.Localidad AS LugarDestino,tmTarifClie,tmUndTarifa,tmTarifTabla,tmTarifPago,tmUndTarPago
,tmVrDeclarado,tmTarifSeguro,tmVrSeguro,tmRemision,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3
,tmDirOrigen,tmDirDestino,tmNitRemite,tmRemitente,tmSedeRem,tmNitDestntario,tmDestinatario,tmSedeDest
,tmdmsLargo,tmdmsAncho,tmdmsAlto,tmVolumen,tmUndVol,tmCases,tmCajas,tmPalets,tmIdUnd,U.Unidad AS UndPresenta
,tmIdMnjo,ManejoMcia,tmIdTmcia,TipoMcia,tmCdRango,DescripRango,tmContenedor1,tmContenedor2,tmPesoCont
 FROM tm_TraDetalle AS T INNER JOIN Localidades AS LO ON T.tmIdOrigen=LO.IdLocal
 INNER JOIN Localidades AS LD ON T.tmIdDestino=LD.IdLocal
 INNER JOIN UndMed AS U ON T.tmIdUnd=U.IdUnd  LEFT JOIN Empaques AS E ON T.tmIdEmp=E.IdEmp
 LEFT JOIN TiposNat AS TN ON T.tmIdNat=TN.IdNat LEFT JOIN TiposMnjo AS MJ ON T.tmIdMnjo=MJ.IdMnjo
 LEFT JOIN TiposMcia AS TM ON T.tmIdTmcia=TM.IdTmcia 
 LEFT JOIN RangosPeso AS RP ON T.tmCdRango=RP.IdRango
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraRemMciasOrd] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,IdMercancia,DescripMcias,Cantidad,PesoNeto,D.UndMed AS UndPeso,Cases,Cajas,Palets,D.IdUnd AS CdUnid,Unidad
,Remision,DocCliente,Referencia1,Referencia2,Referencia3,NitRemite,Remitente,SedeRem,DirOrigen,IdOrigen,LO.Localidad AS CiudadOrigen
,NitDestntario,Destinatario,SedeDest,DirDestino,IdDestino,LD.Localidad AS CiudadDestino
,dmsLargo,dmsAncho,dmsAlto,Volumen,UndVol,Contenedor1,Contenedor2,CdRango,D.IdEmp AS CdEmpaque,Empaque
,IdNat,IdMnjo,IdTmcia,PesoCont,TipDocInv,NumDocInv,IdCiaDocInv,ItemDocInv
 FROM Trn_TraRemMcias AS D INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal
 INNER JOIN UndMed AS U ON D.IdUnd=U.IdUnd INNER JOIN Empaques AS E ON D.IdEmp=E.IdEmp
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryAfDeprecia] @pmNumero INT
 AS 
 SELECT Numero,Fecha,Estado,IdUsuario,FechaCrea,IdCia
 FROM Trn_AfDeprecia
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelAfDeprecia] @pmNumero INT
 AS 
 DELETE FROM Trn_AfDeprecia
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilCostos] @pmTipProc VARCHAR(3),@pmNumProceso INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipProc,NumProceso,IdCia,Fecha,FechaInicio,FechaFinal,IdConcepto,NitProveedor,CodCiaFac,Cantidad,SubTotal,VrTransporte,VrTraVacio,TipCom,Comprobante,IdCiaCom,Anulado,FecDev,NumDev,Observacion,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario
 FROM Trn_OilCostos
 WHERE TipProc=@pmTipProc AND NumProceso=@pmNumProceso AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOilCostos] @pmTipProc VARCHAR(3),@pmNumProceso INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaInicio SMALLDATETIME,@pmFechaFinal SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmNitProveedor VARCHAR(16),@pmCodCiaFac CHAR(2),@pmCantidad DECIMAL(16,6),@pmSubTotal DECIMAL(16,6),@pmVrTransporte DECIMAL(16,6),@pmVrTraVacio DECIMAL(16,6),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmNumDev INT,@pmObservacion VARCHAR(2000),@pmFechaCrea SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmOrigenAdd VARCHAR(10),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_OilCostos (TipProc,NumProceso,IdCia,Fecha,FechaInicio,FechaFinal,IdConcepto,NitProveedor,CodCiaFac,Cantidad,SubTotal,VrTransporte,VrTraVacio,TipCom,Comprobante,IdCiaCom,Anulado,FecDev,NumDev,Observacion,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario)
 VALUES (@pmTipProc,@pmNumProceso,@pmIdCia,@pmFecha,@pmFechaInicio,@pmFechaFinal,@pmIdConcepto,@pmNitProveedor,@pmCodCiaFac,@pmCantidad,@pmSubTotal,@pmVrTransporte,@pmVrTraVacio,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmFecDev,@pmNumDev,@pmObservacion,@pmFechaCrea,@pmIdCiaCrea,@pmOrigenAdd,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpOilCostos] @pmTipProc VARCHAR(3),@pmNumProceso INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaInicio SMALLDATETIME,@pmFechaFinal SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmNitProveedor VARCHAR(16),@pmCodCiaFac CHAR(2),@pmCantidad DECIMAL(16,6),@pmSubTotal DECIMAL(16,6),@pmVrTransporte DECIMAL(16,6),@pmVrTraVacio DECIMAL(16,6),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmNumDev INT,@pmObservacion VARCHAR(2000)
 AS 
 UPDATE Trn_OilCostos SET Fecha=@pmFecha,FechaInicio=@pmFechaInicio,FechaFinal=@pmFechaFinal,IdConcepto=@pmIdConcepto,NitProveedor=@pmNitProveedor,CodCiaFac=@pmCodCiaFac,Cantidad=@pmCantidad,SubTotal=@pmSubTotal,VrTransporte=@pmVrTransporte,VrTraVacio=@pmVrTraVacio,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,FecDev=@pmFecDev,NumDev=@pmNumDev,Observacion=@pmObservacion
 WHERE TipProc=@pmTipProc AND NumProceso=@pmNumProceso AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelOilCostos] @pmTipProc VARCHAR(3),@pmNumProceso INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_OilCostos
 WHERE TipProc=@pmTipProc AND NumProceso=@pmNumProceso AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomAstncia] @pmIdEmpleado VARCHAR(16),@pmFechaDia SMALLDATETIME
 AS 
 DELETE FROM Trn_NomAstncia
 WHERE IdEmpleado=@pmIdEmpleado AND FechaDia=ISNULL(@pmFechaDia,FechaDia)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryActivos_Partes] @pmIdActivo VARCHAR(30),@pmItem INT
 AS 
 SELECT IdActivo,Item,DescParte,NumParte,FecIniDep,MetDeprec,VidaUtil,UndsProd,VrCompra,VrBaseDep,VrMejoras,VrDisminuc,VrSalvamento,VrDepAcum,VrDepAcumHis
,VrAjuAcum,VrAjuDepAcum,NitProv,IdEstado,FecCreacion,IdUsuario,CueEnAfijo,IdCuenta,IdCueDep,IdCueGasto,CodNiifAct,CodNiifDep,CodNiifGasto,Fec_Retiro,VU_Extend
 FROM Activos_Partes
 WHERE IdActivo=@pmIdActivo AND Item=@pmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpActivos_Partes] @pmIdActivo VARCHAR(30),@pmItem INT,@pmDescParte VARCHAR(250),@pmNumParte VARCHAR(30),@pmFecIniDep SMALLDATETIME,@pmMetDeprec VARCHAR(10),@pmVidaUtil INT,@pmUndsProd INT,@pmVrCompra MONEY,@pmVrBaseDep MONEY,@pmVrSalvamento MONEY,@pmVrDepAcum MONEY,@pmVrDepAcumHis MONEY
,@pmNitProv VARCHAR(16),@pmIdEstado VARCHAR(4),@pmCueEnAfijo BIT,@pmIdCuenta VARCHAR(16),@pmIdCueDep VARCHAR(16),@pmIdCueGasto VARCHAR(16),@pmCodNiifAct VARCHAR(16),@pmCodNiifDep VARCHAR(16),@pmCodNiifGasto VARCHAR(16)
,@pmVrMejoras MONEY,@pmVrDisminuc MONEY,@pmVrAjuAcum MONEY,@pmVrAjuDepAcum MONEY,@pmFec_Retiro SMALLDATETIME,@pmVU_Extend INT
 AS 
 UPDATE Activos_Partes SET DescParte=@pmDescParte,NumParte=@pmNumParte,FecIniDep=@pmFecIniDep,MetDeprec=@pmMetDeprec,VidaUtil=@pmVidaUtil,UndsProd=@pmUndsProd,VrCompra=@pmVrCompra,VrBaseDep=@pmVrBaseDep,VrSalvamento=@pmVrSalvamento,VrDepAcum=@pmVrDepAcum,VrDepAcumHis=@pmVrDepAcumHis,NitProv=@pmNitProv,IdEstado=@pmIdEstado
,CueEnAfijo=@pmCueEnAfijo,IdCuenta=@pmIdCuenta,IdCueDep=@pmIdCueDep,IdCueGasto=@pmIdCueGasto,CodNiifAct=@pmCodNiifAct,CodNiifDep=@pmCodNiifDep,CodNiifGasto=@pmCodNiifGasto,VrMejoras=@pmVrMejoras,VrDisminuc=@pmVrDisminuc,VrAjuAcum=@pmVrAjuAcum,VrAjuDepAcum=@pmVrAjuDepAcum,Fec_Retiro=@pmFec_Retiro,VU_Extend=@pmVU_Extend
 WHERE IdActivo=@pmIdActivo AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpActivos_PartesDis] @pmIdActivo VARCHAR(30),@pmItem INT,@pmVrDisminucion MONEY,@pmAuto BIT
 AS 
 IF @pmAuto<>0 
  UPDATE Activos_Partes SET  VrDisminuc=(VrDisminuc+@pmVrDisminucion)  WHERE IdActivo=@pmIdActivo AND Item=@pmItem
 ELSE
  UPDATE Activos_Partes SET  VrDisminuc=@pmVrDisminucion  WHERE IdActivo=@pmIdActivo AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpActivos_PartesMej] @pmIdActivo VARCHAR(30),@pmItem INT,@pmVrMejoras MONEY,@pmAuto BIT
 AS 
 IF @pmAuto<>0 
  UPDATE Activos_Partes SET  VrMejoras=(VrMejoras+@pmVrMejoras)  WHERE IdActivo=@pmIdActivo AND Item=@pmItem
 ELSE
  UPDATE Activos_Partes SET  VrMejoras=@pmVrMejoras  WHERE IdActivo=@pmIdActivo AND Item=@pmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsActivos_Partes] @pmIdActivo VARCHAR(30),@pmItem INT,@pmDescParte VARCHAR(250),@pmNumParte VARCHAR(30),@pmFecIniDep SMALLDATETIME,@pmMetDeprec VARCHAR(10),@pmVidaUtil INT,@pmUndsProd INT,@pmVrCompra MONEY,@pmVrBaseDep MONEY,@pmVrSalvamento MONEY
,@pmVrDepAcum MONEY,@pmVrDepAcumHis MONEY,@pmNitProv VARCHAR(16),@pmIdEstado VARCHAR(4),@pmFecCreacion SMALLDATETIME,@pmIdUsuario VARCHAR(11),@pmCueEnAfijo BIT,@pmIdCuenta VARCHAR(16),@pmIdCueDep VARCHAR(16),@pmIdCueGasto VARCHAR(16),@pmCodNiifAct VARCHAR(16),@pmCodNiifDep VARCHAR(16),@pmCodNiifGasto VARCHAR(16)
,@pmVrMejoras MONEY,@pmVrDisminuc MONEY,@pmVrAjuAcum MONEY,@pmVrAjuDepAcum MONEY,@pmFec_Retiro SMALLDATETIME,@pmVU_Extend INT
 AS 
 INSERT INTO Activos_Partes (IdActivo,Item,DescParte,NumParte,FecIniDep,MetDeprec,VidaUtil,UndsProd,VrCompra,VrBaseDep,VrMejoras,VrDisminuc,VrSalvamento,VrDepAcum,VrDepAcumHis,VrAjuAcum,VrAjuDepAcum,NitProv,IdEstado,FecCreacion,IdUsuario,CueEnAfijo,IdCuenta,IdCueDep,IdCueGasto,CodNiifAct,CodNiifDep,CodNiifGasto,Fec_Retiro,VU_Extend)
 VALUES (@pmIdActivo,@pmItem,@pmDescParte,@pmNumParte,@pmFecIniDep,@pmMetDeprec,@pmVidaUtil,@pmUndsProd,@pmVrCompra,@pmVrBaseDep,@pmVrMejoras,@pmVrDisminuc,@pmVrSalvamento,@pmVrDepAcum,@pmVrDepAcumHis,@pmVrAjuAcum,@pmVrAjuDepAcum,@pmNitProv,@pmIdEstado,@pmFecCreacion,@pmIdUsuario,@pmCueEnAfijo,@pmIdCuenta,@pmIdCueDep,@pmIdCueGasto
,@pmCodNiifAct,@pmCodNiifDep,@pmCodNiifGasto,@pmFec_Retiro,@pmVU_Extend)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposMotdev] @pmIdMotDev VARCHAR(4),@pmMotivoDev VARCHAR(150),@pmTipoFact VARCHAR(3),@pmInactivo BIT
 AS 
 INSERT INTO TiposMotdev (IdMotDev,MotivoDev,TipoFact,Inactivo)
 VALUES (@pmIdMotDev,@pmMotivoDev,@pmTipoFact,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposMotdevDso] @pmTipoFact VARCHAR(4)=Null
 AS 
 --@pmTipoFact NO APLICA AUN
 SELECT IdMotDev,MotivoDev,TipoFact 
 FROM TiposMotdev
 WHERE Inactivo=0
 ORDER BY MotivoDev
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposMotdev] @pmIdMotDev VARCHAR(4),@pmMotivoDev VARCHAR(150),@pmTipoFact VARCHAR(3),@pmInactivo BIT
 AS 
 UPDATE TiposMotdev SET MotivoDev=@pmMotivoDev,TipoFact=@pmTipoFact,Inactivo=@pmInactivo
 WHERE IdMotDev=@pmIdMotDev
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposMotdev] @pmIdMotDev VARCHAR(4)
 AS 
 DELETE FROM TiposMotdev WHERE IdMotDev=@pmIdMotDev
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposMotdev] @pmIdMotDev VARCHAR(4)
 AS 
 IF @pmIdMotDev IS NULL
  BEGIN
   SELECT IdMotDev,MotivoDev,TipoFact 
   FROM TiposMotdev
   WHERE Inactivo=0
  END
 ELSE
  BEGIN
   SELECT IdMotDev,MotivoDev,TipoFact,Inactivo
   FROM TiposMotdev
   WHERE IdMotDev=@pmIdMotDev
  END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoContab,CdComNiif,CdComFiscal,CodCiaTdo,TipoConRev
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposDocInv]
 AS 
 SELECT  IdDoc,TipoDoc,IdDoc+'  '+ TipoDoc AS DsTip
 FROM  Sys_TiposDoc 
 WHERE IdDoc  NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC','FIS','VCC') AND Inactivo=0
 ORDER BY IdDoc
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTiposDocEnt]
 AS 
 SELECT  IdDoc,TipoDoc,IdDoc+'  '+ TipoDoc AS DsTip
 FROM  Sys_TiposDoc 
 WHERE IdDoc IN ('ENT','COM','SAL','SAI') 
 ORDER BY IdDoc
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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','ODP','DOP')
 ORDER BY IdDoc
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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))
 AND (ISNULL(ClaseNiif,'')='CONTAB' OR LEN(ISNULL(ClaseNiif,''))=0)
 UNION
 SELECT   IdDoc AS IdCom,TipoDoc+ ' '+ IdDoc AS DsTip   FROM   Sys_TiposDoc
 WHERE  IdDoc IN ('DVE','DEI','DOP','DEC','DVO','DRV','DRR') AND Inactivo=0
 ORDER BY IdCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CC.FecAdd,CC.FecUpdate,CT.CCosto,SC.SubCosto
 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
 LEFT JOIN CentroCosto AS CT ON CC.CdCCosto=CT.IdCCosto LEFT JOIN SubCentros AS SC ON CC.CdSubCos=SC.IdSubCos
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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','ODP')
 ORDER BY IdDoc
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposDocAut]
 AS 
 SELECT  IdDoc,TipoDoc,IdDoc+'  '+ TipoDoc AS DsTip
 FROM  Sys_TiposDoc 
 WHERE IdDoc IN ('FCR','DFO','ODB','OD2','PR1','NTS','MUC','OSA','OCT','OSM')
 --todas las fac.credito con este codigo FCR
 ORDER BY IdDoc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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)
,@pmEdoCausac INT,@pmTotalOtros MONEY,@pmTipoLiquida VARCHAR(10),@pmTotalCustodia MONEY,@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinCausacion (TipoProc,NumProc,IdCia,Fecha,TotalCapital,TotalInteres,TotalIntMora,TotalOtros,TipoLiquida,TipoCred,TipCom,Comprobante,Anulado,Observacion,EdoCausac,TimeSys,IdUsuario,TotalCustodia)
 VALUES (@pmTipoProc,@pmNumProc,@pmIdCia,@pmFecha,@pmTotalCapital,@pmTotalInteres,@pmTotalIntMora,@pmTotalOtros,@pmTipoLiquida,@pmTipoCred,@pmTipCom,@pmComprobante,@pmAnulado
,@pmObservacion,@pmEdoCausac,@pmTimeSys,@pmIdUsuario,@pmTotalCustodia)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomRetEmp] @pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Trn_NomRet
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CedContparte,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCedContparte VARCHAR(16),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_JurInvesHis (TipDoc,CasoID,IdCia,ActualizaID,FechaUp,Historial,EstadoAct,Pruebas,Documntos,CierreProc,ArchivoProc,NombreCparte,DirCparte,TelCparte,IdEstado,IdUsuario,CedContparte)
 VALUES (@pmTipDoc,@pmCasoID,@pmIdCia,@pmActualizaID,@pmFechaUp,@pmHistorial,@pmEstadoAct,@pmPruebas,@pmDocumntos,@pmCierreProc,@pmArchivoProc,@pmNombreCparte,@pmDirCparte,@pmTelCparte,@pmIdEstado,@pmIdUsuario,@pmCedContparte)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCedContparte VARCHAR(16),@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,CedContparte=@pmCedContparte,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia AND ActualizaID=@pmActualizaID
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTercPropietario] @pmIdPropietario VARCHAR(16)
 AS 
 DELETE FROM TercPropietario
 WHERE IdPropietario=@pmIdPropietario
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraManifiesto] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmFecEntrega SMALLDATETIME,@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmIdRuta VARCHAR(4),@pmIdRutaFle VARCHAR(4),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmnRemolque VARCHAR(10),@pmTipoAfiVehic VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdPoseedor VARCHAR(16),@pmVrFletes MONEY,@pmVrRetencion MONEY
,@pmVrReteIca MONEY,@pmVrDescuento MONEY,@pmVrAnticipo MONEY,@pmVrAntAdic MONEY,@pmVrNeto MONEY,@pmVrPagos MONEY,@pmVrCargos MONEY,@pmVrDctos MONEY,@pmTarifaFlete MONEY,@pmCantidad DECIMAL(14,4),@pmPesoTotal DECIMAL(14,4),@pmBaseRet MONEY,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmIdLocFletes VARCHAR(8),@pmFechaPago SMALLDATETIME,@pmPagoCargue VARCHAR(50),@pmPagoDescargue VARCHAR(50)
,@pmNumMintrans DECIMAL(14,2),@pmEdoMintrans VARCHAR(5),@pmRemesa INT,@pmIdCiaRem CHAR(2),@pmCumplido INT,@pmIdCiaCump CHAR(2),@pmFechaCump SMALLDATETIME,@pmEstCumplido INT,@pmTipOdp VARCHAR(3),@pmOrdPago INT,@pmIdCiaOdp CHAR(2),@pmFechaOdp SMALLDATETIME,@pmEstOrden INT, @pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmVrFleteNeto MONEY,@pmTipInfVia VARCHAR(3),@pmNumViaje INT,@pmCiaInfViaje CHAR(2),@pmCdCatPeaje VARCHAR(4),@pmCdCondRelev VARCHAR(16),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraManifiesto (TipDoc,Manifiesto,IdCia,Fecha,FecDespacho,FecEntrega,IdOrigen,IdDestino,IdRuta,IdRutaFle,IdVehiculo,IdConductor,nRemolque,TipoAfiVehic,IdPropietario,IdPoseedor,VrFletes,VrRetencion,VrReteIca,VrDescuento,VrAnticipo,VrAntAdic,VrNeto,VrPagos,VrCargos,VrDctos,TarifaFlete,Cantidad,PesoTotal,BaseRet,TarifaRet,TarifaIca,CodTarRet,CodTarIca,IdLocFletes,FechaPago,PagoCargue,PagoDescargue,NumMintrans,EdoMintrans,Remesa
,IdCiaRem,Cumplido,IdCiaCump,FechaCump,EstCumplido,TipOdp,OrdPago,IdCiaOdp,FechaOdp,EstOrden,VrFleteNeto,TipInfVia,NumViaje,CiaInfViaje,CdCatPeaje,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,CdCondRelev)
 VALUES (@pmTipDoc,@pmManifiesto,@pmIdCia,@pmFecha,@pmFecDespacho,@pmFecEntrega,@pmIdOrigen,@pmIdDestino,@pmIdRuta,@pmIdRutaFle,@pmIdVehiculo,@pmIdConductor,@pmnRemolque,@pmTipoAfiVehic,@pmIdPropietario,@pmIdPoseedor,@pmVrFletes,@pmVrRetencion,@pmVrReteIca,@pmVrDescuento,@pmVrAnticipo,@pmVrAntAdic,@pmVrNeto,@pmVrPagos,@pmVrCargos,@pmVrDctos,@pmTarifaFlete,@pmCantidad,@pmPesoTotal,@pmBaseRet,@pmTarifaRet,@pmTarifaIca
,@pmCodTarRet,@pmCodTarIca,@pmIdLocFletes,@pmFechaPago,@pmPagoCargue,@pmPagoDescargue,@pmNumMintrans,@pmEdoMintrans,@pmRemesa,@pmIdCiaRem,@pmCumplido,@pmIdCiaCump,@pmFechaCump,@pmEstCumplido,@pmTipOdp,@pmOrdPago,@pmIdCiaOdp,@pmFechaOdp,@pmEstOrden,@pmVrFleteNeto,@pmTipInfVia,@pmNumViaje,@pmCiaInfViaje,@pmCdCatPeaje,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmCdCondRelev)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraManifiesto] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmFecEntrega SMALLDATETIME,@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmIdRuta VARCHAR(4),@pmIdRutaFle VARCHAR(4),@pmIdVehiculo VARCHAR(10)
,@pmIdConductor VARCHAR(16),@pmnRemolque VARCHAR(10),@pmTipoAfiVehic VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdPoseedor VARCHAR(16),@pmVrFletes MONEY,@pmVrRetencion MONEY,@pmVrReteIca MONEY,@pmVrDescuento MONEY,@pmVrAnticipo MONEY,@pmVrAntAdic MONEY,@pmVrNeto MONEY,@pmVrPagos MONEY
,@pmVrCargos MONEY,@pmVrDctos MONEY,@pmTarifaFlete MONEY,@pmCantidad DECIMAL(14,4),@pmPesoTotal DECIMAL(14,4),@pmBaseRet MONEY,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmIdLocFletes VARCHAR(8),@pmFechaPago SMALLDATETIME
,@pmPagoCargue VARCHAR(50),@pmPagoDescargue VARCHAR(50),@pmNumMintrans DECIMAL(14,2),@pmEdoMintrans VARCHAR(5),@pmRemesa INT,@pmIdCiaRem CHAR(2),@pmCumplido INT,@pmIdCiaCump CHAR(2),@pmFechaCump SMALLDATETIME,@pmEstCumplido INT,@pmTipOdp VARCHAR(3),@pmOrdPago INT,@pmIdCiaOdp CHAR(2)
,@pmFechaOdp SMALLDATETIME,@pmEstOrden INT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmVrFleteNeto MONEY,@pmTipInfVia VARCHAR(3),@pmNumViaje INT,@pmCiaInfViaje CHAR(2)
,@pmCdCatPeaje VARCHAR(4),@pmCdCondRelev VARCHAR(16),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraManifiesto SET Fecha=@pmFecha,FecDespacho=@pmFecDespacho,FecEntrega=@pmFecEntrega,IdOrigen=@pmIdOrigen,IdDestino=@pmIdDestino,IdRuta=@pmIdRuta,IdRutaFle=@pmIdRutaFle,IdVehiculo=@pmIdVehiculo,IdConductor=@pmIdConductor,nRemolque=@pmnRemolque,TipoAfiVehic=@pmTipoAfiVehic,IdPropietario=@pmIdPropietario,IdPoseedor=@pmIdPoseedor
,VrFletes=@pmVrFletes,VrRetencion=@pmVrRetencion,VrReteIca=@pmVrReteIca,VrDescuento=@pmVrDescuento,VrAnticipo=@pmVrAnticipo,VrAntAdic=@pmVrAntAdic,VrNeto=@pmVrNeto,VrPagos=@pmVrPagos,VrCargos=@pmVrCargos,VrDctos=@pmVrDctos,TarifaFlete=@pmTarifaFlete,Cantidad=@pmCantidad,PesoTotal=@pmPesoTotal,BaseRet=@pmBaseRet,TarifaRet=@pmTarifaRet
,TarifaIca=@pmTarifaIca,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,IdLocFletes=@pmIdLocFletes,FechaPago=@pmFechaPago,PagoCargue=@pmPagoCargue,PagoDescargue=@pmPagoDescargue,NumMintrans=@pmNumMintrans,EdoMintrans=@pmEdoMintrans,Remesa=@pmRemesa,IdCiaRem=@pmIdCiaRem,Cumplido=@pmCumplido,IdCiaCump=@pmIdCiaCump
,FechaCump=@pmFechaCump,EstCumplido=@pmEstCumplido,TipOdp=@pmTipOdp,OrdPago=@pmOrdPago,IdCiaOdp=@pmIdCiaOdp,FechaOdp=@pmFechaOdp,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,EstOrden=@pmEstOrden,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom
,VrFleteNeto=@pmVrFleteNeto,TipInfVia=@pmTipInfVia,NumViaje=@pmNumViaje,CiaInfViaje=@pmCiaInfViaje,CdCatPeaje=@pmCdCatPeaje,FecUpdate=@pmFecUpdate,CdCondRelev=@pmCdCondRelev
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraManifiesto] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Manifiesto,IdCia,Fecha,FecDespacho,FecEntrega,IdOrigen,IdDestino,IdRuta,IdRutaFle,IdVehiculo,IdConductor,nRemolque,TipoAfiVehic,IdPropietario,IdPoseedor
,VrFletes,VrRetencion,VrReteIca,VrDescuento,VrAnticipo,VrAntAdic,VrNeto,VrPagos,VrCargos,VrDctos,TarifaFlete,Cantidad,PesoTotal,BaseRet,TarifaRet,TarifaIca,CodTarRet
,CodTarIca,IdLocFletes,FechaPago,PagoCargue,PagoDescargue,NumMintrans,EdoMintrans,Remesa,IdCiaRem,Cumplido,IdCiaCump,FechaCump,EstCumplido
,TipOdp,OrdPago,IdCiaOdp,FechaOdp,EstOrden,VrFleteNeto,TipInfVia,NumViaje,CiaInfViaje,CdCatPeaje,OrigenAdd,Anulado,FecDev,CdCondRelev
,Observacion,IdEstado,TipCom,Comprobante,IdCiaCom,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraManifiesto
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsClaseFon] @pmIdClase VARCHAR(4),@pmClaseFondo VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ClaseFon (IdClase,ClaseFondo,Inactivo)
 VALUES (@pmIdClase,@pmClaseFondo,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelClaseFon] @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseFon
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpClaseFon] @pmIdClase VARCHAR(4),@pmClaseFondo VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ClaseFon SET ClaseFondo=@pmClaseFondo,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CdTipoCot,SubTipo_Cot,Sal_Integral,TipoIdent,NotasAportante)
 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,CdTipoCot,SubTipo_Cot,Sal_Integral,TipoIdent,NotasAportante
 FROM Trn_Nomina 
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNominaEmp] @pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Trn_Nomina
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCdTipoCot VARCHAR(4),@pmSubTipo_Cot VARCHAR(3),@pmSal_Integral BIT,@pmTipoIdent CHAR(1),@pmNotasAportante VARCHAR(250),@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
,CdTipoCot=@pmCdTipoCot,SubTipo_Cot=@pmSubTipo_Cot,Sal_Integral=@pmSal_Integral,TipoIdent=@pmTipoIdent,NotasAportante=@pmNotasAportante
 WHERE IdPeriodo=@pmIdPeriodo AND KeyRegistro=@pmKeyRegistro
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,CdTipoCot,SubTipo_Cot,Sal_Integral,TipoIdent,NotasAportante
 FROM Trn_Nomina
 WHERE IdPeriodo=@pmIdPeriodo AND KeyRegistro LIKE ISNULL(@pmKeyRegistro,'%')
 ORDER BY KeyRegistro
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCdTipoCot VARCHAR(4),@pmSubTipo_Cot VARCHAR(3),@pmSal_Integral BIT,@pmTipoIdent CHAR(1),@pmNotasAportante VARCHAR(250),@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
,CdTipoCot,SubTipo_Cot,Sal_Integral,TipoIdent,NotasAportante)
 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,@pmCdTipoCot,@pmSubTipo_Cot,@pmSal_Integral,@pmTipoIdent,@pmNotasAportante)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomCausalesRet] @pmIdCausa VARCHAR(4)
 AS 
 DELETE FROM NomCausalesRet
 WHERE IdCausa=@pmIdCausa
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOilDevCompra] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmNumCompra INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdProveedor VARCHAR(16),@pmNumFactura VARCHAR(15),@pmCxPagar BIT,@pmSubTotal DECIMAL(16,6),@pmDescuento DECIMAL(16,6),@pmImpuesto DECIMAL(16,6),@pmRetencion DECIMAL(16,6),@pmReteIca DECIMAL(16,6),@pmReteIva DECIMAL(16,6),@pmIvaIngProd DECIMAL(16,6),@pmOtrosCargos DECIMAL(16,6),@pmOtrosDctos DECIMAL(16,6),@pmFletes DECIMAL(16,6),@pmVrNeto DECIMAL(16,6),@pmCantidad DECIMAL(16,6),@pmBaseImp DECIMAL(16,6),@pmBaseRet DECIMAL(16,6),@pmBaseIvaIgp DECIMAL(16,6)
,@pmTarifaIva DECIMAL(16,6),@pmTarifaRet DECIMAL(16,6),@pmTarifaIca DECIMAL(16,6),@pmTarifaRiv DECIMAL(16,6),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmModdDev VARCHAR(10),@pmObservacion VARCHAR(2000),@pmFechaCrea SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmOrigenAdd VARCHAR(10),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_OilDevCompra (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,NumCompra,IdCiaDoc,FecDoc,IdProveedor,NumFactura,CxPagar,SubTotal,Descuento,Impuesto,Retencion,ReteIca,ReteIva,IvaIngProd,OtrosCargos,OtrosDctos,Fletes,VrNeto,Cantidad,BaseImp,BaseRet,BaseIvaIgp,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,CdCCosto,CdSubCos,Modalidad,TipCom,Comprobante,IdCiaCom,ModdDev,Observacion,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmNumCompra,@pmIdCiaDoc,@pmFecDoc,@pmIdProveedor,@pmNumFactura,@pmCxPagar,@pmSubTotal,@pmDescuento,@pmImpuesto,@pmRetencion,@pmReteIca,@pmReteIva,@pmIvaIngProd,@pmOtrosCargos,@pmOtrosDctos,@pmFletes,@pmVrNeto,@pmCantidad,@pmBaseImp,@pmBaseRet,@pmBaseIvaIgp,@pmTarifaIva,@pmTarifaRet,@pmTarifaIca,@pmTarifaRiv,@pmCodTarIva,@pmCodTarRet,@pmCodTarIca,@pmCodTarRiv,@pmCdCCosto,@pmCdSubCos,@pmModalidad,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmModdDev,@pmObservacion,@pmFechaCrea,@pmIdCiaCrea,@pmOrigenAdd,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilDevCompra] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,NumCompra,IdCiaDoc,FecDoc,IdProveedor,NumFactura,CxPagar,SubTotal,Descuento,Impuesto,Retencion,ReteIca,ReteIva,IvaIngProd,OtrosCargos,OtrosDctos,Fletes,VrNeto,Cantidad,BaseImp,BaseRet,BaseIvaIgp,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,CdCCosto,CdSubCos,Modalidad,TipCom,Comprobante,IdCiaCom,ModdDev,Observacion,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario
 FROM Trn_OilDevCompra
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpOilDevCompra] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmNumCompra INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdProveedor VARCHAR(16),@pmNumFactura VARCHAR(15),@pmCxPagar BIT,@pmSubTotal DECIMAL(16,6),@pmDescuento DECIMAL(16,6),@pmImpuesto DECIMAL(16,6),@pmRetencion DECIMAL(16,6),@pmReteIca DECIMAL(16,6),@pmReteIva DECIMAL(16,6),@pmIvaIngProd DECIMAL(16,6),@pmOtrosCargos DECIMAL(16,6),@pmOtrosDctos DECIMAL(16,6),@pmFletes DECIMAL(16,6),@pmVrNeto DECIMAL(16,6),@pmCantidad DECIMAL(16,6),@pmBaseImp DECIMAL(16,6),@pmBaseRet DECIMAL(16,6),@pmBaseIvaIgp DECIMAL(16,6)
,@pmTarifaIva DECIMAL(16,6),@pmTarifaRet DECIMAL(16,6),@pmTarifaIca DECIMAL(16,6),@pmTarifaRiv DECIMAL(16,6),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmModdDev VARCHAR(10),@pmObservacion VARCHAR(2000)
 AS 
 UPDATE Trn_OilDevCompra SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,NumCompra=@pmNumCompra,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdProveedor=@pmIdProveedor,NumFactura=@pmNumFactura,CxPagar=@pmCxPagar,SubTotal=@pmSubTotal,Descuento=@pmDescuento,Impuesto=@pmImpuesto,Retencion=@pmRetencion,ReteIca=@pmReteIca,ReteIva=@pmReteIva,IvaIngProd=@pmIvaIngProd,OtrosCargos=@pmOtrosCargos,OtrosDctos=@pmOtrosDctos,Fletes=@pmFletes,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,BaseIvaIgp=@pmBaseIvaIgp,TarifaIva=@pmTarifaIva,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv,CodTarIva=@pmCodTarIva,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos
,Modalidad=@pmModalidad,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,ModdDev=@pmModdDev,Observacion=@pmObservacion
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelOilDevCompra] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_OilDevCompra
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmVrImpCons MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4)
,@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY,@pmTipoFE VARCHAR(3),@pmFechaFE SMALLDATETIME,@pmCodTarArf VARCHAR(4),@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmCodTarAri VARCHAR(4),@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY,@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,VrImpCons=@pmVrImpCons,FecUpdate=@pmFecUpdate
,PlacaVehic=@pmPlacaVehic,KmtVehic=@pmKmtVehic,VrReteCREE=@pmVrReteCREE,TarifaRtc=@pmTarifaRtc,CodTarRtc=@pmCodTarRtc,BaseIvaObsq=@pmBaseIvaObsq,VrIvaObsequio=@pmVrIvaObsequio,TipoFE=@pmTipoFE,FechaFE=@pmFechaFE,CodTarArf=@pmCodTarArf,TarifaArf=@pmTarifaArf,VrAutRetFte=@pmVrAutRetFte
,CodTarAri=@pmCodTarAri,TarifaAri=@pmTarifaAri,VrAutRetIca=@pmVrAutRetIca
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrImpCon)
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,VrImpCons
 FROM Trn_Factcon
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 F.Factura,F.IdCia,FE.Prefijo,FE.NumFace,F.Fecha,F.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,F.IdConcepto AS CdConcepto,Concepto,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,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc,BaseIvaObsq,VrIvaObsequio,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca,TipoFE,FechaFE,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado
,F.TimeSys,F.FecUpdate,F.IdCiaCrea,F.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,F.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 
 LEFT JOIN Trn_Face AS FE ON F.TipDoc=FE.TipDoc AND F.Factura=FE.Documento AND F.IdCia=FE.IdCia
 WHERE F.TipDoc=@pmTipDoc AND F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND F.Factura BETWEEN ISNULL(@pmFacturaIni,0) AND ISNULL(@pmFacturaFin,2147483647)
 AND F.IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.nClieCon LIKE ISNULL(@pmnClieCon,'%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND F.IdCajero LIKE ISNULL(@pmIdCajero,'%') AND F.Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND F.IdVend LIKE ISNULL(@pmIdVend,'%')  AND F.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 AND (F.Anulado=ISNULL(@pmAnulado,0) or F.Anulado=ISNULL(@pmAnulado,1))
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrImpCon)
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,D.VrImpCons
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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.VrImpCons,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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+VrImpCons,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmVrImpCons MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4)
,@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY,@pmTipoFE VARCHAR(3),@pmFechaFE SMALLDATETIME,@pmCodTarArf VARCHAR(4),@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmCodTarAri VARCHAR(4),@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY,@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,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc,BaseIvaObsq,VrIvaObsequio
,TipoFE,FechaFE,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca)
 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,@pmVrImpCons,@pmVrReteCREE,@pmTarifaRtc,@pmCodTarRtc,@pmBaseIvaObsq,@pmVrIvaObsequio,@pmTipoFE,@pmFechaFE
,@pmCodTarArf,@pmTarifaArf,@pmVrAutRetFte,@pmCodTarAri,@pmTarifaAri,@pmVrAutRetIca)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc,BaseIvaObsq,VrIvaObsequio,Observacion,IdEstado
,TipoFE,FechaFE,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Factcon
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,MontoAprob
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 
--**** NO cambiar posición de los primeros 4 campos
 SELECT TipFac,Factura,IdCia,Item,VrFactura-VrAbonado AS ValorSaldo,VrFactura,VrAbonado,TipDoc,Documento,IdCiaDoc,TipRef,DocRef,IdCiaRef,FecEmision,FecVence
,F.IdCuenta AS IdCuent,NomCuenta,TipCom,Comprobante,ItemCom,Referencia,Detalle,IdProveedor,RazonSocial,pVehiculo,VehPropio,EstadoApr,MontoAprob
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryComFacturaDve] @pmIdProveedor VARCHAR(16),@pmTipDoc VARCHAR(3)=Null,@pmDocumento INT=Null,@pmIdCiaDoc CHAR(2)=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 (IdProveedor=@pmIdProveedor OR (TipFac=@pmTipDoc AND TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCiaDoc=@pmIdCiaDoc))
 AND VrFactura>VrAbonado 
 ORDER BY FecVence,TipFac,IdCia,Documento
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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,MontoAprob
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,MontoAprob,FecProgPago,ObservAprob)
 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,MontoAprob,FecProgPago,ObservAprob
 FROM Trn_ComFactura WHERE IdProveedor=@pmIdProveedor
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmMontoAprob MONEY,@pmFecProgPago SMALLDATETIME,@pmObservAprob VARCHAR(250)
 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,MontoAprob,FecProgPago,ObservAprob)
 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,@pmMontoAprob,@pmFecProgPago,@pmObservAprob)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,MontoAprob,FecProgPago,ObservAprob
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,MontoAprob,FecProgPago,ObservAprob
 FROM Trn_ComFactura AS V INNER JOIN Terceros AS T ON V.IdProveedor=T.IdTercero
 WHERE V.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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmMontoAprob MONEY,@pmFecProgPago SMALLDATETIME,@pmObservAprob VARCHAR(250)
 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,MontoAprob=@pmMontoAprob,FecProgPago=@pmFecProgPago,ObservAprob=@pmObservAprob
 WHERE TipFac=@pmTipFac AND Factura=@pmFactura AND IdCia=@pmIdCia AND Item=@pmItem AND IdProveedor=@pmIdProveedor
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmtmImpConEnt MONEY,@pmtmImpConSal MONEY
 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,tmImpConEnt,tmImpConSal)
 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,@pmtmImpConEnt,@pmtmImpConSal)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelProdConsolida] @pmTipDoc VARCHAR(3),@pmNumCons INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_ProdConsolida
 WHERE TipDoc=@pmTipDoc AND NumCons=@pmNumCons AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsProdConsolida] @pmTipDoc VARCHAR(3),@pmNumCons INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdBodega VARCHAR(4),@pmCdBodDest VARCHAR(4),@pmIdBodIns VARCHAR(4),@pmCdBodInsDest VARCHAR(4),@pmAnulado BIT,@pmFecDev SMALLDATETIME
,@pmCritPedidos VARCHAR(500),@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_ProdConsolida (TipDoc,NumCons,IdCia,Fecha,IdBodega,CdBodDest,IdBodIns,CdBodInsDest,OrigenAdd,Anulado,FecDev,CritPedidos,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmNumCons,@pmIdCia,@pmFecha,@pmIdBodega,@pmCdBodDest,@pmIdBodIns,@pmCdBodInsDest,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmCritPedidos,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdConsolida] @pmTipDoc VARCHAR(3),@pmNumCons INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumCons,IdCia,Fecha,IdBodega,CdBodDest,IdBodIns,CdBodInsDest,OrigenAdd,Anulado,FecDev,CritPedidos,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_ProdConsolida
 WHERE TipDoc=@pmTipDoc AND NumCons=@pmNumCons AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpProdConsolida] @pmTipDoc VARCHAR(3),@pmNumCons INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdBodega VARCHAR(4),@pmCdBodDest VARCHAR(4),@pmIdBodIns VARCHAR(4),@pmCdBodInsDest VARCHAR(4),@pmAnulado BIT,@pmFecDev SMALLDATETIME
,@pmCritPedidos VARCHAR(500),@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_ProdConsolida SET Fecha=@pmFecha,IdBodega=@pmIdBodega,CdBodDest=@pmCdBodDest,IdBodIns=@pmIdBodIns,CdBodInsDest=@pmCdBodInsDest,Anulado=@pmAnulado,FecDev=@pmFecDev
,CritPedidos=@pmCritPedidos,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumCons=@pmNumCons AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_KarUbiID] @pmtmNumero VARCHAR(5)
 AS 
 SELECT  MAX(tmItem) AS MXITM
 FROM tm_KarUbi
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryAfGrupos] @pmIdGrupo VARCHAR(4)
 AS 
 SELECT IdGrupo,Grupo,IdLinea,VidaUtil,Inactivo
 FROM AfGrupos
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposBloq] @pmIdTipBloq VARCHAR(4),@pmTipoBloqueo VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposBloq SET TipoBloqueo=@pmTipoBloqueo,Inactivo=@pmInactivo
 WHERE IdTipBloq=@pmIdTipBloq
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposBloq] @pmIdTipBloq VARCHAR(4)
 AS 
 DELETE FROM TiposBloq
 WHERE IdTipBloq=@pmIdTipBloq
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposBloq] @pmIdTipBloq VARCHAR(4),@pmTipoBloqueo VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposBloq (IdTipBloq,TipoBloqueo,Inactivo)
 VALUES (@pmIdTipBloq,@pmTipoBloqueo,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpAduanas] @pmIdAduana VARCHAR(4),@pmNomAduana VARCHAR(150),@pmIdPais VARCHAR(8),@pmInactivo BIT
 AS 
 UPDATE Aduanas SET NomAduana=@pmNomAduana,IdPais=@pmIdPais,Inactivo=@pmInactivo
 WHERE IdAduana=@pmIdAduana
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAduanas] @pmIdAduana VARCHAR(4),@pmNomAduana VARCHAR(150),@pmIdPais VARCHAR(8),@pmInactivo BIT
 AS 
 INSERT INTO Aduanas (IdAduana,NomAduana,IdPais,Inactivo)
 VALUES (@pmIdAduana,@pmNomAduana,@pmIdPais,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraConcCausacDso] 
 AS 
 SELECT IdConcepto,Concepto,Concepto+' '+IdConcepto AS DsCon
 FROM TraConcCausac
 WHERE Inactivo=0 
 ORDER BY Concepto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraConcCausac] @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(250)
,@pmIdCuenta VARCHAR(16),@pmTipoConc VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO TraConcCausac (IdConcepto,Concepto,IdCuenta,TipoConc,Inactivo)
 VALUES (@pmIdConcepto,@pmConcepto,@pmIdCuenta,@pmTipoConc,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraConcCausac] @pmIdConcepto VARCHAR(4)
 AS 
 SELECT IdConcepto,Concepto,IdCuenta,TipoConc,Inactivo
 FROM TraConcCausac
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraConcCausac] @pmIdConcepto VARCHAR(4)
 AS 
 DELETE FROM TraConcCausac
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraConcCausac] @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(250),@pmIdCuenta VARCHAR(16)
,@pmTipoConc VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE TraConcCausac SET Concepto=@pmConcepto,IdCuenta=@pmIdCuenta,TipoConc=@pmTipoConc,Inactivo=@pmInactivo
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_TraDetConcLta] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmNumero,tmItem,tmIdConcepto,tmDescripcion,tmTipoConc,tmVrDebito,tmVrCredito,tmCdCuenta,NomCuenta,tmNitTercero,RazonSocial
,tmCdCCosto,CCosto,tmCdSubCos,SubCosto,tmpVehiculo,tmTipoAfiVehic,tmVrBase,tmTarifBase,tmReferencia
,tmTipDocRef,tmDocRef,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodAgncia,tmCantComb
 FROM tm_TraDetConc AS T LEFT JOIN CentroCosto AS CC ON T.tmCdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON T.tmCdSubCos=SC.IdSubCos
 LEFT JOIN Terceros  AS N ON T.tmNitTercero=N.IdTercero LEFT JOIN Puc AS P ON T.tmCdCuenta=P.IdCuenta
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetConc_Leg]  @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraDetConc (tmNumero,tmItem,tmIdConcepto,tmDescripcion,tmTipoConc,tmVrDebito,tmVrCredito,tmCdCuenta,tmNitTercero,tmCdCCosto,tmCdSubCos,tmpVehiculo
,tmTipoAfiVehic,tmVrBase,tmTarifBase,tmReferencia,tmTipDocRef,tmDocRef,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodAgncia,tmCantComb,tmDocSop,tmTipoImp,tmCodTarImp,tmItemBase,tmTipoDs,tmNumDs,tmIdCiaDs)
 SELECT @pmtmNumero,Item,IdConcepto,DescConcepto,RubroConc,VrDebito,VrCredito,IdCuenta,IdTercero,CdCCosto,CdSubCos,pVehiculo
,'TERCEROS',VrBase,TarifBase,RefConcep,TipSop,NumSoporte,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodAgncia,CantComb,DocSop,TipoImp,CodTarImp,ItemBase,TipSop,NumSoporte,IdCiaSop
 FROM Trn_TraLiqDetalle
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetConc_Sel]  @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraDetConc (tmNumero,tmItem,tmIdConcepto,tmDescripcion,tmTipoConc,tmVrDebito,tmVrCredito,tmCdCuenta,tmNitTercero,tmCdCCosto,tmCdSubCos,tmpVehiculo
,tmTipoAfiVehic,tmVrBase,tmTarifBase,tmReferencia,tmTipDocRef,tmDocRef,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodAgncia,tmCantComb,tmDocSop,tmTipoImp,tmCodTarImp,tmItemBase,tmTipoDs,tmNumDs,tmIdCiaDs)
 SELECT @pmtmNumero,Item,IdConcepto,Descripcion,TipoConc,VrDebito,VrCredito,CdCuenta,NitTercero,CdCCosto,CdSubCos,pVehiculo
,TipoAfiVehic,VrBase,TarifBase,Referencia,TipDocRef,DocRef,TipFac,Factura,IdCiaFac,ItemFac,FecVence,'0',0,0,'','',0,'0',0,'00'
 FROM Trn_TraCauDetalle
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetConc] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdConcepto VARCHAR(4),@pmtmDescripcion VARCHAR(250),@pmtmTipoConc VARCHAR(10),@pmtmVrDebito MONEY,@pmtmVrCredito MONEY,@pmtmCdCuenta VARCHAR(16)
,@pmtmNitTercero VARCHAR(16),@pmtmCdCCosto VARCHAR(16),@pmtmCdSubCos VARCHAR(16),@pmtmpVehiculo VARCHAR(10),@pmtmTipoAfiVehic VARCHAR(10),@pmtmVrBase MONEY,@pmtmTarifBase DECIMAL(14,4),@pmtmReferencia VARCHAR(50)
,@pmtmTipDocRef VARCHAR(3),@pmtmDocRef INT,@pmtmTipFac VARCHAR(3),@pmtmFactura VARCHAR(15),@pmtmIdCiaFac CHAR(2),@pmtmItemFac INT,@pmtmFecVence SMALLDATETIME,@pmtmCodAgncia VARCHAR(16),@pmtmCantComb DECIMAL(14,4)
,@pmtmDocSop BIT,@pmtmTipoImp VARCHAR(4),@pmtmCodTarImp VARCHAR(4),@pmtmItemBase INT,@pmtmTipoDs VARCHAR(3),@pmtmNumDs INT,@pmtmIdCiaDs CHAR(2)
 AS 
 INSERT INTO tm_TraDetConc (tmNumero,tmItem,tmIdConcepto,tmDescripcion,tmTipoConc,tmVrDebito,tmVrCredito,tmCdCuenta,tmNitTercero,tmCdCCosto,tmCdSubCos,tmpVehiculo,tmTipoAfiVehic,tmVrBase,tmTarifBase,tmReferencia,tmTipDocRef
,tmDocRef,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodAgncia,tmCantComb,tmDocSop,tmTipoImp,tmCodTarImp,tmItemBase,tmTipoDs,tmNumDs,tmIdCiaDs)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdConcepto,@pmtmDescripcion,@pmtmTipoConc,@pmtmVrDebito,@pmtmVrCredito,@pmtmCdCuenta,@pmtmNitTercero,@pmtmCdCCosto,@pmtmCdSubCos,@pmtmpVehiculo,@pmtmTipoAfiVehic
,@pmtmVrBase,@pmtmTarifBase,@pmtmReferencia,@pmtmTipDocRef,@pmtmDocRef,@pmtmTipFac,@pmtmFactura,@pmtmIdCiaFac,@pmtmItemFac,@pmtmFecVence,@pmtmCodAgncia,@pmtmCantComb,@pmtmDocSop,@pmtmTipoImp,@pmtmCodTarImp,@pmtmItemBase,@pmtmTipoDs,@pmtmNumDs,@pmtmIdCiaDs)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetConc_Dlg]  @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraDetConc (tmNumero,tmItem,tmIdConcepto,tmDescripcion,tmTipoConc,tmVrDebito,tmVrCredito,tmCdCuenta,tmNitTercero,tmCdCCosto,tmCdSubCos,tmpVehiculo
,tmTipoAfiVehic,tmVrBase,tmTarifBase,tmReferencia,tmTipDocRef,tmDocRef,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodAgncia,tmCantComb,tmDocSop,tmTipoImp,tmCodTarImp,tmItemBase,tmTipoDs,tmNumDs,tmIdCiaDs)
 SELECT @pmtmNumero,Item,IdConcepto,DescConcepto,RubroConc,VrCredito,VrDebito,IdCuenta,IdTercero,CdCCosto,CdSubCos,pVehiculo
,'TERCEROS',VrBase,TarifBase,RefConcep,TipDoc,Liquidacion,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodAgncia,CantComb,DocSop,TipoImp,CodTarImp,ItemBase,TipSop,NumSoporte,IdCiaSop
 FROM Trn_TraLiqDetalle
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetConc_Dev]  @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraDetConc (tmNumero,tmItem,tmIdConcepto,tmDescripcion,tmTipoConc,tmVrDebito,tmVrCredito,tmCdCuenta,tmNitTercero,tmCdCCosto,tmCdSubCos,tmpVehiculo
,tmTipoAfiVehic,tmVrBase,tmTarifBase,tmReferencia,tmTipDocRef,tmDocRef,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodAgncia,tmCantComb,tmDocSop,tmTipoImp,tmCodTarImp,tmItemBase,tmTipoDs,tmNumDs,tmIdCiaDs)
 SELECT @pmtmNumero,Item,IdConcepto,Descripcion,TipoConc,VrCredito,VrDebito,CdCuenta,NitTercero,CdCCosto,CdSubCos,pVehiculo
,TipoAfiVehic,VrBase,TarifBase,Referencia,TipDocRef,DocRef,TipFac,Factura,IdCiaFac,ItemFac,FecVence,'0',0,0,'','',0,'0',0,'00'
 FROM Trn_TraCauDetalle
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_TraDetConc] @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdConcepto,tmDescripcion,tmTipoConc,tmVrDebito,tmVrCredito,tmCdCuenta,tmNitTercero,tmCdCCosto,tmCdSubCos
,tmpVehiculo,tmTipoAfiVehic,tmVrBase,tmTarifBase,tmReferencia,tmTipDocRef,tmDocRef,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodAgncia,tmCantComb
,tmDocSop,tmTipoImp,tmCodTarImp,tmItemBase,tmTipoDs,tmNumDs,tmIdCiaDs
 FROM tm_TraDetConc
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelEntidades] @pmIdTabla VARCHAR(20)
 AS 
 DELETE FROM Sys_Entidades
 WHERE IdTabla=@pmIdTabla
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,@pmtmTarifaRet DECIMAL(14,4),@pmtmVrRetFte MONEY
 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,tmTarifaRet,tmVrRetFte)
 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,@pmtmTarifaRet,@pmtmVrRetFte)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[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,tmTarifaRet,tmVrRetFte
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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmTarifaRet,tmVrRetFte)
 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,TarifaRet,VrRetFte
 FROM Trn_FinCauMora 
 WHERE TipoProc=@pmTipoProc AND  NumProc=@pmNumProc AND  IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraMovLiquida] @pmtmEst CHAR(2),@pmtmTipo VARCHAR(3),@pmtmNumero INT,@pmtmIdCia CHAR(2),@pmtmItem INT,@pmtmFecha SMALLDATETIME,@pmtmFecVence SMALLDATETIME,@pmtmIdPoseedor VARCHAR(16),@pmtmIdVehiculo VARCHAR(10),@pmtmIdConcepto VARCHAR(4),@pmtmDescripcion VARCHAR(250),@pmtmVrDebito MONEY,@pmtmVrCredito MONEY,@pmtmVrAbonado MONEY,@pmtmTipoVehic VARCHAR(10)
,@pmtmVrBase MONEY,@pmtmTarifa DECIMAL(14,4),@pmtmReferencia VARCHAR(50),@pmtmTipDocRef VARCHAR(3),@pmtmDocRef INT,@pmtmEstado INT,@pmtmTipoLiq VARCHAR(3),@pmtmLiquida INT,@pmtmCiaLiq CHAR(2),@pmtmSaldoAnt MONEY,@pmtmSaldoCia MONEY,@pmtmSaldoAntVeh MONEY,@pmtmSaldoVehCia MONEY
 AS 
 INSERT INTO tm_TraMovLiquida (tmEst,tmTipo,tmNumero,tmIdCia,tmItem,tmFecha,tmFecVence,tmIdPoseedor,tmIdVehiculo,tmIdConcepto,tmDescripcion,tmVrDebito,tmVrCredito,tmVrAbonado,tmTipoVehic,tmVrBase,tmTarifa,tmReferencia,tmTipDocRef,tmDocRef,tmEstado,tmTipoLiq,tmLiquida,tmCiaLiq,tmSaldoAnt,tmSaldoCia,tmSaldoAntVeh,tmSaldoVehCia)
 VALUES (@pmtmEst,@pmtmTipo,@pmtmNumero,@pmtmIdCia,@pmtmItem,@pmtmFecha,@pmtmFecVence,@pmtmIdPoseedor,@pmtmIdVehiculo,@pmtmIdConcepto,@pmtmDescripcion,@pmtmVrDebito,@pmtmVrCredito,@pmtmVrAbonado,@pmtmTipoVehic,@pmtmVrBase,@pmtmTarifa,@pmtmReferencia,@pmtmTipDocRef,@pmtmDocRef,@pmtmEstado,@pmtmTipoLiq,@pmtmLiquida,@pmtmCiaLiq,@pmtmSaldoAnt,@pmtmSaldoCia,@pmtmSaldoAntVeh,@pmtmSaldoVehCia)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[paInstm_TraMovLiquidaDcc] @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdPoseedor VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
 AS 
 INSERT INTO tm_TraMovLiquida (tmEst,tmTipo,tmNumero,tmIdCia,tmItem,tmFecha,tmFecVence,tmIdPoseedor,tmIdVehiculo,tmIdConcepto,tmDescripcion
,tmVrDebito,tmVrCredito,tmVrAbonado,tmTipoVehic,tmVrBase,tmTarifa,tmReferencia,tmTipDocRef,tmDocRef,tmEstado,tmTipoLiq,tmLiquida,tmCiaLiq,tmSaldoAnt,tmSaldoCia,tmSaldoAntVeh,tmSaldoVehCia)
SELECT @pmtmEst,D.TipDev,D.Devolucion,D.IdCia,DC.Item,D.Fecha,DC.FecVence,DC.NitTercero,DC.pVehiculo,DC.IdConcepto,DC.Descripcion
,DC.VrCredito,DC.VrDebito,DC.VrAbonado,DC.TipoAfiVehic,DC.VrBase,DC.TarifBase,DC.Referencia,TipDocRef,DocRef,EstadoReg,DC.TipoLiq,DC.NumLiquida,DC.CiaLiquida,0,0,0,0
FROM Trn_TraDevCau AS D INNER JOIN Trn_TraCauDetalle AS DC ON D.TipDoc=DC.TipDoc AND D.Causacion=DC.Causacion AND D.IdCiaDoc=DC.IdCia
WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND DC.TipoConc='CTACOBRAR' AND DC.VrDebito>0
AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND DC.NitTercero LIKE ISNULL(@pmIdPoseedor,'%') AND DC.pVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE FUNCTION [dbo].[FuncAcuInvTallasSan] (@pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16)
,@pmIdBodega VARCHAR(4),@pmIdUbic VARCHAR(10),@pmNumTalla VARCHAR(30))
RETURNS DECIMAL(14,4)
 AS  
BEGIN
 DECLARE @SaldoAnt DECIMAL(14,4)
 SET  @SaldoAnt=(SELECT SUM(SaldoAnt+Entradas-Salidas) FROM AcuInvTallas WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdProducto=@pmIdProducto AND IdBodega=@pmIdBodega
 AND IdUbic=@pmIdUbic AND NumTalla=@pmNumTalla)
 RETURN @SaldoAnt
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmNumTarj VARCHAR(30),@pmPwdTarj VARCHAR(30),@pmFecVenceTc SMALLDATETIME,@pmIncRetFte BIT,@pmFechaImp SMALLDATETIME
,@pmCodCCosto VARCHAR(16),@pmCodSubCosto VARCHAR(16),@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,NumTarj,PwdTarj,FecVenceTc,IncRetFte,FechaImp,CodCCosto,CodSubCosto)
 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,@pmNumTarj,@pmPwdTarj,@pmFecVenceTc,@pmIncRetFte,@pmFechaImp,@pmCodCCosto,@pmCodSubCosto)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,NumTarj,PwdTarj,FecVenceTc,IncRetFte
,FechaCrea,FechaAct,FechaImp,CodCCosto,CodSubCosto,IdUsuario
 FROM TercCliePrestamo
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTercCliePrestamo_Sel] @pmIdClie VARCHAR(16),@pmNewIdClie VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdClie FROM  TercCliePrestamo WHERE  IdClie=@pmIdClie)
  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,NumTarj,PwdTarj,FecVenceTc,IncRetFte,FechaImp,CodCCosto,CodSubCosto)
 SELECT @pmNewIdClie,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,NumTarj,PwdTarj,FecVenceTc,IncRetFte,FechaImp,CodCCosto,CodSubCosto
 FROM TercCliePrestamo WHERE IdClie=@pmIdClie
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmNumTarj VARCHAR(30),@pmPwdTarj VARCHAR(30),@pmFecVenceTc SMALLDATETIME,@pmIncRetFte BIT,@pmFechaImp SMALLDATETIME,@pmCodCCosto VARCHAR(16),@pmCodSubCosto VARCHAR(16),@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,NumTarj=@pmNumTarj,PwdTarj=@pmPwdTarj,FecVenceTc=@pmFecVenceTc
,IncRetFte=@pmIncRetFte,FechaImp=@pmFechaImp,CodCCosto=@pmCodCCosto,CodSubCosto=@pmCodSubCosto
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,NumTarj,PwdTarj,FecVenceTc
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryZonasDso] 
 AS 
 SELECT IdZona,Zona
 FROM Zonas
 WHERE Inactivo=0
 ORDER BY Zona
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryZonas] @pmIdZona VARCHAR(4)
 AS 
 SELECT IdZona,Zona,CdSubCos,Inactivo
 FROM Zonas
 WHERE IdZona=@pmIdZona
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CdProducto,FE_Ticket,IdUsuario
 FROM Trn_Pagos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND IdForma=@pmIdForma  AND NumForma=@pmNumForma AND IdBanco=@pmIdBanco
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,CdProducto,FE_Ticket,Cant_Gals,PrecioBase,PrecioUnit,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_Caja_Rmt] @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,8,TipDoc,Documento,IdCia,Fecha,'0',NitCliente,CdAgencia,VrPagado,VrCambio,0,0,0,0,0,0,0,VrPagado AS ValNeto,CASE EsCaja WHEN 1 THEN VrPagado ELSE 0 END
,0,0,0,0,0,0,0,EsCaja,IdForma,NumForma,IdBanco,Beneficiario,CdCta,FecForma,'0',0,'0',IdUsuario,CdLocal,Referncia1,Referncia2
,'REMESAS',Referncia2,TipRef,DocRef,IdCiaRef,'0',0,'00',0,0,Null,Detalle,Fecha
 FROM Trn_Pagos
 WHERE TipDoc IN ('RMT','ODP','DOP') AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmCdProducto VARCHAR(16),@pmFE_Ticket BIT,@pmCant_Gals DECIMAL(14,4),@pmPrecioBase MONEY,@pmPrecioUnit MONEY,@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
,CdProducto,FE_Ticket,Cant_Gals,PrecioBase,PrecioUnit)
 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,@pmCdProducto,@pmFE_Ticket,@pmCant_Gals,@pmPrecioBase,@pmPrecioUnit)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTarifasFletes] @pmNumero INT
 AS 
 SELECT Numero,IdOrigen,IdDestino,CdRuta,TipoTarifa,TarifClie,TarifPago,TarifTabla,IdMneda,UndMed
,NitCliente,CdNat,CdTipoVeh,CdRango,UndTarifClie,UndTarifPago
,Inactivo,CdMercancia,TipoAfiVehic,TarifAfiliado,TarifTercero,TarifPropio,TipoOper,CdGrupo,MargenMin,CdAgencia,FechaCrea,IdUsuario
 FROM TarifasFletes
 WHERE  Numero=@pmNumero
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTarifasFletes] @pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmNumero 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),@pmUndTarifClie VARCHAR(10),@pmUndTarifPago VARCHAR(10),@pmInactivo BIT,@pmCdRuta VARCHAR(4),@pmCdMercancia VARCHAR(16)
,@pmTipoAfiVehic VARCHAR(10),@pmTarifAfiliado DECIMAL(16,6),@pmTarifTercero DECIMAL(16,6),@pmTarifPropio DECIMAL(16,6),@pmTipoOper VARCHAR(10)
,@pmCdGrupo VARCHAR(4),@pmMargenMin DECIMAL(14,4),@pmCdAgencia VARCHAR(16),@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TarifasFletes (Numero,IdOrigen,IdDestino,CdRuta,TipoTarifa,TarifClie,TarifPago,TarifTabla,IdMneda,UndMed,NitCliente,CdNat,CdTipoVeh,CdRango
,UndTarifClie,UndTarifPago,TipoAfiVehic,TarifAfiliado,TarifTercero,TarifPropio,Inactivo,CdMercancia,FechaCrea,IdUsuario,TipoOper,CdGrupo,MargenMin,CdAgencia)
 VALUES (@pmNumero,@pmIdOrigen,@pmIdDestino,@pmCdRuta,@pmTipoTarifa,@pmTarifClie,@pmTarifPago,@pmTarifTabla,@pmIdMneda,@pmUndMed,@pmNitCliente
,@pmCdNat,@pmCdTipoVeh,@pmCdRango,@pmUndTarifClie,@pmUndTarifPago,@pmTipoAfiVehic,@pmTarifAfiliado,@pmTarifTercero,@pmTarifPropio,@pmInactivo,@pmCdMercancia,@pmFechaCrea,@pmIdUsuario,@pmTipoOper,@pmCdGrupo,@pmMargenMin,@pmCdAgencia)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTarifasFletesLta] @pmTipoTarifa VARCHAR(10)=Null,@pmIdOrigen VARCHAR(8)=Null,@pmIdDestino VARCHAR(8)=Null
,@pmNitCliente VARCHAR(16)=Null,@pmInactivo BIT=Null,@pmCdRuta VARCHAR(4)=Null
 AS 
 SELECT Numero,IdOrigen,O.Localidad AS CiuOrigen,IdDestino,D.Localidad AS CiuDestino,CdRuta,TarifClie,TarifPago,TarifTabla,IdMneda,T.UndMed AS Und_Med,Unidad
,NitCliente,RazonSocial,CdNat,Natlzaprod,CdTipoVeh,TipoVehiculo,CdRango,DescripRango,T.Inactivo AS Inactvo,UndTarifClie,UndTarifPago,CdMercancia,TipoAfiVehic
,TarifAfiliado,TarifTercero,TarifPropio,TipoOper,CdGrupo,GrupoProp,MargenMin,CdAgencia,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 LEFT JOIN GruposPro AS G ON T.CdGrupo=G.IdGrupo
 WHERE TipoTarifa LIKE ISNULL(@pmTipoTarifa,'%') AND  IdOrigen LIKE ISNULL(@pmIdOrigen,'%')
 AND IdDestino LIKE ISNULL(@pmIdDestino,'%') AND NitCliente LIKE ISNULL(@pmNitCliente,'%')
 AND CdRuta LIKE ISNULL(@pmCdRuta,'%')
 AND  (T.Inactivo=ISNULL(@pmInactivo,0) or T.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY O.Localidad,D.Localidad,Numero
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTarifasFletes] @pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmNumero 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)
,@pmUndTarifClie VARCHAR(10),@pmUndTarifPago VARCHAR(10),@pmInactivo BIT,@pmCdRuta VARCHAR(4),@pmCdMercancia VARCHAR(16),@pmTipoAfiVehic VARCHAR(10)
,@pmTarifAfiliado DECIMAL(16,6),@pmTarifTercero DECIMAL(16,6),@pmTarifPropio DECIMAL(16,6),@pmTipoOper VARCHAR(10),@pmCdGrupo VARCHAR(4),@pmMargenMin DECIMAL(14,4),@pmCdAgencia VARCHAR(16)
 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,UndTarifClie=@pmUndTarifClie,UndTarifPago=@pmUndTarifPago,CdRuta=@pmCdRuta
,IdOrigen=@pmIdOrigen,IdDestino=@pmIdDestino,CdMercancia=@pmCdMercancia,TipoAfiVehic=@pmTipoAfiVehic
,TarifAfiliado=@pmTarifAfiliado,TarifTercero=@pmTarifTercero,TarifPropio=@pmTarifPropio,TipoOper=@pmTipoOper,CdGrupo=@pmCdGrupo,MargenMin=@pmMargenMin,CdAgencia=@pmCdAgencia
 WHERE  Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasAge_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuFisCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdTercero,S.CodAgncia,S.TipoAcum,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuFisCuentasAge AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt AND S.TipoAcum=@pmTipoAcum
 AND NOT EXISTS (SELECT * FROM AcuFisCuentasAge AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes AND AC.TipoAcum=@pmTipoAcum
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdTercero=S.IdTercero AND AC.CodAgncia=S.CodAgncia)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuFisCuentasAgeSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuFisCuentasAge SET AcuFisCuentasAge.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuFisCuentasAge INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuFisCuentasAge WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt AND TipoAcum=@pmTipoAcum) AS SA ON AcuFisCuentasAge.IdCia=SA.IdCia AND AcuFisCuentasAge.IdCuenta=SA.IdCuenta
 AND AcuFisCuentasAge.IdTercero=SA.IdTercero AND AcuFisCuentasAge.CodAgncia=SA.CodAgncia AND AcuFisCuentasAge.TipoAcum=SA.TipoAcum
 WHERE AcuFisCuentasAge.nAnno=@pmnAnno AND AcuFisCuentasAge.nMes=@pmnMes AND AcuFisCuentasAge.TipoAcum=@pmTipoAcum
 AND (@pmIdCia IS NULL OR AcuFisCuentasAge.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuFisCuentasAge.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasAge_Uni] @pmIdTercero VARCHAR(16),@pmIdTerceroDos VARCHAR(16),@pmNewTercero VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdTercero FROM AcuFisCuentasAge WHERE IdTercero=@pmIdTercero OR IdTercero=@pmIdTerceroDos)
  INSERT INTO AcuFisCuentasAge (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 AcuFisCuentasAge WHERE IdTercero=@pmIdTercero OR IdTercero=@pmIdTerceroDos
 GROUP BY nAnno,nMes,IdCia,IdCuenta,CodAgncia,TipoAcum
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasAge_Sel]  @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 --nit-veh
  INSERT INTO AcuFisCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdTercero,D.IdVehiculo,'NV',0,SUM(D.VrDebito),SUM(D.VrCredito)
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='FISCAL' AND P.Movimiento=1 AND P.Tercero=1 AND P.Vehiculo=1
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdTercero,D.IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasAge_Veh]  @pmIdVehiculo VARCHAR(10),@pmIdVehiculoDos VARCHAR(10),@pmNewVehiculo VARCHAR(10)
 AS 
 IF EXISTS (SELECT * FROM AcuFisCuentasAge WHERE (CodAgncia=@pmIdVehiculo OR CodAgncia=@pmIdVehiculoDos) AND TipoAcum='NV')
  INSERT INTO AcuFisCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,@pmNewVehiculo,TipoAcum,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  FROM AcuFisCuentasAge WHERE (CodAgncia=@pmIdVehiculo OR CodAgncia=@pmIdVehiculoDos) AND TipoAcum='NV'
  GROUP BY nAnno,nMes,IdCia,IdCuenta,IdTercero,TipoAcum
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasAge] @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 AcuFisCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdTercero,@pmCodAgncia,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasAge_Unf]  @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuFisCuentasAge WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
  INSERT INTO AcuFisCuentasAge (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 AcuFisCuentasAge WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
 GROUP BY nAnno,nMes,IdCia,IdTercero,CodAgncia,TipoAcum
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,TP.Tarifa,TP.Simbolo,TP.Descripcion AS DescIva
,CdTarAut,TA.Tarifa AS TarifaAut,TA.Simbolo AS SimbAut,TA.Descripcion AS DescAut
,CdTarRet,TR.Tarifa AS TarifaRet,TR.Simbolo AS SimbRet,TR.Descripcion AS DescRet
,CdTarIca,TI.Tarifa AS TarifaIca,TI.Simbolo AS SimbIca,TI.Descripcion AS DescIca
,CdTarifImv,TM.Tarifa AS TarifaImv,TM.Simbolo AS SimbImv,TM.Descripcion AS DescImv
,CdTarifIco,TC.Tarifa AS TarifaIco,TC.Simbolo AS SimbIco,TC.Descripcion AS DescIco
,S.CdTarIvp,TPR.Tarifa AS TarifaIvp,TPR.Simbolo AS SimbIvp
,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
 LEFT JOIN Tablapor AS TR ON S.CdTarRet=TR.IdTarifa
 LEFT JOIN Tablapor AS TI ON S.CdTarIca=TI.IdTarifa
 LEFT JOIN Tablapor AS TA ON S.CdTarAut=TA.IdTarifa
 LEFT JOIN Tablapor AS TM ON S.CdTarifImv=TM.IdTarifa
 LEFT JOIN Tablapor AS TC ON S.CdTarifIco=TC.IdTarifa
 LEFT JOIN Tablapor AS TPR ON S.CdTarIvp=TPR.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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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-ISNULL(CantObseq,0)) ELSE 0 END ) AS VtaTotal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrPrecio*(Entradas-ISNULL(CantObseq,0)) 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','VCC')  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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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','VCC')  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','VCC')  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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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','VCC')  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','VCC')  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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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','VCC')  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','VCC')  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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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','VCC')  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','VCC')  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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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','VCC')  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','VCC')  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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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-ISNULL(CantObseq,0)) ELSE 0 END ) AS VtaTotal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrPrecio*(Entradas-ISNULL(CantObseq,0)) 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','VCC')  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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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-ISNULL(CantObseq,0)) ELSE 0 END ) AS VtaTotal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrPrecio*(Entradas-ISNULL(CantObseq,0)) 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','VCC')  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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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-ISNULL(CantObseq,0)) ELSE 0 END ) AS VtaTotal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrPrecio*(Entradas-ISNULL(CantObseq,0)) 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','VCC')  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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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-ISNULL(CantObseq,0)) ELSE 0 END ) AS VtaTotal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrPrecio*(Entradas-ISNULL(CantObseq,0)) 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','VCC')  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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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-ISNULL(CantObseq,0)) ELSE 0 END ) AS VtaTotal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrPrecio*(Entradas-ISNULL(CantObseq,0)) 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','VCC')  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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,S.CdTarifIco
--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,S.CdTarifIco
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,S.CdTarifIco
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryLineas] @pmIdLinea VARCHAR(8)
 AS 
 SELECT IdLinea,Linea,TipoInvent,Inactivo
 FROM Lineas
 WHERE IdLinea=@pmIdLinea
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelPresupuesto] @pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Presupuesto
 WHERE Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsGruposMcia] @pmIdGrupo VARCHAR(10),@pmGrupoMcia VARCHAR(250),@pmInactivo BIT
 AS 
 INSERT INTO GruposMcia (IdGrupo,GrupoMcia,Inactivo)
 VALUES (@pmIdGrupo,@pmGrupoMcia,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpGruposMcia] @pmIdGrupo VARCHAR(10),@pmGrupoMcia VARCHAR(250),@pmInactivo BIT
 AS 
 UPDATE GruposMcia SET GrupoMcia=@pmGrupoMcia,Inactivo=@pmInactivo
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCod_CCosto VARCHAR(16),@pmCod_SubCosto VARCHAR(16),@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,Cod_CCosto=@pmCod_CCosto,Cod_SubCosto=@pmCod_SubCosto,FecUpdate=@pmFecUpdate
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryEdsRangosFor] @pmTipoRango VARCHAR(3),@pmNumSerie VARCHAR(5)
,@pmNumForma INT,@pmIdCia CHAR(2)
 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,Cod_CCosto,Cod_SubCosto,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_EdsRangos
 WHERE TipoRango=@pmTipoRango AND NumSerie=@pmNumSerie
 AND @pmNumForma BETWEEN NumInicial AND NumFinal AND Anulado=0 AND IdCiaCrea=@pmIdCia
 ORDER BY IdRango
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpEdsRangosCon] @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumActual INT
 AS 
 UPDATE Trn_EdsRangos SET  NumActual=@pmNumActual
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCod_CCosto VARCHAR(16),@pmCod_SubCosto VARCHAR(16),@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,Cod_CCosto,Cod_SubCosto)
 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,@pmCod_CCosto,@pmCod_SubCosto)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,Cod_CCosto,Cod_SubCosto,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_EdsRangos
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCdGrupoAct VARCHAR(4),@pmVrSalvamnto MONEY,@pmUnidades INT,@pmCantidad INT,@pmDep_Partes BIT,@pmCantMeses INT,@pmVrDepMejoras MONEY
 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,CdGrupoAct,VrSalvamnto,Unidades,Cantidad,Dep_Partes,CantMeses,VrDepMejoras)
 VALUES (@pmNumero,@pmItem,@pmIdActivo,@pmVrCostoIni,@pmVrMejoras,@pmVrDisminucion,@pmVrAcuAjustes,@pmVrDepreciaHis,@pmVrDepreciaMes,@pmVrAjustesInf,@pmVrAjustesDep,@pmVrDepreciaAcum,@pmVrAjuDepAcum
,@pmTarifaAju,@pmIdCia,@pmCdCCosto,@pmCdSubCos,@pmCdInstala,@pmCdDep,@pmNitRespons,@pmFecIniDep,@pmMesesVida,@pmMetodoDeprec,@pmCdGrupoAct,@pmVrSalvamnto,@pmUnidades,@pmCantidad,@pmDep_Partes,@pmCantMeses,@pmVrDepMejoras)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelAfDetalle] @pmNumero INT,@pmIdActivo VARCHAR(30)
 AS 
 DELETE FROM Trn_AfDetalle
 WHERE Numero=@pmNumero AND IdActivo LIKE ISNULL(@pmIdActivo,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTarifasServ] @pmTipoServ INT,@pmIdCliente VARCHAR(16),@pmIdMercancia VARCHAR(16),@pmIdEstacion VARCHAR(4),@pmUndMed VARCHAR(10)
,@pmTipoAfiVehic VARCHAR(10),@pmCdTipoVeh VARCHAR(4),@pmTarifaClie DECIMAL(16,4),@pmTarifaPago DECIMAL(16,4),@pmInactivo BIT
,@pmMargenMin DECIMAL(14,4),@pmCdRuta VARCHAR(4),@pmCdCiuOrigen VARCHAR(8),@pmCdCiuDestino VARCHAR(8),@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TarifasServ (TipoServ,IdCliente,IdMercancia,IdEstacion,UndMed,TipoAfiVehic,CdTipoVeh,TarifaClie,TarifaPago,Inactivo,FechaCrea,IdUsuario,MargenMin,CdRuta,CdCiuOrigen,CdCiuDestino)
 VALUES (@pmTipoServ,@pmIdCliente,@pmIdMercancia,@pmIdEstacion,@pmUndMed,@pmTipoAfiVehic,@pmCdTipoVeh,@pmTarifaClie,@pmTarifaPago,@pmInactivo,@pmFechaCrea,@pmIdUsuario,@pmMargenMin,@pmCdRuta,@pmCdCiuOrigen,@pmCdCiuDestino)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTarifasServ] @pmNumero INT,@pmTipoServ INT,@pmIdCliente VARCHAR(16),@pmIdMercancia VARCHAR(16),@pmIdEstacion VARCHAR(4),@pmUndMed VARCHAR(10),@pmTipoAfiVehic VARCHAR(10)
,@pmCdTipoVeh VARCHAR(4),@pmTarifaClie DECIMAL(16,4),@pmTarifaPago DECIMAL(16,4),@pmInactivo BIT,@pmMargenMin DECIMAL(14,4),@pmCdRuta VARCHAR(4),@pmCdCiuOrigen VARCHAR(8),@pmCdCiuDestino VARCHAR(8)
 AS 
 UPDATE TarifasServ SET TipoServ=@pmTipoServ,IdCliente=@pmIdCliente,IdMercancia=@pmIdMercancia,IdEstacion=@pmIdEstacion,UndMed=@pmUndMed,TipoAfiVehic=@pmTipoAfiVehic
,CdTipoVeh=@pmCdTipoVeh,TarifaClie=@pmTarifaClie,TarifaPago=@pmTarifaPago,Inactivo=@pmInactivo,MargenMin=@pmMargenMin,CdRuta=@pmCdRuta,CdCiuOrigen=@pmCdCiuOrigen,CdCiuDestino=@pmCdCiuDestino
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTarifasServLta] 
 AS 
SELECT Numero,TipoServ,IdCliente,RazonSocial,T.IdMercancia AS CodMcia,DescripMcia,T.IdEstacion AS IdCampo,E.EstServicio AS DescCampo
,T.UndMed,Unidad,TipoAfiVehic,CdTipoVeh,TarifaClie,TarifaPago,MargenMin,T.Inactivo
,CdRuta,CdCiuOrigen,O.Localidad AS CiuOrigen,CdCiuDestino,D.Localidad AS CiuDestino,T.FechaCrea,T.IdUsuario AS CdUsuario,Usuario
FROM TarifasServ AS T INNER JOIN Terceros AS C ON T.IdCliente=C.IdTercero INNER JOIN EdsServicio AS E ON T.IdEstacion=E.IdEstacion 
INNER JOIN adm_Usuarios AS U ON T.IdUsuario=U.IdUsuario INNER JOIN Sys_Um AS UM ON T.UndMed=UM.UndMed
LEFT JOIN Mercancias AS M ON T.IdMercancia=M.IdMercancia LEFT JOIN Localidades AS O ON T.CdCiuOrigen=O.IdLocal
LEFT JOIN Localidades AS D ON T.CdCiuDestino=D.IdLocal
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTarifasServ] @pmNumero INT
 AS 
 SELECT Numero,TipoServ,IdCliente,IdMercancia,IdEstacion,UndMed,TipoAfiVehic
,CdTipoVeh,TarifaClie,TarifaPago,Inactivo,MargenMin,CdRuta,CdCiuOrigen,CdCiuDestino,FechaCrea,IdUsuario
 FROM TarifasServ
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmVrSeguroCuo MONEY
,@pmVrCustodia MONEY,@pmVrProrroga MONEY,@pmTasaCustodia DECIMAL(16,8)
 AS 
 INSERT INTO Trn_FinAmtza (TipDoc,IdPrestamo,IdCia,Item,FecCausac,FecVence,NumDias,VrCuota,VrCapital,VrInteres,VrSaldo,TasaEfeMes,TasaEfeDia,Causacion,CdCiaCausac,VrCuoCausac,VrIntCausac,VrSaldoCausac,VrSeguroCuo,VrCustodia,VrProrroga,TasaCustodia)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmItem,@pmFecCausac,@pmFecVence,@pmNumDias,@pmVrCuota,@pmVrCapital,@pmVrInteres,@pmVrSaldo,@pmTasaEfeMes,@pmTasaEfeDia,@pmCausacion
,@pmCdCiaCausac,@pmVrCuoCausac,@pmVrIntCausac,@pmVrSaldoCausac,@pmVrSeguroCuo,@pmVrCustodia,@pmVrProrroga,@pmTasaCustodia)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[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,tmVrSeguro,tmVrProrroga,tmVrCustodia,tmTasaCustod,tmTarifIva,tmVrIva,tmTarifRet,tmVrRet)
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,VrSeguroCuo,VrProrroga,VrCustodia,TasaCustodia,0,0,0,0
 FROM Trn_FinAmtza
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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)
,@pmtmUnidades INT,@pmtmSalvamento MONEY,@pmtmDepPartes BIT,@pmtmNumParte INT,@pmtmCantidad INT
 AS 
 INSERT INTO tm_AfIngreso (tmNumero,tmItem,tmIdActivo,tmFecha,tmVrMejoras,tmVrDisminucion,tmVrAjuInfAcum,tmVrDepAcuHis,tmVrDepAcuAju,tmVrAjuInfDep,tmNumMeses
,tmVidaUtil,tmBaseDep,tmMetodo,tmFecUltDep,tmConcepto,tmUnidades,tmSalvamento,tmDepPartes,tmNumParte,tmCantidad)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdActivo,@pmtmFecha,@pmtmVrMejoras,@pmtmVrDisminucion,@pmtmVrAjuInfAcum,@pmtmVrDepAcuHis,@pmtmVrDepAcuAju
,@pmtmVrAjuInfDep,@pmtmNumMeses,@pmtmVidaUtil,@pmtmBaseDep,@pmtmMetodo,@pmtmFecUltDep,@pmtmConcepto,@pmtmUnidades,@pmtmSalvamento,@pmtmDepPartes,@pmtmNumParte,@pmtmCantidad)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_AfIngresoDso] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdActivo,tmNumParte,NomActivo,tmFecha,tmBaseDep,tmVrMejoras,tmVrDisminucion,tmBaseDep+tmVrMejoras-tmVrDisminucion AS TotalBaseDep
,tmVrAjuInfAcum,tmVrDepAcuHis,tmVrDepAcuAju,tmVrAjuInfDep,tmNumMeses,tmFecUltDep,tmSalvamento,tmMetodo,tmVidaUtil,tmUnidades,tmCantidad
,tmDepPartes,tmConcepto,IdEstado,IdCCosto,IdSubCos,IdInstala,IdDep,IdRespons,IdGrupo
 FROM tm_AfIngreso AS T INNER JOIN Activos AS A ON T.tmIdActivo=A.IdActivo
 WHERE tmNumero=@pmtmNumero
 ORDER BY  tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmUnidades
,tmSalvamento,tmDepPartes,tmNumParte,tmCantidad
 FROM tm_AfIngreso
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpNomPrestAbn] @pmNPrestamo INT,@pmTotAbonado MONEY
 AS 
 UPDATE Trn_NomPrest SET TotAbonado=@pmTotAbonado
 WHERE NPrestamo=@pmNPrestamo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCdTipoOper VARCHAR(4)
 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,CdTipoOper)
 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,@pmCdTipoOper)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CdTipoOper
 FROM TercCndtores
 WHERE IdConductor=@pmIdConductor
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCdTipoOper VARCHAR(4)
 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,CdTipoOper=@pmCdTipoOper
 WHERE IdConductor=@pmIdConductor
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CdTipoOper)
  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,CdTipoOper
  FROM TercCndtores 
  WHERE IdConductor=@pmIdConductor
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuNiifCuentasSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuNiifCuentas SET AcuNiifCuentas.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuNiifCuentas INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuNiifCuentas WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt) AS SA ON AcuNiifCuentas.IdCia=SA.IdCia AND AcuNiifCuentas.IdCuenta=SA.IdCuenta
 WHERE AcuNiifCuentas.nAnno=@pmnAnno AND AcuNiifCuentas.nMes=@pmnMes
 AND (@pmIdCia IS NULL OR AcuNiifCuentas.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuNiifCuentas.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentas_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuNiifCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuNiifCuentas AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt 
 AND NOT EXISTS (SELECT * FROM AcuNiifCuentas AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes  
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryAcuNiifCuentasMay] @pmnAnno INT,@pmnMes INT,@pmNiv INT,@pmIdCia CHAR(2)=Null
 AS 
 SELECT SUBSTRING(A.IdCuenta,1,@pmNiv) AS CdCuenta,NomCuenta,Naturaleza
,SUM(A.SaldoAnterior) AS tSaldoAnterior,SUM(A.TotalDebitos) AS tTotalDebitos,SUM(A.TotalCreditos) AS tTotalCreditos
,SUM(A.SaldoAnterior+A.TotalDebitos-A.TotalCreditos) AS NuevoSaldo
 FROM AcuNiifCuentas AS A INNER JOIN PucNiif AS P ON SUBSTRING(A.IdCuenta,1,@pmNiv)=P.IdCuenta
 WHERE A.nAnno=@pmnAnno AND A.nMes=@pmnMes AND (@pmIdCia IS NULL OR A.IdCia=@pmIdCia)
 GROUP BY SUBSTRING(A.IdCuenta,1,@pmNiv),NomCuenta,Naturaleza
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentas_Uni] @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuNiifCuentas WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
  INSERT INTO AcuNiifCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT nAnno,nMes,IdCia,@pmNewCuenta,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  FROM AcuNiifCuentas
  WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
  GROUP BY nAnno,nMes,IdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentas_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuNiifCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,0,SUM(D.VrDebito),SUM(D.VrCredito)
 FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
 INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='NIIF' AND P.Movimiento<>0
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentas] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCuenta VARCHAR(16),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuNiifCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsVehPartes] @pmIdVehiculo VARCHAR(10),@pmNumParte VARCHAR(20),@pmDescripParte VARCHAR(250),@pmIdSistema VARCHAR(4),@pmTipoControl INT,@pmIntervalo_km INT,@pmIntervalo_dias INT,@pmkmActual INT,@pmkmCambio INT
,@pmFechaCambio SMALLDATETIME,@pmCdProdRep VARCHAR(16),@pmTipoUnidad CHAR(1),@pmTipoParte VARCHAR(10),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmFechaRetiro SMALLDATETIME,@pmIntervalo_Horas INT
,@pmkmManual INT,@pmkmSat INT,@pmkmCambioMan INT,@pmkmCambioSat INT,@pmHorasTrab INT,@pmHsUltCambio INT,@pmMarcaParte VARCHAR(20),@pmModeloParte VARCHAR(20),@pmNumSerie VARCHAR(30),@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO VehPartes (IdVehiculo,NumParte,DescripParte,IdSistema,TipoControl,Intervalo_km,Intervalo_dias,Intervalo_Horas,kmActual,kmManual,kmSat,kmCambio,kmCambioMan,kmCambioSat,FechaCambio,CdProdRep,TipoUnidad,TipoParte
,IdEstado,Inactivo,FechaRetiro,FechaCrea,IdUsuario,HorasTrab,HsUltCambio,MarcaParte,ModeloParte,NumSerie)
 VALUES (@pmIdVehiculo,@pmNumParte,@pmDescripParte,@pmIdSistema,@pmTipoControl,@pmIntervalo_km,@pmIntervalo_dias,@pmIntervalo_Horas,@pmkmActual,@pmkmManual,@pmkmSat,@pmkmCambio,@pmkmCambioMan,@pmkmCambioSat,@pmFechaCambio
,@pmCdProdRep,@pmTipoUnidad,@pmTipoParte,@pmIdEstado,@pmInactivo,@pmFechaRetiro,@pmFechaCrea,@pmIdUsuario,@pmHorasTrab,@pmHsUltCambio,@pmMarcaParte,@pmModeloParte,@pmNumSerie)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryVehPartes] @pmIdVehiculo VARCHAR(10),@pmNumParte VARCHAR(20)
 AS 
 SELECT IdVehiculo,NumParte,DescripParte,IdSistema,TipoControl,Intervalo_km,Intervalo_dias,Intervalo_Horas,kmActual
,kmManual,kmSat,kmCambio,kmCambioMan,kmCambioSat,FechaCambio,CdProdRep
,TipoUnidad,TipoParte,IdEstado,Inactivo,FechaRetiro,HorasTrab,HsUltCambio,MarcaParte,ModeloParte,NumSerie,FechaCrea,IdUsuario
 FROM VehPartes
 WHERE IdVehiculo=@pmIdVehiculo AND NumParte=@pmNumParte
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpVehPartes] @pmIdVehiculo VARCHAR(10),@pmNumParte VARCHAR(20),@pmDescripParte VARCHAR(250),@pmIdSistema VARCHAR(4),@pmTipoControl INT,@pmIntervalo_km INT,@pmIntervalo_dias INT,@pmkmActual INT,@pmkmCambio INT
,@pmFechaCambio SMALLDATETIME,@pmCdProdRep VARCHAR(16),@pmTipoUnidad CHAR(1),@pmTipoParte VARCHAR(10),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmFechaRetiro SMALLDATETIME,@pmIntervalo_Horas INT
,@pmkmManual INT,@pmkmSat INT,@pmkmCambioMan INT,@pmkmCambioSat INT,@pmHorasTrab INT,@pmHsUltCambio INT,@pmMarcaParte VARCHAR(20),@pmModeloParte VARCHAR(20),@pmNumSerie VARCHAR(30)
 AS 
 UPDATE VehPartes SET DescripParte=@pmDescripParte,IdSistema=@pmIdSistema,TipoControl=@pmTipoControl,Intervalo_km=@pmIntervalo_km,Intervalo_dias=@pmIntervalo_dias,kmActual=@pmkmActual,kmCambio=@pmkmCambio,FechaCambio=@pmFechaCambio,CdProdRep=@pmCdProdRep
,TipoUnidad=@pmTipoUnidad,TipoParte=@pmTipoParte,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,FechaRetiro=@pmFechaRetiro,Intervalo_Horas=@pmIntervalo_Horas
,kmManual=@pmkmManual,kmSat=@pmkmSat,kmCambioMan=@pmkmCambioMan,kmCambioSat=@pmkmCambioSat,HorasTrab=@pmHorasTrab,HsUltCambio=@pmHsUltCambio,MarcaParte=@pmMarcaParte,ModeloParte=@pmModeloParte,NumSerie=@pmNumSerie
 WHERE IdVehiculo=@pmIdVehiculo AND NumParte=@pmNumParte
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCantDev DECIMAL(14,4),@pmCantFact DECIMAL(14,4)
,@pmCdOperario VARCHAR(16),@pmComision DECIMAL(14,4) 
 AS 
 INSERT INTO Trn_EdsForItems (TipoRango,IdRango,NumSerie,NumForma,Item,IdProducto,IdBodega,Cantidad,VrPrecio,TarifaIva,VrIva,TarifaDct,VrDcto,Servicios,Descripcion,ListaPrec,CantDev,CantFact,CdOperario,Comision)
 VALUES (@pmTipoRango,@pmIdRango,@pmNumSerie,@pmNumForma,@pmItem,@pmIdProducto,@pmIdBodega,@pmCantidad,@pmVrPrecio,@pmTarifaIva,@pmVrIva,@pmTarifaDct,@pmVrDcto,@pmServicios,@pmDescripcion,@pmListaPrec,@pmCantDev,@pmCantFact,@pmCdOperario,@pmComision)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CantDev,CantFact,CdOperario,Comision
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmCantDev,tmCantFact,tmCdOperario,tmComsOper)
 SELECT @pmtmNumero,Item,IdProducto,IdBodega,Cantidad,VrPrecio,TarifaIva,VrIva,TarifaDct,VrDcto,VrPrecio,Descripcion,Servicios,ListaPrec,CantDev,CantFact,CdOperario,Comision
 FROM Trn_EdsForItems
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelAfGruposCue] @pmIdGrupo VARCHAR(4),@pmIdClase VARCHAR(4)
 AS 
 DELETE FROM AfGruposCue
 WHERE IdGrupo=@pmIdGrupo AND IdClase LIKE ISNULL(@pmIdClase,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAfGruposCue] @pmIdGrupo VARCHAR(4),@pmIdClase VARCHAR(4),@pmIdCuenta VARCHAR(16)
 AS 
 INSERT INTO AfGruposCue (IdGrupo,IdClase,IdCuenta)
 VALUES (@pmIdGrupo,@pmIdClase,@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAfGruposCue] @pmIdGrupo VARCHAR(4),@pmIdClase VARCHAR(4),@pmIdCuenta VARCHAR(16)
 AS 
 UPDATE AfGruposCue SET IdCuenta=@pmIdCuenta
 WHERE IdGrupo=@pmIdGrupo AND IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_CajaTot] @pmtmEst CHAR(2)
 AS 
 SELECT tmConcepto,tmValorTotal,tmValorOtros,tmTipo,tmItem
 FROM tm_CajaTot
 WHERE tmEst=@pmtmEst 
 ORDER BY tmTipo,tmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudContNov_Cr] @pmTipDoc VARCHAR(3),@pmContratoIni INT,@pmContratoFin INT,@pmIdCia CHAR(2)
 AS 
SELECT D.TipDoc,D.NumContrato,D.IdCia AS CdCia,D.Item,D.Fecha,D.TipoNov,D.Descripcion,D.TarifaServ,D.TipInv,D.NumInvent,D.CdCiaInv,D.ItemInv
,D.CdArticulo,D.Cantidad,D.CdEstado,Estado,D.CdBodega,B.Bodega AS BodegaOrig,D.CdBodDest,BD.Bodega AS BodegaDest,D.NumDocRef,D.IdUsuario,Usuario
,D.Notas
FROM Trn_MudContNov AS D INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
LEFT JOIN AlmBodegas AS B ON D.CdBodega=B.IdBodega LEFT JOIN AlmBodegas AS BD ON D.CdBodDest=BD.IdBodega
LEFT JOIN MudEstados AS E ON D.CdEstado=E.IdEstado
WHERE D.TipDoc=@pmTipDoc AND D.NumContrato BETWEEN @pmContratoIni AND @pmContratoFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelAlmBodegas] @pmIdBodega VARCHAR(4)
 AS 
 DELETE FROM AlmBodegas
 WHERE IdBodega=@pmIdBodega
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrIvaMvc,CxPagar,DocEquiv
,BaseImp,BaseRet,VrReteCREE,TarifaRtc,CodTarRtc,VrBomberil,TarifaBom,CdCCosto,CdSubCos,Modalidad,ModdDev,BaseIvaObsq,VrIvaObsequio,VrImpCarbono,VrImpuCons,VrSobtasaCons,BaseIvaIgp,VrIvaIngProd,ImpAviTab,TarifaAvta
,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevEnt
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmVrIvaMvc MONEY
,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmVrBomberil MONEY,@pmTarifaBom DECIMAL(14,4),@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY,@pmVrImpCarbono MONEY,@pmVrImpuCons MONEY,@pmVrSobtasaCons MONEY,@pmBaseIvaIgp MONEY,@pmVrIvaIngProd MONEY,@pmImpAviTab MONEY,@pmTarifaAvta DECIMAL(14,4)
,@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,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpCarbono,VrImpuCons,VrSobtasaCons,BaseIvaIgp,VrIvaIngProd,ImpAviTab,TarifaAvta)
 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,@pmVrIvaMvc,@pmVrReteCREE,@pmTarifaRtc,@pmCodTarRtc,@pmVrBomberil,@pmTarifaBom,@pmBaseIvaObsq,@pmVrIvaObsequio,@pmVrImpCarbono,@pmVrImpuCons,@pmVrSobtasaCons,@pmBaseIvaIgp,@pmVrIvaIngProd,@pmImpAviTab,@pmTarifaAvta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpCarbono,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,FletesCosto,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpuCons,VrSobtasaCons
,BaseIvaIgp,VrIvaIngProd,ImpAviTab,TarifaAvta,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmVrIvaMvc MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmVrBomberil MONEY,@pmTarifaBom DECIMAL(14,4),@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY
,@pmVrImpCarbono MONEY,@pmVrImpuCons MONEY,@pmVrSobtasaCons MONEY,@pmBaseIvaIgp MONEY,@pmVrIvaIngProd MONEY,@pmImpAviTab MONEY,@pmTarifaAvta DECIMAL(14,4),@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,VrIvaMvc=@pmVrIvaMvc
,VrReteCREE=@pmVrReteCREE,TarifaRtc=@pmTarifaRtc,CodTarRtc=@pmCodTarRtc,VrBomberil=@pmVrBomberil,TarifaBom=@pmTarifaBom,BaseIvaObsq=@pmBaseIvaObsq,VrIvaObsequio=@pmVrIvaObsequio,VrImpCarbono=@pmVrImpCarbono
,VrImpuCons=@pmVrImpuCons,VrSobtasaCons=@pmVrSobtasaCons,BaseIvaIgp=@pmBaseIvaIgp,VrIvaIngProd=@pmVrIvaIngProd,ImpAviTab=@pmImpAviTab,TarifaAvta=@pmTarifaAvta,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelActivos_Anexo] @pmIdActivo VARCHAR(30)
 AS 
 DELETE FROM Activos_Anexo  WHERE IdActivo=@pmIdActivo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmCodNiifAct VARCHAR(16),@pmCodNiifDep VARCHAR(16),@pmCodNiifGasto VARCHAR(16)
 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,CodNiifAct=@pmCodNiifAct,CodNiifDep=@pmCodNiifDep,CodNiifGasto=@pmCodNiifGasto
 WHERE IdActivo=@pmIdActivo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmCodNiifAct VARCHAR(16),@pmCodNiifDep VARCHAR(16),@pmCodNiifGasto VARCHAR(16)
 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,CodNiifAct,CodNiifDep,CodNiifGasto)
 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,@pmCodNiifAct,@pmCodNiifDep,@pmCodNiifGasto)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CodNiifAct,CodNiifDep,CodNiifGasto
 FROM Activos_Anexo
 WHERE IdActivo=@pmIdActivo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsEstadoCiv] @pmIdEstado VARCHAR(4),@pmEstCivil VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO EstadoCiv (IdEstado,EstCivil,Inactivo)
 VALUES (@pmIdEstado,@pmEstCivil,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelEstadoCiv] @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM EstadoCiv
 WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpEstadoCiv] @pmIdEstado VARCHAR(4),@pmEstCivil VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE EstadoCiv SET EstCivil=@pmEstCivil,Inactivo=@pmInactivo
 WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_vce]
 AS 
 DELETE FROM tm_vce
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_vceLta]
 AS 
 SELECT tmTipDoc,tmDocumento,tmIdCia,tmItem,tmValorFactura,tmValorAbono,tmIdVehiculo,tmFecUltPago
 FROM tm_vce
 ORDER BY tmTipDoc,tmIdCia,tmDocumento,tmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraPorteIntGas_Cr] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT D.TipDoc,D.NumOrden,D.IdCia AS CdCia,D.Item,D.IdConcepto AS CdConcepto,Concepto,D.MontoRem,D.IdMonRem AS CdMonRem,MR.Mneda AS MonedaRem,MR.Simbolo AS MonSimbRem
,D.MontoDest,D.IdMonDest AS CdMonDest,MD.Mneda AS MonedaDest,MD.Simbolo AS MonSimbDest
 FROM Trn_TraPorteIntGas AS D INNER JOIN ConcDiversos AS C ON D.IdConcepto=C.IdConcepto
 LEFT JOIN adm_monedas AS MR ON D.IdMonRem=MR.IdMneda LEFT JOIN adm_monedas AS MD ON D.IdMonDest=MD.IdMneda
 WHERE D.TipDoc=@pmTipDoc AND D.NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelJurCausales] @pmIdCausal VARCHAR(4)
 AS 
 DELETE FROM JurCausales
 WHERE IdCausal=@pmIdCausal
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsJurCausales] @pmIdCausal VARCHAR(4),@pmCausal VARCHAR(50),@pmIdTipoPro VARCHAR(4),@pmInactivo BIT
 AS 
 INSERT INTO JurCausales (IdCausal,Causal,IdTipoPro,Inactivo)
 VALUES (@pmIdCausal,@pmCausal,@pmIdTipoPro,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryJurCausales] @pmIdCausal VARCHAR(4)
 AS 
 SELECT IdCausal,Causal,IdTipoPro,Inactivo
 FROM JurCausales
 WHERE IdCausal=@pmIdCausal
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpJurCausales] @pmIdCausal VARCHAR(4),@pmCausal VARCHAR(50),@pmIdTipoPro VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE JurCausales SET Causal=@pmCausal,IdTipoPro=@pmIdTipoPro,Inactivo=@pmInactivo
 WHERE IdCausal=@pmIdCausal
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraRemCum_Muc] @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_TraRemCum (tmNumero,tmItem,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed,tmVolumen,tmUndVol,tmCases,tmCajas,tmPalets,tmTarifClie,tmTarifPago,tmUndTarif,tmUndTarifPago,tmCantCargue
,tmPesoCargue,tmVolCargue,tmCasesCargue,tmCajasCargue,tmPaletsCargue,tmEstadoCump,tmRemision,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmDetalle,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen,tmNitDestntario,tmDestinatario,tmDirDestino
,tmIdDestino,tmTarifTabla,tmTipoCum,tmMotivoSusp,tmHoraLlegaCargue,tmHoraEntraCargue,tmHoraSaleCargue,tmHoraLlegaDescargue,tmHoraEntraDescargue,tmHoraSaleDescargue,tmCdCCosto,tmCdSubCos,tmNitCliente)
SELECT @pmtmNumero,D.Item,TipRem,Remesa,IdCiaRem,ItemRem,IdMercancia,DescripMcias,Cantidad,PesoNeto,UndMed,Volumen,UndVol,Cases,Cajas,Palets,R.TarifClie,CASE WHEN D.TarifPago>0 THEN D.TarifPago ELSE R.TarifPago END,UndTarifa,UndTarifPago,Cantidad
,PesoNeto,Volumen,Cases,Cajas,Palets,1,Remision,DocCliente,Referencia1,Referencia2,Referencia3,DetalleCump,NitRemite,Remitente,DirOrigen,IdOrigen,NitDestntario,Destinatario,DirDestino,IdDestino,D.TarifTabla
,'','',FecLlegCargue,FecInicioCargue,FecFinCargue,FecLlegdesc,FecIniciodesc,FecFindesc,'','',''
 FROM  Trn_TraManifRem AS D INNER JOIN Trn_TraRemMcias AS R ON D.TipRem=R.TipDoc AND D.Remesa=R.NumOrden AND D.IdCiaRem=R.IdCia AND D.ItemRem=R.Item
 WHERE D.TipDoc=@pmTipDoc AND D.Manifiesto=@pmManifiesto AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetalle_Muc] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraDetalle (tmNumero,tmItem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed,tmdmsAlto,tmdmsAncho,tmdmsLargo,tmVolumen,tmUndVol,tmIdUnd,tmIdEmp,tmIdNat
,tmIdMnjo,tmIdTmcia,tmCdRango,tmCases,tmCajas,tmPalets,tmRemision,tmNumOrden,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen,tmNitDestntario,tmDestinatario,tmDirDestino,tmIdDestino
,tmCdAgencia,tmTipoVehic,tmTarifClie,tmTarifPago,tmTarifTabla,tmVrDeclarado,tmVrSeguro,tmTarifSeguro,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmContenedor1,tmContenedor2,tmUndTarifa
,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmNumMintrans,tmPesoCont,tmUndTarPago,tmCodBodega,tmTipoTraslado,tmTieCargue,tmTieDesc,tmFecIniCargue,tmFecFinCargue,tmFecInidesc,tmFecFindesc,tmCdTipoVeh,tmTipoServicio
,tmSedeRem,tmSedeDest,tmFecLlegaCargue,tmFecLlegaDesc,tmMinCargue,tmMinDescargue)
SELECT @pmtmNumero,RM.Item,IdMercancia,DescripMcias,Cantidad,PesoNeto,UndMed,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol,IdUnd,IdEmp,IdNat
,IdMnjo,IdTmcia,CdRango,Cases,Cajas,Palets,Remision,Factura,NitRemite,Remitente,DirOrigen,IdOrigen,NitDestntario,Destinatario,DirDestino,IdDestino
,CdAgencia,'TERCEROS',TarifClie,RM.TarifPago,RM.TarifTabla,VrDeclarado,VrSeguro,TarifSeguro,DocCliente,Referencia1,Referencia2,Referencia3,Contenedor1,Contenedor2,UndTarifa
,TipRem,Remesa,IdCiaRem,ItemRem,RemMintrans,PesoCont,UndTarifPago,CodBodDtno,TipTraslado,HorasCargue,HorasDesc,FecInicioCargue,FecFinCargue,FecIniciodesc,FecFindesc,'','',SedeRem,SedeDest,Null,Null,0,0
 FROM Trn_TraManifRem AS RM INNER JOIN  Trn_TraRemMcias  AS D ON  RM.TipRem=D.TipDoc AND RM.Remesa=D.NumOrden AND RM.IdCiaRem=D.IdCia   AND RM.ItemRem=D.Item
 WHERE RM.TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND RM.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraManifRem] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipRem VARCHAR(3)
,@pmRemesa INT,@pmIdCiaRem CHAR(2),@pmItemRem INT,@pmTarifTabla MONEY,@pmTarifPago MONEY,@pmRemMintrans DECIMAL(14,2),@pmEdoRemMin VARCHAR(5)
,@pmCodBodDtno VARCHAR(4),@pmTipTraslado INT,@pmHorasCargue DECIMAL(14,4),@pmHorasDesc DECIMAL(14,4),@pmFecInicioCargue SMALLDATETIME,@pmFecFinCargue SMALLDATETIME
,@pmFecIniciodesc SMALLDATETIME,@pmFecFindesc SMALLDATETIME,@pmTiempoCargue DECIMAL(14,4),@pmTiempoDesc DECIMAL(14,4),@pmFecLlegCargue SMALLDATETIME
,@pmFecSalidaCargue SMALLDATETIME,@pmFecLlegdesc SMALLDATETIME,@pmFecSalidadesc SMALLDATETIME
 AS 
 INSERT INTO Trn_TraManifRem (TipDoc,Manifiesto,IdCia,Item,TipRem,Remesa,IdCiaRem,ItemRem,TarifTabla,TarifPago,RemMintrans,EdoRemMin,CodBodDtno,TipTraslado
,HorasCargue,HorasDesc,FecInicioCargue,FecFinCargue,FecIniciodesc,FecFindesc,TiempoCargue,TiempoDesc,FecLlegCargue,FecSalidaCargue,FecLlegdesc,FecSalidadesc)
 VALUES (@pmTipDoc,@pmManifiesto,@pmIdCia,@pmItem,@pmTipRem,@pmRemesa,@pmIdCiaRem,@pmItemRem,@pmTarifTabla,@pmTarifPago,@pmRemMintrans,@pmEdoRemMin,@pmCodBodDtno,@pmTipTraslado
,@pmHorasCargue,@pmHorasDesc,@pmFecInicioCargue,@pmFecFinCargue,@pmFecIniciodesc,@pmFecFindesc,@pmTiempoCargue,@pmTiempoDesc,@pmFecLlegCargue,@pmFecSalidaCargue
,@pmFecLlegdesc,@pmFecSalidadesc)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraManifRem] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Manifiesto,IdCia,Item,TipRem,Remesa,IdCiaRem,ItemRem,TarifTabla,TarifPago,RemMintrans,EdoRemMin,CodBodDtno,TipTraslado
 ,HorasCargue,HorasDesc,FecInicioCargue,FecFinCargue,FecIniciodesc,FecFindesc,TiempoCargue,TiempoDesc,FecLlegCargue,FecSalidaCargue,FecLlegdesc,FecSalidadesc
 FROM Trn_TraManifRem
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTraManifRem] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraManifRem
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpCarbono,VrIvaIngProd,VrNeto,Cantidad,CantPuntos,BaseImp,BaseRet,BaseIvaIgp,IdCCosto,IdSubCos
,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,IdLocEnv,Pedido,IdCiaPed,Remision,IdCiaRem,Cotizacion,IdCiaCot,FecPedido,PlacaVehic,KmtVehic,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc,BaseIvaObsq,VrIvaObsequio
,TarifaArf,VrAutRetFte,TarifaAri,VrAutRetIca,DevMasivo,NumRecibo,CdCiaRec,Modalidad,ModdDev,ZonaFrontera,CdMotDev,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrIvaIngProd,VrNeto,Cantidad,CantPuntos,BaseImp,BaseRet,BaseIvaIgp,IdCCosto,IdSubCos,IdVend,TarifaCom
,CodTarCom,IdLocEnv,Pedido,IdCiaPed,Remision,IdCiaRem,Cotizacion,IdCiaCot,FecPedido,PlacaVehic,KmtVehic,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom
,Observacion,IdEstado,ZonaFrontera,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc,BaseIvaObsq,VrIvaObsequio,VrImpCarbono,DevMasivo,NumRecibo,CdCiaRec,CdMotDev,TarifaArf,VrAutRetFte,TarifaAri,VrAutRetIca
,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevFcr
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpCon)
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,VrImpCons
 FROM Trn_DevFcr
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdCajero,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 DevMasivo<=0 AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 GROUP BY  IdCliente,IdAgencia,IdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@pmVrImpCons MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY,@pmVrImpCarbono MONEY
,@pmDevMasivo INT,@pmNumRecibo INT,@pmCdCiaRec CHAR(2),@pmCdMotDev VARCHAR(4),@pmBaseIvaIgp MONEY,@pmVrIvaIngProd MONEY,@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY,@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,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc,BaseIvaObsq,VrIvaObsequio,VrImpCarbono,DevMasivo,NumRecibo,CdCiaRec,CdMotDev,BaseIvaIgp,VrIvaIngProd,TarifaArf,VrAutRetFte,TarifaAri,VrAutRetIca)
 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,@pmVrImpCons,@pmVrReteCREE,@pmTarifaRtc,@pmCodTarRtc,@pmBaseIvaObsq,@pmVrIvaObsequio,@pmVrImpCarbono,@pmDevMasivo,@pmNumRecibo,@pmCdCiaRec,@pmCdMotDev,@pmBaseIvaIgp,@pmVrIvaIngProd,@pmTarifaArf,@pmVrAutRetFte,@pmTarifaAri,@pmVrAutRetIca)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@pmVrImpCons MONEY,@pmVrReteCREE MONEY
,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY,@pmVrImpCarbono MONEY,@pmDevMasivo INT,@pmNumRecibo INT,@pmCdCiaRec CHAR(2),@pmCdMotDev VARCHAR(4),@pmBaseIvaIgp MONEY,@pmVrIvaIngProd MONEY,@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY,@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,VrImpCons=@pmVrImpCons,FecUpdate=@pmFecUpdate
,VrReteCREE=@pmVrReteCREE,TarifaRtc=@pmTarifaRtc,CodTarRtc=@pmCodTarRtc,BaseIvaObsq=@pmBaseIvaObsq,VrIvaObsequio=@pmVrIvaObsequio,VrImpCarbono=@pmVrImpCarbono,DevMasivo=@pmDevMasivo,NumRecibo=@pmNumRecibo,CdCiaRec=@pmCdCiaRec,CdMotDev=@pmCdMotDev,BaseIvaIgp=@pmBaseIvaIgp,VrIvaIngProd=@pmVrIvaIngProd
,TarifaArf=@pmTarifaArf,VrAutRetFte=@pmVrAutRetFte,TarifaAri=@pmTarifaAri,VrAutRetIca=@pmVrAutRetIca
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMudContNov] @pmTipDoc VARCHAR(3),@pmNumContrato INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmTipoNov VARCHAR(10),@pmDescripcion VARCHAR(500),@pmTarifaServ DECIMAL(16,4),@pmTipInv VARCHAR(3),@pmNumInvent INT,@pmCdCiaInv CHAR(2),@pmItemInv INT
,@pmCdArticulo VARCHAR(16),@pmCantidad DECIMAL(14,4),@pmCdEstado VARCHAR(4),@pmCdBodega VARCHAR(4),@pmCdBodDest VARCHAR(4),@pmNumDocRef VARCHAR(20),@pmIdUsuario VARCHAR(11),@pmNotas VARCHAR(2500)
 AS 
 INSERT INTO Trn_MudContNov (TipDoc,NumContrato,IdCia,Item,Fecha,TipoNov,Descripcion,TarifaServ,TipInv,NumInvent,CdCiaInv,ItemInv,CdArticulo,Cantidad,CdEstado,CdBodega,CdBodDest,NumDocRef,IdUsuario,Notas)
 VALUES (@pmTipDoc,@pmNumContrato,@pmIdCia,@pmItem,@pmFecha,@pmTipoNov,@pmDescripcion,@pmTarifaServ,@pmTipInv,@pmNumInvent,@pmCdCiaInv,@pmItemInv,@pmCdArticulo,@pmCantidad,@pmCdEstado,@pmCdBodega,@pmCdBodDest,@pmNumDocRef,@pmIdUsuario,@pmNotas)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,tmBaseImpu,tmTarifaIva,tmVrIva,tmTarifaRet,tmVrRetFte,tmTarifaIca,tmVrRetIca,tmCodTarifIva,tmCodTarifRet,tmCodTarifIca
 FROM tm_Cargos
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY  tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,tmBaseImpu,tmTarifaIva,tmVrIva,tmTarifaRet,tmVrRetFte,tmTarifaIca,tmVrRetIca,tmCodTarifIva,tmCodTarifRet,tmCodTarifIca
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,@pmtmBaseImpu MONEY,@pmtmTarifaIva DECIMAL(14,4),@pmtmVrIva MONEY,@pmtmTarifaRet DECIMAL(14,4),@pmtmVrRetFte MONEY,@pmtmTarifaIca DECIMAL(14,4),@pmtmVrRetIca MONEY,@pmtmCodTarifIva VARCHAR(4),@pmtmCodTarifRet VARCHAR(4),@pmtmCodTarifIca VARCHAR(4)
 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,tmBaseImpu,tmTarifaIva,tmVrIva,tmTarifaRet,tmVrRetFte,tmTarifaIca,tmVrRetIca,tmCodTarifIva,tmCodTarifRet,tmCodTarifIca)
 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,@pmtmBaseImpu,@pmtmTarifaIva,@pmtmVrIva,@pmtmTarifaRet,@pmtmVrRetFte,@pmtmTarifaIca,@pmtmVrRetIca
,@pmtmCodTarifIva,@pmtmCodTarifRet,@pmtmCodTarifIca)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,nRemesa,CdCiaRem,ItemRem,CdProducto,Modalidad,PrestCaja
,CdForma,NumCheque,CdBanco,MulPlazos,IdPlazo,VrReteCREE,TarifaRtc,CodTarRtc,IdConcFE,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmnRemesa INT,@pmCdCiaRem CHAR(2),@pmItemRem INT,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmIdConcFE INT,@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,nRemesa,CdCiaRem,ItemRem,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,VrReteCREE,TarifaRtc,CodTarRtc,IdConcFE)
 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,@pmnRemesa,@pmCdCiaRem,@pmItemRem,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmVrReteCREE,@pmTarifaRtc,@pmCodTarRtc,@pmIdConcFE)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmnRemesa INT,@pmCdCiaRem CHAR(2),@pmItemRem INT,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmIdConcFE INT,@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,nRemesa=@pmnRemesa,CdCiaRem=@pmCdCiaRem,ItemRem=@pmItemRem,VrReteCREE=@pmVrReteCREE,TarifaRtc=@pmTarifaRtc,CodTarRtc=@pmCodTarRtc,IdConcFE=@pmIdConcFE,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,nRemesa,CdCiaRem,ItemRem,VrReteCREE,TarifaRtc,CodTarRtc,IdConcFE,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado
,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Notas
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_CxpAplicaID] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_CxpAplica
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_CxpAplicaTot] @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmVrAbono) AS STOTABO
 FROM tm_CxpAplica
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMarcas] @pmIdMarca VARCHAR(4)
 AS 
 SELECT IdMarca,Marca,TipoMarca,CodigoMT,Inactivo
 FROM Marcas
 WHERE IdMarca=@pmIdMarca
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpGruposObj] @pmIdGrupo VARCHAR(6),@pmGrupoObjeto VARCHAR(50),@pmnModulo VARCHAR(10)
 AS 
 UPDATE Sys_GruposObj SET GrupoObjeto=@pmGrupoObjeto,nModulo=@pmnModulo
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsGruposObj] @pmIdGrupo VARCHAR(6),@pmGrupoObjeto VARCHAR(50),@pmnModulo VARCHAR(10)
 AS 
 INSERT INTO Sys_GruposObj (IdGrupo,GrupoObjeto,nModulo)
 VALUES (@pmIdGrupo,@pmGrupoObjeto,@pmnModulo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelGruposObj] @pmIdGrupo VARCHAR(6)
 AS 
 DELETE FROM Sys_GruposObj
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CodRuta,CupoElectcom
,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,AgencOperando,TipoFactFletes,CdCCBonif,CdSubCCBonif,CodRuta,CodDiaEnt,CdGruClie,CupoElectcom
 FROM Agencias
 WHERE IdClie=@pmIdClie AND CodAgencia LIKE ISNULL(@pmCodAgencia,'%')
 ORDER BY IdAgencia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMayAgenciasLta] @pmNContratoIni INT,@pmNContratoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Item,CA.IdAgencia AS CdAgencia,Agencia,CodAgencia,A.IdClie AS NitCliente,RazonSocial,A.IdLocal AS CdCiuAgenc,L.Localidad AS CiudadAgenc
,L.IdDep AS CdDptoAgenc,D.Departamento AS DptoAgenc,TelAgncia,FaxAgncia,CodSicom,A.FecIngreso AS Fec_Ingreso
,A.TipoZona AS AgeTipoZona,CdTipAgenc,TipoAgencia,CdSubAgenc,SubTipoAgenc,CdGrupAgenc,GrupoAgenc,CdSubGrupAgenc,SubGrupoAgenc
,Comentarios,A.Inactivo AS AgeInactivo,CA.NContrato AS NumContrato,IdCia
 FROM Trn_MayAgencias AS CA INNER JOIN Agencias AS A ON CA.IdAgencia=A.IdAgencia
 INNER JOIN Terceros AS T ON A.IdClie=T.IdTercero
 INNER JOIN Localidades AS L ON A.IdLocal=L.IdLocal INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN TiposAgenc AS TAG ON A.CdTipAgenc=TAG.IdTipAgenc LEFT JOIN TiposComAgenc AS STA ON A.CdSubAgenc=STA.IdSubAgenc
 LEFT JOIN GruposAgenc AS GA ON A.CdGrupAgenc=GA.IdGrupo LEFT JOIN SubGruposAgenc AS SGA ON A.CdSubGrupAgenc=SGA.IdSubAgenc
 WHERE CA.NContrato BETWEEN @pmNContratoIni AND @pmNContratoFin AND CA.IdCia=@pmIdCia
 ORDER BY Item
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,AgencOperando,TipoFactFletes,CdCCBonif,CdSubCCBonif,CodRuta,CodDiaEnt,CdGruClie,CupoElectcom
 FROM Agencias
 WHERE IdAgencia=@pmIdAgencia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmAgencOperando BIT
,@pmTipoFactFletes INT,@pmCdCCBonif VARCHAR(16),@pmCdSubCCBonif VARCHAR(16),@pmCodRuta VARCHAR(4),@pmCodDiaEnt VARCHAR(4),@pmCdGruClie VARCHAR(4),@pmCupoElectcom 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,AgencOperando,TipoFactFletes,CdCCBonif,CdSubCCBonif,CodRuta,CodDiaEnt,CdGruClie,CupoElectcom)
 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,@pmAgencOperando,@pmTipoFactFletes,@pmCdCCBonif,@pmCdSubCCBonif,@pmCodRuta,@pmCodDiaEnt,@pmCdGruClie,@pmCupoElectcom)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmAgencOperando BIT,@pmTipoFactFletes INT,@pmCdCCBonif VARCHAR(16),@pmCdSubCCBonif VARCHAR(16)
,@pmCodRuta VARCHAR(4),@pmCodDiaEnt VARCHAR(4),@pmCdGruClie VARCHAR(4),@pmCupoElectcom 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,AgencOperando=@pmAgencOperando,TipoFactFletes=@pmTipoFactFletes,CdCCBonif=@pmCdCCBonif,CdSubCCBonif=@pmCdSubCCBonif,CodRuta=@pmCodRuta,CodDiaEnt=@pmCodDiaEnt,CdGruClie=@pmCdGruClie,CupoElectcom=@pmCupoElectcom
 WHERE IdAgencia=@pmIdAgencia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraRemesaLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null,@pmIdVehiculo VARCHAR(10)=Null
 AS 
 SELECT R.NumOrden AS Num_Orden,R.IdCia AS CdCia,Fecha,HoraLlegada,FecDespacho,FecEntrega,IdCliente,NomCliente,R.IdAgencia AS CdAgencia,Agencia,IdClieFact,CF.RazonSocial AS ClienteFact,IdRemitente,NomRemite,IdLocOrigen,LO.Localidad AS CiudadOrigen,LugarCargue
,NomContacto,TelContacto,emlContacto,IdDestinatario,NomDestino,IdLocDestino,LD.Localidad AS CiudadDestino,LugarDescargue,ContacDestino,TelContacDest,emlContacDest,IdLocFletes,LF.Localidad AS LugarFletes
,IdVehiculo,nRemolque,TipoAfiVehic,IdConductor,NC.RazonSocial AS Conductor,Modalidad,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos
,VrDeclarado,VrSeguro,Cantidad,PesoTotal,Items,IdMneda,VrTasa,RA.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,NumPedido,IdCiaPed,FechaPed,TipDcm,NumDocmto,IdCiaDcm,FechaDcm,NumManif,IdCiaManif,EstCumplido,EstFactura,TipOrdMud,NumInvent,IdCiaInv
,CdConcepto,CdRutaTarif,CdBodega,TipCom,Comprobante,IdCiaCom,OrigenAdd,Anulado,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,SerieGuia,NumGuia,CdForma,NumCausac,CdCiaCausac,EdoCausac,VigenciaOdc,RA.CantViajes,PactoCargue,HorasPactoCargue,MinPactoCargue
,HoraCitaCargue,HoraLlegaCargue,HoraEntraCargue,HoraSaleCargue,PactoDescargue,HorasPactoDescargue,MinPactoDescargue,HoraCitaDescargue,HoraLlegaDescargue,HoraEntraDescargue,HoraSaleDescargue,RA.TipoDevRemesa,RA.CodCiuTransb,LT.Localidad AS CiudadTransb,RA.MvoTransbordo
,R.TimeSys,R.FecUpdate,R.IdCiaCrea,R.IdUsuario AS CdUsuario,Usuario
 FROM Trn_TraRemesa AS R  INNER JOIN Trn_TraRemAnexo AS RA ON R.TipDoc=RA.TipDoc  AND R.NumOrden=RA.NumOrden AND R.IdCia=RA.IdCia
 INNER JOIN Agencias AS A ON R.IdAgencia=A.IdAgencia
 INNER JOIN Localidades AS LO ON R.IdLocOrigen=LO.IdLocal INNER JOIN Localidades AS LD ON R.IdLocDestino=LD.IdLocal
 INNER JOIN Localidades AS LF ON R.IdLocFletes=LF.IdLocal  INNER JOIN Terceros AS NC ON R.IdConductor=NC.IdTercero 
 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 VN ON RA.IdVend=VN.IdTercero LEFT JOIN Terceros AS CF ON R.IdClieFact=CF.IdTercero
 LEFT JOIN Localidades AS LT ON RA.CodCiuTransb=LT.IdLocal
 WHERE R.TipDoc=@pmTipDoc AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 ORDER BY R.IdCia,R.NumOrden
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraPedidoLta]  @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT P.Pedido AS NumPedido,P.IdCia AS CdCia,Fecha,FechaVence,FecDespacho,FecEntrega,IdCliente,NomCliente,P.IdAgencia AS Id_Agencia,Agencia,IdClieFact,CF.RazonSocial AS ClienteFact,IdRemitente,NomRemite,IdLocOrigen,LO.Localidad AS CiudadOrigen,LugarCargue
,NomContacto,TelContacto,emlContacto,IdDestinatario,NomDestino,IdLocDestino,LD.Localidad AS CiudadDestino,LugarDescargue,ContacDestino,TelContacDest,emlContacDest,P.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,IdTarifCom,Modalidad
,Vigencia,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos,VrDeclarado,VrSeguro,Cantidad,CantDesp,Cantidad-CantDesp AS CantPendiente
,IdMneda,VrTasa,Cotizacion,IdCiaCot,NumAprob,IdCiaApr,FecAprob,TipOdc,OCargue,IdCiaOdc,FechaOdc,TipRem,Remesa,IdCiaRem,FechaRem,TipFac,Factura,IdCiaFac,FechaFac,OrigenAdd,Anulado
,FecDev,CdRutaTarif,P.Observacion AS Observ,P.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,P.IdUsuario AS CdUsuario,Usuario
--anexo
 FROM Trn_TraPedido AS P INNER JOIN Trn_TraPedAnexo AS PA ON P.TipDoc=PA.TipDoc  AND P.Pedido=PA.Pedido AND P.IdCia=PA.IdCia
 INNER JOIN Terceros AS VN ON P.IdVend=VN.IdTercero  INNER JOIN Agencias AS A ON P.IdAgencia=A.IdAgencia
 INNER JOIN Localidades AS LO ON P.IdLocOrigen=LO.IdLocal INNER JOIN Localidades AS LD ON P.IdLocDestino=LD.IdLocal
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 LEFT JOIN Terceros AS CF ON P.IdClieFact=CF.IdTercero
 WHERE P.TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY P.IdCia,P.Pedido
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTarifasFleMayLta] @pmIdAgencia VARCHAR(16)
 AS 
 SELECT A.IdClie AS NitClie,T.RazonSocial AS NomCliente,F.IdAgencia AS CdAgencia,Agencia,TipoTarifa,Numero
,F.TipoVehic AS CdTipoVeh,TV.TipoVehic AS TipoVeh,F.CapacMax AS CapMaxima,VrTarifa,TipoLiquida,F.Inactivo AS Inactvo
,NitEmpresa,E.RazonSocial AS NomEmpresa,TV.CapacMin AS Cap_Minimo,TV.CapacMax AS Cap_Maximo,FechaCrea,F.IdUsuario AS CdUsuario,Usuario
FROM TarifasFleMay AS F INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario INNER JOIN Agencias AS A ON F.IdAgencia=A.IdAgencia
INNER JOIN Terceros AS T ON A.IdClie=T.IdTercero LEFT JOIN TiposVehFletes AS TV ON F.TipoVehic=TV.Item 
LEFT JOIN Terceros AS E ON F.NitEmpresa=E.IdTercero
WHERE F.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTercClieCuposLta] 
 AS 
 SELECT C.IdClie,RazonSocial,C.IdAgencia AS CdAgencia,Agencia,C.nAnno,C.nMes,C.CupoGals,C.CupoZFE,C.CupoAdic
 FROM TercClieCupos AS C INNER JOIN Terceros AS T ON C.IdClie=T.IdTercero 
 LEFT JOIN Agencias AS A ON C.IdAgencia=A.IdAgencia
 WHERE T.Inactivo=0
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE FUNCTION [dbo].[FuncMayNomAgencia]  (@pmNContrato INT,@pmIdCia CHAR(2))
RETURNS VARCHAR(250)
 AS  
BEGIN 
 DECLARE @NomAgencia  VARCHAR(250)
 SET  @NomAgencia=(SELECT TOP 1 Agencia 
 FROM Trn_MayAgencias AS MA INNER JOIN Agencias AS A ON MA.IdAgencia=A.IdAgencia
 WHERE MA.NContrato=@pmNContrato AND MA.IdCia=@pmIdCia)
 RETURN @NomAgencia
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmVrCompras
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelFields] @pmIdEntidad VARCHAR(30),@pmIdCampo VARCHAR(20)=Null
 AS 
 DELETE FROM Sys_Fields
 WHERE IdEntidad=@pmIdEntidad AND IdCampo LIKE ISNULL(@pmIdCampo,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[paInstm_PreCompra_Sel] @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2) 
 AS 
 INSERT INTO tm_PreCompra (tmNumero,tmItem,tmFactura,tmFecha,tmFecVence,tmDiasPlazo,tmVrFactura,tmDescripcion)
 SELECT @pmtmNumero,Item,NumFactura,Fecha,FechaVence,DiasPlazo,VrFactura,''
 FROM Trn_FinFacturas
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryConcDiversosDso] @pmGrupo VARCHAR(20)=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT C.IdConcepto,Concepto,Grupo,Concepto+' '+C.IdConcepto+' ('+Grupo+')' AS DsCon
 FROM ConcDiversos AS C LEFT JOIN ConcDivCia AS CN ON C.IdConcepto=CN.IdConcepto
 WHERE C.Inactivo=0 AND C.Grupo LIKE ISNULL(@pmGrupo,'%')
 AND (CN.IdCia=@pmIdCia OR CN.IdCia IS NULL)
 ORDER BY Concepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryConcDiversosOsm] @pmIdCia CHAR(2) 
 AS 
 SELECT C.IdConcepto,Concepto,Grupo,Concepto+' '+C.IdConcepto AS DsCon
 FROM ConcDiversos AS C LEFT JOIN ConcDivCia AS CN ON C.IdConcepto=CN.IdConcepto
 WHERE C.Grupo='FACTURA' AND C.EsServicio=1 AND C.OrdenServ=3 AND C.Inactivo=0 
 AND (CN.IdCia=@pmIdCia OR CN.IdCia IS NULL)
 ORDER BY C.Concepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryConcDiversosGas] @pmIdCia CHAR(2) 
 AS 
 SELECT C.IdConcepto,Concepto,Grupo,Concepto+' '+C.IdConcepto AS DsCon
 FROM ConcDiversos AS C LEFT JOIN ConcDivCia AS CN ON C.IdConcepto=CN.IdConcepto
 WHERE C.Grupo IN ('GASTOS','OTROS') AND C.Inactivo=0
 AND (CN.IdCia=@pmIdCia OR CN.IdCia IS NULL)
 ORDER BY Concepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryConcDiversosFac] @pmIdCia CHAR(2) 
 AS 
 SELECT C.IdConcepto,Concepto,Grupo,Concepto+' '+C.IdConcepto AS DsCon
 FROM ConcDiversos AS C LEFT JOIN ConcDivCia AS CN ON C.IdConcepto=CN.IdConcepto
 WHERE C.Grupo='FACTURA' AND C.Inactivo=0 AND (CN.IdCia=@pmIdCia OR CN.IdCia IS NULL)
 ORDER BY C.Concepto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsProdInsumos] @pmIdProducto VARCHAR(16),@pmItem INT,@pmIdInsumo VARCHAR(16),@pmIdTipProc VARCHAR(4),@pmCantidad DECIMAL(14,4),@pmInactivo BIT
 AS 
 INSERT INTO ProdInsumos (IdProducto,Item,IdInsumo,IdTipProc,Cantidad,Inactivo)
 VALUES (@pmIdProducto,@pmItem,@pmIdInsumo,@pmIdTipProc,@pmCantidad,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpProdInsumos] @pmId INT,@pmIdProducto VARCHAR(16),@pmItem INT,@pmIdInsumo VARCHAR(16),@pmIdTipProc VARCHAR(4),@pmCantidad DECIMAL(14,4),@pmInactivo BIT
 AS 
 UPDATE ProdInsumos SET IdProducto=@pmIdProducto,Item=@pmItem,IdInsumo=@pmIdInsumo,IdTipProc=@pmIdTipProc,Cantidad=@pmCantidad,Inactivo=@pmInactivo
 WHERE Id=@pmId
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryGrupos] @pmIdGrupo VARCHAR(8)
 AS 
 SELECT IdGrupo,Grupo,IdLinea,TipoInvent,Inactivo
 FROM Grupos
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryAfLineasDso] 
 AS 
 SELECT IdLinea,Linea,IdLinea+' '+Linea AS DsLin
 FROM AfLineas
 WHERE Inactivo=0 
 ORDER BY IdLinea
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelAfLineas] @pmIdLinea VARCHAR(4)
 AS 
 DELETE FROM AfLineas
 WHERE IdLinea=@pmIdLinea
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCodBod VARCHAR(4),@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,CodBod)
 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,@pmCodBod)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CodBod,TimeSys,FecUpdate,IdUsuario
 FROM Trn_ProdTarf
 WHERE Tipo=@pmTipo AND Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCodBod VARCHAR(4),@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,CodBod=@pmCodBod
 WHERE Tipo=@pmTipo AND Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,ISNULL(CodBod,'') AS CodBodega,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmCodConc,tmCiuOrigen,Localidad,tmCodTarif
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTarifAnticiposLta] @pmTipoCuota VARCHAR(10)=Null,@pmTipoAplica VARCHAR(10)=Null,@pmInactivo BIT=Null
 AS 
 SELECT Numero,TipoAplica,CuotaMax,TipoCuota,CdRuta,CdOrigen,O.Localidad AS CiuOrigen,CdDestino,D.Localidad AS CiuDestino
,NitCliente,RazonSocial,CdTipoVeh,TipoVehiculo,TipoAfilVeh,A.Inactivo AS Inactvo,FechaCrea,A.IdUsuario AS CdUsuario,Usuario
,O.IdDep AS CdDep,DPO.Departamento AS DptoOrigen,D.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino,NitPropietario
 FROM TarifAnticipos AS A INNER JOIN  adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 LEFT JOIN Localidades AS O ON A.CdOrigen=O.IdLocal LEFT JOIN Localidades AS D ON  A.CdDestino=D.IdLocal
 LEFT JOIN Departamentos AS DPO ON O.IdDep=DPO.IdDep LEFT JOIN Departamentos AS DPD ON D.IdDep=DPD.IdDep
 LEFT JOIN Terceros AS TC ON A.NitCliente=TC.IdTercero LEFT JOIN TiposVeh AS TV ON A.CdTipoVeh=TV.IdTipoVeh
 WHERE  TipoCuota LIKE ISNULL(@pmTipoCuota,'%') AND  TipoAplica LIKE ISNULL(@pmTipoAplica,'%')
 AND  (A.Inactivo=ISNULL(@pmInactivo,0) or A.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY Numero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTercSedesDso] @pmIdTercero VARCHAR(16)
 AS 
 SELECT IdSede,NomSede,IdLocSede,Localidad
,NomSede+' '+IdSede+' ('+ISNULL(Localidad,'')+')' AS DsSede
 FROM TercSedes AS S LEFT JOIN Localidades AS L ON S.IdLocSede=L.IdLocal
 WHERE IdTercero=@pmIdTercero
 ORDER BY NomSede
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryLocalidadesPais] @pmIdPais VARCHAR(8)
 AS 
 SELECT L.IdLocal,L.Localidad,L.IdDep AS CodDep,D.Departamento,L.TipLocal
,L.Localidad+ ' ('+ D.Departamento +' '+P.CodISO+')' AS NomCiudad 
 FROM Localidades AS L INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN adm_Paises AS P ON D.IdPais=P.IdPais
 WHERE D.IdPais=@pmIdPais AND L.Inactivo=0
 ORDER BY Localidad
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrImpCarbono,VrIvaIngProd,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,IdTerminal,TipoOrden,Remolque,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTarifasPaqLta] @pmIdOrigen VARCHAR(8)=Null,@pmIdDestino VARCHAR(8)=Null
AS
 SELECT IdOrigen,O.Localidad AS CiuOrigen,O.IdDep AS CdDepOrig,DOR.Departamento AS DptoOrigen
,IdDestino,D.Localidad AS CiuDestino,D.IdDep AS CdDepDtno,DDT.Departamento AS DptoDtno
,TarifaUnd,TiempoEnt,TarifaMin,FormaPago
 FROM TarifasPaq AS T INNER JOIN Localidades AS O ON T.IdOrigen=O.IdLocal INNER JOIN Departamentos AS DOR ON O.IdDep=DOR.IdDep
 INNER JOIN Localidades AS D ON T.IdDestino=D.IdLocal INNER JOIN Departamentos AS DDT ON D.IdDep=DDT.IdDep
 WHERE T.IdOrigen LIKE ISNULL(@pmIdOrigen,'%') AND T.IdDestino LIKE ISNULL(@pmIdDestino,'%')
 AND T.Inactivo=0
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraFijosLta] @pmTipoConc VARCHAR(10)=Null
 AS 
 SELECT Numero,IdConcepto,DescConc,TipoConc,Tarifa,TipoTarif,TipoAfilVeh,CdTipoVeh,TipoVehiculo,TipoTerc
,CdCiuOrigen,LO.Localidad AS CiuOrigen,LO.IdDep AS CdDepOri,DPO.Departamento AS DptoOrigen
,CdCiuDestino,LD.Localidad AS CiuDestino,LD.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino
,pVehiculo,CedConductor,CDT.RazonSocial AS NomConductor,CdRuta,R.Ruta,NitCliente,T.RazonSocial AS NomCliente,CdMercancia,M.DescripMcia
,TipoVigencia,FechaInicial,FechaFinal,F.Anulado
 FROM TraFijos AS F LEFT JOIN TiposVeh AS TV ON F.CdTipoVeh=TV.IdTipoVeh
 LEFT JOIN Localidades AS LO ON F.CdCiuOrigen=LO.IdLocal LEFT JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 LEFT JOIN Localidades AS LD ON F.CdCiuDestino=LD.IdLocal LEFT JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 LEFT JOIN Rutas AS R ON F.CdRuta=R.IdRuta LEFT JOIN Terceros AS CDT ON F.CedConductor=CDT.IdTercero
 LEFT JOIN Terceros AS T ON F.NitCliente=T.IdTercero LEFT JOIN Mercancias AS M ON F.CdMercancia=M.IdMercancia
 WHERE TipoConc LIKE ISNULL(@pmTipoConc,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOrgTransitoLta] 
 AS 
 SELECT IdOrgTra,NomOrgTrans,OG.IdLocal AS CdLocal,Localidad
 FROM OrgTransito AS OG INNER JOIN Localidades AS L ON OG.IdLocal=L.IdLocal
 WHERE OG.Inactivo=0
 ORDER BY NomOrgTrans
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTarifasMasivoLta] @pmIdOrigen VARCHAR(8)=Null,@pmIdDestino VARCHAR(8)=Null
AS
 SELECT IdOrigen,O.Localidad AS CiuOrigen,O.IdDep AS CdDepOrig,DOR.Departamento AS DptoOrigen
,IdDestino,D.Localidad AS CiuDestino,D.IdDep AS CdDepDtno,DDT.Departamento AS DptoDtno
,T.IdTipoVeh AS CdTipoVeh,TipoVehiculo,TarifaUnd,VrFletes
 FROM TarifasMasivo AS T INNER JOIN Localidades AS O ON T.IdOrigen=O.IdLocal INNER JOIN Departamentos AS DOR ON O.IdDep=DOR.IdDep
 INNER JOIN Localidades AS D ON T.IdDestino=D.IdLocal INNER JOIN Departamentos AS DDT ON D.IdDep=DDT.IdDep
 INNER JOIN TiposVeh AS TV ON T.IdTipoVeh=TV.IdTipoVeh
 WHERE T.IdOrigen LIKE ISNULL(@pmIdOrigen,'%') AND T.IdDestino LIKE ISNULL(@pmIdDestino,'%')
 AND T.Inactivo=0
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CdCiaTurno,PuntosRut,R.Inactivo AS Inactvo
 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 IdLocOri like ISNULL(@pmIdLocOri,'%') AND IdLocDes LIKE ISNULL(@pmIdLocDes,'%')
 AND (EsLocal=ISNULL(@pmEsLocal,0) OR EsLocal=ISNULL(@pmEsLocal,1))
 ORDER BY IdRuta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryRutasCiu] @pmIdLocOri VARCHAR(8),@pmIdLocDes VARCHAR(8),@pmInactivo 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,PuntosRut,R.Inactivo AS Inactvo
 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  IdLocOri=@pmIdLocOri AND  IdLocDes=@pmIdLocDes AND EsLocal=0
 AND (R.Inactivo=ISNULL(@pmInactivo,0) or  R.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY IdRuta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,CdDpto,Departamento,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 LEFT JOIN Departamentos AS DP ON PIM.CdDpto=DP.IdDep
 WHERE PIM.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND PIM.IdClase LIKE ISNULL(@pmIdClase,'%')
 ORDER BY  DescripProd,PIM.IdClase,PIM.IdLocal,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoOrden,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraPresAntFmt] @pmNumPstoIni INT,@pmNumPstoFin INT
 AS 
 SELECT P.NumPsto AS NumPrespto,Fecha,IdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,IdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,TipoRuta,CdRuta,Ruta
,FecInicio,FecFinal,VrGastos,VrComb,VrPeajes,VrAnticipo,CdCat,Catpeaje,CdTipoVeh,TipoVehiculo,TipoAfilVeh,CdTipoMot,TipoMotor
,CdCom,TipoComb,CdRango,DescripRango,ValorFijo,P.Observacion,P.Inactivo AS Inactvo,NitCliente,CL.RazonSocial AS Cliente,CdMercancia,DescripMcia
,CdTipoCarr,TipoCar,P.TipoServicio,Cantgalones,VrExtralegal,FechaCrea,FechaAct,P.IdUsuario AS CdUsuario,Usuario
,D.IdConcepto AS CdConcepto,Concepto,Descripcion,Cantidad,VrUnitario,VrTotal,TipoRubro,CdClase,ClasePeaje,NitTercero,T.RazonSocial,CdTipOper,TipoOperacion
 FROM Trn_TraPresAnt AS P INNER JOIN Trn_TraPresItems AS D ON P.NumPsto=D.NumPsto
 INNER JOIN Localidades AS CO ON  P.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON P.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 LEFT JOIN Rutas AS R ON P.CdRuta=R.IdRuta LEFT JOIN TiposVeh AS TV ON P.CdTipoVeh=TV.IdTipoVeh 
 LEFT JOIN PeajesCat AS CP ON P.CdCat=CP.IdCat LEFT JOIN TiposMot AS TM ON P.CdTipoMot=TM.IdTipoMot
 LEFT JOIN TiposFuel AS TF ON P.CdCom=TF.IdCom LEFT JOIN RangosPeso AS RP ON P.CdRango=RP.IdRango
 LEFT JOIN ConcDiversos AS C ON D.IdConcepto=C.IdConcepto LEFT JOIN PeajesClase AS PC ON D.CdClase=PC.IdClase
 LEFT JOIN Terceros AS T ON D.NitTercero=T.IdTercero LEFT JOIN TiposOperac AS TP ON D.CdTipOper=TP.IdTipoOper
 LEFT JOIN Terceros AS CL ON P.NitCliente=CL.IdTercero LEFT JOIN Mercancias AS MC ON P.CdMercancia=MC.IdMercancia
 LEFT JOIN TiposCar AS TCA ON P.CdTipoCarr=TCA.IdCrceria
 WHERE P.NumPsto BETWEEN @pmNumPstoIni AND @pmNumPstoFin
 ORDER BY P.NumPsto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraPresAntLta] @pmIdOrigen VARCHAR(8)=Null,@pmIdDestino VARCHAR(8)=Null,@pmTipoAfilVeh VARCHAR(10)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT P.NumPsto AS NumPrespto,Fecha,IdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,IdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,TipoRuta,CdRuta,Ruta
,FecInicio,FecFinal,VrGastos,VrComb,VrPeajes,VrAnticipo,CdCat,Catpeaje,CdTipoVeh,TipoVehiculo,TipoAfilVeh,CdTipoMot,TipoMotor
,CdCom,TipoComb,CdRango,DescripRango,ValorFijo,P.Observacion,P.Inactivo AS Inactvo,NitCliente,CL.RazonSocial AS Cliente,CdMercancia,DescripMcia
,CdTipoCarr,TipoCar,Cantgalones,VrExtralegal,FechaCrea,FechaAct,P.IdUsuario AS CdUsuario,Usuario
,D.IdConcepto AS CdConcepto,Concepto,Descripcion,Cantidad,VrUnitario,VrTotal,TipoRubro,CdClase,ClasePeaje,NitTercero,T.RazonSocial,CdTipOper,TipoOperacion
 FROM Trn_TraPresAnt AS P INNER JOIN Trn_TraPresItems AS D ON P.NumPsto=D.NumPsto
 INNER JOIN Localidades AS CO ON  P.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON P.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 LEFT JOIN Rutas AS R ON P.CdRuta=R.IdRuta LEFT JOIN TiposVeh AS TV ON P.CdTipoVeh=TV.IdTipoVeh 
 LEFT JOIN PeajesCat AS CP ON P.CdCat=CP.IdCat LEFT JOIN TiposMot AS TM ON P.CdTipoMot=TM.IdTipoMot
 LEFT JOIN TiposFuel AS TF ON P.CdCom=TF.IdCom LEFT JOIN RangosPeso AS RP ON P.CdRango=RP.IdRango
 LEFT JOIN ConcDiversos AS C ON D.IdConcepto=C.IdConcepto LEFT JOIN PeajesClase AS PC ON D.CdClase=PC.IdClase
 LEFT JOIN Terceros AS T ON D.NitTercero=T.IdTercero LEFT JOIN TiposOperac AS TP ON D.CdTipOper=TP.IdTipoOper
 LEFT JOIN Terceros AS CL ON P.NitCliente=CL.IdTercero LEFT JOIN Mercancias AS MC ON P.CdMercancia=MC.IdMercancia
 LEFT JOIN TiposCar AS TCA ON P.CdTipoCarr=TCA.IdCrceria
 WHERE IdOrigen LIKE ISNULL(@pmIdOrigen,'%') AND IdDestino LIKE ISNULL(@pmIdDestino,'%') AND TipoAfilVeh LIKE ISNULL(@pmTipoAfilVeh,'%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY P.NumPsto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryExpLicenciasDso]
 AS 
 SELECT IdLugar,LugarLic,Dpto,LugarLic+' ('+ Dpto + ')' AS DsLug  FROM ExpLicencias
 WHERE Inactivo=0
 ORDER BY LugarLic
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelExpLicencias] @pmIdLugar VARCHAR(8)
 AS 
 DELETE FROM ExpLicencias
 WHERE IdLugar=@pmIdLugar
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsDocSoporteDet] @pmTipDoc VARCHAR(3),@pmNumDoc INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdProducto VARCHAR(16),@pmDescripcion VARCHAR(1000),@pmCantidad DECIMAL(16,6),@pmVrUnitario DECIMAL(16,6),@pmValorOper DECIMAL(16,6),@pmVrDcto DECIMAL(16,6),@pmVrRetencion DECIMAL(16,6),@pmVrRetIca DECIMAL(16,6)
,@pmVrBomberil DECIMAL(16,6),@pmVrAviTab DECIMAL(16,6),@pmTarifaDct DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaBom DECIMAL(14,4),@pmTarifaAvta DECIMAL(14,4),@pmCodTarDct VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarifBom VARCHAR(4),@pmCodTarifAvta VARCHAR(4),@pmItemCom INT
,@pmCdCuenta VARCHAR(16),@pmCdCueCre VARCHAR(16),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmDetalle VARCHAR(500),@pmNitTercero VARCHAR(16)
 AS 
 INSERT INTO Trn_DocSoporteDet (TipDoc,NumDoc,IdCia,Item,IdProducto,Descripcion,Cantidad,VrUnitario,ValorOper,VrDcto,VrRetencion,VrRetIca,VrBomberil,VrAviTab,TarifaDct,TarifaRet,TarifaIca,TarifaBom,TarifaAvta,CodTarDct,CodTarRet,CodTarIca,CodTarifBom,CodTarifAvta,ItemCom,CdCuenta,CdCueCre,TipCom,Comprobante,IdCiaCom,Detalle,NitTercero)
 VALUES (@pmTipDoc,@pmNumDoc,@pmIdCia,@pmItem,@pmIdProducto,@pmDescripcion,@pmCantidad,@pmVrUnitario,@pmValorOper,@pmVrDcto,@pmVrRetencion,@pmVrRetIca,@pmVrBomberil,@pmVrAviTab,@pmTarifaDct,@pmTarifaRet,@pmTarifaIca,@pmTarifaBom,@pmTarifaAvta,@pmCodTarDct,@pmCodTarRet,@pmCodTarIca,@pmCodTarifBom,@pmCodTarifAvta,@pmItemCom,@pmCdCuenta,@pmCdCueCre,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmDetalle,@pmNitTercero)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_BalAcumGCue] @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCuenta,COUNT(tmItem) AS SCANT
 FROM tm_BalAcum
 WHERE tmEst=@pmtmEst
 GROUP BY tmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_BalAcum] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_BalAcum  WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelSubgruposCue] @pmIdSubgrupo VARCHAR(8),@pmIdClase VARCHAR(4)
 AS 
 DELETE FROM SubgruposCue
 WHERE IdSubgrupo=@pmIdSubgrupo AND IdClase LIKE ISNULL(@pmIdClase,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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','VCC')
  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','VCC')
  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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,P.Tercero,P.Vehiculo,PC.Tercero AS CComTerc,PC.Vehiculo AS CComVehic
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdEntregaTallasFmt] @pmTipDoc VARCHAR(3),@pmNumEntregaIni INT,@pmNumEntregaFin INT,@pmIdCia CHAR(2)
AS
SELECT TipDoc,NumEntrega,IdCia,Item,ItemDet,NumTalla,Cantidad,Defectuoso,Rechazado
FROM Trn_ProdEntregaTallas
WHERE TipDoc=@pmTipDoc AND NumEntrega BETWEEN @pmNumEntregaIni AND @pmNumEntregaFin
AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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=1
 AND (@pmIdCia IS NULL OR IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR D.IdCuenta=@pmIdCuenta) 
 GROUP BY  YEAR(Fecha),IdCia,D.IdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuCuentas_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuCuentas AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt 
 AND NOT EXISTS (SELECT * FROM AcuCuentas AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes  
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuCuentasSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuCuentas SET AcuCuentas.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuCuentas INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuCuentas WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt) AS SA ON AcuCuentas.IdCia=SA.IdCia AND AcuCuentas.IdCuenta=SA.IdCuenta
 WHERE AcuCuentas.nAnno=@pmnAnno AND AcuCuentas.nMes=@pmnMes
 AND (@pmIdCia IS NULL OR AcuCuentas.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuCuentas.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsJurRecOfrecmto] @pmTipDoc VARCHAR(3),@pmReclamac INT,@pmIdCia CHAR(2),@pmItem INT,@pmFechaOfrec SMALLDATETIME,@pmVrOfrecmto MONEY,@pmOfcAceptado INT,@pmFecAceptado SMALLDATETIME
,@pmOfcPagado INT,@pmNumSolGiro VARCHAR(30),@pmComentarios VARCHAR(1000),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_JurRecOfrecmto (TipDoc,Reclamac,IdCia,Item,FechaOfrec,VrOfrecmto,OfcAceptado,FecAceptado,OfcPagado,NumSolGiro,Comentarios,TimeSys,IdUsuario)
 VALUES (@pmTipDoc,@pmReclamac,@pmIdCia,@pmItem,@pmFechaOfrec,@pmVrOfrecmto,@pmOfcAceptado,@pmFecAceptado,@pmOfcPagado,@pmNumSolGiro,@pmComentarios,@pmTimeSys,@pmIdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpJurRecOfrecmto] @pmTipDoc VARCHAR(3),@pmReclamac INT,@pmIdCia CHAR(2),@pmItem INT,@pmFechaOfrec SMALLDATETIME,@pmVrOfrecmto MONEY,@pmOfcAceptado INT
,@pmFecAceptado SMALLDATETIME,@pmOfcPagado INT,@pmNumSolGiro VARCHAR(30),@pmComentarios VARCHAR(1000)
 AS 
 UPDATE Trn_JurRecOfrecmto SET FechaOfrec=@pmFechaOfrec,VrOfrecmto=@pmVrOfrecmto,OfcAceptado=@pmOfcAceptado,FecAceptado=@pmFecAceptado,OfcPagado=@pmOfcPagado
,NumSolGiro=@pmNumSolGiro,Comentarios=@pmComentarios
 WHERE TipDoc=@pmTipDoc AND Reclamac=@pmReclamac AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraPresAnt] @pmNumPsto INT
 AS 
 BEGIN
  DECLARE @TrnNombre VARCHAR(20)
  SET @TrnNombre = 'TrnPresAnt'
  BEGIN TRANSACTION @TrnNombre
   DELETE FROM Trn_TraPresItems  WHERE NumPsto=@pmNumPsto    
   DELETE FROM Trn_TraPresAnt  WHERE NumPsto=@pmNumPsto
  COMMIT TRANSACTION  @TrnNombre
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraPresAnt] @pmNumPsto INT
 AS 
 SELECT NumPsto,Fecha,IdOrigen,IdDestino,TipoRuta,CdRuta,FecInicio,FecFinal,VrGastos,VrComb,VrPeajes,VrAnticipo,CdCat,CdTipoVeh
,TipoAfilVeh,CdTipoMot,CdCom,CdRango,ValorFijo,Observacion,Inactivo,NitCliente,CdMercancia,TipoServicio,Cantgalones,VrExtralegal,CdTipoCarr
,FechaCrea,FechaAct,IdUsuario
 FROM Trn_TraPresAnt
 WHERE NumPsto=@pmNumPsto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraPresAnt] @pmNumPsto INT,@pmFecha SMALLDATETIME,@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmFecInicio SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmVrGastos MONEY,@pmVrComb MONEY,@pmVrPeajes MONEY,@pmVrAnticipo MONEY,@pmCdCat VARCHAR(4)
,@pmCdTipoVeh VARCHAR(4),@pmTipoAfilVeh VARCHAR(10),@pmCdTipoMot VARCHAR(4),@pmCdCom VARCHAR(4),@pmCdRango VARCHAR(4),@pmValorFijo BIT,@pmObservacion VARCHAR(250),@pmInactivo BIT,@pmTipoRuta VARCHAR(10),@pmCdRuta  VARCHAR(4)
,@pmNitCliente VARCHAR(16),@pmCdMercancia VARCHAR(16),@pmTipoServicio INT,@pmCantgalones DECIMAL(14,4),@pmVrExtralegal MONEY,@pmCdTipoCarr VARCHAR(4),@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraPresAnt (NumPsto,Fecha,IdOrigen,IdDestino,TipoRuta,CdRuta,FecInicio,FecFinal,VrGastos,VrComb,VrPeajes,VrAnticipo,CdCat,CdTipoVeh,TipoAfilVeh,CdTipoMot,CdCom,CdRango,ValorFijo,Observacion,Inactivo,FechaCrea,IdUsuario,NitCliente,CdMercancia,TipoServicio,Cantgalones,VrExtralegal,CdTipoCarr)
 VALUES (@pmNumPsto,@pmFecha,@pmIdOrigen,@pmIdDestino,@pmTipoRuta,@pmCdRuta,@pmFecInicio,@pmFecFinal,@pmVrGastos,@pmVrComb,@pmVrPeajes,@pmVrAnticipo,@pmCdCat,@pmCdTipoVeh,@pmTipoAfilVeh,@pmCdTipoMot,@pmCdCom
,@pmCdRango,@pmValorFijo,@pmObservacion,@pmInactivo,@pmFechaCrea,@pmIdUsuario,@pmNitCliente,@pmCdMercancia,@pmTipoServicio,@pmCantgalones,@pmVrExtralegal,@pmCdTipoCarr)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraPresAnt] @pmNumPsto INT,@pmFecha SMALLDATETIME,@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmFecInicio SMALLDATETIME,@pmFecFinal SMALLDATETIME
,@pmVrGastos MONEY,@pmVrComb MONEY,@pmVrPeajes MONEY,@pmVrAnticipo MONEY,@pmCdCat VARCHAR(4),@pmCdTipoVeh VARCHAR(4),@pmTipoAfilVeh VARCHAR(10),@pmCdTipoMot VARCHAR(4)
,@pmCdCom VARCHAR(4),@pmCdRango VARCHAR(4),@pmValorFijo BIT,@pmObservacion VARCHAR(250),@pmInactivo BIT,@pmTipoRuta VARCHAR(10),@pmCdRuta  VARCHAR(4),@pmNitCliente VARCHAR(16)
,@pmCdMercancia VARCHAR(16),@pmTipoServicio INT,@pmCantgalones DECIMAL(14,4),@pmVrExtralegal MONEY,@pmCdTipoCarr VARCHAR(4),@pmFechaAct SMALLDATETIME 
 AS 
 UPDATE Trn_TraPresAnt SET Fecha=@pmFecha,IdOrigen=@pmIdOrigen,IdDestino=@pmIdDestino,FecInicio=@pmFecInicio,FecFinal=@pmFecFinal,VrGastos=@pmVrGastos,VrComb=@pmVrComb,VrPeajes=@pmVrPeajes
,VrAnticipo=@pmVrAnticipo,CdCat=@pmCdCat,CdTipoVeh=@pmCdTipoVeh,TipoAfilVeh=@pmTipoAfilVeh,CdTipoMot=@pmCdTipoMot,CdCom=@pmCdCom,CdRango=@pmCdRango,ValorFijo=@pmValorFijo
,Observacion=@pmObservacion,Inactivo=@pmInactivo,TipoRuta=@pmTipoRuta,CdRuta=@pmCdRuta,FechaAct=@pmFechaAct,NitCliente=@pmNitCliente,CdMercancia=@pmCdMercancia,TipoServicio=@pmTipoServicio
,Cantgalones=@pmCantgalones,VrExtralegal=@pmVrExtralegal,CdTipoCarr=@pmCdTipoCarr
 WHERE NumPsto=@pmNumPsto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOrgTransito] @pmIdOrgTra VARCHAR(8)
 AS 
 SELECT IdOrgTra,NomOrgTrans,IdLocal,Inactivo
 FROM OrgTransito
 WHERE IdOrgTra=@pmIdOrgTra
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpOrgTransito] @pmIdOrgTra VARCHAR(8),@pmNomOrgTrans VARCHAR(250),@pmIdLocal VARCHAR(8),@pmInactivo BIT
 AS 
 UPDATE OrgTransito SET NomOrgTrans=@pmNomOrgTrans,IdLocal=@pmIdLocal,Inactivo=@pmInactivo
 WHERE IdOrgTra=@pmIdOrgTra
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOrgTransito] @pmIdOrgTra VARCHAR(8),@pmNomOrgTrans VARCHAR(250),@pmIdLocal VARCHAR(8),@pmInactivo BIT
 AS 
 INSERT INTO OrgTransito (IdOrgTra,NomOrgTrans,IdLocal,Inactivo)
 VALUES (@pmIdOrgTra,@pmNomOrgTrans,@pmIdLocal,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[paInsAcuPropietariosUni] @pmIdVehiculo VARCHAR(10),@pmIdVehiculoDos VARCHAR(10),@pmNewVehiculo VARCHAR(10)
AS
IF EXISTS (SELECT * FROM AcuPropietarios WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos)
 INSERT INTO AcuPropietarios (nAnno,IdReg,nMes,IdPropietario,IdVehiculo,IdClase,IdCia,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT nAnno,MAX(IdReg),nMes,IdPropietario,@pmNewVehiculo,IdClase,IdCia,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
 FROM AcuPropietarios WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos
 GROUP BY nAnno,nMes,IdPropietario,IdClase,IdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposTasas] @pmIdTasa VARCHAR(4),@pmTipoTasa VARCHAR(50),@pmSimbolo VARCHAR(10),@pmUnidad VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposTasas (IdTasa,TipoTasa,Simbolo,Unidad,Inactivo)
 VALUES (@pmIdTasa,@pmTipoTasa,@pmSimbolo,@pmUnidad,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposTasas] @pmIdTasa VARCHAR(4),@pmTipoTasa VARCHAR(50),@pmSimbolo VARCHAR(10),@pmUnidad VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposTasas SET TipoTasa=@pmTipoTasa,Simbolo=@pmSimbolo,Unidad=@pmUnidad,Inactivo=@pmInactivo
 WHERE IdTasa=@pmIdTasa
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposTasas] @pmIdTasa VARCHAR(4)
 AS 
 SELECT IdTasa,TipoTasa,Simbolo,Unidad,Inactivo
 FROM TiposTasas
 WHERE IdTasa=@pmIdTasa
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevCau] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Causacion,IdCiaDoc,FecDoc,IdProv,Factura,VrTotal,VrOtros,VrNeto
,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraDevCau
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraDevCau] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraDevCau
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevCauDoc] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,FecDoc,IdProv,ModdDev
 FROM Trn_TraDevCau
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCiaDoc=@pmIdCiaDoc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraDevCau] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdProv VARCHAR(16),@pmFactura VARCHAR(20),@pmVrTotal MONEY,@pmVrOtros MONEY
,@pmVrNeto MONEY,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraDevCau SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Causacion=@pmCausacion,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdProv=@pmIdProv,Factura=@pmFactura,VrTotal=@pmVrTotal,VrOtros=@pmVrOtros,VrNeto=@pmVrNeto,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraDevCau] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdProv VARCHAR(16),@pmFactura VARCHAR(20),@pmVrTotal MONEY,@pmVrOtros MONEY
,@pmVrNeto 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_TraDevCau (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Causacion,IdCiaDoc,FecDoc,IdProv,Factura,VrTotal,VrOtros,VrNeto,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmCausacion,@pmIdCiaDoc,@pmFecDoc,@pmIdProv,@pmFactura,@pmVrTotal,@pmVrOtros,@pmVrNeto,@pmModdDev,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,NumDocRef,Num_Contrato,Nit_Tercero,HoraInicial,HoraFinal
 FROM Trn_NomNov
 WHERE IdPeriodo=@pmIdPeriodo AND IdEmpleado=@pmIdEmpleado
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmNumDocRef VARCHAR(20),@pmNum_Contrato INT,@pmNit_Tercero VARCHAR(16),@pmHoraInicial DATETIME,@pmHoraFinal DATETIME,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomNov SET IdConcepto=@pmIdConcepto,Detalle=@pmDetalle,Cantidad=@pmCantidad,VrUnitario=@pmVrUnitario,VrTotal=@pmVrTotal,VrOrigen=@pmVrOrigen
,Tarifa=@pmTarifa,OrigCargue=@pmOrigCargue,NumDocRef=@pmNumDocRef,Num_Contrato=@pmNum_Contrato,FecUpdate=@pmFecUpdate,Nit_Tercero=@pmNit_Tercero,HoraInicial=@pmHoraInicial,HoraFinal=@pmHoraFinal
 WHERE IdPeriodo=@pmIdPeriodo AND IdEmpleado=@pmIdEmpleado AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,Num_Contrato,Nit_Tercero,HoraInicial,HoraFinal
 FROM Trn_NomNov
 WHERE IdPeriodo=@pmIdPeriodo AND IdEmpleado=@pmIdEmpleado
 AND IdConcepto=@pmIdConcepto
 AND OrigCargue LIKE ISNULL(@pmOrigCargue,'%')
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmNumDocRef VARCHAR(20),@pmNum_Contrato INT,@pmNit_Tercero VARCHAR(16),@pmHoraInicial DATETIME,@pmHoraFinal DATETIME,@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomNov (IdPeriodo,IdEmpleado,Item,IdConcepto,Detalle,Cantidad,VrUnitario,VrTotal,VrOrigen,Tarifa,OrigCargue,TimeSys,IdUsuario,NumDocRef,Num_Contrato,Nit_Tercero,HoraInicial,HoraFinal)
 VALUES (@pmIdPeriodo,@pmIdEmpleado,@pmItem,@pmIdConcepto,@pmDetalle,@pmCantidad,@pmVrUnitario,@pmVrTotal,@pmVrOrigen,@pmTarifa,@pmOrigCargue, @pmTimeSys,@pmIdUsuario,@pmNumDocRef,@pmNum_Contrato,@pmNit_Tercero,@pmHoraInicial,@pmHoraFinal)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_InfDiario] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_InfDiario
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@pmIdTerminal VARCHAR(4),@pmTipoOrden INT,@pmRemolque VARCHAR(10),@pmVrImpCarbono MONEY,@pmVrIvaIngProd MONEY
,@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,IdTerminal,OrigenAdd,ZonaFrontera,TipoOrden,Remolque,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,VrImpCarbono,VrIvaIngProd)
 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,@pmIdTerminal,@pmOrigenAdd,@pmZonaFrontera,@pmTipoOrden,@pmRemolque,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmVrImpCarbono,@pmVrIvaIngProd)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@pmIdTerminal VARCHAR(4),@pmTipoOrden INT,@pmRemolque VARCHAR(10),@pmVrImpCarbono MONEY,@pmVrIvaIngProd MONEY,@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,IdTerminal=@pmIdTerminal,TipoOrden=@pmTipoOrden,Remolque=@pmRemolque,VrImpCarbono=@pmVrImpCarbono,VrIvaIngProd=@pmVrIvaIngProd,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND OCargue=@pmOCargue AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrImpCarbono,VrIvaIngProd,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,TipoOrden,IdTerminal,Remolque,Anulado,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_OComb
 WHERE TipDoc=@pmTipDoc AND OCargue=@pmOCargue AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_AutTotItm] @pmtmEst VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM,COUNT(tmItem) AS ScanItm
 FROM tm_AutTot
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraOrdenLiq] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraOrdenLiq WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraOrdenLiqAnu] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_TraOrdenLiq SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraOrdenLiq] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipOds VARCHAR(3),@pmNumOrden INT,@pmIdCiaOds CHAR(2),@pmIdVehiculo VARCHAR(10),@pmIdPoseedor VARCHAR(16),@pmIdConductor VARCHAR(16),@pmTipoAfiVehic VARCHAR(10),@pmVrTotal MONEY,@pmVrCosto MONEY
,@pmVrAnticipo MONEY,@pmVrGastos MONEY,@pmVrIva MONEY,@pmVrRetencion MONEY,@pmVrReteIca MONEY,@pmVrReteCREE MONEY,@pmVrReteIva MONEY,@pmVrOtros MONEY,@pmVrDescuento MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmTarifaPago MONEY,@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmTarifaImp DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4)
,@pmTarifaIca DECIMAL(16,6),@pmTarifaRiv DECIMAL(14,4),@pmTarifaRtc DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmCodTarRtc VARCHAR(4),@pmReferencia VARCHAR(50),@pmCxPagar BIT,@pmCdPlazo VARCHAR(4),@pmFechaVence SMALLDATETIME,@pmUndMed VARCHAR(10)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4),@pmTipEgr VARCHAR(3),@pmEgreso INT,@pmIdCiaEgr CHAR(2),@pmNumCheque VARCHAR(20),@pmVrConcCargos MONEY,@pmVrConcDed MONEY,@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16)
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraOrdenLiq (TipDoc,Liquidacion,IdCia,Fecha,IdConcepto,TipOds,NumOrden,IdCiaOds,IdVehiculo,IdPoseedor,IdConductor,TipoAfiVehic,VrTotal,VrCosto,VrAnticipo,VrGastos,VrIva,VrRetencion,VrReteIca,VrReteCREE,VrReteIva,VrOtros,VrDescuento,VrNeto,Cantidad,TarifaPago,BaseImp,BaseRet,BaseIca,TarifaImp,TarifaRet,TarifaIca,TarifaRiv
,TarifaRtc,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,CodTarRtc,Referencia,CxPagar,CdPlazo,FechaVence,UndMed,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario,TipEgr,Egreso,IdCiaEgr,NumCheque,VrConcCargos,VrConcDed,CdCCosto,CdSubCos)
 VALUES (@pmTipDoc,@pmLiquidacion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipOds,@pmNumOrden,@pmIdCiaOds,@pmIdVehiculo,@pmIdPoseedor,@pmIdConductor,@pmTipoAfiVehic,@pmVrTotal,@pmVrCosto,@pmVrAnticipo,@pmVrGastos,@pmVrIva,@pmVrRetencion,@pmVrReteIca,@pmVrReteCREE,@pmVrReteIva,@pmVrOtros,@pmVrDescuento,@pmVrNeto,@pmCantidad,@pmTarifaPago,@pmBaseImp
,@pmBaseRet,@pmBaseIca,@pmTarifaImp,@pmTarifaRet,@pmTarifaIca,@pmTarifaRiv,@pmTarifaRtc,@pmCodTarIva,@pmCodTarRet,@pmCodTarIca,@pmCodTarRiv,@pmCodTarRtc,@pmReferencia,@pmCxPagar,@pmCdPlazo,@pmFechaVence,@pmUndMed,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmTipEgr,@pmEgreso,@pmIdCiaEgr,@pmNumCheque,@pmVrConcCargos,@pmVrConcDed,@pmCdCCosto,@pmCdSubCos)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraOrdenLiq] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipOds VARCHAR(3),@pmNumOrden INT,@pmIdCiaOds CHAR(2),@pmIdVehiculo VARCHAR(10),@pmIdPoseedor VARCHAR(16),@pmIdConductor VARCHAR(16),@pmTipoAfiVehic VARCHAR(10),@pmVrTotal MONEY,@pmVrCosto MONEY
,@pmVrAnticipo MONEY,@pmVrGastos MONEY,@pmVrIva MONEY,@pmVrRetencion MONEY,@pmVrReteIca MONEY,@pmVrReteCREE MONEY,@pmVrReteIva MONEY,@pmVrOtros MONEY,@pmVrDescuento MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmTarifaPago MONEY,@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmTarifaImp DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4)
,@pmTarifaIca DECIMAL(16,6),@pmTarifaRiv DECIMAL(14,4),@pmTarifaRtc DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmCodTarRtc VARCHAR(4),@pmReferencia VARCHAR(50),@pmCxPagar BIT,@pmCdPlazo VARCHAR(4),@pmFechaVence SMALLDATETIME,@pmUndMed VARCHAR(10)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4),@pmTipEgr VARCHAR(3),@pmEgreso INT,@pmIdCiaEgr CHAR(2),@pmNumCheque VARCHAR(20),@pmVrConcCargos MONEY,@pmVrConcDed MONEY,@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraOrdenLiq SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipOds=@pmTipOds,NumOrden=@pmNumOrden,IdCiaOds=@pmIdCiaOds,IdVehiculo=@pmIdVehiculo,IdPoseedor=@pmIdPoseedor,IdConductor=@pmIdConductor,TipoAfiVehic=@pmTipoAfiVehic,VrTotal=@pmVrTotal,VrCosto=@pmVrCosto,VrAnticipo=@pmVrAnticipo,VrGastos=@pmVrGastos,VrIva=@pmVrIva,VrRetencion=@pmVrRetencion
,VrReteIca=@pmVrReteIca,VrReteCREE=@pmVrReteCREE,VrReteIva=@pmVrReteIva,VrOtros=@pmVrOtros,VrDescuento=@pmVrDescuento,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,TarifaPago=@pmTarifaPago,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,TarifaImp=@pmTarifaImp,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv,TarifaRtc=@pmTarifaRtc
,CodTarIva=@pmCodTarIva,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,CodTarRtc=@pmCodTarRtc,Referencia=@pmReferencia,CxPagar=@pmCxPagar,CdPlazo=@pmCdPlazo,FechaVence=@pmFechaVence,UndMed=@pmUndMed,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,TipEgr=@pmTipEgr,Egreso=@pmEgreso,IdCiaEgr=@pmIdCiaEgr,NumCheque=@pmNumCheque,VrConcCargos=@pmVrConcCargos,VrConcDed=@pmVrConcDed,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenLiq] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Liquidacion,IdCia,Fecha,IdConcepto,TipOds,NumOrden,IdCiaOds,IdVehiculo,IdPoseedor,IdConductor,TipoAfiVehic,VrTotal,VrCosto,VrAnticipo,VrGastos,VrIva,VrRetencion,VrReteIca,VrReteCREE,VrReteIva,VrOtros,VrDescuento,VrNeto,Cantidad,TarifaPago,BaseImp,BaseRet,BaseIca,TarifaImp,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,CodTarRtc
,Referencia,CxPagar,CdPlazo,FechaVence,UndMed,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TipEgr,Egreso,IdCiaEgr,NumCheque,VrConcCargos,VrConcDed,CdCCosto,CdSubCos,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraOrdenLiq
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsDocMemo] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmComentarios VARCHAR(6500)
,@pmNota1 VARCHAR(500),@pmNota2 VARCHAR(500),@pmNota3 VARCHAR(500),@pmCantImp INT,@pmArchivoImg VARCHAR(150)
 AS 
 INSERT INTO Trn_DocMemo (TipDoc,Documento,IdCia,Comentarios,Nota1,Nota2,Nota3,CantImp,ArchivoImg)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmComentarios,@pmNota1,@pmNota2,@pmNota3,@pmCantImp,@pmArchivoImg)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryDocMemo] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Documento,IdCia,Comentarios,Nota1,Nota2,Nota3,CantImp,ArchivoImg
 FROM Trn_DocMemo
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpDocMemoImp] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 IF EXISTS (SELECT * FROM Trn_DocMemo WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia)
  BEGIN
   UPDATE Trn_DocMemo SET CantImp=ISNULL(CantImp,0)+1
   WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
  END
 ELSE
  BEGIN
   INSERT INTO Trn_DocMemo (TipDoc,Documento,IdCia,Comentarios,Nota1,Nota2,Nota3,CantImp,ArchivoImg)
   VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,'','','','',1,Null)
  END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpDocMemo] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmComentarios VARCHAR(6500)
,@pmNota1 VARCHAR(500),@pmNota2 VARCHAR(500),@pmNota3 VARCHAR(500),@pmCantImp INT,@pmArchivoImg VARCHAR(150)
 AS 
 UPDATE Trn_DocMemo SET Comentarios=@pmComentarios
,Nota1=@pmNota1,Nota2=@pmNota2,Nota3=@pmNota3,ArchivoImg=@pmArchivoImg
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_CausaVales
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCodTarArf VARCHAR(4),@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmCodTarAri VARCHAR(4),@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY,@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,CodTarArf=@pmCodTarArf,TarifaArf=@pmTarifaArf,VrAutRetFte=@pmVrAutRetFte,CodTarAri=@pmCodTarAri,TarifaAri=@pmTarifaAri,VrAutRetIca=@pmVrAutRetIca,FecUpdate=@pmFecUpdate 
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCodTarArf VARCHAR(4),@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmCodTarAri VARCHAR(4),@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY
,@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,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca)
 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,@pmCodTarArf,@pmTarifaArf,@pmVrAutRetFte,@pmCodTarAri,@pmTarifaAri,@pmVrAutRetIca)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasAge_Unf]  @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuNiifCuentasAge WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
  INSERT INTO AcuNiifCuentasAge (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 AcuNiifCuentasAge WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
 GROUP BY nAnno,nMes,IdCia,IdTercero,CodAgncia,TipoAcum
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasAge_Sel]  @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 --nit-veh
  INSERT INTO AcuNiifCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdTercero,D.IdVehiculo,'NV',0,SUM(D.VrDebito),SUM(D.VrCredito)
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='NIIF' AND P.Movimiento=1 AND P.Tercero=1 AND P.Vehiculo=1
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdTercero,D.IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuNiifCuentasAgeSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuNiifCuentasAge SET AcuNiifCuentasAge.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuNiifCuentasAge INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuNiifCuentasAge WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt AND TipoAcum=@pmTipoAcum) AS SA ON AcuNiifCuentasAge.IdCia=SA.IdCia AND AcuNiifCuentasAge.IdCuenta=SA.IdCuenta
 AND AcuNiifCuentasAge.IdTercero=SA.IdTercero AND AcuNiifCuentasAge.CodAgncia=SA.CodAgncia AND AcuNiifCuentasAge.TipoAcum=SA.TipoAcum
 WHERE AcuNiifCuentasAge.nAnno=@pmnAnno AND AcuNiifCuentasAge.nMes=@pmnMes AND AcuNiifCuentasAge.TipoAcum=@pmTipoAcum
 AND (@pmIdCia IS NULL OR AcuNiifCuentasAge.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuNiifCuentasAge.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasAge] @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 AcuNiifCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdTercero,@pmCodAgncia,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasAge_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuNiifCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdTercero,S.CodAgncia,S.TipoAcum,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuNiifCuentasAge AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt AND S.TipoAcum=@pmTipoAcum
 AND NOT EXISTS (SELECT * FROM AcuNiifCuentasAge AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes AND AC.TipoAcum=@pmTipoAcum
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdTercero=S.IdTercero AND AC.CodAgncia=S.CodAgncia)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasAge_Uni] @pmIdTercero VARCHAR(16),@pmIdTerceroDos VARCHAR(16),@pmNewTercero VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdTercero FROM AcuNiifCuentasAge WHERE IdTercero=@pmIdTercero OR IdTercero=@pmIdTerceroDos)
  INSERT INTO AcuNiifCuentasAge (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 AcuNiifCuentasAge WHERE IdTercero=@pmIdTercero OR IdTercero=@pmIdTerceroDos
 GROUP BY nAnno,nMes,IdCia,IdCuenta,CodAgncia,TipoAcum
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasAge_Veh]  @pmIdVehiculo VARCHAR(10),@pmIdVehiculoDos VARCHAR(10),@pmNewVehiculo VARCHAR(10)
 AS 
 IF EXISTS (SELECT * FROM AcuNiifCuentasAge WHERE (CodAgncia=@pmIdVehiculo OR CodAgncia=@pmIdVehiculoDos) AND TipoAcum='NV')
  INSERT INTO AcuNiifCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,@pmNewVehiculo,TipoAcum,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  FROM AcuNiifCuentasAge WHERE (CodAgncia=@pmIdVehiculo OR CodAgncia=@pmIdVehiculoDos) AND TipoAcum='NV'
  GROUP BY nAnno,nMes,IdCia,IdCuenta,IdTercero,TipoAcum
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraLiqViajes] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Liquidacion,IdCia,Fecha,IdConcepto,Modalidad,TipMuc,Manifiesto,IdCiaMuc,FechaMuc,IdVehiculo,IdConductor,IdPropietario,IdPoseedor
,NitEmpresa,VrAnticipos,VrGastos,VrCombustible,VrOtrosCred,VrDiferencia,galsComb,PesoTotal,Cantidad,Volumen,IdRuta,IdLocOrigen,IdLocDestino,Referencia,DescMcias,kmtInicial,kmtFinal
,EstFactura,TipDocRef,NumDocRef,IdCiaRef,FecDocRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,LiqMixto,Num_Ppto
,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraLiqViajes
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraLiqViajes] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmModalidad VARCHAR(10),@pmTipMuc VARCHAR(3),@pmManifiesto INT,@pmIdCiaMuc CHAR(2),@pmFechaMuc SMALLDATETIME,@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmIdPropietario VARCHAR(16),@pmIdPoseedor VARCHAR(16),@pmNitEmpresa VARCHAR(16),@pmVrAnticipos MONEY,@pmVrGastos MONEY,@pmVrCombustible MONEY
,@pmVrDiferencia MONEY,@pmgalsComb DECIMAL(14,4),@pmPesoTotal DECIMAL(14,4),@pmCantidad DECIMAL(14,4),@pmVolumen DECIMAL(14,4),@pmIdRuta VARCHAR(4),@pmIdLocOrigen VARCHAR(8),@pmIdLocDestino VARCHAR(8),@pmReferencia VARCHAR(50),@pmDescMcias VARCHAR(150),@pmkmtInicial DECIMAL(14,4),@pmkmtFinal DECIMAL(14,4),@pmEstFactura INT
,@pmTipDocRef VARCHAR(3),@pmNumDocRef INT,@pmIdCiaRef CHAR(2),@pmFecDocRef SMALLDATETIME,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmVrOtrosCred MONEY,@pmLiqMixto BIT,@pmNum_Ppto INT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraLiqViajes SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,Modalidad=@pmModalidad,TipMuc=@pmTipMuc,Manifiesto=@pmManifiesto,IdCiaMuc=@pmIdCiaMuc,FechaMuc=@pmFechaMuc,IdVehiculo=@pmIdVehiculo,IdConductor=@pmIdConductor,IdPropietario=@pmIdPropietario,IdPoseedor=@pmIdPoseedor,NitEmpresa=@pmNitEmpresa,VrAnticipos=@pmVrAnticipos,VrGastos=@pmVrGastos,VrCombustible=@pmVrCombustible,VrDiferencia=@pmVrDiferencia,galsComb=@pmgalsComb
,PesoTotal=@pmPesoTotal,Cantidad=@pmCantidad,Volumen=@pmVolumen,IdRuta=@pmIdRuta,IdLocOrigen=@pmIdLocOrigen,IdLocDestino=@pmIdLocDestino,Referencia=@pmReferencia,DescMcias=@pmDescMcias,kmtInicial=@pmkmtInicial,kmtFinal=@pmkmtFinal,EstFactura=@pmEstFactura,TipDocRef=@pmTipDocRef,NumDocRef=@pmNumDocRef,IdCiaRef=@pmIdCiaRef,FecDocRef=@pmFecDocRef,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom
,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,VrOtrosCred=@pmVrOtrosCred,LiqMixto=@pmLiqMixto,Num_Ppto=@pmNum_Ppto,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraLiqViajesAnu] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_TraLiqViajes SET  Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraLiqViajes] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmModalidad VARCHAR(10),@pmTipMuc VARCHAR(3),@pmManifiesto INT,@pmIdCiaMuc CHAR(2),@pmFechaMuc SMALLDATETIME,@pmIdVehiculo VARCHAR(10)
,@pmIdConductor VARCHAR(16),@pmIdPropietario VARCHAR(16),@pmIdPoseedor VARCHAR(16),@pmNitEmpresa VARCHAR(16),@pmVrAnticipos MONEY,@pmVrGastos MONEY,@pmVrCombustible MONEY,@pmVrDiferencia MONEY,@pmgalsComb DECIMAL(14,4),@pmPesoTotal DECIMAL(14,4),@pmCantidad DECIMAL(14,4),@pmVolumen DECIMAL(14,4)
,@pmIdRuta VARCHAR(4),@pmIdLocOrigen VARCHAR(8),@pmIdLocDestino VARCHAR(8),@pmReferencia VARCHAR(50),@pmDescMcias VARCHAR(150),@pmkmtInicial DECIMAL(14,4),@pmkmtFinal DECIMAL(14,4),@pmEstFactura INT,@pmTipDocRef VARCHAR(3),@pmNumDocRef INT,@pmIdCiaRef CHAR(2),@pmFecDocRef SMALLDATETIME
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmVrOtrosCred MONEY,@pmLiqMixto BIT,@pmNum_Ppto INT,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraLiqViajes (TipDoc,Liquidacion,IdCia,Fecha,IdConcepto,Modalidad,TipMuc,Manifiesto,IdCiaMuc,FechaMuc,IdVehiculo,IdConductor,IdPropietario,IdPoseedor,NitEmpresa,VrAnticipos,VrGastos,VrCombustible,VrOtrosCred,VrDiferencia,galsComb,PesoTotal,Cantidad,Volumen,IdRuta,IdLocOrigen,IdLocDestino,Referencia,DescMcias
,kmtInicial,kmtFinal,EstFactura,TipDocRef,NumDocRef,IdCiaRef,FecDocRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario,LiqMixto,Num_Ppto)
 VALUES (@pmTipDoc,@pmLiquidacion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmModalidad,@pmTipMuc,@pmManifiesto,@pmIdCiaMuc,@pmFechaMuc,@pmIdVehiculo,@pmIdConductor,@pmIdPropietario,@pmIdPoseedor,@pmNitEmpresa,@pmVrAnticipos,@pmVrGastos,@pmVrCombustible,@pmVrOtrosCred,@pmVrDiferencia,@pmgalsComb
,@pmPesoTotal,@pmCantidad,@pmVolumen,@pmIdRuta,@pmIdLocOrigen,@pmIdLocDestino,@pmReferencia,@pmDescMcias,@pmkmtInicial,@pmkmtFinal,@pmEstFactura,@pmTipDocRef,@pmNumDocRef,@pmIdCiaRef,@pmFecDocRef,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmLiqMixto,@pmNum_Ppto)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraLiqViajes] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraLiqViajes
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_FactNeto] @pmtmEst CHAR(2),@pmtmTipFac VARCHAR(3),@pmtmFactura INT,@pmtmIdCia CHAR(2),@pmtmItem INT,@pmtmSubTotal MONEY,@pmtmDescuento MONEY,@pmtmImpuesto MONEY,@pmtmRetencion MONEY,@pmtmReteICA MONEY,@pmtmReteIVA MONEY,@pmtmFletes MONEY,@pmtmOtros MONEY,@pmtmCargos MONEY,@pmtmOtrDcto MONEY,@pmtmCostos MONEY,@pmtmSobretasa MONEY,@pmtmImpGlobal MONEY,@pmtmFaltantes MONEY
,@pmtmAnticipos MONEY,@pmtmNeto MONEY,@pmtmImpCons MONEY,@pmtmReteCREE MONEY,@pmtmEntradas DECIMAL(16,4),@pmtmSalidas DECIMAL(16,4),@pmtmTasaNac DECIMAL(16,4),@pmtmTasaDep DECIMAL(16,4),@pmtmTasaMun DECIMAL(16,4),@pmtmRec_Costo DECIMAL(16,4),@pmtmMgenCont DECIMAL(16,4),@pmtmIvaInf DECIMAL(16,4)
 AS 
 INSERT INTO tm_FactNeto (tmEst,tmTipFac,tmFactura,tmIdCia,tmItem,tmSubTotal,tmDescuento,tmImpuesto,tmRetencion,tmReteICA,tmReteIVA,tmFletes,tmOtros,tmCargos,tmOtrDcto,tmCostos,tmSobretasa,tmImpGlobal,tmFaltantes,tmAnticipos,tmNeto,tmImpCons,tmReteCREE,tmEntradas,tmSalidas,tmTasaNac,tmTasaDep,tmTasaMun,tmRec_Costo,tmMgenCont,tmIvaInf)
 VALUES (@pmtmEst,@pmtmTipFac,@pmtmFactura,@pmtmIdCia,@pmtmItem,@pmtmSubTotal,@pmtmDescuento,@pmtmImpuesto,@pmtmRetencion,@pmtmReteICA,@pmtmReteIVA,@pmtmFletes,@pmtmOtros,@pmtmCargos,@pmtmOtrDcto,@pmtmCostos,@pmtmSobretasa,@pmtmImpGlobal,@pmtmFaltantes,@pmtmAnticipos,@pmtmNeto,@pmtmImpCons,@pmtmReteCREE,@pmtmEntradas,@pmtmSalidas,@pmtmTasaNac,@pmtmTasaDep,@pmtmTasaMun,@pmtmRec_Costo,@pmtmMgenCont,@pmtmIvaInf)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinAbonosExto] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2),@pmFecCorte SMALLDATETIME
 AS 
 SELECT TipRec,Recibo,A.IdCia AS CdCia,A.Item AS ItemRec,FecPago,A.Fecha AS FechaRec,A.TipDoc AS Tip_Doc,A.IdPrestamo AS NumPtmo,IdCiaPre,ItemPre
,TotalAbono,VrBaseCms,A.Detalle AS DetallePago,TipoAplica
--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
 WHERE A.TipDoc=@pmTipDoc AND A.FecPago BETWEEN @pmFechaIni AND @pmFechaFin AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create FUNCTION [dbo].[FuncVrAboInt]  (@pmTipDoc VARCHAR(3),@pmIdprest INT,@pmIdCia CHAR(2))--,@PmConcepto VARCHAR(10))
RETURNS MONEY
 AS  
BEGIN 
 DECLARE @vrAbonosint  MONEY
 SET  @vrAbonosint=(
 
 SELECT   ISNULL(sum(Trn_FinAbonos.TotalAbono ),0) FROM Trn_fincuotas
 LEFT join Trn_FinAbonos on 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 Trn_fincuotas.TipDoc=@pmtipdoc AND Trn_fincuotas.IdPrestamo=@pmidprest and Trn_fincuotas.IdCia =@pmIdcia and Concepto ='INTERESES'
  group by Trn_fincuotas.tipdoc,Trn_fincuotas.idprestamo,Trn_fincuotas.idcia
 
 -- COUNT(*) FROM Trn_MayAgencias WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia)
 )
 RETURN @vrAbonosint
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create FUNCTION [dbo].[FuncVrAboCap]  (@pmTipDoc VARCHAR(3),@pmIdprest INT,@pmIdCia CHAR(2))--,@PmConcepto VARCHAR(10))
RETURNS MONEY
 AS  
BEGIN 
 DECLARE @vrAbonoscap  MONEY
 SET  @vrAbonoscap=(
 
 SELECT   ISNULL(sum(Trn_FinAbonos.TotalAbono ),0) FROM Trn_fincuotas
 LEFT join Trn_FinAbonos on 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 Trn_fincuotas.TipDoc=@pmtipdoc AND Trn_fincuotas.IdPrestamo=@pmidprest and Trn_fincuotas.IdCia =@pmIdcia and Concepto ='CAPITAL'
  group by Trn_fincuotas.tipdoc,Trn_fincuotas.idprestamo,Trn_fincuotas.idcia
 
 -- COUNT(*) FROM Trn_MayAgencias WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia)
 )
 RETURN @vrAbonoscap
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncVrAbotMOR]  (@pmTipDoc VARCHAR(3),@pmIdprest INT,@pmIdCia CHAR(2))--,@PmConcepto VARCHAR(10))
RETURNS MONEY
 AS  
BEGIN 
 DECLARE @vrAbonoMor  MONEY
 SET  @vrAbonoMor=(
 
 SELECT   ISNULL(sum(Trn_FinAbonos.TotalAbono ),0) FROM Trn_fincuotas
 LEFT join Trn_FinAbonos on 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 Trn_fincuotas.TipDoc=@pmtipdoc AND Trn_fincuotas.IdPrestamo=@pmidprest and Trn_fincuotas.IdCia =@pmIdcia and Concepto ='INTMORA'
  group by Trn_fincuotas.tipdoc,Trn_fincuotas.idprestamo,Trn_fincuotas.idcia
 
 -- COUNT(*) FROM Trn_MayAgencias WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia)
 )
 RETURN @vrAbonoMor
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinCuotasSal] @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2) 
 AS 
 SELECT C.TipDoc AS TipPre,C.IdPrestamo AS NumPrest,C.IdCia AS CdCia,C.Item AS Item_Pre,VrTotal,VrAbonado,ISNULL(TotalAbo,0) AS TotAbonado
 FROM Trn_FinCuotas AS C LEFT JOIN (SELECT TipDoc,IdPrestamo,IdCiaPre,ItemPre,SUM(TotalAbono) AS TotalAbo
 FROM Trn_FinAbonos WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCiaPre=@pmIdCia
 GROUP BY TipDoc,IdPrestamo,IdCiaPre,ItemPre) AS A ON C.TipDoc=A.TipDoc  AND C.IdPrestamo=A.IdPrestamo
 AND C.IdCia=A.IdCiaPre AND C.Item=A.ItemPre
 WHERE C.TipDoc=@pmTipDoc AND C.IdPrestamo=@pmIdPrestamo AND C.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncVrAbotros]  (@pmTipDoc VARCHAR(3),@pmIdprest INT,@pmIdCia CHAR(2))--,@PmConcepto VARCHAR(10))
RETURNS MONEY
 AS  
BEGIN 
 DECLARE @vrAbonosotr  MONEY
 SET  @vrAbonosotr=(
 
 SELECT   ISNULL(sum(Trn_FinAbonos.TotalAbono ),0) FROM Trn_fincuotas
 LEFT join Trn_FinAbonos on 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 Trn_fincuotas.TipDoc=@pmtipdoc AND Trn_fincuotas.IdPrestamo=@pmidprest and Trn_fincuotas.IdCia =@pmIdcia and Concepto ='OTRO'
  group by Trn_fincuotas.tipdoc,Trn_fincuotas.idprestamo,Trn_fincuotas.idcia
 
 -- COUNT(*) FROM Trn_MayAgencias WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia)
 )
 RETURN @vrAbonosotr
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncVrAbonos]  (@pmTipDoc VARCHAR(3),@pmIdprest INT,@pmIdCia CHAR(2))--,@PmConcepto VARCHAR(10))
RETURNS MONEY
 AS  
BEGIN 
 DECLARE @vrAbonos  MONEY
 SET  @vrAbonos=(
 
 SELECT   ISNULL(sum(Trn_FinAbonos.TotalAbono ),0) FROM Trn_fincuotas
 LEFT join Trn_FinAbonos on 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 Trn_fincuotas.TipDoc=@pmtipdoc AND Trn_fincuotas.IdPrestamo=@pmidprest and Trn_fincuotas.IdCia =@pmIdcia and Concepto IN ('CAPITAL','INTERESES','SEGURO','INTMORA','OTRO')
  group by Trn_fincuotas.tipdoc,Trn_fincuotas.idprestamo,Trn_fincuotas.idcia
 
 -- COUNT(*) FROM Trn_MayAgencias WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia)
 )
 RETURN @vrAbonos
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncVrAbotSeg]  (@pmTipDoc VARCHAR(3),@pmIdprest INT,@pmIdCia CHAR(2))--,@PmConcepto VARCHAR(10))
RETURNS MONEY
 AS  
BEGIN 
 DECLARE @vrAbonosseg  MONEY
 SET  @vrAbonosseg=(
 
 SELECT   ISNULL(sum(Trn_FinAbonos.TotalAbono ),0) FROM Trn_fincuotas
 LEFT join Trn_FinAbonos on 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 Trn_fincuotas.TipDoc=@pmtipdoc AND Trn_fincuotas.IdPrestamo=@pmidprest and Trn_fincuotas.IdCia =@pmIdcia and Concepto ='SEGURO'
  group by Trn_fincuotas.tipdoc,Trn_fincuotas.idprestamo,Trn_fincuotas.idcia
 
 -- COUNT(*) FROM Trn_MayAgencias WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia)
 )
 RETURN @vrAbonosseg
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelUm] @pmUndMed VARCHAR(10)
 AS 
 DELETE FROM Sys_Um
 WHERE UndMed=@pmUndMed
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryUm] @pmUndMed VARCHAR(10)
 AS 
 SELECT UndMed,Unidad,Carga,CodUMFE
 FROM Sys_Um
 WHERE UndMed LIKE ISNULL(@pmUndMed,'%')
 ORDER BY UndMed
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpUm] @pmUndMed VARCHAR(10),@pmUnidad VARCHAR(50),@pmCarga BIT,@pmCodUMFE VARCHAR(20)
 AS 
 UPDATE Sys_Um SET Unidad=@pmUnidad,Carga=@pmCarga,CodUMFE=@pmCodUMFE
 WHERE UndMed=@pmUndMed
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsUm] @pmUndMed VARCHAR(10),@pmUnidad VARCHAR(50),@pmCarga BIT,@pmCodUMFE VARCHAR(20)
 AS 
 INSERT INTO Sys_Um (UndMed,Unidad,Carga,CodUMFE)
 VALUES (@pmUndMed,@pmUnidad,@pmCarga,@pmCodUMFE)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelOpciones] @pmIdOpc VARCHAR(3)
 AS 
 DELETE FROM adm_Opciones
 WHERE IdOpc LIKE ISNULL(@pmIdOpc,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelAfTiposDif] @pmIdTipDif VARCHAR(4)
 AS 
 DELETE FROM AfTiposDif
 WHERE IdTipDif=@pmIdTipDif
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAfTiposDif] @pmIdTipDif VARCHAR(4),@pmTipoDiferido VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO AfTiposDif (IdTipDif,TipoDiferido,Inactivo)
 VALUES (@pmIdTipDif,@pmTipoDiferido,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpAfTiposDif] @pmIdTipDif VARCHAR(4),@pmTipoDiferido VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE AfTiposDif SET TipoDiferido=@pmTipoDiferido,Inactivo=@pmInactivo
 WHERE IdTipDif=@pmIdTipDif
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTanqAforoFrac] @pmIdTanque VARCHAR(4),@pmUndAforo DECIMAL(14,4)
 AS 
 SELECT IdTanque,UndAforo,Fraccion,Milimetros
 FROM TanqAforoFrac
 WHERE IdTanque=@pmIdTanque AND UndAforo=@pmUndAforo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraRemFact_Sel] @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraRemFact (tmNumero,tmItem,tmTipoReg,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmDescripcion,tmCantidad,tmVrUnitario,tmVrCosto,tmUndTarifa,tmUndCosto,tmUnidades,tmPesoNeto,tmUndMed,tmVolumen,tmUndVol,tmCases,tmCajas,tmPalets
,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDscto,tmTarifaRet,tmVrRetFte,tmTarifaIca,tmVrReteIca,tmVrFaltante,tmRemision,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmCdMercancia,tmCdConc,tmCdCCosto,tmCdSubCos,tmNitTercero,tmCdAgencia
,tmpVehiculo,tmTipoVehic,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen,tmNitDestntario,tmDestinatario,tmDirDestino,tmIdDestino,tmCantFalt,tmUnidadFalt,tmCdTarRet,tmCdTarIca,tmCdTarDct,tmPedRem,tmCiaPed,tmEtdoFact,tmFecRemesa,tmIntCumplido
,tmVrDeclarado,tmTarifSeguro,tmVrSeguro,tmNitAsegurad,tmCostoCump,tmTipoOrd,tmNumOrden,tmIdCiaOrd,tmCantPago,tmVrAutoIca,tmRemTransb,tmCostoAdic,tmNitMucTbdo)
 SELECT @pmtmNumero,Item,TipoReg,TipRem,Remesa,IdCiaRem,ItemRem,Descripcion,Cantidad,VrUnitario,VrCosto,UndTarifa,UndCosto,Unidades,PesoNeto,UndMed,Volumen,UndVol,Cases,Cajas,Palets
,TarifaIva,VrImpuesto,TarifaDct,VrDescuento,TarifaRet,VrRetencion,TarifaIca,VrReteIca,VrFaltante,Remision,DocCliente,Referencia1,Referencia2,Referencia3,CdMercancia,CdConcepto,CdCCosto,CdSubCos,NitTercero,CdAgencia
,pVehiculo,TipoAfiVehic,'0','','',IdOrigen,'0','','',IdDestino,CantidadFalt,UnidadFalt,CodTarRet,CodTarIca,CodTarDct,NumPedRem,CiaPedRem,0,FecRemesa,0,VrDeclMcia,TarifaSeg,VrSeguroRem,NitAsegurad,0,TipDocRef,NumDocRef,IdCiaRef,CantPago,VrAutRetIca
,RemTransb,CostoAdicTbdo,NitMucTbdo
 FROM Trn_TraFacRemesas
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraFacRemesasItem] @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,Factura,IdCia,Item,TipoReg,FechaFact,TipRem,Remesa,IdCiaRem,ItemRem,FecRemesa,Descripcion,Cantidad,VrUnitario,VrCosto,UndTarifa,UndCosto,Unidades,PesoNeto,UndMed,Volumen,UndVol,Cases,Cajas,Palets
,TarifaIva,VrImpuesto,TarifaDct,VrDescuento,TarifaRet,VrRetencion,TarifaIca,VrReteIca,VrFaltante,Remision,DocCliente,Referencia1,Referencia2,Referencia3,CdMercancia,CdConcepto,CdCCosto,CdSubCos,NitTercero
,CdAgencia,pVehiculo,TipoAfiVehic,IdOrigen,IdDestino,Anulado,TipDocRef,NumDocRef,IdCiaRef,FecDocRef,CantidadFalt,UnidadFalt,CodTarRet,CodTarIca,CodTarDct,NumPedRem,CiaPedRem,VrDeclMcia,TarifaSeg,VrSeguroRem
,NitAsegurad,CantPago,VrAutRetIca,RemTransb,CostoAdicTbdo,NitMucTbdo
 FROM Trn_TraFacRemesas
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraFacRemesas] @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraFacRemesas
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraFacRemesas] @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipoReg INT,@pmFechaFact SMALLDATETIME,@pmTipRem VARCHAR(3),@pmRemesa INT,@pmIdCiaRem CHAR(2),@pmItemRem INT,@pmFecRemesa SMALLDATETIME,@pmDescripcion VARCHAR(250),@pmCantidad DECIMAL(14,4),@pmVrUnitario MONEY,@pmVrCosto MONEY,@pmUndTarifa VARCHAR(10),@pmUndCosto VARCHAR(10),@pmUnidades DECIMAL(14,4),@pmPesoNeto DECIMAL(14,4),@pmUndMed VARCHAR(10),@pmVolumen DECIMAL(14,4),@pmUndVol VARCHAR(10)
,@pmCases INT,@pmCajas INT,@pmPalets INT,@pmTarifaIva DECIMAL(14,4),@pmVrImpuesto MONEY,@pmTarifaDct DECIMAL(14,4),@pmVrDescuento MONEY,@pmTarifaRet DECIMAL(14,4),@pmVrRetencion MONEY,@pmTarifaIca DECIMAL(14,4),@pmVrReteIca MONEY,@pmVrFaltante MONEY,@pmRemision DECIMAL(18,2),@pmDocCliente VARCHAR(30),@pmReferencia1 VARCHAR(50),@pmReferencia2 VARCHAR(50),@pmReferencia3 VARCHAR(50),@pmCdMercancia VARCHAR(16),@pmCdConcepto VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16)
,@pmNitTercero VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmTipoAfiVehic VARCHAR(10),@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmAnulado INT,@pmTipDocRef VARCHAR(3),@pmNumDocRef INT,@pmIdCiaRef CHAR(2),@pmFecDocRef SMALLDATETIME,@pmCantidadFalt DECIMAL(14,4),@pmUnidadFalt VARCHAR(10),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarDct VARCHAR(4),@pmNumPedRem INT,@pmCiaPedRem CHAR(2),@pmVrDeclMcia MONEY,@pmTarifaSeg DECIMAL(14,4),@pmVrSeguroRem MONEY
,@pmNitAsegurad VARCHAR(16),@pmCantPago DECIMAL(14,4),@pmVrAutRetIca MONEY,@pmRemTransb INT,@pmCostoAdicTbdo MONEY,@pmNitMucTbdo VARCHAR(16)
 AS 
 INSERT INTO Trn_TraFacRemesas (TipDoc,Factura,IdCia,Item,TipoReg,FechaFact,TipRem,Remesa,IdCiaRem,ItemRem,FecRemesa,Descripcion,Cantidad,VrUnitario,VrCosto,UndTarifa,UndCosto,Unidades,PesoNeto,UndMed,Volumen,UndVol,Cases,Cajas,Palets,TarifaIva,VrImpuesto,TarifaDct,VrDescuento,TarifaRet,VrRetencion,TarifaIca,VrReteIca,VrFaltante,Remision,DocCliente,Referencia1,Referencia2,Referencia3,CdMercancia,CdConcepto,CdCCosto,CdSubCos,NitTercero,CdAgencia,pVehiculo,TipoAfiVehic,IdOrigen,IdDestino
,Anulado,TipDocRef,NumDocRef,IdCiaRef,FecDocRef,CantidadFalt,UnidadFalt,CodTarRet,CodTarIca,CodTarDct,NumPedRem,CiaPedRem,VrDeclMcia,TarifaSeg,VrSeguroRem,NitAsegurad,CantPago,VrAutRetIca,RemTransb,CostoAdicTbdo,NitMucTbdo)
 VALUES (@pmTipDoc,@pmFactura,@pmIdCia,@pmItem,@pmTipoReg,@pmFechaFact,@pmTipRem,@pmRemesa,@pmIdCiaRem,@pmItemRem,@pmFecRemesa,@pmDescripcion,@pmCantidad,@pmVrUnitario,@pmVrCosto,@pmUndTarifa,@pmUndCosto,@pmUnidades,@pmPesoNeto,@pmUndMed,@pmVolumen,@pmUndVol,@pmCases,@pmCajas,@pmPalets,@pmTarifaIva,@pmVrImpuesto,@pmTarifaDct,@pmVrDescuento,@pmTarifaRet,@pmVrRetencion,@pmTarifaIca,@pmVrReteIca,@pmVrFaltante,@pmRemision,@pmDocCliente,@pmReferencia1,@pmReferencia2,@pmReferencia3
,@pmCdMercancia,@pmCdConcepto,@pmCdCCosto,@pmCdSubCos,@pmNitTercero,@pmCdAgencia,@pmpVehiculo,@pmTipoAfiVehic,@pmIdOrigen,@pmIdDestino,@pmAnulado,@pmTipDocRef,@pmNumDocRef,@pmIdCiaRef,@pmFecDocRef,@pmCantidadFalt,@pmUnidadFalt,@pmCodTarRet,@pmCodTarIca,@pmCodTarDct,@pmNumPedRem,@pmCiaPedRem,@pmVrDeclMcia,@pmTarifaSeg,@pmVrSeguroRem,@pmNitAsegurad,@pmCantPago,@pmVrAutRetIca,@pmRemTransb,@pmCostoAdicTbdo,@pmNitMucTbdo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraRemesa] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmFecEntrega SMALLDATETIME,@pmHoraLlegada SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdClieFact VARCHAR(16),@pmIdRemitente VARCHAR(16),@pmIdDestinatario VARCHAR(16),@pmIdLocOrigen VARCHAR(8),@pmIdLocDestino VARCHAR(8),@pmIdLocFletes VARCHAR(8),@pmIdVehiculo VARCHAR(10)
,@pmIdConductor VARCHAR(16),@pmnRemolque VARCHAR(10),@pmTipoAfiVehic VARCHAR(10),@pmModalidad VARCHAR(10),@pmTipoTarifa VARCHAR(10),@pmVrCobro MONEY,@pmVrPagos MONEY,@pmVrFletes MONEY,@pmVrCargue MONEY,@pmVrDesCargue MONEY,@pmVrEscolta MONEY,@pmVrDevContdor MONEY,@pmVrTraUrbano MONEY,@pmVrEmbalajes MONEY,@pmVrCargos MONEY,@pmVrDctos MONEY,@pmVrDeclarado MONEY,@pmVrSeguro MONEY,@pmCantidad DECIMAL(14,4)
,@pmPesoTotal DECIMAL(14,4),@pmItems INT,@pmIdMneda VARCHAR(5),@pmVrTasa DECIMAL(14,4),@pmNumPedido INT,@pmIdCiaPed CHAR(2),@pmFechaPed SMALLDATETIME,@pmTipDcm VARCHAR(3),@pmNumDocmto INT,@pmIdCiaDcm CHAR(2),@pmFechaDcm SMALLDATETIME,@pmNumManif INT,@pmIdCiaManif CHAR(2),@pmEstCumplido INT,@pmEstFactura INT,@pmCdConcepto VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmCdRutaTarif VARCHAR(4),@pmSerieGuia CHAR(2),@pmNumGuia INT,@pmCdForma VARCHAR(4),@pmNumCausac INT,@pmCdCiaCausac CHAR(2),@pmEdoCausac INT,@pmCdBodega VARCHAR(4),@pmVigenciaOdc INT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraRemesa SET Fecha=@pmFecha,FecDespacho=@pmFecDespacho,FecEntrega=@pmFecEntrega,HoraLlegada=@pmHoraLlegada,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdClieFact=@pmIdClieFact,IdRemitente=@pmIdRemitente,IdDestinatario=@pmIdDestinatario,IdLocOrigen=@pmIdLocOrigen,IdLocDestino=@pmIdLocDestino,IdLocFletes=@pmIdLocFletes,IdVehiculo=@pmIdVehiculo,IdConductor=@pmIdConductor,nRemolque=@pmnRemolque,TipoAfiVehic=@pmTipoAfiVehic,Modalidad=@pmModalidad,TipoTarifa=@pmTipoTarifa
,VrCobro=@pmVrCobro,VrPagos=@pmVrPagos,VrFletes=@pmVrFletes,VrCargue=@pmVrCargue,VrDesCargue=@pmVrDesCargue,VrEscolta=@pmVrEscolta,VrDevContdor=@pmVrDevContdor,VrTraUrbano=@pmVrTraUrbano,VrEmbalajes=@pmVrEmbalajes,VrCargos=@pmVrCargos,VrDctos=@pmVrDctos,VrDeclarado=@pmVrDeclarado,VrSeguro=@pmVrSeguro,Cantidad=@pmCantidad,PesoTotal=@pmPesoTotal,Items=@pmItems,IdMneda=@pmIdMneda,VrTasa=@pmVrTasa,NumPedido=@pmNumPedido,IdCiaPed=@pmIdCiaPed,FechaPed=@pmFechaPed
,TipDcm=@pmTipDcm,NumDocmto=@pmNumDocmto,IdCiaDcm=@pmIdCiaDcm,FechaDcm=@pmFechaDcm,NumManif=@pmNumManif,IdCiaManif=@pmIdCiaManif,EstCumplido=@pmEstCumplido,EstFactura=@pmEstFactura,CdConcepto=@pmCdConcepto,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,CdRutaTarif=@pmCdRutaTarif,SerieGuia=@pmSerieGuia,NumGuia=@pmNumGuia,CdForma=@pmCdForma
,NumCausac=@pmNumCausac,CdCiaCausac=@pmCdCiaCausac,EdoCausac=@pmEdoCausac,CdBodega=@pmCdBodega,VigenciaOdc=@pmVigenciaOdc,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraRemesa] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmFecEntrega SMALLDATETIME,@pmHoraLlegada SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdClieFact VARCHAR(16),@pmIdRemitente VARCHAR(16),@pmIdDestinatario VARCHAR(16),@pmIdLocOrigen VARCHAR(8),@pmIdLocDestino VARCHAR(8)
,@pmIdLocFletes VARCHAR(8),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmnRemolque VARCHAR(10),@pmTipoAfiVehic VARCHAR(10),@pmModalidad VARCHAR(10),@pmTipoTarifa VARCHAR(10),@pmVrCobro MONEY,@pmVrPagos MONEY,@pmVrFletes MONEY,@pmVrCargue MONEY,@pmVrDesCargue MONEY,@pmVrEscolta MONEY,@pmVrDevContdor MONEY,@pmVrTraUrbano MONEY,@pmVrEmbalajes MONEY,@pmVrCargos MONEY
,@pmVrDctos MONEY,@pmVrDeclarado MONEY,@pmVrSeguro MONEY,@pmCantidad DECIMAL(14,4),@pmPesoTotal DECIMAL(14,4),@pmItems INT,@pmIdMneda VARCHAR(5),@pmVrTasa DECIMAL(14,4),@pmNumPedido INT,@pmIdCiaPed CHAR(2),@pmFechaPed SMALLDATETIME,@pmTipDcm VARCHAR(3),@pmNumDocmto INT,@pmIdCiaDcm CHAR(2),@pmFechaDcm SMALLDATETIME,@pmNumManif INT,@pmIdCiaManif CHAR(2),@pmEstCumplido INT
,@pmEstFactura INT,@pmCdConcepto VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmCdRutaTarif VARCHAR(4),@pmSerieGuia CHAR(2),@pmNumGuia INT,@pmCdForma VARCHAR(4),@pmNumCausac INT,@pmCdCiaCausac CHAR(2),@pmEdoCausac INT,@pmCdBodega VARCHAR(4),@pmVigenciaOdc INT
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraRemesa (TipDoc,NumOrden,IdCia,Fecha,FecDespacho,FecEntrega,HoraLlegada,IdCliente,IdAgencia,IdClieFact,IdRemitente,IdDestinatario,IdLocOrigen,IdLocDestino,IdLocFletes,IdVehiculo,IdConductor,nRemolque,TipoAfiVehic,Modalidad,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos,VrDeclarado,VrSeguro,Cantidad,PesoTotal,Items,IdMneda,VrTasa,NumPedido,IdCiaPed,FechaPed
,TipDcm,NumDocmto,IdCiaDcm,FechaDcm,NumManif,IdCiaManif,EstCumplido,EstFactura,CdConcepto,CdRutaTarif,SerieGuia,NumGuia,CdForma,NumCausac,CdCiaCausac,EdoCausac,CdBodega,TipCom,Comprobante,IdCiaCom,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,VigenciaOdc)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmFecha,@pmFecDespacho,@pmFecEntrega,@pmHoraLlegada,@pmIdCliente,@pmIdAgencia,@pmIdClieFact,@pmIdRemitente,@pmIdDestinatario,@pmIdLocOrigen,@pmIdLocDestino,@pmIdLocFletes,@pmIdVehiculo,@pmIdConductor,@pmnRemolque,@pmTipoAfiVehic,@pmModalidad,@pmTipoTarifa,@pmVrCobro,@pmVrPagos,@pmVrFletes,@pmVrCargue,@pmVrDesCargue,@pmVrEscolta,@pmVrDevContdor,@pmVrTraUrbano
,@pmVrEmbalajes,@pmVrCargos,@pmVrDctos,@pmVrDeclarado,@pmVrSeguro,@pmCantidad,@pmPesoTotal,@pmItems,@pmIdMneda,@pmVrTasa,@pmNumPedido,@pmIdCiaPed,@pmFechaPed,@pmTipDcm,@pmNumDocmto,@pmIdCiaDcm,@pmFechaDcm,@pmNumManif,@pmIdCiaManif,@pmEstCumplido,@pmEstFactura,@pmCdConcepto,@pmCdRutaTarif,@pmSerieGuia,@pmNumGuia,@pmCdForma,@pmNumCausac,@pmCdCiaCausac,@pmEdoCausac,@pmCdBodega,@pmTipCom,@pmComprobante,@pmIdCiaCom
,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmVigenciaOdc)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraRemesa] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumOrden,IdCia,Fecha,FecDespacho,FecEntrega,HoraLlegada,IdCliente,IdAgencia,IdClieFact,IdRemitente,IdDestinatario,IdLocOrigen,IdLocDestino,IdLocFletes,IdVehiculo,IdConductor,nRemolque,TipoAfiVehic,Modalidad,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue
,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos,VrDeclarado,VrSeguro,Cantidad,PesoTotal,Items,IdMneda,VrTasa,NumPedido,IdCiaPed,FechaPed,TipDcm,NumDocmto,IdCiaDcm,FechaDcm,NumManif,IdCiaManif,EstCumplido,EstFactura,CdConcepto,CdRutaTarif
,SerieGuia,NumGuia,CdForma,NumCausac,CdCiaCausac,EdoCausac,CdBodega,TipCom,Comprobante,IdCiaCom,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario,VigenciaOdc
 FROM Trn_TraRemesa
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,DiasPlazo
 FROM Trn_FinFacturas
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmDiasPlazo INT
 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,DiasPlazo)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmItem,@pmNumFactura,@pmFecha,@pmFechaVence,@pmVrFactura,@pmVrInteres,@pmDiasLiq
,@pmTasaEfeMes,@pmTasaEfeDia,@pmFechaPago,@pmItemPago,@pmFechaLiq,@pmFecInicio,@pmFecFinal,@pmVrAbono,@pmFacturado,@pmTipFac,@pmFacturaInt,@pmCdCiaFac,@pmFecFactura,@pmDiasPlazo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrySAutorizaDso]
 AS 
  SELECT IdConcepto,ConceptoAut,ConceptoAut+' '+IdConcepto AS DsCon
  FROM Sys_Autoriza
  WHERE Inactivo=0
  ORDER BY ConceptoAut
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelSAutoriza] @pmIdConcepto VARCHAR(4)
 AS 
 DELETE FROM Sys_Autoriza
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsSAutoriza] @pmIdConcepto VARCHAR(4),@pmConceptoAut VARCHAR(150),@pmInactivo BIT
 AS 
 INSERT INTO Sys_Autoriza (IdConcepto,ConceptoAut,Inactivo)
 VALUES (@pmIdConcepto,@pmConceptoAut,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpSAutoriza] @pmIdConcepto VARCHAR(4),@pmConceptoAut VARCHAR(150),@pmInactivo BIT
 AS 
 UPDATE Sys_Autoriza SET ConceptoAut=@pmConceptoAut,Inactivo=@pmInactivo
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMudOrdenVeh] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdVehiculo VARCHAR(10),@pmTipoAfiVehic VARCHAR(10)
,@pmNitEmpresa VARCHAR(16),@pmNomEmpresa VARCHAR(150),@pmnRemolque VARCHAR(10),@pmIdConductor VARCHAR(16),@pmIdCondAux VARCHAR(16)
,@pmTipDocRef VARCHAR(3),@pmNumDocRef INT,@pmCdCiaRef CHAR(2),@pmCdTipoOper VARCHAR(4),@pmTipoIngreso VARCHAR(10),@pmTarifaIng DECIMAL(14,4),@pmVrIngreso MONEY
 AS 
 INSERT INTO Trn_MudOrdenVeh (TipDoc,NumOrden,IdCia,Item,IdVehiculo,TipoAfiVehic,NitEmpresa,NomEmpresa,nRemolque,IdConductor,IdCondAux,TipDocRef,NumDocRef,CdCiaRef,CdTipoOper,TipoIngreso,TarifaIng,VrIngreso)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmItem,@pmIdVehiculo,@pmTipoAfiVehic,@pmNitEmpresa,@pmNomEmpresa,@pmnRemolque,@pmIdConductor,@pmIdCondAux,@pmTipDocRef,@pmNumDocRef,@pmCdCiaRef,@pmCdTipoOper,@pmTipoIngreso,@pmTarifaIng,@pmVrIngreso)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 Semstre=0 AND Anulado=0 AND IdEmpleado=@pmIdEmpleado
 AND Fecha>=@pmFechLiq
 ORDER BY L.Numero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomLiquidaId]  @pmTipoLiq VARCHAR(3)
AS
 SELECT MAX(Numero) AS MXNUM FROM Trn_NomLiquida
 WHERE TipoLiq=@pmTipoLiq
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelNomLiquida] @pmTipoLiq VARCHAR(3),@pmNumero INT
 AS 
 DELETE FROM Trn_NomLiquida
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTipoActualiza VARCHAR(10),@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,TipoActualiza=@pmTipoActualiza
 WHERE Numero=@pmNumero AND IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTipoActualiza VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomSueldos (Numero,IdEmpleado,Fecha,NContrato,VrSueAnterior,VrSueNuevo,VrAtAnterior,VrAtNuevo,MetodoUp,Reemplzado,FecUltAumto,CritSelect,Observacion,TipoActualiza,TimeSys,IdUsuario)
 VALUES (@pmNumero,@pmIdEmpleado,@pmFecha,@pmNContrato,@pmVrSueAnterior,@pmVrSueNuevo,@pmVrAtAnterior,@pmVrAtNuevo,@pmMetodoUp,@pmReemplzado,@pmFecUltAumto
,@pmCritSelect,@pmObservacion,@pmTipoActualiza,@pmTimeSys,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNomSueldos] @pmNumero INT,@pmIdEmpleado VARCHAR(16)
 AS 
 SELECT Numero,IdEmpleado,Fecha,NContrato,VrSueAnterior,VrSueNuevo,VrAtAnterior,VrAtNuevo,MetodoUp,Reemplzado
,FecUltAumto,CritSelect,Observacion,TipoActualiza,TimeSys,IdUsuario
 FROM Trn_NomSueldos
 WHERE Numero=@pmNumero AND IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 ORDER BY IdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmtmCdGrupo VARCHAR(4),@pmtmTipoContab VARCHAR(10)
 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,tmCdGrupo,tmTipoContab)
 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,@pmtmCdGrupo,@pmtmTipoContab)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevReexDoc] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,IdEmpTrans,ModdDev
 FROM Trn_TraDevReex
 WHERE  TipDoc=@pmTipDoc  AND Causacion=@pmCausacion AND IdCiaDoc=@pmIdCiaDoc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevReex] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Causacion,IdCiaDoc,FecDoc,IdEmpTrans,NumFactura,FecFactura,VrTotal,VrImpuesto,VrRetencion,VrReteIca,VrOtrosCargos,VrOtrosDctos,VrNeto
,PesoTotal,CxPagar,TipCom,Comprobante,IdCiaCom,ModdDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraDevReex
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraDevReex] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdEmpTrans VARCHAR(16),@pmNumFactura VARCHAR(15)
,@pmFecFactura SMALLDATETIME,@pmVrTotal MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteIca MONEY,@pmVrOtrosCargos MONEY,@pmVrOtrosDctos MONEY,@pmVrNeto MONEY,@pmPesoTotal DECIMAL(14,4),@pmCxPagar BIT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmModdDev VARCHAR(10),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraDevReex SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Causacion=@pmCausacion,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdEmpTrans=@pmIdEmpTrans,NumFactura=@pmNumFactura,FecFactura=@pmFecFactura,VrTotal=@pmVrTotal,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteIca=@pmVrReteIca
,VrOtrosCargos=@pmVrOtrosCargos,VrOtrosDctos=@pmVrOtrosDctos,VrNeto=@pmVrNeto,PesoTotal=@pmPesoTotal,CxPagar=@pmCxPagar,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,ModdDev=@pmModdDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraDevReex] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdEmpTrans VARCHAR(16),@pmNumFactura VARCHAR(15)
,@pmFecFactura SMALLDATETIME,@pmVrTotal MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteIca MONEY,@pmVrOtrosCargos MONEY,@pmVrOtrosDctos MONEY,@pmVrNeto MONEY,@pmPesoTotal DECIMAL(14,4),@pmCxPagar BIT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmModdDev VARCHAR(10),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraDevReex (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Causacion,IdCiaDoc,FecDoc,IdEmpTrans,NumFactura,FecFactura,VrTotal,VrImpuesto,VrRetencion,VrReteIca,VrOtrosCargos,VrOtrosDctos,VrNeto,PesoTotal,CxPagar,TipCom,Comprobante,IdCiaCom,ModdDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmCausacion,@pmIdCiaDoc,@pmFecDoc,@pmIdEmpTrans,@pmNumFactura,@pmFecFactura,@pmVrTotal,@pmVrImpuesto,@pmVrRetencion,@pmVrReteIca,@pmVrOtrosCargos,@pmVrOtrosDctos,@pmVrNeto,@pmPesoTotal,@pmCxPagar,@pmTipCom,@pmComprobante,@pmIdCiaCom
,@pmModdDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraDevReex] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraDevReex
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,@pmTipoNiif VARCHAR(10)
,@pmCodCueNiif VARCHAR(16),@pmVrPrespNiif MONEY,@pmSAnteriorNiif MONEY,@pmDebitosNiif MONEY,@pmCreditosNiif MONEY,@pmCdUsuario VARCHAR(11),@pmFecUpdate SMALLDATETIME
 AS 
 INSERT INTO Trn_PresDetalle (Numero,IdCia,Item,IdCuenta,IdCCosto,IdSubCos,VrPresupuesto,SaldoAnterior,TotalDebitos,TotalCreditos,TipoNiif,CodCueNiif,VrPrespNiif,SAnteriorNiif,DebitosNiif,CreditosNiif,CdUsuario,FecUpdate)
 VALUES (@pmNumero,@pmIdCia,@pmItem,@pmIdCuenta,@pmIdCCosto,@pmIdSubCos,@pmVrPresupuesto,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos,@pmTipoNiif,@pmCodCueNiif,@pmVrPrespNiif,@pmSAnteriorNiif,@pmDebitosNiif,@pmCreditosNiif,@pmCdUsuario,@pmFecUpdate)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmTarifa,tmTipoTarif,tmCodConc,tmCodTarif)
 SELECT @pmtmNumero,Item,ISNULL(TipoNiif,'0'),IdCuenta,VrPresupuesto,'PRESUPUESTO','0','0',IdCCosto,IdSubCos
,'0',VrPrespNiif,'','','00',0,FecUpdate,ISNULL(CodCueNiif,''),'',CdUsuario,0,'$','',''
 FROM Trn_PresDetalle
 WHERE Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoInf,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Disciplinario
 WHERE TipDoc=@pmTipDoc AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTipoInf VARCHAR(10),@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,TipoInf=@pmTipoInf
 WHERE TipDoc=@pmTipDoc AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTipoInf VARCHAR(10)
,@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,TipoInf,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,@pmTipoInf,@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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncComDetalleBasesCcSub] (@pmIdCuenta VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null)
RETURNS DECIMAL(16,4)
 AS  
BEGIN 
 DECLARE @BaseGrav DECIMAL(16,4)
 SET @BaseGrav=(SELECT SUM(VrBase) FROM Trn_ComDetalle
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCuenta=@pmIdCuenta AND IdCCosto=@pmIdCCosto 
 AND IdSubCos=@pmIdSubCos AND IdCia LIKE ISNULL(@pmIdCia,'%%'))
 RETURN @BaseGrav
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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=1 AND Tercero=1 AND Agencia=1
  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  IdCia,D.IdCuenta,IdTercero,CodAgncia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncComDetalleBasesCue] (@pmIdCuenta VARCHAR(16),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null)
RETURNS DECIMAL(16,4)
 AS  
BEGIN 
 DECLARE @BaseGrav DECIMAL(16,4)
 SET @BaseGrav=(SELECT SUM(VrBase) FROM Trn_ComDetalle
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCuenta=@pmIdCuenta AND IdCia LIKE ISNULL(@pmIdCia,'%%'))
 RETURN @BaseGrav
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncComDetalleBasesNitCc] (@pmIdCuenta VARCHAR(16),@pmIdTercero VARCHAR(16)
,@pmIdCCosto VARCHAR(16),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null)
RETURNS DECIMAL(16,4)
 AS  
BEGIN 
 DECLARE @BaseGrav DECIMAL(16,4)
 SET @BaseGrav=(SELECT SUM(VrBase) FROM Trn_ComDetalle
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCuenta=@pmIdCuenta AND IdTercero=@pmIdTercero
 AND IdCCosto=@pmIdCCosto AND IdCia LIKE ISNULL(@pmIdCia,'%%'))
 RETURN @BaseGrav
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@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),@pmCdConcTrib VARCHAR(4),@pmCdTarifTrib VARCHAR(4),@pmNumEstablec VARCHAR(20),@pmPtoEmision VARCHAR(20),@pmNum_Autoriza VARCHAR(50),@pmFechAutoriza SMALLDATETIME,@pmCdCuentaNiif VARCHAR(16)
 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,CdConcTrib,CdTarifTrib,NumEstablec,PtoEmision,Num_Autoriza,FechAutoriza,CdCuentaNiif)
 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,@pmCdConcTrib,@pmCdTarifTrib,@pmNumEstablec,@pmPtoEmision,@pmNum_Autoriza,@pmFechAutoriza,@pmCdCuentaNiif)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CdConcTrib,CdTarifTrib,NumEstablec,PtoEmision,Num_Autoriza,FechAutoriza,CdCuentaNiif
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@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
,@pmCdConcTrib VARCHAR(4),@pmCdTarifTrib VARCHAR(4),@pmNumEstablec VARCHAR(20),@pmPtoEmision VARCHAR(20),@pmNum_Autoriza VARCHAR(50),@pmFechAutoriza SMALLDATETIME,@pmCdCuentaNiif VARCHAR(16)
 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
,CdConcTrib=@pmCdConcTrib,CdTarifTrib=@pmCdTarifTrib,NumEstablec=@pmNumEstablec,PtoEmision=@pmPtoEmision,Num_Autoriza=@pmNum_Autoriza,FechAutoriza=@pmFechAutoriza,CdCuentaNiif=@pmCdCuentaNiif
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncComDetalleBasesNit] (@pmIdCuenta VARCHAR(16),@pmIdTercero VARCHAR(16)
,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null)
RETURNS DECIMAL(16,4)
 AS  
BEGIN 
 DECLARE @BaseGrav DECIMAL(16,4)
 SET @BaseGrav=(SELECT SUM(VrBase) FROM Trn_ComDetalle
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCuenta=@pmIdCuenta AND IdTercero=@pmIdTercero
 AND IdCia LIKE ISNULL(@pmIdCia,'%%'))
 RETURN @BaseGrav
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncComDetalleBasesCc] (@pmIdCuenta VARCHAR(16),@pmIdCCosto VARCHAR(16)
,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null)
RETURNS DECIMAL(16,4)
 AS  
BEGIN 
 DECLARE @BaseGrav DECIMAL(16,4)
 SET @BaseGrav=(SELECT SUM(VrBase) FROM Trn_ComDetalle
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCuenta=@pmIdCuenta AND IdCCosto=@pmIdCCosto 
 AND IdCia LIKE ISNULL(@pmIdCia,'%%'))
 RETURN @BaseGrav
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryComprobantesNit] @pmTipCom VARCHAR(3),@pmComprobanteIni INT,@pmComprobanteFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipCom AS CdTipComp,C.Comprobante AS NumComp,C.IdCia AS CdCia
,D.IdCuenta AS CdCuenta,NomCuenta,D.IdTercero AS NitTercero,DT.RazonSocial AS NomTercero,SUM(VrCredito) AS TotalCre
 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 Puc AS P ON D.IdCuenta=P.IdCuenta  LEFT JOIN Terceros AS DT ON D.IdTercero=DT.IdTercero 
 WHERE C.TipCom=@pmTipCom AND C.Comprobante BETWEEN @pmComprobanteIni AND @pmComprobanteFin AND C.IdCia=@pmIdCia
 AND VrCredito>0
 GROUP BY  C.TipCom,C.Comprobante,C.IdCia,D.IdCuenta,NomCuenta,D.IdTercero,DT.RazonSocial
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncComDetalleBasesNitCcSub] (@pmIdCuenta VARCHAR(16),@pmIdTercero VARCHAR(16),@pmIdCCosto VARCHAR(16)
,@pmIdSubCos VARCHAR(16),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null)
RETURNS DECIMAL(16,4)
 AS  
BEGIN 
 DECLARE @BaseGrav DECIMAL(16,4)
 SET @BaseGrav=(SELECT SUM(VrBase) FROM Trn_ComDetalle
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCuenta=@pmIdCuenta AND IdTercero=@pmIdTercero AND IdCCosto=@pmIdCCosto 
 AND IdSubCos=@pmIdSubCos AND IdCia LIKE ISNULL(@pmIdCia,'%%'))
 RETURN @BaseGrav
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryPaises] @pmIdPais VARCHAR(8)
 AS 
 SELECT IdPais,CodISO,NombrePais,IdMneda
 FROM adm_Paises
 WHERE IdPais=@pmIdPais
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPaises] @pmIdPais VARCHAR(8)
 AS 
 DELETE FROM adm_Paises
 WHERE IdPais=@pmIdPais
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPaisesIso] @pmCodISO VARCHAR(3)
 AS 
 SELECT IdPais,CodISO,NombrePais,IdMneda
 FROM adm_Paises
 WHERE CodISO=@pmCodISO
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdOrdenTallasFmt] @pmTipDoc VARCHAR(3),@pmNumOrdenIni INT,@pmNumOrdenFin INT,@pmIdCia CHAR(2)
AS
SELECT TipDoc,NumOrden,IdCia,Item,ItemOrden,NumTalla,Cantidad,Terminado,Defectuoso,Rechazado,CantEntrada
FROM Trn_ProdOrdenTallas
WHERE TipDoc=@pmTipDoc AND NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin
AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdOrdenTallasLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS
SELECT TL.TipDoc,TL.NumOrden,TL.IdCia,O.Fecha,TL.Item,TL.ItemOrden,TL.NumTalla,TL.Cantidad,TL.Terminado,TL.Defectuoso,TL.Rechazado,TL.CantEntrada
FROM Trn_ProdOrdenTallas AS TL INNER JOIN Trn_ProdOrden AS O ON TL.TipDoc=O.TipDoc AND TL.NumOrden=O.NumOrden AND TL.IdCia=O.IdCia
WHERE TL.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
AND (TL.IdCia=@pmIdCia OR @pmIdCia IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrySubGrupos] @pmIdSubgrupo VARCHAR(8)
 AS 
 SELECT IdSubgrupo,Subgrupo,IdGrupo,CdTarIva,CdTarAut,CdTarRet,CdTarIca
,TipoInvent,CdTarifImv,CdTarifIco,CdTarIvp,Inactivo
 FROM SubGrupos
 WHERE IdSubgrupo=@pmIdSubgrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCdTarifImv VARCHAR(4),@pmCdTarifIco VARCHAR(4),@pmCdTarIvp VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE SubGrupos SET Subgrupo=@pmSubgrupo,IdGrupo=@pmIdGrupo,CdTarIva=@pmCdTarIva
,CdTarAut=@pmCdTarAut,CdTarRet=@pmCdTarRet,CdTarIca=@pmCdTarIca
,TipoInvent=@pmTipoInvent,Inactivo=@pmInactivo,CdTarifImv=@pmCdTarifImv,CdTarifIco=@pmCdTarifIco,CdTarIvp=@pmCdTarIvp
 WHERE IdSubgrupo=@pmIdSubgrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMayBonifLta] @pmNContratoIni INT,@pmNContratoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,TipoProd,VrInicial,VrFinal,Unidad,Periodo,IntvExcluido,TipoTarifa,Tarifa,TipoMargen
,CdSubgrupo,Subgrupo,IntvGlobal,IvaIncluido,NContrato,IdCia
 FROM Trn_MayBonif  AS  B  LEFT JOIN SubGrupos AS S ON B.CdSubgrupo=S.IdSubgrupo
 WHERE NContrato BETWEEN @pmNContratoIni AND @pmNContratoFin AND  IdCia=@pmIdCia  
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,CdTarifImv,TM.Tarifa AS TarfImv,TM.Simbolo AS SimbImv
,CdTarifIco,TIC.Tarifa AS TarfIco,TIC.Simbolo AS SimbIco
 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
 LEFT JOIN Tablapor AS TM ON S.CdTarifImv=TM.IdTarifa
 LEFT JOIN Tablapor AS TIC ON S.CdTarifIco=TIC.IdTarifa
 WHERE IdSubgrupo=@pmIdSubgrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCdTarifImv VARCHAR(4),@pmCdTarifIco VARCHAR(4),@pmCdTarIvp VARCHAR(4),@pmInactivo BIT
 AS 
 INSERT INTO SubGrupos (IdSubgrupo,Subgrupo,IdGrupo,CdTarIva,CdTarAut,CdTarRet,CdTarIca,TipoInvent,Inactivo,CdTarifImv,CdTarifIco,CdTarIvp)
 VALUES (@pmIdSubgrupo,@pmSubgrupo,@pmIdGrupo,@pmCdTarIva,@pmCdTarAut,@pmCdTarRet,@pmCdTarIca,@pmTipoInvent,@pmInactivo,@pmCdTarifImv,@pmCdTarifIco,@pmCdTarIvp)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,@pmCodRetCom VARCHAR(4),@pmCupoElectcom DECIMAL(14,4),@pmAutoIca BIT,@pmCodRetIca VARCHAR(4),@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,CodRetCom=@pmCodRetCom,CupoElectcom=@pmCupoElectcom,AutoIca=@pmAutoIca,CodRetIca=@pmCodRetIca,FechaUpdate=@pmFechaUpdate
 WHERE IdProv=@pmIdProv
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,@pmCodRetCom VARCHAR(4),@pmCupoElectcom DECIMAL(14,4),@pmAutoIca BIT,@pmCodRetIca VARCHAR(4),@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,CodRetCom,CupoElectcom,AutoIca,CodRetIca)
 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,@pmCodRetCom,@pmCupoElectcom,@pmAutoIca,@pmCodRetIca)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CodRetCom,CupoElectcom,AutoIca,CodRetIca
 FROM TercProvee
 WHERE IdProv=@pmIdProv
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CodRetCom,CupoElectcom,AutoIca,CodRetIca)
 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,CodRetCom,CupoElectcom,AutoIca,CodRetIca
 FROM TercProvee WHERE  IdProv=@pmIdProv
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetalle_Rem] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraDetalle (tmNumero,tmItem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed,tmdmsAlto,tmdmsAncho,tmdmsLargo,tmVolumen,tmUndVol,tmIdUnd,tmIdEmp,tmIdNat
,tmIdMnjo,tmIdTmcia,tmCdRango,tmCases,tmCajas,tmPalets,tmRemision,tmNumOrden,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen,tmNitDestntario,tmDestinatario,tmDirDestino,tmIdDestino
,tmCdAgencia,tmTipoVehic,tmTarifClie,tmTarifPago,tmTarifTabla,tmVrDeclarado,tmVrSeguro,tmTarifSeguro,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmContenedor1,tmContenedor2,tmUndTarifa
,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmNumMintrans,tmPesoCont,tmUndTarPago,tmCodBodega,tmTipoTraslado,tmTieCargue,tmTieDesc,tmFecIniCargue,tmFecFinCargue,tmFecInidesc,tmFecFindesc,tmCdTipoVeh
,tmTipoServicio,tmSedeRem,tmSedeDest,tmFecLlegaCargue,tmFecLlegaDesc,tmMinCargue,tmMinDescargue)
SELECT @pmtmNumero,Item,IdMercancia,DescripMcias,Cantidad,PesoNeto,UndMed,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol,IdUnd,IdEmp,IdNat
,IdMnjo,IdTmcia,CdRango,Cases,Cajas,Palets,Remision,Factura,NitRemite,Remitente,DirOrigen,IdOrigen,NitDestntario,Destinatario,DirDestino,IdDestino
,CdAgencia,'TERCEROS',TarifClie,TarifPago,TarifTabla,VrDeclarado,VrSeguro,TarifSeguro,DocCliente,Referencia1,Referencia2,Referencia3,Contenedor1,Contenedor2,UndTarifa
,CASE WHEN TipDocInv='SAL' AND NumDocInv>0 THEN TipDocInv ELSE TipFac END,CASE WHEN TipDocInv='SAL' AND NumDocInv>0 THEN NumDocInv ELSE Factura END
,CASE WHEN TipDocInv='SAL' AND NumDocInv>0 THEN IdCiaDocInv ELSE IdCiaFac END,CASE WHEN TipDocInv='SAL' AND NumDocInv>0 THEN ItemDocInv ELSE 0 END
,0,PesoCont,UndTarifPago,'',0,0,0,Null,Null,Null,Null,'','',SedeRem,SedeDest,Null,Null,0,0
 FROM Trn_TraRemMcias
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraRemMcias] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdMercancia VARCHAR(16),@pmDescripMcias VARCHAR(250),@pmCantidad DECIMAL(14,4),@pmPesoNeto DECIMAL(14,4),@pmUndMed VARCHAR(10),@pmdmsAlto DECIMAL(14,4),@pmdmsAncho DECIMAL(14,4),@pmdmsLargo DECIMAL(14,4),@pmVolumen DECIMAL(14,4),@pmUndVol VARCHAR(10),@pmIdUnd VARCHAR(4)
,@pmIdEmp VARCHAR(4),@pmIdNat VARCHAR(4),@pmIdTmcia VARCHAR(4),@pmIdMnjo VARCHAR(4),@pmCdRango VARCHAR(4),@pmCases INT,@pmCajas INT,@pmPalets INT,@pmNitRemite VARCHAR(16),@pmRemitente VARCHAR(250),@pmDirOrigen VARCHAR(250),@pmIdOrigen VARCHAR(8),@pmNitDestntario VARCHAR(16),@pmDestinatario VARCHAR(250),@pmDirDestino VARCHAR(250),@pmIdDestino VARCHAR(8),@pmTarifClie MONEY
,@pmTarifPago MONEY,@pmTarifTabla MONEY,@pmVrDeclarado MONEY,@pmVrSeguro MONEY,@pmTarifSeguro DECIMAL(14,4),@pmUndTarifa VARCHAR(10),@pmRemision DECIMAL(18,2),@pmDocCliente VARCHAR(30),@pmReferencia1 VARCHAR(50),@pmReferencia2 VARCHAR(50),@pmReferencia3 VARCHAR(50),@pmContenedor1 VARCHAR(50),@pmContenedor2 VARCHAR(50),@pmCdAgencia VARCHAR(16),@pmCumplido INT,@pmIdCiaCump CHAR(2),@pmFechaCump SMALLDATETIME
,@pmDetalleCump VARCHAR(250),@pmCantidadCump DECIMAL(14,4),@pmPesoCump DECIMAL(14,4),@pmVolCump DECIMAL(14,4),@pmCasesCump INT,@pmCajasCump INT,@pmPaletsCump INT,@pmTarifCump MONEY,@pmPagoCump MONEY,@pmUndTarCump VARCHAR(10),@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFac SMALLDATETIME,@pmTarifClieFac MONEY,@pmTipOdp VARCHAR(3),@pmNumeroOdp INT
,@pmIdCiaOdp CHAR(2),@pmTarifOdp MONEY,@pmPesoCont DECIMAL(14,4),@pmUndTarifPago VARCHAR(10),@pmUndTarPagoCump VARCHAR(10),@pmSedeRem VARCHAR(10),@pmSedeDest VARCHAR(10),@pmTipDocInv VARCHAR(3),@pmNumDocInv INT,@pmIdCiaDocInv CHAR(2),@pmItemDocInv INT
 AS 
 INSERT INTO Trn_TraRemMcias (TipDoc,NumOrden,IdCia,Item,IdMercancia,DescripMcias,Cantidad,PesoNeto,UndMed,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol,IdUnd,IdEmp,IdNat,IdTmcia,IdMnjo,CdRango,Cases,Cajas,Palets,NitRemite,Remitente,DirOrigen,IdOrigen,NitDestntario,Destinatario,DirDestino,IdDestino,TarifClie,TarifPago,TarifTabla,VrDeclarado,VrSeguro,TarifSeguro,UndTarifa,UndTarifPago,Remision,DocCliente,Referencia1,Referencia2,Referencia3,Contenedor1,Contenedor2,CdAgencia
,Cumplido,IdCiaCump,FechaCump,DetalleCump,CantidadCump,PesoCump,VolCump,CasesCump,CajasCump,PaletsCump,TarifCump,PagoCump,UndTarCump,UndTarPagoCump,TipFac,Factura,IdCiaFac,FechaFac,TarifClieFac,TipOdp,NumeroOdp,IdCiaOdp,TarifOdp,PesoCont,SedeRem,SedeDest,TipDocInv,NumDocInv,IdCiaDocInv,ItemDocInv)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmItem,@pmIdMercancia,@pmDescripMcias,@pmCantidad,@pmPesoNeto,@pmUndMed,@pmdmsAlto,@pmdmsAncho,@pmdmsLargo,@pmVolumen,@pmUndVol,@pmIdUnd,@pmIdEmp,@pmIdNat,@pmIdTmcia,@pmIdMnjo,@pmCdRango,@pmCases,@pmCajas,@pmPalets,@pmNitRemite,@pmRemitente,@pmDirOrigen,@pmIdOrigen,@pmNitDestntario,@pmDestinatario,@pmDirDestino,@pmIdDestino,@pmTarifClie
,@pmTarifPago,@pmTarifTabla,@pmVrDeclarado,@pmVrSeguro,@pmTarifSeguro,@pmUndTarifa,@pmUndTarifPago,@pmRemision,@pmDocCliente,@pmReferencia1,@pmReferencia2,@pmReferencia3,@pmContenedor1,@pmContenedor2,@pmCdAgencia,@pmCumplido,@pmIdCiaCump,@pmFechaCump,@pmDetalleCump,@pmCantidadCump,@pmPesoCump,@pmVolCump,@pmCasesCump,@pmCajasCump,@pmPaletsCump,@pmTarifCump,@pmPagoCump,@pmUndTarCump,@pmUndTarPagoCump,@pmTipFac,@pmFactura
,@pmIdCiaFac,@pmFechaFac,@pmTarifClieFac,@pmTipOdp,@pmNumeroOdp,@pmIdCiaOdp,@pmTarifOdp,@pmPesoCont,@pmSedeRem,@pmSedeDest,@pmTipDocInv,@pmNumDocInv,@pmIdCiaDocInv,@pmItemDocInv)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraRemMcias] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,NumOrden,IdCia,Item,IdMercancia,DescripMcias,Cantidad,PesoNeto,UndMed,dmsAlto,dmsAncho,dmsLargo,Volumen,UndVol,IdUnd,IdEmp,IdNat,IdTmcia,IdMnjo,CdRango,Cases,Cajas,Palets
,NitRemite,Remitente,DirOrigen,IdOrigen,NitDestntario,Destinatario,DirDestino,IdDestino,TarifClie,TarifPago,TarifTabla,VrDeclarado,VrSeguro,TarifSeguro,UndTarifa,UndTarifPago,Remision,DocCliente,Referencia1,Referencia2,Referencia3
,Contenedor1,Contenedor2,CdAgencia,Cumplido,IdCiaCump,FechaCump,DetalleCump,CantidadCump,PesoCump,VolCump,CasesCump,CajasCump,PaletsCump,TarifCump,PagoCump,UndTarCump,UndTarPagoCump
,TipFac,Factura,IdCiaFac,FechaFac,TarifClieFac,TipOdp,NumeroOdp,IdCiaOdp,TarifOdp,PesoCont,SedeRem,SedeDest,TipDocInv,NumDocInv,IdCiaDocInv,ItemDocInv
 FROM Trn_TraRemMcias
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
 AND Item BETWEEN ISNULL(@pmItem,-1) AND  ISNULL(@pmItem,2147483647) 
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTraRemMcias] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraRemMcias
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraRemCum_Trr] @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmTraslado INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_TraRemCum (tmNumero,tmItem,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed,tmVolumen,tmUndVol,tmCases,tmCajas,tmPalets,tmTarifClie,tmTarifPago,tmUndTarif,tmUndTarifPago,tmCantCargue
,tmPesoCargue,tmVolCargue,tmCasesCargue,tmCajasCargue,tmPaletsCargue,tmEstadoCump,tmRemision,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmDetalle,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen,tmNitDestntario,tmDestinatario,tmDirDestino
,tmIdDestino,tmTarifTabla,tmTipoCum,tmMotivoSusp,tmHoraLlegaCargue,tmHoraEntraCargue,tmHoraSaleCargue,tmHoraLlegaDescargue,tmHoraEntraDescargue,tmHoraSaleDescargue,tmCdCCosto,tmCdSubCos,tmNitCliente)
SELECT @pmtmNumero,D.Item,D.TipRem,D.Remesa,D.IdCiaRem,D.ItemRem,IdMercancia,DescripMcias,Cantidad,PesoNeto,UndMed,Volumen,UndVol,Cases,Cajas,Palets,TarifClie,TarifPago,UndTarifa,UndTarifPago,CantidadCump
,PesoCump,VolCump,CasesCump,CajasCump,PaletsCump,0,Remision,DocCliente,Referencia1,Referencia2,Referencia3,D.Comentarios,NitRemite,Remitente,DirOrigen,IdOrigen,NitDestntario,Destinatario,DirDestino,IdDestino,TarifTabla
,'','',Null,Null,Null,Null,Null,Null,'','',''
FROM Trn_TraTrasladoRem AS D INNER JOIN Trn_TraRemMcias AS R ON D.TipRem=R.TipDoc AND D.Remesa=R.NumOrden AND D.IdCiaRem=R.IdCia AND D.ItemRem=R.Item
WHERE D.TipDoc=@pmTipDoc AND D.Traslado=@pmTraslado AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraRemMciasMuc] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdMercancia VARCHAR(16),@pmDescripMcias VARCHAR(250),@pmCantidad DECIMAL(14,4),@pmPesoNeto DECIMAL(14,4),@pmUndMed VARCHAR(10),@pmdmsAlto DECIMAL(14,4),@pmdmsAncho DECIMAL(14,4),@pmdmsLargo DECIMAL(14,4),@pmVolumen DECIMAL(14,4)
,@pmUndVol VARCHAR(10),@pmIdUnd VARCHAR(4),@pmIdEmp VARCHAR(4),@pmIdNat VARCHAR(4),@pmIdTmcia VARCHAR(4),@pmIdMnjo VARCHAR(4),@pmCdRango VARCHAR(4),@pmCases INT,@pmCajas INT,@pmPalets INT,@pmNitRemite VARCHAR(16),@pmRemitente VARCHAR(250),@pmDirOrigen VARCHAR(250),@pmIdOrigen VARCHAR(8),@pmNitDestntario VARCHAR(16),@pmDestinatario VARCHAR(250)
,@pmDirDestino VARCHAR(250),@pmIdDestino VARCHAR(8),@pmTarifPago MONEY,@pmTarifTabla MONEY,@pmRemision DECIMAL(18,2),@pmDocCliente VARCHAR(30),@pmReferencia1 VARCHAR(50),@pmReferencia2 VARCHAR(50),@pmReferencia3 VARCHAR(50),@pmSedeRem VARCHAR(10),@pmSedeDest VARCHAR(10)
 AS 
 UPDATE Trn_TraRemMcias SET IdMercancia=@pmIdMercancia,DescripMcias=@pmDescripMcias,Cantidad=@pmCantidad,PesoNeto=@pmPesoNeto,UndMed=@pmUndMed,dmsAlto=@pmdmsAlto,dmsAncho=@pmdmsAncho,dmsLargo=@pmdmsLargo,Volumen=@pmVolumen,UndVol=@pmUndVol,IdUnd=@pmIdUnd,IdEmp=@pmIdEmp,IdNat=@pmIdNat,IdTmcia=@pmIdTmcia,IdMnjo=@pmIdMnjo,CdRango=@pmCdRango
,Cases=@pmCases,Cajas=@pmCajas,Palets=@pmPalets,NitRemite=@pmNitRemite,Remitente=@pmRemitente,DirOrigen=@pmDirOrigen,IdOrigen=ISNULL(@pmIdOrigen,IdOrigen),NitDestntario=@pmNitDestntario,Destinatario=@pmDestinatario,DirDestino=@pmDirDestino,IdDestino=ISNULL(@pmIdDestino,IdDestino),TarifPago=@pmTarifPago,TarifTabla=@pmTarifTabla,Remision=@pmRemision,DocCliente=@pmDocCliente
,Referencia1=@pmReferencia1,Referencia2=@pmReferencia2,Referencia3=@pmReferencia3,SedeRem=@pmSedeRem,SedeDest=@pmSedeDest
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelEdsFormasPla] @pmPlanilla INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_EdsFormas
 WHERE Planilla=@pmPlanilla AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrPrecio,NumRef2,VrBase,VrPrecioEsp
 FROM Trn_EdsFormas
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmNumRef2 VARCHAR(50),@pmVrBase MONEY,@pmVrPrecioEsp MONEY,@pmVrPrecio MONEY
 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
,NumRef2=@pmNumRef2,VrBase=@pmVrBase,VrPrecioEsp=@pmVrPrecioEsp,VrPrecio=@pmVrPrecio
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
 AND  Planilla=@pmPlanilla AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NumRef2,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmVrPrecio MONEY,@pmNumRef2 VARCHAR(50),@pmVrBase MONEY,@pmVrPrecioEsp MONEY
 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,VrPrecio=@pmVrPrecio,NumRef2=@pmNumRef2,VrBase=@pmVrBase,VrPrecioEsp=@pmVrPrecioEsp
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmVrPrecio MONEY,@pmNumRef2 VARCHAR(50),@pmVrBase MONEY,@pmVrPrecioEsp MONEY
 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,VrPrecio,NumRef2,VrBase,VrPrecioEsp)
 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,@pmVrPrecio,@pmNumRef2,@pmVrBase,@pmVrPrecioEsp)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryPeajesClase] @pmIdClase VARCHAR(4)
 AS 
 IF @pmIdClase IS NULL
 SELECT IdClase,ClasePeaje,NitConsec
 FROM PeajesClase
 WHERE Inactivo=0
 ORDER BY IdClase
 ELSE
 SELECT IdClase,ClasePeaje,NitConsec,Inactivo
 FROM PeajesClase
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpPeajesClase] @pmIdClase VARCHAR(4),@pmClasePeaje VARCHAR(50),@pmNitConsec VARCHAR(16),@pmInactivo BIT
 AS 
 UPDATE PeajesClase SET ClasePeaje=@pmClasePeaje,NitConsec=@pmNitConsec,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsPeajesClase] @pmIdClase VARCHAR(4),@pmClasePeaje VARCHAR(50),@pmNitConsec VARCHAR(16),@pmInactivo BIT
 AS 
 INSERT INTO PeajesClase (IdClase,ClasePeaje,NitConsec,Inactivo)
 VALUES (@pmIdClase,@pmClasePeaje,@pmNitConsec,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposMcia] @pmIdTmcia VARCHAR(4),@pmTipoMcia VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposMcia (IdTmcia,TipoMcia,Inactivo)
 VALUES (@pmIdTmcia,@pmTipoMcia,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposMcia] @pmIdTmcia VARCHAR(4),@pmTipoMcia VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposMcia SET TipoMcia=@pmTipoMcia,Inactivo=@pmInactivo
 WHERE IdTmcia=@pmIdTmcia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposMcia] @pmIdTmcia VARCHAR(4)
 AS 
 DELETE FROM TiposMcia
 WHERE IdTmcia=@pmIdTmcia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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)
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,CdCausal,Cedtraparte,PolizaResCivil,VigencResCivil,NitEmpAseg,NombreAseg,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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)
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID  AND  IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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)
,@pmCdCausal VARCHAR(4),@pmCedtraparte VARCHAR(16),@pmPolizaResCivil VARCHAR(30),@pmVigencResCivil SMALLDATETIME,@pmNitEmpAseg VARCHAR(16),@pmNombreAseg VARCHAR(150),@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
,CdCausal=@pmCdCausal,Cedtraparte=@pmCedtraparte,PolizaResCivil=@pmPolizaResCivil,VigencResCivil=@pmVigencResCivil,NitEmpAseg=@pmNitEmpAseg,NombreAseg=@pmNombreAseg
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,@pmCedtraparte VARCHAR(16)
AS 
 UPDATE Trn_JurCasos SET  Contraparte=@pmContraparte, DirContraparte=@pmDirContraparte,TelContraparte=@pmTelContraparte,ValDanosCParte=@pmValDanosCParte, ValDanosEmp=@pmValDanosEmp, ValDeducible=@pmValDeducible
, EmpAsegdora=@pmEmpAsegdora, AuxMutuo=@pmAuxMutuo,Cedtraparte=@pmCedtraparte,FecUpdate=@pmFecUpdate,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID  AND  IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,CdCausal,Cedtraparte,PolizaResCivil,VigencResCivil,NitEmpAseg,NombreAseg,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_JurCasos
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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)
,@pmCdCausal VARCHAR(4),@pmCedtraparte VARCHAR(16),@pmPolizaResCivil VARCHAR(30),@pmVigencResCivil SMALLDATETIME,@pmNitEmpAseg VARCHAR(16),@pmNombreAseg VARCHAR(150),@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,CdCausal,Cedtraparte,PolizaResCivil,VigencResCivil,NitEmpAseg,NombreAseg)
 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,@pmCdCausal,@pmCedtraparte,@pmPolizaResCivil,@pmVigencResCivil,@pmNitEmpAseg,@pmNombreAseg)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmtmImpConEnt2 MONEY,@pmtmImpConSal2 MONEY
 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,tmImpConEnt2=@pmtmImpConEnt2,tmImpConSal2=@pmtmImpConSal2
 WHERE tmEst=@pmtmEst AND tmItem=@pmtmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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)
,@pmtmImpConEnt MONEY,@pmtmImpConSal MONEY,@pmtmImpConEnt2 MONEY,@pmtmImpConSal2 MONEY
 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
,tmImpConEnt,tmImpConSal,tmImpConEnt2,tmImpConSal2)
 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,@pmtmImpConEnt,@pmtmImpConSal,@pmtmImpConEnt2,@pmtmImpConSal2)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_MovVentas] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_MovVentas  WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraAntAbonos] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Documento,IdCia,Item,Fecha,TipAnt,Anticipo,IdCiaAnt,VrAbono,TipoAplica,Detalle,CdCuentaAnt
 FROM Trn_TraAntAbonos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 ORDER BY  Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraAnticiposApp] @pmIdCia CHAR(2)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
 UPDATE Trn_TraAnticipos SET  VrAbonado=(SELECT ISNULL(SUM(Trn_TraAntAbonos.VrAbono),0)
 FROM Trn_TraAntAbonos WHERE Trn_TraAntAbonos.TipAnt=Trn_TraAnticipos.TipDoc AND Trn_TraAntAbonos.Anticipo=Trn_TraAnticipos.Anticipo
  AND Trn_TraAntAbonos.IdCiaAnt=Trn_TraAnticipos.IdCia)
 WHERE  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraAntAbonos] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmTipAnt VARCHAR(3)
,@pmAnticipo INT,@pmIdCiaAnt CHAR(2),@pmVrAbono MONEY,@pmTipoAplica VARCHAR(10),@pmDetalle VARCHAR(100),@pmCdCuentaAnt VARCHAR(16)
 AS 
 INSERT INTO Trn_TraAntAbonos (TipDoc,Documento,IdCia,Item,Fecha,TipAnt,Anticipo,IdCiaAnt,VrAbono,TipoAplica,Detalle,CdCuentaAnt)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmItem,@pmFecha,@pmTipAnt,@pmAnticipo,@pmIdCiaAnt,@pmVrAbono,@pmTipoAplica,@pmDetalle,@pmCdCuentaAnt)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraAnticiposApl] @pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCia CHAR(2)
 AS 
 UPDATE Trn_TraAnticipos SET  VrAbonado=(SELECT ISNULL(SUM(Trn_TraAntAbonos.VrAbono),0)
 FROM Trn_TraAntAbonos WHERE Trn_TraAntAbonos.TipAnt=Trn_TraAnticipos.TipDoc AND Trn_TraAntAbonos.Anticipo=Trn_TraAnticipos.Anticipo
  AND Trn_TraAntAbonos.IdCiaAnt=Trn_TraAnticipos.IdCia)
 WHERE TipDoc=@pmTipDoc AND Anticipo=@pmAnticipo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTraAntAbonos] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraAntAbonos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[paInstm_TraAnt_Sel] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraAnt (tmNumero,tmItem,tmTipAnt,tmAnticipo,tmIdCia,tmValor,tmVrAnticipo,tmTotalAbonos,tmDetalle,tmCdCueAnt)
 SELECT @pmtmNumero,Item,TipAnt,A.Anticipo,IdCiaAnt,A.VrAbono,AN.VrAnticipo,AN.VrAbonado,Detalle,CdCuentaAnt
 FROM Trn_TraAntAbonos AS A INNER JOIN Trn_TraAnticipos AS AN ON A.TipAnt=AN.TipDoc AND A.Anticipo=AN.Anticipo AND A.IdCiaAnt=AN.IdCia
 WHERE  A.TipDoc=@pmTipDoc AND A.Documento=@pmDocumento AND A.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraLiqCausac] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipCau VARCHAR(3),@pmtmCausacion INT,@pmtmIdCiaCau CHAR(2),@pmtmItemCau INT,@pmtmFecha SMALLDATETIME,@pmtmIdConcepto VARCHAR(4),@pmtmDescripcion VARCHAR(250)
,@pmtmVrAbono MONEY,@pmtmNit VARCHAR(16),@pmtmVehiculo VARCHAR(10),@pmtmTipOdp VARCHAR(3),@pmtmOrdenPago INT,@pmtmIdCiaOdp CHAR(2),@pmtmTipMuc VARCHAR(3),@pmtmManifiesto INT,@pmtmIdCiaMuc CHAR(2),@pmtmItemFact INT,@pmtmVrSaldo MONEY
,@pmtmEsVence BIT,@pmtmCdCuenta VARCHAR(16),@pmtmCuePagar VARCHAR(16),@pmtmFactura VARCHAR(20),@pmtmConcValor MONEY,@pmtmConcAbono MONEY,@pmtmCdCCosto VARCHAR(16),@pmtmCdSubCos VARCHAR(16),@pmtmTipoFac VARCHAR(3)
 AS 
 INSERT INTO tm_TraLiqCausac (tmNumero,tmItem,tmTipCau,tmCausacion,tmIdCiaCau,tmItemCau,tmFecha,tmIdConcepto,tmDescripcion,tmVrAbono,tmNit,tmVehiculo,tmTipOdp,tmOrdenPago,tmIdCiaOdp,tmTipMuc,tmManifiesto,tmIdCiaMuc
,tmItemFact,tmVrSaldo,tmEsVence,tmCdCuenta,tmCuePagar,tmFactura,tmConcValor,tmConcAbono,tmCdCCosto,tmCdSubCos,tmTipoFac)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipCau,@pmtmCausacion,@pmtmIdCiaCau,@pmtmItemCau,@pmtmFecha,@pmtmIdConcepto,@pmtmDescripcion,@pmtmVrAbono,@pmtmNit,@pmtmVehiculo,@pmtmTipOdp,@pmtmOrdenPago,@pmtmIdCiaOdp
,@pmtmTipMuc,@pmtmManifiesto,@pmtmIdCiaMuc,@pmtmItemFact,@pmtmVrSaldo,@pmtmEsVence,@pmtmCdCuenta,@pmtmCuePagar,@pmtmFactura,@pmtmConcValor,@pmtmConcAbono,@pmtmCdCCosto,@pmtmCdSubCos,@pmtmTipoFac)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTerminales] @pmIdTerminal VARCHAR(4),@pmTerminal VARCHAR(50),@pmIdCia CHAR(2),@pmInactivo BIT
 AS 
 INSERT INTO Terminales (IdTerminal,Terminal,IdCia,Inactivo)
 VALUES (@pmIdTerminal,@pmTerminal,@pmIdCia,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTerminales] @pmIdTerminal VARCHAR(4)
 AS 
 SELECT IdTerminal,Terminal,IdCia,Inactivo
 FROM Terminales
 WHERE IdTerminal=@pmIdTerminal
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTerminales] @pmIdTerminal VARCHAR(4),@pmTerminal VARCHAR(50),@pmIdCia CHAR(2),@pmInactivo BIT
 AS 
 UPDATE Terminales SET Terminal=@pmTerminal,IdCia=@pmIdCia,Inactivo=@pmInactivo
 WHERE IdTerminal=@pmIdTerminal
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTerminales] @pmIdTerminal VARCHAR(4)
 AS 
 DELETE FROM Terminales
 WHERE IdTerminal=@pmIdTerminal
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelNomRangosFsu] @pmIdRango INT
 AS 
 DELETE FROM NomRangosFsu
 WHERE IdRango=@pmIdRango
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomRangosFsuId] 
AS
 SELECT  MAX(IdRango) AS MXRANGO,MAX(VrFinal) AS MXFINAL
 FROM  NomRangosFsu
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomRangosFsuLta] 
AS
 SELECT   IdRango, VrInicial, VrFinal, Tarifa 
 FROM  NomRangosFsu
 ORDER BY IdRango
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryNomRangosFsu] @pmIdRango INT
AS
 SELECT     IdRango, VrInicial, VrFinal, Tarifa 
 FROM  NomRangosFsu
 WHERE IdRango=@pmIdRango
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposOperac] @pmIdTipoOper VARCHAR(4)
 AS 
 IF @pmIdTipoOper IS NULL
  BEGIN
   SELECT IdTipoOper,TipoOperacion,TipoIngreso,TarifaIng 
   FROM TiposOperac WHERE Inactivo=0
   ORDER BY TipoOperacion
  END
 ELSE
  BEGIN
   SELECT IdTipoOper,TipoOperacion,TipoIngreso,TarifaIng,Inactivo
   FROM TiposOperac WHERE IdTipoOper=@pmIdTipoOper
  END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposOperac] @pmIdTipoOper VARCHAR(4),@pmTipoOperacion VARCHAR(50),@pmInactivo BIT,@pmTipoIngreso VARCHAR(10),@pmTarifaIng DECIMAL(14,4)
 AS 
 INSERT INTO TiposOperac (IdTipoOper,TipoOperacion,Inactivo,TipoIngreso,TarifaIng)
 VALUES (@pmIdTipoOper,@pmTipoOperacion,@pmInactivo,@pmTipoIngreso,@pmTarifaIng)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposOperac] @pmIdTipoOper VARCHAR(4),@pmTipoOperacion VARCHAR(50),@pmInactivo BIT
,@pmTipoIngreso VARCHAR(10),@pmTarifaIng DECIMAL(14,4)
 AS 
 UPDATE TiposOperac SET TipoOperacion=@pmTipoOperacion,Inactivo=@pmInactivo,TipoIngreso=@pmTipoIngreso,TarifaIng=@pmTarifaIng
 WHERE IdTipoOper=@pmIdTipoOper
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposOperac] @pmIdTipoOper VARCHAR(4)
 AS 
 DELETE FROM TiposOperac WHERE IdTipoOper=@pmIdTipoOper
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAfPartes] @pmNumero INT,@pmItem INT,@pmIdActivo VARCHAR(30),@pmIdParte INT,@pmVrBaseDep MONEY,@pmVrCostoIni MONEY,@pmVrMejoras MONEY,@pmVrDisminuc MONEY,@pmVrAcumAju MONEY,@pmVrDepAcum MONEY,@pmVrDeprecia MONEY
,@pmVrDepHis MONEY,@pmVrAjusteInf MONEY,@pmVrAjuDeprec MONEY,@pmVrAcumAjuDep MONEY,@pmVrSalvamnto MONEY,@pmUnidades INT,@pmCantidad INT,@pmFecIniDep SMALLDATETIME,@pmVida_Util INT,@pmMetDeprec VARCHAR(10),@pmCantCuoIni INT,@pmVrDepMejoras MONEY
 AS 
 INSERT INTO Trn_AfPartes (Numero,Item,IdActivo,IdParte,VrBaseDep,VrCostoIni,VrMejoras,VrDisminuc,VrAcumAju,VrDepAcum,VrDeprecia,VrDepHis,VrAjusteInf,VrAjuDeprec,VrAcumAjuDep,VrSalvamnto,Unidades,Cantidad,FecIniDep,Vida_Util,MetDeprec,CantCuoIni,VrDepMejoras)
 VALUES (@pmNumero,@pmItem,@pmIdActivo,@pmIdParte,@pmVrBaseDep,@pmVrCostoIni,@pmVrMejoras,@pmVrDisminuc,@pmVrAcumAju,@pmVrDepAcum,@pmVrDeprecia,@pmVrDepHis,@pmVrAjusteInf,@pmVrAjuDeprec,@pmVrAcumAjuDep,@pmVrSalvamnto,@pmUnidades
,@pmCantidad,@pmFecIniDep,@pmVida_Util,@pmMetDeprec,@pmCantCuoIni,@pmVrDepMejoras)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_AfDepPartesSel] @pmtmNumero VARCHAR(5),@pmNumero INT
 AS 
 INSERT INTO tm_AfDepPartes (tmNumero,tmItem,tmIdActivo,tmIdParte,tmCostoIni,tmMejoras,tmDisminucion,tmAcuAjustes,tmDepreciaHis,tmDepreciaMes,tmAjustesInf,tmAjustesDep,tmDepreciaAcum
,tmAjuDepAcum,tmTarifa,tmVidaUtil,tmMetodo,tmFechaIni,tmEstadoReg,tmObservacion,tmSalvamento,tmUnidades,tmCantidad,tmDepMejoras)
SELECT @pmtmNumero,Item,IdActivo,IdParte,VrCostoIni,VrMejoras,VrDisminuc,VrAcumAju,VrDepHis,VrDeprecia,VrAjusteInf,VrAjuDeprec,VrDepAcum,VrAcumAjuDep
,0,Vida_Util,MetDeprec,FecIniDep,0,'',VrSalvamnto,Unidades,Cantidad,VrDepMejoras
FROM Trn_AfPartes
WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelClaseDiv] @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseDiv
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsClaseDiv] @pmIdClase VARCHAR(4),@pmNomClase VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ClaseDiv (IdClase,NomClase,Inactivo)
 VALUES (@pmIdClase,@pmNomClase,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpClaseDiv] @pmIdClase VARCHAR(4),@pmNomClase VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ClaseDiv SET NomClase=@pmNomClase,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuCuentasAgeSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuCuentasAge SET AcuCuentasAge.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuCuentasAge INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuCuentasAge WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt AND TipoAcum=@pmTipoAcum) AS SA ON AcuCuentasAge.IdCia=SA.IdCia AND AcuCuentasAge.IdCuenta=SA.IdCuenta
 AND AcuCuentasAge.IdTercero=SA.IdTercero AND AcuCuentasAge.CodAgncia=SA.CodAgncia AND AcuCuentasAge.TipoAcum=SA.TipoAcum
 WHERE AcuCuentasAge.nAnno=@pmnAnno AND AcuCuentasAge.nMes=@pmnMes AND AcuCuentasAge.TipoAcum=@pmTipoAcum
 AND (@pmIdCia IS NULL OR AcuCuentasAge.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuCuentasAge.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuCuentasAge_Pla]  @pmIdVehiculo VARCHAR(10),@pmIdVehiculoDos VARCHAR(10),@pmNewVehiculo VARCHAR(10)
 AS 
 IF EXISTS (SELECT * FROM AcuCuentasAge WHERE (CodAgncia=@pmIdVehiculo OR CodAgncia=@pmIdVehiculoDos) AND TipoAcum='NV')
  INSERT INTO AcuCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,@pmNewVehiculo,TipoAcum,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
 FROM AcuCuentasAge WHERE (CodAgncia=@pmIdVehiculo OR CodAgncia=@pmIdVehiculoDos) AND TipoAcum='NV'
 GROUP BY nAnno,nMes,IdCia,IdCuenta,IdTercero,TipoAcum
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 Agencia=1 AND TipoAcum=@pmTipoAcum
  AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 GROUP BY  IdCia,A.IdCuenta,IdTercero,CodAgncia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuCuentasAge_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdTercero,S.CodAgncia,S.TipoAcum,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuCuentasAge AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt AND S.TipoAcum=@pmTipoAcum
 AND NOT EXISTS (SELECT * FROM AcuCuentasAge AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes AND AC.TipoAcum=@pmTipoAcum
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdTercero=S.IdTercero AND AC.CodAgncia=S.CodAgncia)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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=1 AND Agencia=1
  AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 ORDER BY A.IdCuenta,IdCia,IdTercero,CodAgncia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraDevCum] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraDevCum
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevCum] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Cumplido,IdCiaDoc,FecDoc,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,TimeSys,IdCiaCrea,IdUsuario
 FROM Trn_TraDevCum
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraDevCum] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME
,@pmModdDev VARCHAR(10),@pmOrigenAdd VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraDevCum (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Cumplido,IdCiaDoc,FecDoc,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmCumplido,@pmIdCiaDoc,@pmFecDoc,@pmModdDev,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryVehRemPolizasLta] 
 AS 
SELECT P.IdRemque,P.IdClase AS CdClase,ClasePoliza,P.Numero,P.FecEmision,P.FecVigencia AS FecVence,P.NitCompania,RazonSocial
,P.Valor,P.DiasAviso,P.Comntarios 
FROM VehRemPolizas AS P INNER JOIN ClasePol AS CL ON P.IdClase=CL.IdClase
INNER JOIN VehRemolq AS R ON P.IdRemque=R.IdRemque
LEFT JOIN Terceros AS T ON P.NitCompania=T.IdTercero
WHERE R.Inactivo=0
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmHidrostatica VARCHAR(30),@pmFecPruebaHid SMALLDATETIME,@pmVigPruebaHid SMALLDATETIME,@pmkmRuta INT,@pmkmOtro INT
,@pmKingpin VARCHAR(50),@pmFecKingpin SMALLDATETIME,@pmVigKingpin SMALLDATETIME,@pmArcFotoRem VARCHAR(50),@pmFecVigExtintor SMALLDATETIME,@pmCdLocTarj VARCHAR(8),@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
,Hidrostatica=@pmHidrostatica,FecPruebaHid=@pmFecPruebaHid,VigPruebaHid=@pmVigPruebaHid,kmRuta=@pmkmRuta,kmOtro=@pmkmOtro,Kingpin=@pmKingpin,FecKingpin=@pmFecKingpin,VigKingpin=@pmVigKingpin,ArcFotoRem=@pmArcFotoRem,FecVigExtintor=@pmFecVigExtintor,CdLocTarj=@pmCdLocTarj
 WHERE IdRemque=@pmIdRemque
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,Hidrostatica,FecPruebaHid,VigPruebaHid
,kmRuta,kmOtro,Kingpin,FecKingpin,VigKingpin,ArcFotoRem,FecVigExtintor,CdLocTarj
 FROM VehRemolq
 WHERE IdRemque=@pmIdRemque
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsVehRemolq_Sel] @pmIdRemque VARCHAR(10),@pmNewId VARCHAR(10)
 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,Hidrostatica,FecPruebaHid,VigPruebaHid,kmRuta,kmOtro,Kingpin,FecKingpin,VigKingpin,ArcFotoRem,FecVigExtintor,CdLocTarj)
SELECT @pmNewId,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,Hidrostatica,FecPruebaHid,VigPruebaHid,kmRuta,kmOtro,Kingpin,FecKingpin,VigKingpin,ArcFotoRem,FecVigExtintor,CdLocTarj
FROM VehRemolq WHERE IdRemque=@pmIdRemque
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmHidrostatica VARCHAR(30),@pmFecPruebaHid SMALLDATETIME,@pmVigPruebaHid SMALLDATETIME,@pmkmRuta INT,@pmkmOtro INT,@pmKingpin VARCHAR(50),@pmFecKingpin SMALLDATETIME
,@pmVigKingpin SMALLDATETIME,@pmArcFotoRem VARCHAR(50),@pmFecVigExtintor SMALLDATETIME,@pmCdLocTarj VARCHAR(8),@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,Hidrostatica,FecPruebaHid,VigPruebaHid,kmRuta,kmOtro,Kingpin,FecKingpin,VigKingpin,ArcFotoRem,FecVigExtintor,CdLocTarj)
 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,@pmHidrostatica,@pmFecPruebaHid,@pmVigPruebaHid,@pmkmRuta,@pmkmOtro,@pmKingpin,@pmFecKingpin,@pmVigKingpin,@pmArcFotoRem,@pmFecVigExtintor,@pmCdLocTarj)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraOrdenManif] @pmTipDoc VARCHAR(3),@pmOrdPago INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipMuc VARCHAR(3),@pmManifiesto INT,@pmIdCiaMuc CHAR(2),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmIdPropietario VARCHAR(16),@pmIdPoseedor VARCHAR(16),@pmVrTotalFletes MONEY,@pmVrDescuento MONEY,@pmVrRetencion MONEY,@pmVrReteIca MONEY
,@pmVrAnticipos MONEY,@pmVrFaltantes MONEY,@pmVrSeguros MONEY,@pmVrFondos MONEY,@pmVrAportes MONEY,@pmVrOtrosDctos MONEY,@pmVrImpuestos MONEY,@pmVrEstampilla MONEY,@pmVrOtrosPagos MONEY,@pmVrNeto MONEY,@pmTarifaTabla MONEY,@pmTarifaFlete MONEY,@pmUnidTarifa VARCHAR(10),@pmPesoTotal DECIMAL(14,4),@pmUnidades DECIMAL(14,4),@pmVolumen DECIMAL(14,4),@pmPesoOrigen DECIMAL(14,4)
,@pmUnidOrigen DECIMAL(14,4),@pmVolOrigen DECIMAL(14,4),@pmPesoDestino  DECIMAL(14,4),@pmUnidDestino DECIMAL(14,4),@pmVolDestino DECIMAL(14,4),@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseSeg MONEY,@pmBaseImp MONEY,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaSeg DECIMAL(14,4),@pmTarifaImp DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmTipoLiq INT
,@pmTipEgr VARCHAR(3),@pmEgreso INT,@pmIdCiaEgr CHAR(2),@pmNumCheque VARCHAR(20),@pmReferencia VARCHAR(50),@pmCantFaltante DECIMAL(14,4),@pmUnidadFalt VARCHAR(10),@pmTolFaltNeto DECIMAL(14,4),@pmVrConcPagos MONEY,@pmVrConcDctos MONEY,@pmVrConcFondo MONEY,@pmVrConcSeguro MONEY,@pmVrConcAporte MONEY,@pmVrConcImpuesto MONEY
,@pmEdoLiqCausac INT,@pmVrRemesas MONEY,@pmVrRecCaja MONEY,@pmCdForma VARCHAR(4),@pmFechaEgr SMALLDATETIME,@pmVrSeguroRc MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmCdCenCosto VARCHAR(16),@pmCdSubCenCos VARCHAR(16),@pmCodTarifAvta VARCHAR(4),@pmTarifaAvta DECIMAL(14,4),@pmVrImpAvTa MONEY,@pmVrDctoPago MONEY,@pmCodTarifDcpp VARCHAR(4)
 AS 
 INSERT INTO Trn_TraOrdenManif (TipDoc,OrdPago,IdCia,Item,TipMuc,Manifiesto,IdCiaMuc,IdVehiculo,IdConductor,IdPropietario,IdPoseedor,VrTotalFletes,VrDescuento,VrRetencion,VrReteIca,VrAnticipos,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,VrNeto,TarifaTabla
,TarifaFlete,UnidTarifa,PesoTotal,Unidades,Volumen,PesoOrigen,UnidOrigen,VolOrigen,PesoDestino,UnidDestino,VolDestino,BaseRet,BaseIca,BaseSeg,BaseImp,TarifaRet,TarifaIca,TarifaSeg,TarifaImp,CodTarRet,CodTarIca,TipoLiq,TipEgr,Egreso,IdCiaEgr,NumCheque,Referencia,CantFaltante,UnidadFalt,TolFaltNeto
,VrConcPagos,VrConcDctos,VrConcFondo,VrConcSeguro,VrConcAporte,VrConcImpuesto,EdoLiqCausac,VrRemesas,VrRecCaja,CdForma,FechaEgr,VrSeguroRc,VrReteCREE,TarifaRtc,CodTarRtc,CdCenCosto,CdSubCenCos,CodTarifAvta,TarifaAvta,VrImpAvTa,VrDctoPago,CodTarifDcpp)
 VALUES (@pmTipDoc,@pmOrdPago,@pmIdCia,@pmItem,@pmTipMuc,@pmManifiesto,@pmIdCiaMuc,@pmIdVehiculo,@pmIdConductor,@pmIdPropietario,@pmIdPoseedor,@pmVrTotalFletes,@pmVrDescuento,@pmVrRetencion,@pmVrReteIca,@pmVrAnticipos,@pmVrFaltantes,@pmVrSeguros
,@pmVrFondos,@pmVrAportes,@pmVrOtrosDctos,@pmVrImpuestos,@pmVrEstampilla,@pmVrOtrosPagos,@pmVrNeto,@pmTarifaTabla,@pmTarifaFlete,@pmUnidTarifa,@pmPesoTotal,@pmUnidades,@pmVolumen,@pmPesoOrigen,@pmUnidOrigen,@pmVolOrigen,@pmPesoDestino,@pmUnidDestino,@pmVolDestino,@pmBaseRet,@pmBaseIca,@pmBaseSeg,@pmBaseImp,@pmTarifaRet,@pmTarifaIca,@pmTarifaSeg,@pmTarifaImp,@pmCodTarRet,@pmCodTarIca,@pmTipoLiq
,@pmTipEgr,@pmEgreso,@pmIdCiaEgr,@pmNumCheque,@pmReferencia,@pmCantFaltante,@pmUnidadFalt,@pmTolFaltNeto,@pmVrConcPagos,@pmVrConcDctos,@pmVrConcFondo,@pmVrConcSeguro,@pmVrConcAporte,@pmVrConcImpuesto,@pmEdoLiqCausac,@pmVrRemesas,@pmVrRecCaja,@pmCdForma,@pmFechaEgr,@pmVrSeguroRc,@pmVrReteCREE,@pmTarifaRtc,@pmCodTarRtc,@pmCdCenCosto,@pmCdSubCenCos,@pmCodTarifAvta,@pmTarifaAvta,@pmVrImpAvTa,@pmVrDctoPago,@pmCodTarifDcpp)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraOrdenManif] @pmTipDoc VARCHAR(3),@pmOrdPago INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraOrdenManif
 WHERE TipDoc=@pmTipDoc AND OrdPago=@pmOrdPago AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenManif] @pmTipDoc VARCHAR(3),@pmOrdPago INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,OrdPago,IdCia,Item,TipMuc,Manifiesto,IdCiaMuc,IdVehiculo,IdConductor,IdPropietario,IdPoseedor,VrTotalFletes,VrDescuento,VrRetencion
,VrReteIca,VrAnticipos,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,VrNeto,TarifaTabla,TarifaFlete,UnidTarifa,PesoTotal,Unidades
,Volumen,PesoOrigen,UnidOrigen,VolOrigen,PesoDestino,UnidDestino,VolDestino,BaseRet,BaseIca,BaseSeg,BaseImp,TarifaRet,TarifaIca,TarifaSeg,TarifaImp,CodTarRet,CodTarIca
,TipoLiq,TipEgr,Egreso,IdCiaEgr,NumCheque,Referencia,CantFaltante,UnidadFalt,TolFaltNeto,VrConcPagos,VrConcDctos
,VrConcFondo,VrConcSeguro,VrConcAporte,VrConcImpuesto,EdoLiqCausac,VrRemesas,VrRecCaja,CdForma,FechaEgr,VrSeguroRc,VrReteCREE,TarifaRtc,CodTarRtc,CdCenCosto,CdSubCenCos
,CodTarifAvta,TarifaAvta,VrImpAvTa,VrDctoPago,CodTarifDcpp
 FROM Trn_TraOrdenManif
 WHERE TipDoc=@pmTipDoc AND OrdPago=@pmOrdPago AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraOrdenCum] @pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraOrdenCum
 WHERE TipDoc=@pmTipDoc AND Cumplido=@pmCumplido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraOrdenCum] @pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipOds VARCHAR(3),@pmNumOrden INT,@pmIdCiaOds CHAR(2),@pmTipoAfiVehic VARCHAR(10),@pmFecDespacho SMALLDATETIME,@pmFecRecibo SMALLDATETIME,@pmFecIngCertif SMALLDATETIME,@pmFecCertificado SMALLDATETIME,@pmDescServicio VARCHAR(100)
,@pmCantidad DECIMAL(14,4),@pmVrTotal MONEY,@pmVrCosto MONEY,@pmRemCliente VARCHAR(30),@pmFuncionario VARCHAR(150),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraOrdenCum SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipOds=@pmTipOds,NumOrden=@pmNumOrden,IdCiaOds=@pmIdCiaOds,TipoAfiVehic=@pmTipoAfiVehic,FecDespacho=@pmFecDespacho,FecRecibo=@pmFecRecibo,FecIngCertif=@pmFecIngCertif,FecCertificado=@pmFecCertificado,DescServicio=@pmDescServicio,Cantidad=@pmCantidad,VrTotal=@pmVrTotal,VrCosto=@pmVrCosto
,RemCliente=@pmRemCliente,Funcionario=@pmFuncionario,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Cumplido=@pmCumplido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenCum] @pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Cumplido,IdCia,Fecha,IdConcepto,TipOds,NumOrden,IdCiaOds,TipoAfiVehic,FecDespacho,FecRecibo,FecIngCertif,FecCertificado,DescServicio,Cantidad,VrTotal,VrCosto,RemCliente,Funcionario,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraOrdenCum
 WHERE TipDoc=@pmTipDoc AND Cumplido=@pmCumplido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraOrdenCum] @pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipOds VARCHAR(3),@pmNumOrden INT,@pmIdCiaOds CHAR(2),@pmTipoAfiVehic VARCHAR(10),@pmFecDespacho SMALLDATETIME,@pmFecRecibo SMALLDATETIME,@pmFecIngCertif SMALLDATETIME,@pmFecCertificado SMALLDATETIME,@pmDescServicio VARCHAR(100)
,@pmCantidad DECIMAL(14,4),@pmVrTotal MONEY,@pmVrCosto MONEY,@pmRemCliente VARCHAR(30),@pmFuncionario VARCHAR(150),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraOrdenCum (TipDoc,Cumplido,IdCia,Fecha,IdConcepto,TipOds,NumOrden,IdCiaOds,TipoAfiVehic,FecDespacho,FecRecibo,FecIngCertif,FecCertificado,DescServicio,Cantidad,VrTotal,VrCosto,RemCliente,Funcionario,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmCumplido,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipOds,@pmNumOrden,@pmIdCiaOds,@pmTipoAfiVehic,@pmFecDespacho,@pmFecRecibo,@pmFecIngCertif,@pmFecCertificado,@pmDescServicio,@pmCantidad,@pmVrTotal,@pmVrCosto,@pmRemCliente,@pmFuncionario,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraOrdenCumAnu] @pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4)
AS 
 UPDATE Trn_TraOrdenCum SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Cumplido=@pmCumplido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMayLiquidaId] @pmIdCia CHAR(2)
 AS 
 SELECT MAX(NLiquida) AS MXID
 FROM Trn_MayLiquida
 WHERE   IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMayLiquida] @pmNLiquida INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_MayLiquida
 WHERE NLiquida=@pmNLiquida AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelFuentesPago] @pmIdFuente VARCHAR(4)
 AS 
 DELETE FROM FuentesPago
 WHERE IdFuente=@pmIdFuente
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsFuentesPago] @pmIdFuente VARCHAR(4),@pmFuentePago VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO FuentesPago (IdFuente,FuentePago,Inactivo)
 VALUES (@pmIdFuente,@pmFuentePago,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpFuentesPago] @pmIdFuente VARCHAR(4),@pmFuentePago VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE FuentesPago SET FuentePago=@pmFuentePago,Inactivo=@pmInactivo
 WHERE IdFuente=@pmIdFuente
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdEntregaDtlFmt] @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
AS
SELECT TipDev,Devolucion,IdCia,Item,ItemDet,NumTalla,Cantidad
FROM Trn_ProdEntregaDtl
WHERE TipDev=@pmTipDev AND Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin
AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmDiasSan DECIMAL(14,4),@pmDiasDnrPen INT,@pmDiasDnrSal INT,@pmNovDsr INT,@pmFecNovIng SMALLDATETIME,@pmFecNovRet SMALLDATETIME,@pmFecNovVsp SMALLDATETIME
,@pmFecIniSln SMALLDATETIME,@pmFecFinSln SMALLDATETIME,@pmFecIniIge SMALLDATETIME,@pmFecFinIge SMALLDATETIME,@pmFecIniLma SMALLDATETIME,@pmFecFinLma SMALLDATETIME,@pmFecIniVac SMALLDATETIME,@pmFecFinVac SMALLDATETIME
,@pmFecIniVct SMALLDATETIME,@pmFecFinVct SMALLDATETIME,@pmFecIniIrl SMALLDATETIME,@pmFecFinIrl SMALLDATETIME
 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,DiasSan=@pmDiasSan,DiasDnrPen=@pmDiasDnrPen,DiasDnrSal=@pmDiasDnrSal,NovDsr=@pmNovDsr,FecNovIng=@pmFecNovIng,FecNovRet=@pmFecNovRet
,FecNovVsp=@pmFecNovVsp,FecIniSln=@pmFecIniSln,FecFinSln=@pmFecFinSln,FecIniIge=@pmFecIniIge,FecFinIge=@pmFecFinIge,FecIniLma=@pmFecIniLma,FecFinLma=@pmFecFinLma,FecIniVac=@pmFecIniVac,FecFinVac=@pmFecFinVac
,FecIniVct=@pmFecIniVct,FecFinVct=@pmFecFinVct,FecIniIrl=@pmFecIniIrl,FecFinIrl=@pmFecFinIrl
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,DiasSan,DiasDnrPen,DiasDnrSal,NovDsr,FecNovIng,FecNovRet,FecNovVsp
,FecIniSln,FecFinSln,FecIniIge,FecFinIge,FecIniLma,FecFinLma,FecIniVac,FecFinVac,FecIniVct,FecFinVct,FecIniIrl,FecFinIrl
 FROM Trn_NomAlqNov
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY  Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmDiasSan DECIMAL(14,4),@pmDiasDnrPen INT,@pmDiasDnrSal INT,@pmNovDsr INT,@pmFecNovIng SMALLDATETIME,@pmFecNovRet SMALLDATETIME,@pmFecNovVsp SMALLDATETIME
,@pmFecIniSln SMALLDATETIME,@pmFecFinSln SMALLDATETIME,@pmFecIniIge SMALLDATETIME,@pmFecFinIge SMALLDATETIME,@pmFecIniLma SMALLDATETIME,@pmFecFinLma SMALLDATETIME,@pmFecIniVac SMALLDATETIME,@pmFecFinVac SMALLDATETIME
,@pmFecIniVct SMALLDATETIME,@pmFecFinVct SMALLDATETIME,@pmFecIniIrl SMALLDATETIME,@pmFecFinIrl SMALLDATETIME
 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,DiasSan,DiasDnrPen,DiasDnrSal
,NovDsr,FecNovIng,FecNovRet,FecNovVsp,FecIniSln,FecFinSln,FecIniIge,FecFinIge,FecIniLma,FecFinLma,FecIniVac,FecFinVac,FecIniVct,FecFinVct,FecIniIrl,FecFinIrl)
 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,@pmDiasSan,@pmDiasDnrPen,@pmDiasDnrSal,@pmNovDsr,@pmFecNovIng,@pmFecNovRet,@pmFecNovVsp,@pmFecIniSln,@pmFecFinSln,@pmFecIniIge,@pmFecFinIge,@pmFecIniLma,@pmFecFinLma
,@pmFecIniVac,@pmFecFinVac,@pmFecIniVct,@pmFecFinVct,@pmFecIniIrl,@pmFecFinIrl)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasAux_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum VARCHAR(4),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuNiifCuentasAux (nAnno,nMes,IdCia,nReg,IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,SubAuxiliar,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos,TipoAfiVehic)
 SELECT @pmnAnno,@pmnMes,S.IdCia,0,S.IdCuenta,S.IdTercero,S.CodAgncia,S.IdCCosto,S.IdSubCos,S.SubAuxiliar,S.TipoAcum,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0,S.TipoAfiVehic
 FROM AcuNiifCuentasAux AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt AND S.TipoAcum=@pmTipoAcum
 AND NOT EXISTS (SELECT * FROM AcuNiifCuentasAux AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes 
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdTercero=S.IdTercero AND AC.CodAgncia=S.CodAgncia AND AC.IdCCosto=S.IdCCosto AND AC.IdSubCos=S.IdSubCos
 AND AC.SubAuxiliar=S.SubAuxiliar AND AC.TipoAcum=S.TipoAcum AND AC.TipoAfiVehic=S.TipoAfiVehic)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncAuxInvSaldoAct] (@pmIdProducto VARCHAR(16),@pmIdCia CHAR(2),@pmIdBodega VARCHAR(4))
RETURNS DECIMAL(14,4)
 AS  
 BEGIN
  DECLARE @SaldoAct DECIMAL(14,4)
  IF (@pmIdCia='00' OR @pmIdCia='0')
   BEGIN
    IF @pmIdBodega='0'
     BEGIN
      SET @SaldoAct=(SELECT SUM(SaldoActual) FROM ProdSaldos WHERE IdProducto=@pmIdProducto)
     END
    ELSE
     BEGIN
      SET @SaldoAct=(SELECT SUM(SaldoActual) FROM ProdSaldos WHERE IdProducto=@pmIdProducto AND IdBodega=@pmIdBodega)
     END
   END
  ELSE
   BEGIN
    IF @pmIdBodega='0'
     BEGIN
      SET @SaldoAct=(SELECT SUM(SaldoActual) FROM ProdSaldos WHERE IdProducto=@pmIdProducto AND IdCia=@pmIdCia)
     END
    ELSE
     BEGIN
      SET @SaldoAct=(SELECT SUM(SaldoActual) FROM ProdSaldos WHERE IdProducto=@pmIdProducto AND IdBodega=@pmIdBodega AND IdCia=@pmIdCia)
     END
   END
  RETURN @SaldoAct
 END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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','VCC') 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','VCC') AND TipoRef<>'SERVICIO'
  AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  GROUP BY K.IdProducto,K.IdBodega,IdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpProdMciasSal] @pmIdProducto VARCHAR(16)=Null
 AS 
 IF @pmIdProducto IS NULL
  BEGIN
   UPDATE ProdMcias 
   SET ExtciaAct=(SELECT ISNULL(SUM(SaldoActual),0) FROM ProdSaldos WHERE ProdSaldos.IdProducto=ProdMcias.IdProducto)
   WHERE TipoRef<>'SERVICIO'
  END
 ELSE
  BEGIN
   UPDATE ProdMcias 
   SET ExtciaAct=(SELECT ISNULL(SUM(SaldoActual),0) FROM ProdSaldos WHERE ProdSaldos.IdProducto=ProdMcias.IdProducto)
   WHERE IdProducto=@pmIdProducto
  END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryVehLecturasId] @pmTipDoc VARCHAR(3),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 SELECT MAX(Item) AS MxNum
 FROM Trn_VehLecturas
 WHERE TipDoc=@pmTipDoc AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelVehLecturas] @pmTipDoc VARCHAR(3),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_VehLecturas  WHERE TipDoc=@pmTipDoc AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsVehLecturas] @pmTipDoc VARCHAR(3),@pmNumero INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmTipoUnidad VARCHAR(3),@pmpVehiculo VARCHAR(10)
,@pmKmsAnterior INT,@pmKmsActual INT,@pmKms2Anterior INT,@pmKms2Actual INT,@pmTipoLectura VARCHAR(10),@pmObservacion VARCHAR(250),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_VehLecturas (TipDoc,Numero,IdCia,Item,Fecha,TipoUnidad,pVehiculo,KmsAnterior,KmsActual,Kms2Anterior,Kms2Actual,TipoLectura,Observacion,TimeSys,IdUsuario)
 VALUES (@pmTipDoc,@pmNumero,@pmIdCia,@pmItem,@pmFecha,@pmTipoUnidad,@pmpVehiculo,@pmKmsAnterior,@pmKmsActual,@pmKms2Anterior,@pmKms2Actual,@pmTipoLectura,@pmObservacion,@pmTimeSys,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryVehLecturas] @pmTipDoc VARCHAR(3),@pmNumero INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,Numero,IdCia,Item,Fecha,TipoUnidad,pVehiculo,KmsAnterior,KmsActual,Kms2Anterior,Kms2Actual,TipoLectura,Observacion,TimeSys,IdUsuario
 FROM Trn_VehLecturas
 WHERE TipDoc=@pmTipDoc AND Numero=@pmNumero AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpVehLecturas] @pmTipDoc VARCHAR(3),@pmNumero INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmTipoUnidad VARCHAR(3),@pmpVehiculo VARCHAR(10)
,@pmKmsAnterior INT,@pmKmsActual INT,@pmKms2Anterior INT,@pmKms2Actual INT,@pmTipoLectura VARCHAR(10),@pmObservacion VARCHAR(250)
 AS 
 UPDATE Trn_VehLecturas SET Fecha=@pmFecha,TipoUnidad=@pmTipoUnidad,pVehiculo=@pmpVehiculo,KmsAnterior=@pmKmsAnterior,KmsActual=@pmKmsActual,Kms2Anterior=@pmKms2Anterior,Kms2Actual=@pmKms2Actual
,TipoLectura=@pmTipoLectura,Observacion=@pmObservacion
 WHERE TipDoc=@pmTipDoc AND Numero=@pmNumero AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryVehLecturasVeh] @pmTipoUnidad VARCHAR(3),@pmpVehiculo VARCHAR(10)=Null
 AS 
 SELECT TipDoc,Numero,IdCia,Item,Fecha,TipoUnidad,pVehiculo,KmsAnterior,KmsActual
,Kms2Anterior,Kms2Actual,TipoLectura,Observacion,TimeSys,IdUsuario
 FROM Trn_VehLecturas
 WHERE TipoUnidad=@pmTipoUnidad AND pVehiculo LIKE ISNULL(@pmpVehiculo,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposMnjo] @pmIdMnjo VARCHAR(4),@pmManejoMcia VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposMnjo SET ManejoMcia=@pmManejoMcia,Inactivo=@pmInactivo
 WHERE IdMnjo=@pmIdMnjo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposMnjo] @pmIdMnjo VARCHAR(4),@pmManejoMcia VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposMnjo (IdMnjo,ManejoMcia,Inactivo)
 VALUES (@pmIdMnjo,@pmManejoMcia,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposMnjo] @pmIdMnjo VARCHAR(4)
 AS 
 DELETE FROM TiposMnjo
 WHERE IdMnjo=@pmIdMnjo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelBusClaseCue] @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM BusClaseCue
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_NiifCom] @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmIdCia CHAR(2),@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
,@pmtmSaldoAnterior2 MONEY,@pmtmTotalDebitos2 MONEY,@pmtmTotalCreditos2 MONEY,@pmtmNuevoSaldo2 MONEY,@pmtmVrPresupto MONEY
 AS 
 INSERT INTO tm_NiifCom (tmEst,tmIdCuenta,tmIdCia,tmItem,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmCodAgncia,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos
,tmNuevoSaldo,tmSaldoAnterior2,tmTotalDebitos2,tmTotalCreditos2,tmNuevoSaldo2,tmVrPresupto)
 VALUES (@pmtmEst,@pmtmIdCuenta,@pmtmIdCia,@pmtmItem,@pmtmIdTercero,@pmtmIdVehiculo,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmCodAgncia,@pmtmTipo,@pmtmSaldoAnterior,@pmtmTotalDebitos
,@pmtmTotalCreditos,@pmtmNuevoSaldo,@pmtmSaldoAnterior2,@pmtmTotalDebitos2,@pmtmTotalCreditos2,@pmtmNuevoSaldo2,@pmtmVrPresupto)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelNovedadRep] @pmIdNov VARCHAR(4)
 AS 
 DELETE FROM NovedadRep
 WHERE IdNov=@pmIdNov
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpNovedadRep] @pmIdNov VARCHAR(4),@pmNovedad VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE NovedadRep SET Novedad=@pmNovedad,Inactivo=@pmInactivo
 WHERE IdNov=@pmIdNov
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsNovedadRep] @pmIdNov VARCHAR(4),@pmNovedad VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO NovedadRep (IdNov,Novedad,Inactivo)
 VALUES (@pmIdNov,@pmNovedad,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNovedadRep] @pmIdNov VARCHAR(4)
 AS 
 IF @pmIdNov IS NULL
  SELECT IdNov,Novedad
  FROM NovedadRep
  WHERE Inactivo=0
  ORDER BY IdNov
 ELSE
  SELECT IdNov,Novedad,Inactivo
  FROM NovedadRep
  WHERE IdNov=@pmIdNov
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryLineasCredLta]
 AS 
 SELECT  IdLinea,LinCredito,CdTasaDef,TipoInteres,TasaEfectiva,CdTarifIva
,TIV.Tarifa AS TarifIva,CdTarifRet,TR.Tarifa AS TarifRet
 FROM LineasCred AS L LEFT JOIN Tablapor AS TIV ON L.CdTarifIva=TIV.IdTarifa
 LEFT JOIN Tablapor AS TR ON L.CdTarifRet=TR.IdTarifa
 LEFT JOIN TasasInteres AS TA ON L.CdTasaDef=TA.IdTasa
 WHERE L.Inactivo=0
 ORDER BY LinCredito
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMayAnotacionesLta] @pmNContratoIni INT,@pmNContratoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,Fecha,Detalle,A.IdUsuario AS CdUsuario,Usuario,NContrato,IdCia
 FROM Trn_MayAnotaciones AS A INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 WHERE NContrato BETWEEN @pmNContratoIni AND @pmNContratoFin AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMayAnotacionesItm] @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT MAX(Item) AS MXITM
 FROM Trn_MayAnotaciones
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposVeh] @pmIdTipoVeh VARCHAR(4)
 AS 
 SELECT IdTipoVeh,TipoVehiculo,Configuracion,Ejes,Llantas,CapacPeso,UndCapac,CapacVol
,UndCapVol,CubAlto,CubLargo,CubAncho,PesoBruto,Inactivo
 FROM TiposVeh
 WHERE IdTipoVeh=@pmIdTipoVeh
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposVeh] @pmIdTipoVeh VARCHAR(4),@pmTipoVehiculo VARCHAR(50)
,@pmConfiguracion VARCHAR(6),@pmEjes INT,@pmLlantas INT,@pmCapacPeso DECIMAL(14,4),@pmUndCapac VARCHAR(10)
,@pmCapacVol DECIMAL(14,4),@pmUndCapVol VARCHAR(10),@pmCubAlto DECIMAL(14,4),@pmCubLargo DECIMAL(14,4),@pmCubAncho DECIMAL(14,4)
,@pmPesoBruto DECIMAL(14,4),@pmInactivo BIT
 AS 
 INSERT INTO TiposVeh (IdTipoVeh,TipoVehiculo,Configuracion,Ejes,Llantas,CapacPeso,UndCapac,CapacVol,UndCapVol,CubAlto,CubLargo,CubAncho,PesoBruto,Inactivo)
 VALUES (@pmIdTipoVeh,@pmTipoVehiculo,@pmConfiguracion,@pmEjes,@pmLlantas,@pmCapacPeso,@pmUndCapac,@pmCapacVol,@pmUndCapVol,@pmCubAlto,@pmCubLargo,@pmCubAncho,@pmPesoBruto,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposVeh] @pmIdTipoVeh VARCHAR(4),@pmTipoVehiculo VARCHAR(50)
,@pmConfiguracion VARCHAR(6),@pmEjes INT,@pmLlantas INT,@pmCapacPeso DECIMAL(14,4),@pmUndCapac VARCHAR(10)
,@pmCapacVol DECIMAL(14,4),@pmUndCapVol VARCHAR(10),@pmCubAlto DECIMAL(14,4),@pmCubLargo DECIMAL(14,4),@pmCubAncho DECIMAL(14,4)
,@pmPesoBruto DECIMAL(14,4),@pmInactivo BIT
 AS 
 UPDATE TiposVeh SET TipoVehiculo=@pmTipoVehiculo,Configuracion=@pmConfiguracion,Ejes=@pmEjes,Llantas=@pmLlantas
,CapacPeso=@pmCapacPeso,UndCapac=@pmUndCapac,CapacVol=@pmCapacVol,UndCapVol=@pmUndCapVol,CubAlto=@pmCubAlto
,CubLargo=@pmCubLargo,CubAncho=@pmCubAncho,PesoBruto=@pmPesoBruto,Inactivo=@pmInactivo
 WHERE IdTipoVeh=@pmIdTipoVeh
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposVehDso] 
 AS 
 SELECT IdTipoVeh,TipoVehiculo,Configuracion,Ejes,Llantas,PesoBruto,CapacPeso,UndCapac
 ,CubAlto,CubLargo,CubAncho,CapacVol,UndCapVol
 FROM TiposVeh
 WHERE Inactivo=0
 ORDER BY TipoVehiculo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraOrdenServ] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmFecRecibo SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdPoseedor VARCHAR(16),@pmIdConductor VARCHAR(16),@pmnRemolque VARCHAR(10),@pmTipoAfiVehic VARCHAR(10),@pmModalidad VARCHAR(10)
,@pmVrTotal MONEY,@pmVrCosto MONEY,@pmCantidad DECIMAL(14,4),@pmVrAnticipo MONEY,@pmIdDestino VARCHAR(8),@pmTipoOrden VARCHAR(10),@pmIdEstacion VARCHAR(4),@pmNomEstacion VARCHAR(50),@pmTipoResiduo VARCHAR(100),@pmRemCliente VARCHAR(30),@pmNomRecibido VARCHAR(150),@pmFecIngCertif SMALLDATETIME,@pmFecCertificado SMALLDATETIME,@pmNumRemesa INT,@pmCdCiaRem CHAR(2)
,@pmEstCumplido INT,@pmEstFactura INT,@pmTipFact VARCHAR(3),@pmNumFactura INT,@pmCdCiaFact CHAR(2),@pmkmtInicial INT,@pmkmtFinal INT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4),@pmEstOrden INT,@pmTipLiq VARCHAR(3),@pmNumLiquida INT,@pmCdCiaLiq CHAR(2),@pmNumAprob INT,@pmIdCiaApr CHAR(2),@pmFecAprob SMALLDATETIME,@pmUsuAprob VARCHAR(11),@pmObservAprob VARCHAR(250)
,@pmCdRuta VARCHAR(4),@pmIdOrigen VARCHAR(8),@pmCantkms INT,@pmNumCump INT,@pmCdCiaCum CHAR(2),@pmCdTarIva VARCHAR(4),@pmCdTarRiv VARCHAR(4),@pmCdTarRet VARCHAR(4),@pmCdTarIca VARCHAR(4),@pmCdTarRtc VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraOrdenServ (TipDoc,NumOrden,IdCia,Fecha,FecDespacho,FecRecibo,IdCliente,IdAgencia,IdVehiculo,IdPoseedor,IdConductor,nRemolque,TipoAfiVehic,Modalidad,VrTotal,VrCosto,Cantidad,VrAnticipo,IdDestino,TipoOrden,IdEstacion,NomEstacion,TipoResiduo,RemCliente,NomRecibido,FecIngCertif,FecCertificado,NumRemesa,CdCiaRem,EstCumplido,EstFactura,TipFact,NumFactura,CdCiaFact
,kmtInicial,kmtFinal,EstOrden,TipLiq,NumLiquida,CdCiaLiq,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,NumAprob,IdCiaApr,FecAprob,UsuAprob,ObservAprob,CdRuta,IdOrigen,Cantkms,NumCump,CdCiaCum,CdTarIva,CdTarRiv,CdTarRet,CdTarIca,CdTarRtc)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmFecha,@pmFecDespacho,@pmFecRecibo,@pmIdCliente,@pmIdAgencia,@pmIdVehiculo,@pmIdPoseedor,@pmIdConductor,@pmnRemolque,@pmTipoAfiVehic,@pmModalidad,@pmVrTotal,@pmVrCosto,@pmCantidad,@pmVrAnticipo,@pmIdDestino,@pmTipoOrden,@pmIdEstacion,@pmNomEstacion,@pmTipoResiduo,@pmRemCliente,@pmNomRecibido,@pmFecIngCertif,@pmFecCertificado,@pmNumRemesa
,@pmCdCiaRem,@pmEstCumplido,@pmEstFactura,@pmTipFact,@pmNumFactura,@pmCdCiaFact,@pmkmtInicial,@pmkmtFinal,@pmEstOrden,@pmTipLiq,@pmNumLiquida,@pmCdCiaLiq,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmNumAprob,@pmIdCiaApr,@pmFecAprob,@pmUsuAprob,@pmObservAprob,@pmCdRuta,@pmIdOrigen,@pmCantkms,@pmNumCump,@pmCdCiaCum,@pmCdTarIva,@pmCdTarRiv,@pmCdTarRet,@pmCdTarIca,@pmCdTarRtc)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenServ] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumOrden,IdCia,Fecha,FecDespacho,FecRecibo,IdCliente,IdAgencia,IdVehiculo,IdPoseedor,IdConductor,nRemolque,TipoAfiVehic,Modalidad,VrTotal,VrCosto,Cantidad,VrAnticipo,IdDestino,TipoOrden,IdEstacion,NomEstacion,TipoResiduo,RemCliente,NomRecibido
,FecIngCertif,FecCertificado,NumRemesa,CdCiaRem,EstCumplido,EstFactura,TipFact,NumFactura,CdCiaFact,kmtInicial,kmtFinal,EstOrden,TipLiq,NumLiquida,CdCiaLiq,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,NumAprob,IdCiaApr,FecAprob,UsuAprob,ObservAprob
,CdRuta,IdOrigen,Cantkms,NumCump,CdCiaCum,CdTarIva,CdTarRiv,CdTarRet,CdTarIca,CdTarRtc,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraOrdenServ
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraOrdenServ] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmFecRecibo SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdPoseedor VARCHAR(16),@pmIdConductor VARCHAR(16),@pmnRemolque VARCHAR(10),@pmTipoAfiVehic VARCHAR(10),@pmModalidad VARCHAR(10)
,@pmVrTotal MONEY,@pmVrCosto MONEY,@pmCantidad DECIMAL(14,4),@pmVrAnticipo MONEY,@pmIdDestino VARCHAR(8),@pmTipoOrden VARCHAR(10),@pmIdEstacion VARCHAR(4),@pmNomEstacion VARCHAR(50),@pmTipoResiduo VARCHAR(100),@pmRemCliente VARCHAR(30),@pmNomRecibido VARCHAR(150),@pmFecIngCertif SMALLDATETIME,@pmFecCertificado SMALLDATETIME,@pmNumRemesa INT,@pmCdCiaRem CHAR(2)
,@pmEstCumplido INT,@pmEstFactura INT,@pmTipFact VARCHAR(3),@pmNumFactura INT,@pmCdCiaFact CHAR(2),@pmkmtInicial INT,@pmkmtFinal INT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4),@pmEstOrden INT,@pmTipLiq VARCHAR(3),@pmNumLiquida INT,@pmCdCiaLiq CHAR(2),@pmNumAprob INT,@pmIdCiaApr CHAR(2),@pmFecAprob SMALLDATETIME,@pmUsuAprob VARCHAR(11),@pmObservAprob VARCHAR(250)
,@pmCdRuta VARCHAR(4),@pmIdOrigen VARCHAR(8),@pmCantkms INT,@pmNumCump INT,@pmCdCiaCum CHAR(2),@pmCdTarIva VARCHAR(4),@pmCdTarRiv VARCHAR(4),@pmCdTarRet VARCHAR(4),@pmCdTarIca VARCHAR(4),@pmCdTarRtc VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraOrdenServ SET Fecha=@pmFecha,FecDespacho=@pmFecDespacho,FecRecibo=@pmFecRecibo,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdVehiculo=@pmIdVehiculo,IdPoseedor=@pmIdPoseedor,IdConductor=@pmIdConductor,nRemolque=@pmnRemolque,TipoAfiVehic=@pmTipoAfiVehic,Modalidad=@pmModalidad,VrTotal=@pmVrTotal,VrCosto=@pmVrCosto,Cantidad=@pmCantidad,VrAnticipo=@pmVrAnticipo
,IdDestino=@pmIdDestino,TipoOrden=@pmTipoOrden,IdEstacion=@pmIdEstacion,NomEstacion=@pmNomEstacion,TipoResiduo=@pmTipoResiduo,RemCliente=@pmRemCliente,NomRecibido=@pmNomRecibido,FecIngCertif=@pmFecIngCertif,FecCertificado=@pmFecCertificado,NumRemesa=@pmNumRemesa,CdCiaRem=@pmCdCiaRem,EstCumplido=@pmEstCumplido,EstFactura=@pmEstFactura,TipFact=@pmTipFact,NumFactura=@pmNumFactura,CdCiaFact=@pmCdCiaFact
,kmtInicial=@pmkmtInicial,kmtFinal=@pmkmtFinal,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,EstOrden=@pmEstOrden,TipLiq=@pmTipLiq,NumLiquida=@pmNumLiquida,CdCiaLiq=@pmCdCiaLiq,NumAprob=@pmNumAprob,IdCiaApr=@pmIdCiaApr,FecAprob=@pmFecAprob,UsuAprob=@pmUsuAprob,ObservAprob=@pmObservAprob
,CdRuta=@pmCdRuta,IdOrigen=@pmIdOrigen,Cantkms=@pmCantkms,NumCump=@pmNumCump,CdCiaCum=@pmCdCiaCum,CdTarIva=@pmCdTarIva,CdTarRiv=@pmCdTarRiv,CdTarRet=@pmCdTarRet,CdTarIca=@pmCdTarIca,CdTarRtc=@pmCdTarRtc
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsClasePol] @pmIdClase VARCHAR(4),@pmClasePoliza VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ClasePol (IdClase,ClasePoliza,Inactivo)
 VALUES (@pmIdClase,@pmClasePoliza,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpClasePol] @pmIdClase VARCHAR(4),@pmClasePoliza VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ClasePol SET ClasePoliza=@pmClasePoliza,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelClasePol] @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClasePol
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNomPrendas] @pmIdPrend VARCHAR(4)
 AS 
 SELECT IdPrend,DescPrenda,TipoPrenda,VrCostoUnd,Inactivo
 FROM NomPrendas WHERE IdPrend=@pmIdPrend
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpNomPrendas] @pmIdPrend VARCHAR(4),@pmDescPrenda VARCHAR(100),@pmTipoPrenda VARCHAR(10),@pmVrCostoUnd MONEY,@pmInactivo BIT
 AS 
 UPDATE NomPrendas SET DescPrenda=@pmDescPrenda,TipoPrenda=@pmTipoPrenda,Inactivo=@pmInactivo,VrCostoUnd=@pmVrCostoUnd
 WHERE IdPrend=@pmIdPrend
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelNomPrendas] @pmIdPrend VARCHAR(4)
 AS 
 DELETE FROM NomPrendas WHERE IdPrend=@pmIdPrend
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsNomPrendas] @pmIdPrend VARCHAR(4),@pmDescPrenda VARCHAR(100),@pmTipoPrenda VARCHAR(10),@pmVrCostoUnd MONEY,@pmInactivo BIT
 AS 
 INSERT INTO NomPrendas (IdPrend,DescPrenda,TipoPrenda,Inactivo,VrCostoUnd)
 VALUES (@pmIdPrend,@pmDescPrenda,@pmTipoPrenda,@pmInactivo,@pmVrCostoUnd)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPermisos] @pmIdUsuario VARCHAR(11),@pmIdObj VARCHAR(10)=Null
 AS 
 DELETE FROM adm_Permisos
 WHERE IdUsuario=@pmIdUsuario AND IdObj LIKE ISNULL(@pmIdObj,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudInventario] @pmTipDoc VARCHAR(3),@pmNumInvent INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumInvent,IdCia,Fecha,FecDespacho,IdTipoServ,IdCliente,IdAgencia,IdRemitente,Remitente,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,IdDestinatario,Destinatario,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest,IdOrigen,IdDestino,IdVend,pVehiculo,CdConductor,nRemolque
,TipoAfiVehic,NitEmpresa,NomEmpresa,CajasEmp,CajasClie,VrDeclarado,VrServicio,NumCotizac,NumPlanilla,NumActa,TipOsm,NumOrden,IdCiaOsm,NomContacto,TelsContacto,emlContacto,Responsable,FormaPago,ContactoDest,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_MudInventario
 WHERE TipDoc=@pmTipDoc AND NumInvent=@pmNumInvent AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMudInventario] @pmTipDoc VARCHAR(3),@pmNumInvent INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_MudInventario
 WHERE TipDoc=@pmTipDoc AND NumInvent=@pmNumInvent AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMudInventario] @pmTipDoc VARCHAR(3),@pmNumInvent INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmIdTipoServ VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdRemitente VARCHAR(16),@pmRemitente VARCHAR(150),@pmDirCargue VARCHAR(500),@pmTipoInmRem VARCHAR(30),@pmPisoCargue VARCHAR(10),@pmTelsRemite VARCHAR(30),@pmCelRemite VARCHAR(30),@pmIdDestinatario VARCHAR(16),@pmDestinatario VARCHAR(150),@pmDirDescargue VARCHAR(500)
,@pmTipoInmDest VARCHAR(30),@pmPisoDescargue VARCHAR(10),@pmTelsDest VARCHAR(30),@pmCelDest VARCHAR(30),@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmIdVend VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmnRemolque VARCHAR(10),@pmTipoAfiVehic VARCHAR(10),@pmNitEmpresa VARCHAR(16),@pmNomEmpresa VARCHAR(150),@pmCajasEmp INT,@pmCajasClie INT,@pmVrDeclarado MONEY,@pmVrServicio MONEY,@pmNumCotizac VARCHAR(20),@pmNumPlanilla VARCHAR(20),@pmNumActa VARCHAR(20)
,@pmTipOsm VARCHAR(3),@pmNumOrden INT,@pmIdCiaOsm CHAR(2),@pmNomContacto VARCHAR(150),@pmTelsContacto VARCHAR(30),@pmemlContacto VARCHAR(100),@pmResponsable VARCHAR(150),@pmFormaPago VARCHAR(50),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2500),@pmIdEstado VARCHAR(4),@pmContactoDest VARCHAR(150),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_MudInventario (TipDoc,NumInvent,IdCia,Fecha,FecDespacho,IdTipoServ,IdCliente,IdAgencia,IdRemitente,Remitente,DirCargue,TipoInmRem,PisoCargue,TelsRemite,CelRemite,IdDestinatario,Destinatario,DirDescargue,TipoInmDest,PisoDescargue,TelsDest,CelDest,IdOrigen,IdDestino,IdVend,pVehiculo,CdConductor,nRemolque,TipoAfiVehic,NitEmpresa,NomEmpresa,CajasEmp,CajasClie,VrDeclarado,VrServicio,NumCotizac,NumPlanilla,NumActa,TipOsm,NumOrden,IdCiaOsm,NomContacto,TelsContacto,emlContacto,Responsable
,FormaPago,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,ContactoDest)
 VALUES (@pmTipDoc,@pmNumInvent,@pmIdCia,@pmFecha,@pmFecDespacho,@pmIdTipoServ,@pmIdCliente,@pmIdAgencia,@pmIdRemitente,@pmRemitente,@pmDirCargue,@pmTipoInmRem,@pmPisoCargue,@pmTelsRemite,@pmCelRemite,@pmIdDestinatario,@pmDestinatario,@pmDirDescargue,@pmTipoInmDest,@pmPisoDescargue,@pmTelsDest,@pmCelDest,@pmIdOrigen,@pmIdDestino,@pmIdVend,@pmpVehiculo,@pmCdConductor,@pmnRemolque,@pmTipoAfiVehic,@pmNitEmpresa,@pmNomEmpresa,@pmCajasEmp,@pmCajasClie,@pmVrDeclarado,@pmVrServicio
,@pmNumCotizac,@pmNumPlanilla,@pmNumActa,@pmTipOsm,@pmNumOrden,@pmIdCiaOsm,@pmNomContacto,@pmTelsContacto,@pmemlContacto,@pmResponsable,@pmFormaPago,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmContactoDest)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMudInventario] @pmTipDoc VARCHAR(3),@pmNumInvent INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmIdTipoServ VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdRemitente VARCHAR(16),@pmRemitente VARCHAR(150),@pmDirCargue VARCHAR(500),@pmTipoInmRem VARCHAR(30),@pmPisoCargue VARCHAR(10),@pmTelsRemite VARCHAR(30),@pmCelRemite VARCHAR(30),@pmIdDestinatario VARCHAR(16),@pmDestinatario VARCHAR(150),@pmDirDescargue VARCHAR(500)
,@pmTipoInmDest VARCHAR(30),@pmPisoDescargue VARCHAR(10),@pmTelsDest VARCHAR(30),@pmCelDest VARCHAR(30),@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmIdVend VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmnRemolque VARCHAR(10),@pmTipoAfiVehic VARCHAR(10),@pmNitEmpresa VARCHAR(16),@pmNomEmpresa VARCHAR(150),@pmCajasEmp INT,@pmCajasClie INT,@pmVrDeclarado MONEY,@pmVrServicio MONEY,@pmNumCotizac VARCHAR(20),@pmNumPlanilla VARCHAR(20),@pmNumActa VARCHAR(20)
,@pmTipOsm VARCHAR(3),@pmNumOrden INT,@pmIdCiaOsm CHAR(2),@pmNomContacto VARCHAR(150),@pmTelsContacto VARCHAR(30),@pmemlContacto VARCHAR(100),@pmResponsable VARCHAR(150),@pmFormaPago VARCHAR(50),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2500),@pmIdEstado VARCHAR(4),@pmContactoDest VARCHAR(150),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_MudInventario SET Fecha=@pmFecha,FecDespacho=@pmFecDespacho,IdTipoServ=@pmIdTipoServ,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdRemitente=@pmIdRemitente,Remitente=@pmRemitente,DirCargue=@pmDirCargue,TipoInmRem=@pmTipoInmRem,PisoCargue=@pmPisoCargue,TelsRemite=@pmTelsRemite,CelRemite=@pmCelRemite,IdDestinatario=@pmIdDestinatario,Destinatario=@pmDestinatario,DirDescargue=@pmDirDescargue,TipoInmDest=@pmTipoInmDest,PisoDescargue=@pmPisoDescargue,TelsDest=@pmTelsDest,CelDest=@pmCelDest
,IdOrigen=@pmIdOrigen,IdDestino=@pmIdDestino,IdVend=@pmIdVend,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,nRemolque=@pmnRemolque,TipoAfiVehic=@pmTipoAfiVehic,NitEmpresa=@pmNitEmpresa,NomEmpresa=@pmNomEmpresa,CajasEmp=@pmCajasEmp,CajasClie=@pmCajasClie,VrDeclarado=@pmVrDeclarado,VrServicio=@pmVrServicio,NumCotizac=@pmNumCotizac,NumPlanilla=@pmNumPlanilla,NumActa=@pmNumActa,TipOsm=@pmTipOsm,NumOrden=@pmNumOrden,IdCiaOsm=@pmIdCiaOsm,NomContacto=@pmNomContacto,TelsContacto=@pmTelsContacto,emlContacto=@pmemlContacto
,Responsable=@pmResponsable,FormaPago=@pmFormaPago,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,ContactoDest=@pmContactoDest,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumInvent=@pmNumInvent AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTercClieRefItm] @pmIdClie VARCHAR(16)
 AS 
 SELECT MAX(Item) AS MXNUM 
 FROM TercClieRef
 WHERE IdClie=@pmIdClie
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMudArticulos] @pmIdArticulo VARCHAR(16),@pmArticulo VARCHAR(250),@pmIdSeccion VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE MudArticulos SET Articulo=@pmArticulo,IdSeccion=@pmIdSeccion,Inactivo=@pmInactivo
 WHERE IdArticulo=@pmIdArticulo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMudArticulos] @pmIdArticulo VARCHAR(16)
 AS 
 DELETE FROM MudArticulos
 WHERE IdArticulo=@pmIdArticulo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMudArticulos] @pmIdArticulo VARCHAR(16),@pmArticulo VARCHAR(250),@pmIdSeccion VARCHAR(4),@pmInactivo BIT
 AS 
 INSERT INTO MudArticulos (IdArticulo,Articulo,IdSeccion,Inactivo)
 VALUES (@pmIdArticulo,@pmArticulo,@pmIdSeccion,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudArticulos] @pmIdArticulo VARCHAR(16)
 AS 
 SELECT IdArticulo,Articulo,IdSeccion,Inactivo
 FROM MudArticulos
 WHERE IdArticulo=@pmIdArticulo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudArticulosLta] 
 AS 
 SELECT IdArticulo,Articulo,A.IdSeccion AS CdSeccion,Seccion,A.Inactivo
 FROM MudArticulos AS A INNER JOIN MudSecciones AS S ON A.IdSeccion=S.IdSeccion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTFmtos] @pmnFormato VARCHAR(10),@pmFmtoFactura VARCHAR(50),@pmTipFac VARCHAR(3)
 AS 
 UPDATE Sys_TFmtos SET FmtoFactura=@pmFmtoFactura,TipFac=@pmTipFac
 WHERE nFormato=@pmnFormato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTFmtos] @pmnFormato VARCHAR(10),@pmFmtoFactura VARCHAR(50),@pmTipFac VARCHAR(3)
 AS 
 INSERT INTO Sys_TFmtos (nFormato,FmtoFactura,TipFac)
 VALUES (@pmnFormato,@pmFmtoFactura,@pmTipFac)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTFmtos] @pmnFormato VARCHAR(10)
 AS 
 DELETE FROM Sys_TFmtos
 WHERE nFormato=@pmnFormato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmPrecioVenta1 MONEY,@pmPrecioVenta2 MONEY,@pmPrecioVenta3 MONEY
,@pmPrecioVenta4 MONEY,@pmPrecioVenta5 MONEY
 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,PrecioVenta1,PrecioVenta2,PrecioVenta3,PrecioVenta4,PrecioVenta5)
 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,@pmPrecioVenta1,@pmPrecioVenta2,@pmPrecioVenta3,@pmPrecioVenta4,@pmPrecioVenta5)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDeltm_ListaPrec] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_ListaPrec
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelModulos] @pmIdMod VARCHAR(3)
 AS 
 DELETE FROM Sys_Modulos
 WHERE IdMod LIKE ISNULL(@pmIdMod,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryModulosId] @pmnModulo VARCHAR(10)=Null
 AS 
 SELECT MAX(Consecutivo) AS MXconce
 FROM Sys_Modulos
 WHERE nModulo LIKE ISNULL(@pmnModulo,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTaxRutasDso] 
 AS 
 SELECT   IdRuta,NomRuta,IdRuta+' '+NomRuta AS DSRUT
 FROM TaxRutas
 WHERE  Inactivo=0
 ORDER BY IdRuta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTaxRutas] @pmIdRuta VARCHAR(4)
 AS 
 SELECT IdRuta,NomRuta,TipoRuta,TarifPago,TarifCobro,Origen,Destino,CodRuta,Inactivo
 FROM TaxRutas
 WHERE IdRuta=@pmIdRuta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTaxRutas] @pmIdRuta VARCHAR(4)
 AS 
 DELETE FROM TaxRutas  WHERE IdRuta=@pmIdRuta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsGruposAgenc] @pmIdGrupo VARCHAR(4),@pmGrupoAgenc VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO GruposAgenc (IdGrupo,GrupoAgenc,Inactivo)
 VALUES (@pmIdGrupo,@pmGrupoAgenc,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelGruposAgenc] @pmIdGrupo VARCHAR(4)
 AS 
 DELETE FROM GruposAgenc
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpGruposAgenc] @pmIdGrupo VARCHAR(4),@pmGrupoAgenc VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE GruposAgenc SET GrupoAgenc=@pmGrupoAgenc,Inactivo=@pmInactivo
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpProdProcesos] @pmId INT,@pmIdProducto VARCHAR(16),@pmItem INT,@pmIdTipProc VARCHAR(4),@pmCostoEst MONEY,@pmUltCosto MONEY,@pmProcFinal BIT
 AS 
 UPDATE ProdProcesos SET IdProducto=@pmIdProducto,Item=@pmItem,IdTipProc=@pmIdTipProc,CostoEst=@pmCostoEst,UltCosto=@pmUltCosto,ProcFinal=@pmProcFinal
 WHERE [Id]=@pmId
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsProdProcesos] @pmIdProducto VARCHAR(16),@pmItem INT,@pmIdTipProc VARCHAR(4),@pmCostoEst MONEY,@pmUltCosto MONEY,@pmProcFinal BIT
 AS 
 INSERT INTO ProdProcesos (IdProducto,Item,IdTipProc,CostoEst,UltCosto,ProcFinal)
 VALUES (@pmIdProducto,@pmItem,@pmIdTipProc,@pmCostoEst,@pmUltCosto,@pmProcFinal)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPucLta] 
 AS 
 SELECT P.IdCuenta,P.NomCuenta,P.Mayor,P.Movimiento,P.Tercero,P.Vehiculo,P.Agencia,P.CentroCosto,P.SubCentro,P.Vencimiento,P.BaseGravable,P.Porcntaje
,P.Naturaleza,P.Ajustable,P.CuentaAjuDeb,P.CuentaAjuCre,P.FmtDian,P.ConDian,P.RenDec,P.Cheques,P.Referncias,P.CdTipDoc,P.FechaAdd,P.FechaUpdate,P.IdUsuario
,P.Inactivo,P.Afijos,P.ListarConc,P.ReqAutorizac,P.AntProveedor,P.IdCueNiif,PN.NomCuenta AS NiifNombre,U.Usuario
 FROM Puc AS P LEFT JOIN PucNiif AS PN ON P.IdCueNiif=PN.IdCuenta
 LEFT JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 ORDER BY P.IdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,IdCueNiif
  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,IdCueNiif
   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,IdCueNiif
   FROM  Puc
   WHERE Movimiento<>0 AND Ajustable<>0 AND  IdCuenta BETWEEN @pmCueIni AND @pmCueFin
   ORDER BY IdCuenta
 END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,ListarConc,ReqAutorizac,AntProveedor,IdCueNiif)
 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,ListarConc,ReqAutorizac,AntProveedor,IdCueNiif
 FROM Puc WHERE IdCuenta=@pmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,@pmListarConc VARCHAR(4),@pmReqAutorizac BIT,@pmAntProveedor BIT,@pmIdCueNiif 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,ListarConc,ReqAutorizac,AntProveedor,IdCueNiif)
 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,@pmListarConc,@pmReqAutorizac,@pmAntProveedor,@pmIdCueNiif)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,@pmListarConc VARCHAR(4),@pmReqAutorizac BIT,@pmAntProveedor BIT,@pmIdCueNiif VARCHAR(16)
 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,ListarConc=@pmListarConc,ReqAutorizac=@pmReqAutorizac,AntProveedor=@pmAntProveedor,IdCueNiif=@pmIdCueNiif 
 WHERE IdCuenta=@pmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CodFEDian,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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.Inactivo AS Inactvo,OrdenServ,NovedadNom,CdConcDeveng,CdConcDed,RubroConc,MudReajuste,CodFEDian
,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 Grupo LIKE ISNULL(@pmGrupo,'%')
 AND  (EsServicio=ISNULL(@pmEsServicio,0) or EsServicio=ISNULL(@pmEsServicio,1))
 ORDER BY Concepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryLineasCredCueLta] @pmIdLinea VARCHAR(4)=Null
AS 
SELECT LN.IdLinea AS CdLinea,LinCredito,LN.IdTipoTerc AS CdTipoTerc,TipoTercero,Concepto,IdCuentaDb,P.NomCuenta AS NomCueDebito,IdCuentaCr,PC.NomCuenta AS NomCueCredito
FROM LineasCredCue AS LN INNER JOIN LineasCred AS L ON LN.IdLinea=L.IdLinea INNER JOIN TiposTerc AS T ON LN.IdTipoTerc=T.IdTipoTerc
LEFT JOIN Puc AS P ON LN.IdCuentaDb=P.IdCuenta LEFT JOIN Puc AS PC ON LN.IdCuentaCr=PC.IdCuenta
WHERE LN.IdLinea LIKE ISNULL(@pmIdLinea,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,Inactivo,Afijos,ListarConc,ReqAutorizac,AntProveedor,IdCueNiif
  FROM Puc 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,ListarConc,ReqAutorizac,AntProveedor,IdCueNiif
  FROM Puc
  WHERE IdCuenta=@pmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncProdOrdenNum] (@pmTipDoc VARCHAR(3),@pmNumCons INT,@pmIdCia CHAR(2),@pmItemCons INT,@pmNumOrdenIni INT,@pmNumOrdenFin INT)
RETURNS BIT
 AS  
 BEGIN
  DECLARE @Ordenes BIT
  IF @pmNumOrdenIni IS NULL
   SET @Ordenes=1
  ELSE
   BEGIN 
    IF EXISTS (SELECT 1 FROM Trn_ProdOrdenDet WHERE TipCons=@pmTipDoc AND NumCons=@pmNumCons AND IdCiaCons=@pmIdCia AND ItemCons=@pmItemCons
     AND NumOrden BETWEEN @pmNumOrdenIni AND @pmNumOrdenFin)
     BEGIN
      SET @Ordenes=1
     END
    ELSE
     BEGIN
      SET @Ordenes=0
     END
   END
 RETURN @Ordenes
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncProdOrdenTipo] (@pmTipDoc VARCHAR(3),@pmNumCons INT,@pmIdCia CHAR(2),@pmItemCons INT,@pmModalidad VARCHAR(10))
RETURNS BIT
 AS  
 BEGIN
  DECLARE @Ordenes BIT
  IF @pmModalidad IS NULL
   SET @Ordenes=1
  ELSE
   BEGIN 
    IF EXISTS (SELECT 1 FROM Trn_ProdOrden INNER JOIN Trn_ProdOrdenDet ON Trn_ProdOrden.TipDoc=Trn_ProdOrdenDet.TipDoc AND Trn_ProdOrden.NumOrden=Trn_ProdOrdenDet.NumOrden AND Trn_ProdOrden.IdCia=Trn_ProdOrdenDet.IdCia
     WHERE Trn_ProdOrdenDet.TipCons=@pmTipDoc AND Trn_ProdOrdenDet.NumCons=@pmNumCons AND Trn_ProdOrdenDet.IdCiaCons=@pmIdCia AND Trn_ProdOrdenDet.ItemCons=@pmItemCons
     AND Trn_ProdOrden.Anulado=0 AND Trn_ProdOrden.Modalidad=@pmModalidad)
     BEGIN
      SET @Ordenes=1
     END
    ELSE
     BEGIN
      SET @Ordenes=0
     END
   END
 RETURN @Ordenes
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncProdOrdenFecha] (@pmTipDoc VARCHAR(3),@pmNumCons INT,@pmIdCia CHAR(2),@pmItemCons INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME)
RETURNS BIT
 AS  
 BEGIN
  DECLARE @Ordenes BIT
  IF @pmFechaIni IS NULL
   SET @Ordenes=1
  ELSE
   BEGIN 
    IF EXISTS (SELECT 1 FROM Trn_ProdOrden INNER JOIN Trn_ProdOrdenDet ON Trn_ProdOrden.TipDoc=Trn_ProdOrdenDet.TipDoc AND Trn_ProdOrden.NumOrden=Trn_ProdOrdenDet.NumOrden AND Trn_ProdOrden.IdCia=Trn_ProdOrdenDet.IdCia
     WHERE Trn_ProdOrdenDet.TipCons=@pmTipDoc AND Trn_ProdOrdenDet.NumCons=@pmNumCons AND Trn_ProdOrdenDet.IdCiaCons=@pmIdCia AND Trn_ProdOrdenDet.ItemCons=@pmItemCons
     AND Trn_ProdOrden.Anulado=0 AND Trn_ProdOrden.Fecha BETWEEN @pmFechaIni AND @pmFechaFin)
     BEGIN
      SET @Ordenes=1
     END
    ELSE
     BEGIN
      SET @Ordenes=0
     END
   END
 RETURN @Ordenes
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmIngNoSalario MONEY,@pmPagosMedicPre MONEY,@pmAportesVolPen MONEY,@pmGastosRep MONEY,@pmIndemnizac 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,IngNoSalario=@pmIngNoSalario,PagosMedicPre=@pmPagosMedicPre,AportesVolPen=@pmAportesVolPen,GastosRep=@pmGastosRep,Indemnizac=@pmIndemnizac
 WHERE IdEmpleado=@pmIdEmpleado AND nAnno=@pmnAnno AND nMes=@pmnMes
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,IngNoSalario,PagosMedicPre,AportesVolPen,GastosRep,Indemnizac,FecAdd,FecUpdate,IdUsuario
 FROM Trn_NomFijos
 WHERE IdEmpleado=@pmIdEmpleado AND nAnno=@pmnAnno AND nMes=@pmnMes
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmIngNoSalario MONEY,@pmPagosMedicPre MONEY,@pmAportesVolPen MONEY,@pmGastosRep MONEY,@pmIndemnizac 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,IngNoSalario,PagosMedicPre,AportesVolPen,GastosRep,Indemnizac)
 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,@pmIngNoSalario,@pmPagosMedicPre,@pmAportesVolPen,@pmGastosRep,@pmIndemnizac)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrOtrosIng,VrSIntExcto
,IngNoSalario,VrIngresos,VrCertVda,VrCertEdu,PagosMedicPre,VrSalud,VrDedcido,VrPensiones,AportesVolPen
,VrCertAFC,GastosRep,Indemnizac,VrCertfcado,VrExcento,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_NomRes] @pmtmNumero VARCHAR(5)
 AS 
 DELETE FROM tm_NomRes
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoControles] @pmTipoUnidad CHAR(1),@pmNumUnidad VARCHAR(10),@pmIdConc VARCHAR(4)
 AS 
 SELECT TipoUnidad,NumUnidad,IdConc,TipoControl,Intervalo_km,Intervalo_dias,Intervalo_Horas,kmUltMtto,kmUltMttoRuta,kmUltMttoSat
,FecUltMtto,HsUltMtto,Comentarios,Inactivo,FechaCrea,IdUsuario
 FROM MttoControles
 WHERE TipoUnidad=@pmTipoUnidad AND NumUnidad=@pmNumUnidad AND IdConc=@pmIdConc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMttoControles] @pmTipoUnidad CHAR(1),@pmNumUnidad VARCHAR(10),@pmIdConc VARCHAR(4),@pmTipoControl INT,@pmIntervalo_km INT,@pmIntervalo_dias INT,@pmIntervalo_Horas INT,@pmkmUltMtto INT,@pmkmUltMttoRuta INT
,@pmkmUltMttoSat INT,@pmFecUltMtto SMALLDATETIME,@pmComentarios VARCHAR(1000),@pmInactivo BIT,@pmHsUltMtto INT
 AS 
 UPDATE MttoControles SET TipoControl=@pmTipoControl,Intervalo_km=@pmIntervalo_km,Intervalo_dias=@pmIntervalo_dias,Intervalo_Horas=@pmIntervalo_Horas,kmUltMtto=@pmkmUltMtto,kmUltMttoRuta=@pmkmUltMttoRuta,kmUltMttoSat=@pmkmUltMttoSat,FecUltMtto=@pmFecUltMtto
,HsUltMtto=@pmHsUltMtto,Comentarios=@pmComentarios,Inactivo=@pmInactivo
 WHERE TipoUnidad=@pmTipoUnidad AND NumUnidad=@pmNumUnidad AND IdConc=@pmIdConc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMttoControles] @pmTipoUnidad CHAR(1),@pmNumUnidad VARCHAR(10),@pmIdConc VARCHAR(4),@pmTipoControl INT,@pmIntervalo_km INT,@pmIntervalo_dias INT,@pmIntervalo_Horas INT,@pmkmUltMtto INT,@pmkmUltMttoRuta INT
,@pmkmUltMttoSat INT,@pmFecUltMtto SMALLDATETIME,@pmComentarios VARCHAR(1000),@pmInactivo BIT,@pmHsUltMtto INT,@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO MttoControles (TipoUnidad,NumUnidad,IdConc,TipoControl,Intervalo_km,Intervalo_dias,Intervalo_Horas,kmUltMtto,kmUltMttoRuta,kmUltMttoSat,FecUltMtto,HsUltMtto,Comentarios,Inactivo,FechaCrea,IdUsuario)
 VALUES (@pmTipoUnidad,@pmNumUnidad,@pmIdConc,@pmTipoControl,@pmIntervalo_km,@pmIntervalo_dias,@pmIntervalo_Horas,@pmkmUltMtto,@pmkmUltMttoRuta,@pmkmUltMttoSat,@pmFecUltMtto,@pmHsUltMtto,@pmComentarios,@pmInactivo,@pmFechaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTercCndDocLta] 
 AS 
 SELECT IdConductor,RazonSocial,Item,D.IdDoc AS CdDoc,Documento,Vigencia,D.Fecha,D.FecVence,Radicado,D.Descripcion
 FROM TercCndDoc AS D INNER JOIN ClaseDocCond AS C ON D.IdDoc=C.IdDoc
 INNER JOIN Terceros AS T ON D.IdConductor=T.IdTercero
 WHERE C.Inactivo=0
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpClaseDocCond] @pmIdDoc VARCHAR(4),@pmDocumento VARCHAR(150),@pmVigencia BIT,@pmInactivo BIT
 AS 
 UPDATE ClaseDocCond SET Documento=@pmDocumento,Vigencia=@pmVigencia,Inactivo=@pmInactivo
 WHERE IdDoc=@pmIdDoc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsClaseDocCond] @pmIdDoc VARCHAR(4),@pmDocumento VARCHAR(150),@pmVigencia BIT,@pmInactivo BIT
 AS 
 INSERT INTO ClaseDocCond (IdDoc,Documento,Vigencia,Inactivo)
 VALUES (@pmIdDoc,@pmDocumento,@pmVigencia,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryClaseDocCond] @pmIdDoc VARCHAR(4)
 AS 
 IF @pmIdDoc IS NULL
  BEGIN
   SELECT IdDoc,Documento,Vigencia FROM ClaseDocCond
   WHERE Inactivo=0
  END
 ELSE
  BEGIN
   SELECT IdDoc,Documento,Vigencia,Inactivo FROM ClaseDocCond
   WHERE IdDoc=@pmIdDoc
  END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelClaseDocCond] @pmIdDoc VARCHAR(4)
 AS 
 DELETE FROM ClaseDocCond
 WHERE IdDoc=@pmIdDoc
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasCc_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 BEGIN
  DECLARE @TrnNombre VARCHAR(20)
  SET @TrnNombre = 'AcuNiifCuentasCc'
  BEGIN TRANSACTION @TrnNombre
  INSERT INTO AcuNiifCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdCCosto,'-1','CC',0,SUM(D.VrDebito),SUM(D.VrCredito)
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='NIIF' AND P.Movimiento=1 AND P.CentroCosto=1
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdCCosto
 --centro de costo-subcentro
  INSERT INTO AcuNiifCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdCCosto,D.IdSubCos,'CS',0,SUM(D.VrDebito),SUM(D.VrCredito)
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='NIIF' AND P.Movimiento=1 AND CentroCosto=1 AND SubCentro=1
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdCCosto,D.IdSubCos
 COMMIT TRANSACTION  @TrnNombre
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsNiifDetalle] @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 INT,@pmReferncia VARCHAR(50),@pmTipDocRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2),@pmTimeSys SMALLDATETIME
,@pmFecUpdate SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11),@pmCdConcTrib VARCHAR(4),@pmCdTarifTrib VARCHAR(4),@pmNumEstablec VARCHAR(20),@pmPtoEmision VARCHAR(20),@pmNum_Autoriza VARCHAR(50),@pmFechAutoriza SMALLDATETIME,@pmCodCuentaCG VARCHAR(16)
 AS 
 INSERT INTO Trn_NiifDetalle (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,TimeSys,FecUpdate,IdCiaCrea,IdUsuario,CdConcTrib,CdTarifTrib,NumEstablec,PtoEmision,Num_Autoriza,FechAutoriza,CodCuentaCG)
 VALUES (@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,@pmTimeSys,@pmFecUpdate,@pmIdCiaCrea,@pmIdUsuario,@pmCdConcTrib,@pmCdTarifTrib,@pmNumEstablec,@pmPtoEmision,@pmNum_Autoriza,@pmFechAutoriza,@pmCodCuentaCG)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasVeh_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuFisCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdVehiculo,0,SUM(D.VrDebito),SUM(D.VrCredito)
 FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='FISCAL' AND P.Movimiento=1 AND P.Vehiculo=1
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasNit_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuNiifCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdTercero,0,SUM(D.VrDebito),SUM(D.VrCredito)
 FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
 INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='NIIF' AND P.Movimiento=1 AND P.Tercero=1
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasVeh_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuNiifCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdVehiculo,0,SUM(D.VrDebito),SUM(D.VrCredito)
 FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='NIIF' AND P.Movimiento=1 AND P.Vehiculo=1
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasCc_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 BEGIN
  DECLARE @TrnNombre VARCHAR(20)
  SET @TrnNombre = 'AcuFisCuentasCc'
  BEGIN TRANSACTION @TrnNombre
  INSERT INTO AcuFisCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdCCosto,'-1','CC',0,SUM(D.VrDebito),SUM(D.VrCredito)
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='FISCAL' AND P.Movimiento=1 AND P.CentroCosto=1
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdCCosto
 --centro de costo-subcentro
  INSERT INTO AcuFisCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdCCosto,D.IdSubCos,'CS',0,SUM(D.VrDebito),SUM(D.VrCredito)
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='FISCAL' AND P.Movimiento=1 AND CentroCosto=1 AND SubCentro=1
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdCCosto,D.IdSubCos
 COMMIT TRANSACTION  @TrnNombre
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasNit_Sel] @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuFisCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(D.Fecha),@pmnMes,D.IdCia,D.IdCuenta,D.IdTercero,0,SUM(D.VrDebito),SUM(D.VrCredito)
 FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
 INNER JOIN PucNiif AS P ON D.IdCuenta=P.IdCuenta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif='FISCAL' AND P.Movimiento=1 AND P.Tercero=1
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(D.Fecha),D.IdCia,D.IdCuenta,D.IdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_NiifMov_Sel] @pmtmEst CHAR(2),@pmTipoNiif VARCHAR(10),@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_NiifMov (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,CodCuentaCG)
  SELECT  @pmtmEst,D.TipCom,D.Comprobante,D.IdCia,D.Item,D.Fecha,D.IdCuenta,Detalle,VrDebito,VrCredito,D.IdTercero,D.IdVehiculo,D.IdCCosto,D.IdSubCos,VrBase,TarifaBase,D.TipDoc,D.Documento,D.IdCiaDoc,D.CodConce,NitDoc,TipFac,Factura
  ,IdCiaFac,ItemFac,FecVence,D.CodCta,D.NumCheque,D.Integrado,D.TipoAplica,D.Consolida,CodCargo,NitOtros,CodSubgpo,D.CiuOrigen,D.CodAgncia,D.VehPropio,D.Referncia,TipDocRef,DocRef,IdCiaRef,CodCuentaCG
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif=@pmTipoNiif AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdSubCos LIKE ISNULL(@pmIdSubCos,'%') AND D.CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
 AND D.TipCom LIKE ISNULL(@pmTipCom,'%')
 AND  (D.Integrado=ISNULL(@pmIntegrado,0) or  D.Integrado=ISNULL(@pmIntegrado,1))
ELSE
  INSERT INTO tm_NiifMov (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,CodCuentaCG)
  SELECT  @pmtmEst,D.TipCom,D.Comprobante,D.IdCia,Item,D.Fecha,D.IdCuenta,Detalle,D.VrDebito,D.VrCredito,D.IdTercero,D.IdVehiculo,D.IdCCosto,D.IdSubCos,D.VrBase,D.TarifaBase,D.TipDoc,D.Documento,D.IdCiaDoc,D.CodConce,NitDoc,TipFac,Factura
  ,IdCiaFac,ItemFac,D.FecVence,D.CodCta,D.NumCheque,D.Integrado,D.TipoAplica,D.Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,D.CodAgncia,D.VehPropio,D.Referncia,TipDocRef,DocRef,IdCiaRef,CodCuentaCG
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif=@pmTipoNiif AND D.IdCuenta BETWEEN @pmCueIni AND @pmCueFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdSubCos LIKE ISNULL(@pmIdSubCos,'%') AND D.CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
 AND D.TipCom LIKE ISNULL(@pmTipCom,'%')
 AND  (D.Integrado=ISNULL(@pmIntegrado,0) or  D.Integrado=ISNULL(@pmIntegrado,1))
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_NiifMov_Acc] @pmtmEst CHAR(2),@pmTipoNiif VARCHAR(10),@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_NiifMov (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,CodCuentaCG)
  SELECT  @pmtmEst,D.TipCom,D.Comprobante,D.IdCia,D.Item,D.Fecha,D.IdCuenta,Detalle,VrDebito,VrCredito,D.IdTercero,D.IdVehiculo,D.IdCCosto,D.IdSubCos,VrBase,TarifaBase,D.TipDoc,D.Documento,D.IdCiaDoc,D.CodConce,NitDoc,TipFac,Factura
  ,IdCiaFac,ItemFac,FecVence,D.CodCta,D.NumCheque,D.Integrado,D.TipoAplica,D.Consolida,CodCargo,NitOtros,CodSubgpo,D.CiuOrigen,D.CodAgncia,D.VehPropio,D.Referncia,TipDocRef,DocRef,IdCiaRef,CodCuentaCG
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
  INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
  WHERE  D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif=@pmTipoNiif AND EsAccnista<>0 AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdSubCos LIKE ISNULL(@pmIdSubCos,'%') AND D.CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
 AND D.TipCom LIKE ISNULL(@pmTipCom,'%')
 AND  (D.Integrado=ISNULL(@pmIntegrado,0) or  D.Integrado=ISNULL(@pmIntegrado,1))
ELSE
  INSERT INTO tm_NiifMov (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,CodCuentaCG)
  SELECT  @pmtmEst,D.TipCom,D.Comprobante,D.IdCia,Item,D.Fecha,D.IdCuenta,Detalle,D.VrDebito,D.VrCredito,D.IdTercero,D.IdVehiculo,D.IdCCosto,D.IdSubCos,D.VrBase,D.TarifaBase,D.TipDoc,D.Documento,D.IdCiaDoc,D.CodConce,NitDoc,TipFac,Factura
  ,IdCiaFac,ItemFac,D.FecVence,D.CodCta,D.NumCheque,D.Integrado,D.TipoAplica,D.Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,D.CodAgncia,D.VehPropio,D.Referncia,TipDocRef,DocRef,IdCiaRef,CodCuentaCG
  FROM Trn_NiifDetalle AS D INNER JOIN Trn_NiifComp AS C ON D.TipCom=C.TipCom AND D.Comprobante=C.Comprobante AND D.IdCia=C.IdCia
   INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipoNiif=@pmTipoNiif AND D.IdCuenta BETWEEN @pmCueIni AND @pmCueFin AND EsAccnista<>0
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdSubCos LIKE ISNULL(@pmIdSubCos,'%') AND D.CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
 AND D.TipCom LIKE ISNULL(@pmTipCom,'%')
 AND  (D.Integrado=ISNULL(@pmIntegrado,0) or  D.Integrado=ISNULL(@pmIntegrado,1))
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpNiifDetallePla] @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),@pmCodCuentaCG VARCHAR(16)
 AS 
 UPDATE Trn_NiifDetalle SET IdCuenta=@pmIdCuenta,VrDebito=@pmVrDebito,IdTercero=@pmIdTercero,CodAgncia=@pmCodAgncia,IdVehiculo=@pmIdVehiculo
,Referncia=@pmReferncia,FecUpdate=@pmFecUpdate,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,CodCuentaCG=@pmCodCuentaCG
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_NiifLib] @pmtmEst CHAR(2),@pmtmTipCom VARCHAR(3),@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_NiifLib (tmEst,tmTipCom,tmIdCuenta,tmIdTercero,tmIdCCosto,tmIdSubCos,tmIdCia,tmTotalDebitos,tmTotalCreditos,tmSaldoAnterior,tmSanNit,tmSanNitcc,tmSanNitCcSub)
 VALUES (@pmtmEst,@pmtmTipCom,@pmtmIdCuenta,@pmtmIdTercero,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmIdCia,@pmtmTotalDebitos,@pmtmTotalCreditos,@pmtmSaldoAnterior,@pmtmSanNit,@pmtmSanNitcc,@pmtmSanNitCcSub)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelJornadas] @pmIdJornada VARCHAR(4)
 AS 
 DELETE FROM Jornadas
 WHERE IdJornada=@pmIdJornada
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelConcDivCue] @pmIdConcepto VARCHAR(4),@pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ConcDivCue
 WHERE IdConcepto=@pmIdConcepto AND IdClase LIKE ISNULL(@pmIdClase,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmConcTrib,tmTarifTrib,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut
 FROM tm_Compau
 WHERE tmNumero=@pmtmNumero AND tmTipCom=@pmtmTipCom AND tmComprobante=@pmtmComprobante AND tmIdCia=@pmtmIdCia
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCdCiaTurno CHAR(2),@pmPuntosRut DECIMAL(14,4),@pmInactivo BIT
 AS 
 UPDATE Rutas SET Ruta=@pmRuta,IdLocOri=@pmIdLocOri,IdLocDes=@pmIdLocDes,EsLocal=@pmEsLocal,Distkmt=@pmDistkmt
,TmHoras=@pmTmHoras,CodigoMt=@pmCodigoMt,FleteTon=@pmFleteTon,CdCiaTurno=@pmCdCiaTurno,PuntosRut=@pmPuntosRut,Inactivo=@pmInactivo
 WHERE IdRuta=@pmIdRuta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryRutasTra]  @pmEsLocal BIT=Null
 AS 
 SELECT IdRuta,Ruta,Ruta+' '+IdRuta AS DsRut,CodigoMt
 FROM Rutas 
 WHERE  Inactivo=0  
 AND (EsLocal=ISNULL(@pmEsLocal,0) or EsLocal=ISNULL(@pmEsLocal,1))
 ORDER BY Ruta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCdCiaTurno CHAR(2),@pmPuntosRut DECIMAL(14,4),@pmInactivo BIT
 AS 
 INSERT INTO Rutas (IdRuta,Ruta,IdLocOri,IdLocDes,EsLocal,Distkmt,TmHoras,CodigoMt,FleteTon,Inactivo,CdCiaTurno,PuntosRut)
 VALUES (@pmIdRuta,@pmRuta,@pmIdLocOri,@pmIdLocDes,@pmEsLocal,@pmDistkmt,@pmTmHoras,@pmCodigoMt,@pmFleteTon,@pmInactivo,@pmCdCiaTurno,@pmPuntosRut)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryRutas] @pmIdRuta VARCHAR(4)
 AS 
 SELECT IdRuta,Ruta,IdLocOri,IdLocDes,EsLocal,Distkmt,TmHoras,CodigoMt,FleteTon,CdCiaTurno,PuntosRut,Inactivo
 FROM Rutas
 WHERE IdRuta=@pmIdRuta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOilCompraDesp] @pmTipDoc VARCHAR(3),@pmNumCompra INT,@pmIdCia CHAR(2),@pmItem INT,@pmNumGuia INT,@pmIdCiaGuia CHAR(2),@pmTipEnt VARCHAR(3),@pmNumEntrada INT,@pmIdCiaEnt CHAR(2),@pmItemEnt INT,@pmIdProducto VARCHAR(16),@pmCantBruto DECIMAL(16,6),@pmCantNeto DECIMAL(16,6),@pmCantEntrada DECIMAL(16,6)
,@pmUndMed VARCHAR(10),@pmUMCargue VARCHAR(10),@pmVrUnitario DECIMAL(16,6),@pmNumTasas INT,@pmTasaBrent DECIMAL(16,6),@pmTasaWti DECIMAL(16,6),@pmTRMdolar DECIMAL(16,6),@pmPuntos DECIMAL(16,6),@pmPrecioUnd DECIMAL(16,6),@pmVrTotal DECIMAL(16,6),@pmCdMoneda VARCHAR(5),@pmUMprecio VARCHAR(10)
 AS 
 INSERT INTO Trn_OilCompraDesp (TipDoc,NumCompra,IdCia,Item,NumGuia,IdCiaGuia,TipEnt,NumEntrada,IdCiaEnt,ItemEnt,IdProducto,CantBruto,CantNeto,CantEntrada,UndMed,UMCargue,VrUnitario,NumTasas,TasaBrent,TasaWti,TRMdolar,Puntos,PrecioUnd,VrTotal,CdMoneda,UMprecio)
 VALUES (@pmTipDoc,@pmNumCompra,@pmIdCia,@pmItem,@pmNumGuia,@pmIdCiaGuia,@pmTipEnt,@pmNumEntrada,@pmIdCiaEnt,@pmItemEnt,@pmIdProducto,@pmCantBruto,@pmCantNeto,@pmCantEntrada,@pmUndMed,@pmUMCargue,@pmVrUnitario,@pmNumTasas,@pmTasaBrent,@pmTasaWti,@pmTRMdolar,@pmPuntos,@pmPrecioUnd,@pmVrTotal,@pmCdMoneda,@pmUMprecio)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMudEstados] @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM MudEstados
 WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMudEstados] @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 INSERT INTO MudEstados (IdEstado,Estado,NColor,Inactivo)
 VALUES (@pmIdEstado,@pmEstado,@pmNColor,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMudEstados] @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 UPDATE MudEstados SET Estado=@pmEstado,NColor=@pmNColor,Inactivo=@pmInactivo
 WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudEstados] @pmIdEstado VARCHAR(4)
 AS 
 IF @pmIdEstado IS NULL
  BEGIN
   SELECT IdEstado,Estado,NColor
   FROM MudEstados
   WHERE Inactivo=0
   ORDER BY IdEstado
  END
 ELSE
  BEGIN
   SELECT IdEstado,Estado,NColor,Inactivo
   FROM MudEstados
   WHERE IdEstado=@pmIdEstado
  END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrySubGruposAgenc] @pmIdSubAgenc VARCHAR(4)
 AS 
 SELECT IdSubAgenc,SubGrupoAgenc,IdGrupo,Inactivo
 FROM SubGruposAgenc
 WHERE IdSubAgenc=@pmIdSubAgenc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelSubGruposAgenc] @pmIdSubAgenc VARCHAR(4)
 AS 
 DELETE FROM SubGruposAgenc
 WHERE IdSubAgenc=@pmIdSubAgenc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryAfMovValoriza] @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdMov,Numero,IdCia,Item,IdActivo,Concepto,Valor,NitProvee,FecAdquis,NumDocmto,DescripParte,Num_Parte
 FROM Trn_AfMovValoriza
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmNum_Parte INT
 AS 
 INSERT INTO Trn_AfMovValoriza (IdMov,Numero,IdCia,Item,IdActivo,Concepto,Valor,NitProvee,FecAdquis,NumDocmto,DescripParte,Num_Parte)
 VALUES (@pmIdMov,@pmNumero,@pmIdCia,@pmItem,@pmIdActivo,@pmConcepto,@pmValor,@pmNitProvee,@pmFecAdquis,@pmNumDocmto,@pmDescripParte,@pmNum_Parte)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTercClieVeh] @pmIdClie VARCHAR(16),@pmpVehiculo VARCHAR(10)
 AS 
 DELETE FROM TercClieVeh
 WHERE IdClie=@pmIdClie AND pVehiculo LIKE ISNULL(@pmpVehiculo,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTercClieVehPla] @pmpVehiculo VARCHAR(10)
 AS 
 SELECT IdClie,pVehiculo,CdConductor,NumVehiculo,Descripcion
 FROM TercClieVeh
 WHERE pVehiculo=@pmpVehiculo
 ORDER BY IdClie
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraPresItems] @pmNumPsto INT
 AS 
 SELECT NumPsto,Item,IdConcepto,Descripcion,Cantidad,VrUnitario,VrTotal,TipoRubro,CdClase,NitTercero,CdTipOper
 FROM Trn_TraPresItems
 WHERE NumPsto=@pmNumPsto
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTraPresItems] @pmNumPsto INT,@pmItem INT,@pmIdConcepto VARCHAR(4),@pmDescripcion VARCHAR(250)
,@pmCantidad DECIMAL(14,4),@pmVrUnitario MONEY,@pmVrTotal MONEY,@pmTipoRubro VARCHAR(10),@pmCdClase VARCHAR(4),@pmNitTercero VARCHAR(16),@pmCdTipOper VARCHAR(4)
 AS 
 INSERT INTO Trn_TraPresItems (NumPsto,Item,IdConcepto,Descripcion,Cantidad,VrUnitario,VrTotal,TipoRubro,CdClase,NitTercero,CdTipOper)
 VALUES (@pmNumPsto,@pmItem,@pmIdConcepto,@pmDescripcion,@pmCantidad,@pmVrUnitario,@pmVrTotal,@pmTipoRubro,@pmCdClase,@pmNitTercero,@pmCdTipOper)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTraPresItems] @pmNumPsto INT,@pmItem INT
 AS 
 DELETE FROM Trn_TraPresItems
 WHERE NumPsto=@pmNumPsto 
 AND (Item>=ISNULL(@pmItem,-1)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraCumRadica] @pmNumRadica INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmTipMuc VARCHAR(3),@pmManifiesto INT,@pmIdCiaMuc CHAR(2),@pmCdCiaRad CHAR(2),@pmDocAnexos VARCHAR(1000),@pmCantDoc INT,@pmFormaEnvio VARCHAR(50)
,@pmNomRadica VARCHAR(150),@pmCedRadica VARCHAR(16),@pmFuncionario VARCHAR(150),@pmObservacion VARCHAR(2000),@pmNumeroCum INT,@pmIdCiaCum CHAR(2),@pmFechaCum SMALLDATETIME
 AS 
 UPDATE Trn_TraCumRadica SET Fecha=@pmFecha,TipMuc=@pmTipMuc,Manifiesto=@pmManifiesto,IdCiaMuc=@pmIdCiaMuc,CdCiaRad=@pmCdCiaRad,DocAnexos=@pmDocAnexos,CantDoc=@pmCantDoc,FormaEnvio=@pmFormaEnvio,NomRadica=@pmNomRadica,CedRadica=@pmCedRadica,Funcionario=@pmFuncionario
,Observacion=@pmObservacion,NumeroCum=@pmNumeroCum,IdCiaCum=@pmIdCiaCum,FechaCum=@pmFechaCum
 WHERE NumRadica=@pmNumRadica AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraCumRadica] @pmNumRadica INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraCumRadica
 WHERE NumRadica=@pmNumRadica AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraCumRadica] @pmNumRadica INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmTipMuc VARCHAR(3),@pmManifiesto INT,@pmIdCiaMuc CHAR(2),@pmCdCiaRad CHAR(2),@pmDocAnexos VARCHAR(1000)
,@pmCantDoc INT,@pmFormaEnvio VARCHAR(50),@pmNomRadica VARCHAR(150),@pmCedRadica VARCHAR(16),@pmFuncionario VARCHAR(150),@pmObservacion VARCHAR(2000),@pmNumeroCum INT,@pmIdCiaCum CHAR(2)
,@pmFechaCum SMALLDATETIME,@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraCumRadica (NumRadica,IdCia,Fecha,TipMuc,Manifiesto,IdCiaMuc,CdCiaRad,DocAnexos,CantDoc,FormaEnvio,NomRadica,CedRadica,Funcionario,Observacion,NumeroCum,IdCiaCum,FechaCum,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmNumRadica,@pmIdCia,@pmFecha,@pmTipMuc,@pmManifiesto,@pmIdCiaMuc,@pmCdCiaRad,@pmDocAnexos,@pmCantDoc,@pmFormaEnvio,@pmNomRadica,@pmCedRadica,@pmFuncionario,@pmObservacion
,@pmNumeroCum,@pmIdCiaCum,@pmFechaCum,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraCumRadica] @pmNumRadica INT,@pmIdCia CHAR(2)
 AS 
 SELECT NumRadica,IdCia,Fecha,TipMuc,Manifiesto,IdCiaMuc,CdCiaRad,DocAnexos,CantDoc,FormaEnvio
,NomRadica,CedRadica,Funcionario,Observacion,NumeroCum,IdCiaCum,FechaCum,TimeSys,IdCiaCrea,IdUsuario
 FROM Trn_TraCumRadica
 WHERE NumRadica=@pmNumRadica AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelEstadoPro] @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM EstadoPro
 WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraPedidoApr] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
,@pmNumAprob INT,@pmIdCiaApr CHAR(2),@pmFecAprob SMALLDATETIME,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_TraPedido SET  NumAprob=@pmNumAprob,IdCiaApr=@pmIdCiaApr,FecAprob=@pmFecAprob,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraPedido] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmFecEntrega SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdClieFact VARCHAR(16),@pmIdRemitente VARCHAR(16),@pmIdDestinatario VARCHAR(16),@pmIdLocOrigen VARCHAR(8),@pmIdLocDestino VARCHAR(8),@pmIdVend VARCHAR(16),@pmIdTarifCom VARCHAR(4),@pmModalidad VARCHAR(10),@pmVigencia VARCHAR(10),@pmTipoTarifa VARCHAR(10)
,@pmVrCobro MONEY,@pmVrPagos MONEY,@pmVrFletes MONEY,@pmVrCargue MONEY,@pmVrDesCargue MONEY,@pmVrEscolta MONEY,@pmVrDevContdor MONEY,@pmVrTraUrbano MONEY,@pmVrEmbalajes MONEY,@pmVrCargos MONEY,@pmVrDctos MONEY,@pmVrDeclarado MONEY,@pmVrSeguro MONEY,@pmCantidad DECIMAL(14,4),@pmCantDesp DECIMAL(14,4),@pmIdMneda VARCHAR(5),@pmVrTasa DECIMAL(14,4),@pmCotizacion INT,@pmIdCiaCot CHAR(2),@pmNumAprob INT,@pmIdCiaApr CHAR(2),@pmFecAprob SMALLDATETIME,@pmTipOdc VARCHAR(3),@pmOCargue INT,@pmIdCiaOdc CHAR(2)
,@pmFechaOdc SMALLDATETIME,@pmTipRem VARCHAR(3),@pmRemesa INT,@pmIdCiaRem CHAR(2),@pmFechaRem SMALLDATETIME,@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFac SMALLDATETIME,@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_TraPedido (TipDoc,Pedido,IdCia,Fecha,FechaVence,FecDespacho,FecEntrega,IdCliente,IdAgencia,IdClieFact,IdRemitente,IdDestinatario,IdLocOrigen,IdLocDestino,IdVend,IdTarifCom,Modalidad,Vigencia,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue,VrDesCargue,VrEscolta,VrDevContdor,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos,VrDeclarado,VrSeguro,Cantidad,CantDesp,IdMneda,VrTasa,Cotizacion,IdCiaCot,NumAprob,IdCiaApr,FecAprob,TipOdc,OCargue,IdCiaOdc,FechaOdc,TipRem,Remesa,IdCiaRem,FechaRem,TipFac,Factura,IdCiaFac,FechaFac,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmPedido,@pmIdCia,@pmFecha,@pmFechaVence,@pmFecDespacho,@pmFecEntrega,@pmIdCliente,@pmIdAgencia,@pmIdClieFact,@pmIdRemitente,@pmIdDestinatario,@pmIdLocOrigen,@pmIdLocDestino,@pmIdVend,@pmIdTarifCom,@pmModalidad,@pmVigencia,@pmTipoTarifa,@pmVrCobro,@pmVrPagos,@pmVrFletes,@pmVrCargue,@pmVrDesCargue,@pmVrEscolta,@pmVrDevContdor,@pmVrTraUrbano,@pmVrEmbalajes,@pmVrCargos,@pmVrDctos,@pmVrDeclarado,@pmVrSeguro,@pmCantidad,@pmCantDesp,@pmIdMneda,@pmVrTasa,@pmCotizacion,@pmIdCiaCot,@pmNumAprob,@pmIdCiaApr
,@pmFecAprob,@pmTipOdc,@pmOCargue,@pmIdCiaOdc,@pmFechaOdc,@pmTipRem,@pmRemesa,@pmIdCiaRem,@pmFechaRem,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmFechaFac,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[paUpTraPedidoAnu] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_TraPedido SET Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraPedido] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmFecEntrega SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdClieFact VARCHAR(16),@pmIdRemitente VARCHAR(16),@pmIdDestinatario VARCHAR(16),@pmIdLocOrigen VARCHAR(8),@pmIdLocDestino VARCHAR(8)
,@pmIdVend VARCHAR(16),@pmIdTarifCom VARCHAR(4),@pmModalidad VARCHAR(10),@pmVigencia VARCHAR(10),@pmTipoTarifa VARCHAR(10),@pmVrCobro MONEY,@pmVrPagos MONEY,@pmVrFletes MONEY,@pmVrCargue MONEY,@pmVrDesCargue MONEY,@pmVrEscolta MONEY,@pmVrDevContdor MONEY,@pmVrTraUrbano MONEY,@pmVrEmbalajes MONEY,@pmVrCargos MONEY,@pmVrDctos MONEY,@pmVrDeclarado MONEY,@pmVrSeguro MONEY
,@pmCantidad DECIMAL(14,4),@pmCantDesp DECIMAL(14,4),@pmIdMneda VARCHAR(5),@pmVrTasa DECIMAL(14,4),@pmCotizacion INT,@pmIdCiaCot CHAR(2),@pmNumAprob INT,@pmIdCiaApr CHAR(2),@pmFecAprob SMALLDATETIME,@pmTipOdc VARCHAR(3),@pmOCargue INT,@pmIdCiaOdc CHAR(2),@pmFechaOdc SMALLDATETIME,@pmTipRem VARCHAR(3),@pmRemesa INT,@pmIdCiaRem CHAR(2),@pmFechaRem SMALLDATETIME,@pmTipFac VARCHAR(3)
,@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFac SMALLDATETIME,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraPedido SET Fecha=@pmFecha,FechaVence=@pmFechaVence,FecDespacho=@pmFecDespacho,FecEntrega=@pmFecEntrega,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdClieFact=@pmIdClieFact,IdRemitente=@pmIdRemitente,IdDestinatario=@pmIdDestinatario,IdLocOrigen=@pmIdLocOrigen,IdLocDestino=@pmIdLocDestino,IdVend=@pmIdVend,IdTarifCom=@pmIdTarifCom,Modalidad=@pmModalidad,Vigencia=@pmVigencia
,TipoTarifa=@pmTipoTarifa,VrCobro=@pmVrCobro,VrPagos=@pmVrPagos,VrFletes=@pmVrFletes,VrCargue=@pmVrCargue,VrDesCargue=@pmVrDesCargue,VrEscolta=@pmVrEscolta,VrDevContdor=@pmVrDevContdor,VrTraUrbano=@pmVrTraUrbano,VrEmbalajes=@pmVrEmbalajes,VrCargos=@pmVrCargos,VrDctos=@pmVrDctos,VrDeclarado=@pmVrDeclarado,VrSeguro=@pmVrSeguro,Cantidad=@pmCantidad,CantDesp=@pmCantDesp
,IdMneda=@pmIdMneda,VrTasa=@pmVrTasa,Cotizacion=@pmCotizacion,IdCiaCot=@pmIdCiaCot,NumAprob=@pmNumAprob,IdCiaApr=@pmIdCiaApr,FecAprob=@pmFecAprob,TipOdc=@pmTipOdc,OCargue=@pmOCargue,IdCiaOdc=@pmIdCiaOdc,FechaOdc=@pmFechaOdc,TipRem=@pmTipRem,Remesa=@pmRemesa,IdCiaRem=@pmIdCiaRem,FechaRem=@pmFechaRem,TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac,FechaFac=@pmFechaFac
,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTraPedido] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
 AS 
 BEGIN
  DECLARE @TrnNombre VARCHAR(20)
  SET @TrnNombre = 'TrnPedido'
   BEGIN TRANSACTION @TrnNombre
   DELETE FROM Trn_TraPedAnexo WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
   DELETE FROM Trn_TraPedido WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
  COMMIT TRANSACTION  @TrnNombre
 END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraPedido] @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Pedido,IdCia,Fecha,FechaVence,FecDespacho,FecEntrega,IdCliente,IdAgencia,IdClieFact,IdRemitente,IdDestinatario,IdLocOrigen,IdLocDestino,IdVend,IdTarifCom,Modalidad,Vigencia,TipoTarifa,VrCobro,VrPagos,VrFletes,VrCargue,VrDesCargue,VrEscolta,VrDevContdor
,VrTraUrbano,VrEmbalajes,VrCargos,VrDctos,VrDeclarado,VrSeguro,Cantidad,CantDesp,IdMneda,VrTasa,Cotizacion,IdCiaCot,NumAprob,IdCiaApr,FecAprob,TipOdc,OCargue,IdCiaOdc,FechaOdc,TipRem,Remesa,IdCiaRem,FechaRem,TipFac,Factura,IdCiaFac,FechaFac,OrigenAdd,Anulado
,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraPedido
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncVrCarSeg]  (@pmTipDoc VARCHAR(3),@pmIdprest INT,@pmIdCia CHAR(2))--@PmConcepto VARCHAR(10))
RETURNS MONEY
 AS  
BEGIN 
 DECLARE @vrcarSeg  MONEY
  SET  @vrcarSeg=(
  SELECT sum(vrtotal) FROM Trn_fincuotas
 where TipDoc=@pmtipdoc AND IdPrestamo=@pmidprest and IdCia =@pmIdcia and Concepto ='SEGURO'
  group by tipdoc,idprestamo,idcia
 
 -- COUNT(*) FROM Trn_MayAgencias WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia)
 )
 RETURN @vrcarSeg
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncVrCarMor]  (@pmTipDoc VARCHAR(3),@pmIdprest INT,@pmIdCia CHAR(2))--@PmConcepto VARCHAR(10))
RETURNS MONEY
 AS  
BEGIN 
 DECLARE @vrcarMor  MONEY
  SET  @vrcarMor=(
  SELECT sum(vrtotal) FROM Trn_fincuotas
 where TipDoc=@pmtipdoc AND IdPrestamo=@pmidprest and IdCia =@pmIdcia and Concepto ='INTMORA'
  group by tipdoc,idprestamo,idcia
 
 -- COUNT(*) FROM Trn_MayAgencias WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia)
 )
 RETURN @vrcarMor
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncVrCargos]  (@pmTipDoc VARCHAR(3),@pmIdprest INT,@pmIdCia CHAR(2))--@PmConcepto VARCHAR(10))
RETURNS MONEY
 AS  
BEGIN 
 DECLARE @vrcargos  MONEY
  SET  @vrcargos=(
  SELECT isnull( sum(vrtotal),0) FROM Trn_fincuotas
 where TipDoc=@pmtipdoc AND IdPrestamo=@pmidprest and IdCia =@pmIdcia and Concepto IN('CAPITAL','INTERESES','SEGURO','INTMORA','OTRO')
  group by tipdoc,idprestamo,idcia
 
 -- COUNT(*) FROM Trn_MayAgencias WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia)
 )
 RETURN @vrcargos
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[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,tmVrSeguro,tmVrProrroga,tmVrCustodia,tmTasaCustod,tmTarifIva,tmVrIva,tmTarifRet,tmVrRet)
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,0,0,0,0
,TarifIvaInt,VrIvaInt,TarifRetInt,VrRetInt
 FROM Trn_FinCuotas
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoCausac,NumCausacion,IdCiaCau,Detalle,FecUltPago,FecLiqMora,FechaCrea,CodLinCred,NumFactura
,VrInteresFact,DiasLiquida,FecUltCausac,VrAcumCapital,Convenio,NitConvenio,NumPreMora,FacturaInt,TipFacInt,NumFacInt,CdCiaFacInt,FechaFacInt
,TarifIvaInt,VrIvaInt,TarifRetInt,VrRetInt
 FROM Trn_FinCuotas
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAcuPrestamosLinCau] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null
AS 
INSERT INTO AcuPrestamosLin (nAnno,nMes,IdCia,IdCliente,IdAgencia,IdLinea,Concepto,SaldoAnt,TotalCargos,TotalAbonos)
SELECT YEAR(Fecha),MONTH(Fecha),IdCia,IdCliente,IdAgencia,ISNULL(CodLinCred,'0001'),Concepto,0,SUM(VrTotal),0
FROM Trn_FinCuotas 
WHERE (TipDoc='ND2' OR TipDoc='FIF' OR (TipDoc='PR1' AND Concepto<>'CAPITAL'))
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,CodLinCred,Concepto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncVrCarInt]  (@pmTipDoc VARCHAR(3),@pmIdprest INT,@pmIdCia CHAR(2))--@PmConcepto VARCHAR(10))
RETURNS MONEY
 AS  
BEGIN 
 DECLARE @vrcarINT  MONEY
  SET  @vrcarINT=(
  SELECT isnull(sum(vrtotal),0) FROM Trn_fincuotas
 where TipDoc=@pmtipdoc AND IdPrestamo=@pmidprest and IdCia =@pmIdcia and Concepto ='INTERESES'
  group by tipdoc,idprestamo,idcia
 
 -- COUNT(*) FROM Trn_MayAgencias WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia)
 )
 RETURN @vrcarINT
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,@pmTipoCausac VARCHAR(3),@pmFacturaInt INT,@pmTipFacInt VARCHAR(3),@pmNumFacInt INT,@pmCdCiaFacInt CHAR(2),@pmFechaFacInt SMALLDATETIME,@pmTarifIvaInt DECIMAL(14,4),@pmVrIvaInt MONEY,@pmTarifRetInt DECIMAL(14,4),@pmVrRetInt MONEY

 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,TipoCausac,NumCausacion,IdCiaCau,Detalle,FecUltPago,FecLiqMora
,FechaCrea,CodLinCred,NumFactura,VrInteresFact,DiasLiquida,FecUltCausac,VrAcumCapital,Convenio,NitConvenio,NumPreMora,FacturaInt,TipFacInt,NumFacInt,CdCiaFacInt,FechaFacInt,TarifIvaInt,VrIvaInt,TarifRetInt,VrRetInt)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmItem,@pmNumCuota,@pmFecha,@pmFechaVence,@pmVrTotal,@pmVrAbonado,@pmTipoTasa,@pmDTF_EA,@pmTasaNom,@pmSpread_TA,@pmTasaEfe,@pmIdCliente
,@pmIdAgencia,@pmConcepto,@pmTipoCuota,@pmTipoCausac,@pmNumCausacion,@pmIdCiaCau,@pmDetalle,@pmFecUltPago,@pmFecLiqMora,@pmFechaCrea,@pmCodLinCred,@pmNumFactura,@pmVrInteresFact,@pmDiasLiquida,@pmFecUltCausac,@pmVrAcumCapital,@pmConvenio,@pmNitConvenio,@pmNumPreMora
,@pmFacturaInt,@pmTipFacInt,@pmNumFacInt,@pmCdCiaFacInt,@pmFechaFacInt,@pmTarifIvaInt,@pmVrIvaInt,@pmTarifRetInt,@pmVrRetInt)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncVrCarOtr]  (@pmTipDoc VARCHAR(3),@pmIdprest INT,@pmIdCia CHAR(2))--@PmConcepto VARCHAR(10))
RETURNS MONEY
 AS  
BEGIN 
 DECLARE @vrcarOtr  MONEY
  SET  @vrcarOtr=(
  SELECT sum(vrtotal) FROM Trn_fincuotas
 where TipDoc=@pmtipdoc AND IdPrestamo=@pmidprest and IdCia =@pmIdcia and Concepto ='OTRO'
  group by tipdoc,idprestamo,idcia
 
 -- COUNT(*) FROM Trn_MayAgencias WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia)
 )
 RETURN @vrcarOtr
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncVrCarCap]  (@pmTipDoc VARCHAR(3),@pmIdprest INT,@pmIdCia CHAR(2))--@PmConcepto VARCHAR(10))
RETURNS MONEY
 AS  
BEGIN 
 DECLARE @vrcarCAP  MONEY
  SET  @vrcarCAP=(
  SELECT sum(vrtotal) FROM Trn_fincuotas
 where TipDoc=@pmtipdoc AND IdPrestamo=@pmidprest and IdCia =@pmIdcia and Concepto ='CAPITAL'
  group by tipdoc,idprestamo,idcia
 
 -- COUNT(*) FROM Trn_MayAgencias WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia)
 )
 RETURN @vrcarCAP
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncProdPedidoInsu] (@pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16),@pmIdInsumo VARCHAR(16))
RETURNS BIT
 AS  
 BEGIN
  DECLARE @Insumos BIT
  IF @pmIdInsumo IS NULL
   SET @Insumos=1
  ELSE
   BEGIN 
    IF EXISTS (SELECT 1 FROM Trn_ProdPedidoInsu WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia AND IdProducto=@pmIdProducto AND IdInsumo LIKE @pmIdInsumo)
     BEGIN
      SET @Insumos=1
     END
    ELSE
     BEGIN
      SET @Insumos=0
     END
   END
 RETURN @Insumos
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelEdsServicio] @pmIdEstacion VARCHAR(4)
 AS 
 DELETE FROM EdsServicio
 WHERE IdEstacion=@pmIdEstacion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryAreas] @pmIdArea VARCHAR(4)
 AS 
  SELECT IdArea,Area,JefeArea,IdCCosto,Inactivo
  FROM Areas
  WHERE IdArea=@pmIdArea
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasCc_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuFisCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdCCosto,S.IdSubCos,S.TipoAcum,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuFisCuentasCc AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt AND S.TipoAcum=@pmTipoAcum
 AND NOT EXISTS (SELECT * FROM AcuFisCuentasCc AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes AND AC.TipoAcum=@pmTipoAcum
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdCCosto=S.IdCCosto AND AC.IdSubCos=S.IdSubCos)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuFisCuentasCcSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuFisCuentasCc SET AcuFisCuentasCc.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuFisCuentasCc INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuFisCuentasCc WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt AND TipoAcum=@pmTipoAcum) AS SA ON AcuFisCuentasCc.IdCia=SA.IdCia AND AcuFisCuentasCc.IdCuenta=SA.IdCuenta
 AND AcuFisCuentasCc.IdCCosto=SA.IdCCosto AND AcuFisCuentasCc.IdSubCos=SA.IdSubCos AND AcuFisCuentasCc.TipoAcum=SA.TipoAcum
 WHERE AcuFisCuentasCc.nAnno=@pmnAnno AND AcuFisCuentasCc.nMes=@pmnMes AND AcuFisCuentasCc.TipoAcum=@pmTipoAcum
 AND (@pmIdCia IS NULL OR AcuFisCuentasCc.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuFisCuentasCc.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasCc] @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 AcuFisCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdCCosto,@pmIdSubCos
,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasCc_Uni]  @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuFisCuentasCc WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
  INSERT INTO AcuFisCuentasCc (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 AcuFisCuentasCc WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
  GROUP BY nAnno,nMes,IdCia,IdCCosto,IdSubCos,TipoAcum
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTaxTarifas] @pmTipoTarif VARCHAR(10),@pmItem INT
 AS 
 DELETE FROM TaxTarifas
 WHERE TipoTarif=@pmTipoTarif AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAfDeprecCom] @pmNumero INT,@pmIdCia CHAR(2),@pmTipCom VARCHAR(3)
,@pmComprobante INT,@pmTipComAju VARCHAR(3),@pmCompAjustes INT
,@pmTipoCompNiif VARCHAR(3),@pmNumCompNiif INT,@pmTipoCompFis VARCHAR(3),@pmNumCompFis INT
 AS 
 INSERT INTO Trn_AfDeprecCom (Numero,IdCia,TipCom,Comprobante,TipComAju,CompAjustes,TipoCompNiif,NumCompNiif,TipoCompFis,NumCompFis)
 VALUES (@pmNumero,@pmIdCia,@pmTipCom,@pmComprobante,@pmTipComAju,@pmCompAjustes,@pmTipoCompNiif,@pmNumCompNiif,@pmTipoCompFis,@pmNumCompFis)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,@pmActFechaIni INT,@pmCdZonaCont VARCHAR(4),@pmContVigente BIT,@pmVolContrac INT,@pmDescHipotec VARCHAR(250),@pmVrAvaComerc MONEY,@pmFecRegEscritura SMALLDATETIME,@pmNotariaEsc VARCHAR(50),@pmCdTipPrenda VARCHAR(4),@pmVrAvaPrenda MONEY
,@pmPolizaPrenda VARCHAR(30),@pmFecVigPrenda SMALLDATETIME,@pmVrAsegPrenda MONEY,@pmContOperando BIT
 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
,ActFechaIni,CdZonaCont,ContVigente,VolContrac,DescHipotec,VrAvaComerc,FecRegEscritura,NotariaEsc,CdTipPrenda,VrAvaPrenda,PolizaPrenda,FecVigPrenda,VrAsegPrenda,ContOperando)
 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,@pmActFechaIni,@pmCdZonaCont,@pmContVigente,@pmVolContrac,@pmDescHipotec,@pmVrAvaComerc,@pmFecRegEscritura,@pmNotariaEsc,@pmCdTipPrenda,@pmVrAvaPrenda,@pmPolizaPrenda,@pmFecVigPrenda,@pmVrAsegPrenda,@pmContOperando)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMayContratosId] @pmIdCia CHAR(2)
 AS 
 SELECT MAX(NContrato) AS MXITM
 FROM Trn_MayContratos
 WHERE  IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,@pmActFechaIni INT,@pmCdZonaCont VARCHAR(4),@pmContVigente BIT,@pmVolContrac INT,@pmDescHipotec VARCHAR(250),@pmVrAvaComerc MONEY,@pmFecRegEscritura SMALLDATETIME,@pmNotariaEsc VARCHAR(50),@pmCdTipPrenda VARCHAR(4),@pmVrAvaPrenda MONEY
,@pmPolizaPrenda VARCHAR(30),@pmFecVigPrenda SMALLDATETIME,@pmVrAsegPrenda MONEY,@pmContOperando BIT
 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,ActFechaIni=@pmActFechaIni,CdZonaCont=@pmCdZonaCont,ContVigente=@pmContVigente,VolContrac=@pmVolContrac
,DescHipotec=@pmDescHipotec,VrAvaComerc=@pmVrAvaComerc,FecRegEscritura=@pmFecRegEscritura,NotariaEsc=@pmNotariaEsc,CdTipPrenda=@pmCdTipPrenda,VrAvaPrenda=@pmVrAvaPrenda,PolizaPrenda=@pmPolizaPrenda,FecVigPrenda=@pmFecVigPrenda,VrAsegPrenda=@pmVrAsegPrenda,ContOperando=@pmContOperando
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,ActFechaIni,CdZonaCont,ContVigente,VolContrac,DescHipotec,VrAvaComerc,FecRegEscritura,NotariaEsc,CdTipPrenda,VrAvaPrenda,PolizaPrenda,FecVigPrenda,VrAsegPrenda,ContOperando
 FROM Trn_MayContratos
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTanqSaldosTan] @pmIdCia CHAR(2)=Null
 AS 
 SELECT IdTanque,SUM(SaldoActual)  AS STOTSAL
 FROM TanqSaldos
 WHERE  IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY IdTanque
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTanqSaldos] @pmIdTanque VARCHAR(4),@pmIdCia CHAR(2),@pmSaldoActual DECIMAL(14,4)
 AS 
 INSERT INTO TanqSaldos (IdTanque,IdCia,SaldoActual)
 VALUES (@pmIdTanque,@pmIdCia,@pmSaldoActual)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTanqSaldos] @pmIdTanque VARCHAR(4),@pmIdCia CHAR(2)
 AS 
 DELETE FROM TanqSaldos
 WHERE IdTanque LIKE ISNULL(@pmIdTanque,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTanqSaldos] @pmIdTanque VARCHAR(4),@pmIdCia CHAR(2)
 AS 
 SELECT IdTanque,IdCia,SaldoActual
 FROM TanqSaldos
 WHERE IdTanque=@pmIdTanque AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMttoConceptos] @pmIdConc VARCHAR(4),@pmConcepto VARCHAR(150),@pmIdSistema VARCHAR(4),@pmIdMtto VARCHAR(4),@pmTipoControl INT
,@pmIntervalo_km INT,@pmIntervalo_dias INT,@pmReq_Parte BIT,@pmInactivo BIT,@pmCodigoMO VARCHAR(16),@pmIntervalo_Horas INT
,@pmCantHoras DECIMAL(14,2),@pmTarifaCosto DECIMAL(16,4),@pmTarifaClie DECIMAL(16,4),@pmIntvAlarma VARCHAR(50),@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO MttoConceptos (IdConc,Concepto,IdSistema,IdMtto,TipoControl,Intervalo_km,Intervalo_dias,Intervalo_Horas,Req_Parte,CodigoMO,CantHoras,TarifaCosto,Inactivo,FechaCrea,IdUsuario,TarifaClie,IntvAlarma)
 VALUES (@pmIdConc,@pmConcepto,@pmIdSistema,@pmIdMtto,@pmTipoControl,@pmIntervalo_km,@pmIntervalo_dias,@pmIntervalo_Horas,@pmReq_Parte,@pmCodigoMO,@pmCantHoras,@pmTarifaCosto,@pmInactivo,@pmFechaCrea,@pmIdUsuario,@pmTarifaClie,@pmIntvAlarma)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoConceptos] @pmIdConc VARCHAR(4)
 AS 
 SELECT IdConc,Concepto,IdSistema,IdMtto,TipoControl,Intervalo_km,Intervalo_dias,Req_Parte,Intervalo_Horas,CodigoMO
,CantHoras,TarifaCosto,TarifaClie,IntvAlarma,Inactivo,FechaCrea,IdUsuario
 FROM MttoConceptos
 WHERE IdConc=@pmIdConc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoConceptosLta] @pmIdSistema VARCHAR(4)=Null,@pmIdMtto VARCHAR(4)=Null
 AS 
 SELECT IdConc,Concepto,C.IdSistema AS CdSistema,Sistema,C.IdMtto AS CdMtto,TipoMtto,TipoControl
,Intervalo_km,Intervalo_dias,Intervalo_Horas,Req_Parte,CodigoMO,DescripProd,CantHoras,TarifaCosto,TarifaClie,IntvAlarma
,C.Inactivo,C.FechaCrea,C.IdUsuario AS CdUsuario,Usuario
 FROM MttoConceptos AS C INNER JOIN MttoSistemas AS S ON C.IdSistema=S.IdSistema
 INNER JOIN MttoTipos AS TM ON C.IdMtto=TM.IdMtto
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 LEFT JOIN ProdMcias AS P ON C.CodigoMO=P.IdProducto
 WHERE C.IdSistema LIKE ISNULL(@pmIdSistema,'%') AND C.IdMtto LIKE ISNULL(@pmIdMtto,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMttoConceptos] @pmIdConc VARCHAR(4),@pmConcepto VARCHAR(150),@pmIdSistema VARCHAR(4),@pmIdMtto VARCHAR(4),@pmTipoControl INT
,@pmIntervalo_km INT,@pmIntervalo_dias INT,@pmReq_Parte BIT,@pmInactivo BIT,@pmCodigoMO VARCHAR(16),@pmIntervalo_Horas INT,@pmCantHoras DECIMAL(14,2)
,@pmTarifaCosto DECIMAL(16,4),@pmTarifaClie DECIMAL(16,4),@pmIntvAlarma VARCHAR(50)
 AS 
 UPDATE MttoConceptos SET Concepto=@pmConcepto,IdSistema=@pmIdSistema,IdMtto=@pmIdMtto,TipoControl=@pmTipoControl,Intervalo_km=@pmIntervalo_km
,Intervalo_dias=@pmIntervalo_dias,Req_Parte=@pmReq_Parte,Inactivo=@pmInactivo,CodigoMO=@pmCodigoMO,Intervalo_Horas=@pmIntervalo_Horas 
,CantHoras=@pmCantHoras,TarifaCosto=@pmTarifaCosto,TarifaClie=@pmTarifaClie,IntvAlarma=@pmIntvAlarma
 WHERE IdConc=@pmIdConc
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryConcDiversos] @pmIdConcepto VARCHAR(4)
 AS 
 SELECT IdConcepto,Concepto,Grupo,EsServicio,DeManfsto,CdIva,CdRet,CdIca,IdCueDeb,IdCueCre,Inactivo
,CdAutoret,OrdenServ,NovedadNom,CdConcDeveng,CdConcDed,RubroConc,MudReajuste,CodFEDian,FechaAdd,FechaUpdate,IdUsuario
 FROM ConcDiversos
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmOrdenServ INT,@pmNovedadNom BIT,@pmCdConcDeveng VARCHAR(4),@pmCdConcDed VARCHAR(4),@pmRubroConc VARCHAR(50),@pmMudReajuste BIT,@pmCodFEDian VARCHAR(20)
,@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO ConcDiversos (IdConcepto,Concepto,Grupo,EsServicio,DeManfsto,CdIva,CdRet,CdIca,CdAutoret,IdCueDeb,IdCueCre,Inactivo,FechaAdd,IdUsuario,OrdenServ,NovedadNom,CdConcDeveng,CdConcDed,RubroConc,MudReajuste,CodFEDian)
 VALUES (@pmIdConcepto,@pmConcepto,@pmGrupo,@pmEsServicio,@pmDeManfsto,@pmCdIva,@pmCdRet,@pmCdIca,@pmCdAutoret,@pmIdCueDeb,@pmIdCueCre,@pmInactivo,@pmFechaAdd
,@pmIdUsuario,@pmOrdenServ,@pmNovedadNom,@pmCdConcDeveng,@pmCdConcDed,@pmRubroConc,@pmMudReajuste,@pmCodFEDian)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmOrdenServ INT,@pmNovedadNom BIT,@pmCdConcDeveng VARCHAR(4),@pmCdConcDed VARCHAR(4)
,@pmRubroConc VARCHAR(50),@pmMudReajuste BIT,@pmCodFEDian VARCHAR(20),@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
,NovedadNom=@pmNovedadNom,CdConcDeveng=@pmCdConcDeveng,CdConcDed=@pmCdConcDed,FechaUpdate=@pmFechaUpdate,OrdenServ=@pmOrdenServ
,RubroConc=@pmRubroConc,MudReajuste=@pmMudReajuste,CodFEDian=@pmCodFEDian
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMudSecciones] @pmIdSeccion VARCHAR(4)
 AS 
 DELETE FROM MudSecciones
 WHERE IdSeccion=@pmIdSeccion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMudSecciones] @pmIdSeccion VARCHAR(4),@pmSeccion VARCHAR(150),@pmInactivo BIT
 AS 
 UPDATE MudSecciones SET Seccion=@pmSeccion,Inactivo=@pmInactivo
 WHERE IdSeccion=@pmIdSeccion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMudSecciones] @pmIdSeccion VARCHAR(4),@pmSeccion VARCHAR(150),@pmInactivo BIT
 AS 
 INSERT INTO MudSecciones (IdSeccion,Seccion,Inactivo)
 VALUES (@pmIdSeccion,@pmSeccion,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMudSecciones] @pmIdSeccion VARCHAR(4)
 AS 
 IF @pmIdSeccion IS NULL
  BEGIN
   SELECT IdSeccion,Seccion FROM MudSecciones
   WHERE Inactivo=0
   ORDER BY Seccion
  END
 ELSE
  BEGIN
   SELECT IdSeccion,Seccion,Inactivo 
   FROM MudSecciones WHERE IdSeccion=@pmIdSeccion
  END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposAgenc] @pmIdTipAgenc VARCHAR(4)
 AS 
 DELETE FROM TiposAgenc
 WHERE IdTipAgenc=@pmIdTipAgenc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposAgenc] @pmIdTipAgenc VARCHAR(4),@pmTipoAgencia VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposAgenc (IdTipAgenc,TipoAgencia,Inactivo)
 VALUES (@pmIdTipAgenc,@pmTipoAgencia,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposAgenc] @pmIdTipAgenc VARCHAR(4),@pmTipoAgencia VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposAgenc SET TipoAgencia=@pmTipoAgencia,Inactivo=@pmInactivo
 WHERE IdTipAgenc=@pmIdTipAgenc
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelNomDefault] @pmnAnno INT,@pmIdOpc VARCHAR(3)
 AS 
 DELETE FROM NomDefault
 WHERE nAnno=@pmnAnno AND IdOpc LIKE ISNULL(@pmIdOpc,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelProdPrecios] @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2)
 AS 
 DELETE FROM ProdPrecios
 WHERE IdProducto=@pmIdProducto AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsAcuPrestamosLin] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCliente VARCHAR(16)
,@pmIdAgencia VARCHAR(16),@pmIdLinea VARCHAR(4),@pmConcepto VARCHAR(10),@pmSaldoAnt MONEY,@pmTotalCargos MONEY,@pmTotalAbonos MONEY
 AS 
 INSERT INTO AcuPrestamosLin (nAnno,nMes,IdCia,IdCliente,IdAgencia,IdLinea,Concepto,SaldoAnt,TotalCargos,TotalAbonos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCliente,@pmIdAgencia,@pmIdLinea,@pmConcepto,@pmSaldoAnt,@pmTotalCargos,@pmTotalAbonos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_BalGenGcue] @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCuenta,count(tmIdCuenta) AS SCANT
 FROM tm_BalGen
 WHERE tmEst=@pmtmEst
 GROUP BY tmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_BalGen] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_BalGen
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraDevLeg] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraDevLeg
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevLeg] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Liquidacion,IdCiaDoc,FecDoc,Modalidad,TipMuc,Manifiesto,IdCiaMuc
,IdVehiculo,IdConductor,IdPropietario,IdPoseedor,NitEmpresa,VrAnticipos,VrGastos,VrCombustible,VrDiferencia,VrOtrosCred,galsComb,PesoTotal,Cantidad,Volumen
,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraDevLeg
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevLegDoc] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,FecDoc,IdVehiculo,IdConductor,IdPropietario,IdPoseedor,ModdDev
 FROM Trn_TraDevLeg
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCiaDoc=@pmIdCiaDoc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraDevLeg] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmModalidad VARCHAR(10),@pmTipMuc VARCHAR(3),@pmManifiesto INT,@pmIdCiaMuc CHAR(2),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmIdPropietario VARCHAR(16)
,@pmIdPoseedor VARCHAR(16),@pmNitEmpresa VARCHAR(16),@pmVrAnticipos MONEY,@pmVrGastos MONEY,@pmVrCombustible MONEY,@pmVrDiferencia MONEY,@pmgalsComb DECIMAL(14,4),@pmPesoTotal DECIMAL(14,4),@pmCantidad DECIMAL(14,4),@pmVolumen DECIMAL(14,4),@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmVrOtrosCred MONEY,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraDevLeg (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Liquidacion,IdCiaDoc,FecDoc,Modalidad,TipMuc,Manifiesto,IdCiaMuc,IdVehiculo,IdConductor,IdPropietario,IdPoseedor,NitEmpresa,VrAnticipos,VrGastos,VrCombustible,VrDiferencia,VrOtrosCred,galsComb,PesoTotal,Cantidad,Volumen,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmLiquidacion,@pmIdCiaDoc,@pmFecDoc,@pmModalidad,@pmTipMuc,@pmManifiesto,@pmIdCiaMuc,@pmIdVehiculo,@pmIdConductor,@pmIdPropietario,@pmIdPoseedor,@pmNitEmpresa,@pmVrAnticipos,@pmVrGastos,@pmVrCombustible,@pmVrDiferencia,@pmVrOtrosCred,@pmgalsComb,@pmPesoTotal
,@pmCantidad,@pmVolumen,@pmModdDev,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraDevLeg] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmModalidad VARCHAR(10),@pmTipMuc VARCHAR(3),@pmManifiesto INT,@pmIdCiaMuc CHAR(2),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmIdPropietario VARCHAR(16),@pmIdPoseedor VARCHAR(16)
,@pmNitEmpresa VARCHAR(16),@pmVrAnticipos MONEY,@pmVrGastos MONEY,@pmVrCombustible MONEY,@pmVrDiferencia MONEY,@pmgalsComb DECIMAL(14,4),@pmPesoTotal DECIMAL(14,4),@pmCantidad DECIMAL(14,4),@pmVolumen DECIMAL(14,4),@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmVrOtrosCred MONEY,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraDevLeg SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Liquidacion=@pmLiquidacion,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,Modalidad=@pmModalidad,TipMuc=@pmTipMuc,Manifiesto=@pmManifiesto,IdCiaMuc=@pmIdCiaMuc,IdVehiculo=@pmIdVehiculo,IdConductor=@pmIdConductor,IdPropietario=@pmIdPropietario,IdPoseedor=@pmIdPoseedor,NitEmpresa=@pmNitEmpresa,VrAnticipos=@pmVrAnticipos,VrGastos=@pmVrGastos,VrCombustible=@pmVrCombustible
,VrDiferencia=@pmVrDiferencia,VrOtrosCred=@pmVrOtrosCred,galsComb=@pmgalsComb,PesoTotal=@pmPesoTotal,Cantidad=@pmCantidad,Volumen=@pmVolumen,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelAfTiposMov] @pmIdMov VARCHAR(4)
 AS 
 DELETE FROM AfTiposMov
 WHERE IdMov=@pmIdMov
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_Saldos] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_Saldos  WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelAfAmortiza] @pmNumero INT
 AS 
 DELETE FROM Trn_AfAmortiza
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryAfAmortiza] @pmNumero INT
 AS 
 SELECT Numero,Fecha,Estado,IdUsuario,FechaCrea,IdCia
 FROM Trn_AfAmortiza
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraRemCum_Sel] @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_TraRemCum (tmNumero,tmItem,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed,tmVolumen,tmUndVol,tmCases,tmCajas,tmPalets,tmTarifClie,tmTarifPago,tmUndTarif,tmUndTarifPago,tmCantCargue
,tmPesoCargue,tmVolCargue,tmCasesCargue,tmCajasCargue,tmPaletsCargue,tmEstadoCump,tmRemision,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmDetalle,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen,tmNitDestntario,tmDestinatario,tmDirDestino
,tmIdDestino,tmTarifTabla,tmTipoCum,tmMotivoSusp,tmHoraLlegaCargue,tmHoraEntraCargue,tmHoraSaleCargue,tmHoraLlegaDescargue,tmHoraEntraDescargue,tmHoraSaleDescargue,tmCdCCosto,tmCdSubCos,tmNitCliente)
SELECT @pmtmNumero,Item,TipRem,Remesa,IdCiaRem,ItemRem,'0','0',Cantidad,PesoNeto,UndMed,Volumen,UndVol,Cases,Cajas,Palets,TarifClie,TarifPago,UndTarifClie,UndTarifPago,CantCargue,PesoCargue,VolCargue,CasesCargue,CajasCargue,PaletsCargue,EstadoCump
,Remision,DocCliente,Referencia1,Referencia2,Referencia3,Detalle,'0','','','0','0','','','0',TarifFlete,TipoCumRemesa,MotivoSuspRem,HoraLlegaCargue,HoraEntraCargue,HoraSaleCargue,HoraLlegaDescargue,HoraEntraDescargue,HoraSaleDescargue,CodCCosto,CodSubCos,NitClieRem
 FROM Trn_TraCumRemesas 
 WHERE TipDoc=@pmTipDoc AND Cumplido=@pmCumplido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraCumRemesas] @pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipRem VARCHAR(3),@pmRemesa INT,@pmIdCiaRem CHAR(2),@pmItemRem INT,@pmCantidad DECIMAL(14,4),@pmPesoNeto DECIMAL(14,4),@pmUndMed VARCHAR(10),@pmVolumen DECIMAL(14,4),@pmUndVol VARCHAR(10)
,@pmCases INT,@pmCajas INT,@pmPalets INT,@pmTarifClie MONEY,@pmTarifPago MONEY,@pmUndTarifClie VARCHAR(10),@pmUndTarifPago VARCHAR(10),@pmCantCargue DECIMAL(14,4),@pmPesoCargue DECIMAL(14,4),@pmVolCargue DECIMAL(14,4),@pmCasesCargue INT,@pmCajasCargue INT,@pmPaletsCargue INT,@pmEstadoCump INT
,@pmRemision DECIMAL(18,2),@pmDocCliente VARCHAR(30),@pmReferencia1 VARCHAR(50),@pmReferencia2 VARCHAR(50),@pmReferencia3 VARCHAR(50),@pmDetalle VARCHAR(250),@pmTarifFlete MONEY,@pmTipoCumRemesa VARCHAR(3),@pmMotivoSuspRem VARCHAR(3),@pmHoraLlegaCargue SMALLDATETIME,@pmHoraEntraCargue SMALLDATETIME
,@pmHoraSaleCargue SMALLDATETIME,@pmHoraLlegaDescargue SMALLDATETIME,@pmHoraEntraDescargue SMALLDATETIME,@pmHoraSaleDescargue SMALLDATETIME,@pmCodCCosto VARCHAR(16),@pmCodSubCos VARCHAR(16),@pmNitClieRem VARCHAR(16)
 AS 
 INSERT INTO Trn_TraCumRemesas (TipDoc,Cumplido,IdCia,Item,TipRem,Remesa,IdCiaRem,ItemRem,Cantidad,PesoNeto,UndMed,Volumen,UndVol,Cases,Cajas,Palets,TarifClie,TarifPago,UndTarifClie,UndTarifPago,CantCargue,PesoCargue,VolCargue,CasesCargue,CajasCargue,PaletsCargue,EstadoCump,Remision,DocCliente
,Referencia1,Referencia2,Referencia3,Detalle,TarifFlete,TipoCumRemesa,MotivoSuspRem,HoraLlegaCargue,HoraEntraCargue,HoraSaleCargue,HoraLlegaDescargue,HoraEntraDescargue,HoraSaleDescargue,CodCCosto,CodSubCos,NitClieRem)
 VALUES (@pmTipDoc,@pmCumplido,@pmIdCia,@pmItem,@pmTipRem,@pmRemesa,@pmIdCiaRem,@pmItemRem,@pmCantidad,@pmPesoNeto,@pmUndMed,@pmVolumen,@pmUndVol,@pmCases,@pmCajas,@pmPalets,@pmTarifClie,@pmTarifPago,@pmUndTarifClie,@pmUndTarifPago,@pmCantCargue,@pmPesoCargue,@pmVolCargue,@pmCasesCargue
,@pmCajasCargue,@pmPaletsCargue,@pmEstadoCump,@pmRemision,@pmDocCliente,@pmReferencia1,@pmReferencia2,@pmReferencia3,@pmDetalle,@pmTarifFlete,@pmTipoCumRemesa,@pmMotivoSuspRem,@pmHoraLlegaCargue,@pmHoraEntraCargue,@pmHoraSaleCargue,@pmHoraLlegaDescargue,@pmHoraEntraDescargue,@pmHoraSaleDescargue,@pmCodCCosto,@pmCodSubCos,@pmNitClieRem)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTraCumRemesas] @pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCia CHAR(2) 
 AS 
 DELETE FROM Trn_TraCumRemesas
 WHERE TipDoc=@pmTipDoc AND Cumplido=@pmCumplido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposEnt] @pmIdEnt VARCHAR(4),@pmDiaEntrega VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposEnt SET DiaEntrega=@pmDiaEntrega,Inactivo=@pmInactivo
 WHERE IdEnt=@pmIdEnt
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposEnt] @pmIdEnt VARCHAR(4)
 AS 
 IF @pmIdEnt IS NULL
  SELECT IdEnt,DiaEntrega
  FROM TiposEnt
  WHERE Inactivo=0
 ELSE
  SELECT IdEnt,DiaEntrega,Inactivo
  FROM TiposEnt WHERE IdEnt=@pmIdEnt
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposEnt] @pmIdEnt VARCHAR(4),@pmDiaEntrega VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposEnt (IdEnt,DiaEntrega,Inactivo)
 VALUES (@pmIdEnt,@pmDiaEntrega,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposEnt] @pmIdEnt VARCHAR(4)
 AS 
 DELETE FROM TiposEnt WHERE IdEnt=@pmIdEnt
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposNat] @pmIdNat VARCHAR(4),@pmNatlzaprod VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposNat (IdNat,Natlzaprod,Inactivo)
 VALUES (@pmIdNat,@pmNatlzaprod,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposNat] @pmIdNat VARCHAR(4)
 AS 
 DELETE FROM TiposNat
 WHERE IdNat=@pmIdNat
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposNat] @pmIdNat VARCHAR(4),@pmNatlzaprod VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposNat SET Natlzaprod=@pmNatlzaprod,Inactivo=@pmInactivo
 WHERE IdNat=@pmIdNat
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTarifasMasivo] @pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmIdTipoVeh VARCHAR(4),@pmTarifaUnd DECIMAL(14,4),@pmVrFletes DECIMAL(14,4),@pmInactivo BIT
 AS 
 INSERT INTO TarifasMasivo (IdOrigen,IdDestino,IdTipoVeh,TarifaUnd,VrFletes,Inactivo)
 VALUES (@pmIdOrigen,@pmIdDestino,@pmIdTipoVeh,@pmTarifaUnd,@pmVrFletes,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTarifasMasivo] @pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmIdTipoVeh VARCHAR(4),@pmTarifaUnd DECIMAL(14,4),@pmVrFletes DECIMAL(14,4),@pmInactivo BIT
 AS 
 UPDATE TarifasMasivo SET TarifaUnd=@pmTarifaUnd,VrFletes=@pmVrFletes,Inactivo=@pmInactivo
 WHERE IdOrigen=@pmIdOrigen AND IdDestino=@pmIdDestino AND IdTipoVeh=@pmIdTipoVeh
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexPdc] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmCdTanque,tmSalidas,tmVrPrecio,tmTarifaDct,tmVrDcto,(tmSalidas*tmVrPrecio)-tmVrDcto AS VrSubTotal
,tmIvaComb*tmSalidas AS BaseIva,tmTarifaIva,tmVrIva,tmBaseIvp*tmSalidas AS BaseIngProd,tmTarifaIvp,tmIvaIngProd,((tmSalidas*tmVrPrecio)-tmVrDcto)+tmVrIva+tmIvaIngProd AS VrTotal
,tmComptmntos,tmSobretasa,tmImpGlobal,tmSoldicom,tmTasaNac,tmTasaDep,tmTasaMun,tmImpCarb,tmRec_Costo,tmMgenCont,tmVrBruto,tmListaPrec
,tmUnidades,tmReferencia,tmDescripcion,tmCodTarDct,tmIdVend,tmComision,tmCodTarCom,tmCdOperario,tmComisnOper,tmCodTarCmc,tmServcios,Tanques,tmVrUnitario,tmVrBase
,tmCdMoneda,tmVrTasaCamb,tmTipDoc,tmDocumento,tmIdCia,tmIdBodega,tmpVehiculo,tmIvaComb,tmBaseIvp
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmCantObseq
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,SUM(tmVrImpCon) AS SIMPCON,SUM(tmIvaObseq) AS SIVAOBSEQ
 FROM  tm_Kdex
 WHERE tmNumero=@pmtmNumero AND tmEsProdBase=0 AND tmEsCombo<>0
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexGui] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmCdTanque,tmSalidas,tmVrPrecio,tmSalidas*tmVrPrecio AS VrSubTotal
,tmIvaComb*tmSalidas AS BaseIva,tmTarifaIva,tmVrIva,tmBaseIvp*tmSalidas AS BaseIngProd,tmTarifaIvp,tmIvaIngProd
,((tmSalidas*tmVrPrecio)-tmVrDcto)+tmVrIva+tmIvaIngProd AS VrTotal,tmComptmntos,tmSobretasa,tmImpGlobal,tmSoldicom
,tmTasaNac,tmTasaDep,tmTasaMun,tmImpCarb,tmRec_Costo,tmMgenCont,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad
,tmUnidades,tmDescripcion,tmReferencia,tmReferencia2,tmListaPrec,tmVrBruto,tmVrUnitario,tmVrBase,tmServcios,Tanques,tmEsCombo
,tmCdMoneda,tmVrTasaCamb,tmCdLocal,tmIdVend,tmComision,tmCodTarCom,tmpVehiculo,tmIdBodega,tmIvaComb,tmBaseIvp
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmMgenCont
,tmCotizacion,tmCiaCotiza,tmVrImvCosto,tmTarifaIco,tmVrImpCon,tmCantObseq,tmIvaObseq,tmIvaComb,tmImpCarb,tmIngCombo,tmTarifaStc,tmSobtasaCons,tmCodTarIco,tmBaseIvp,tmTarifaIvp,tmIvaIngProd)
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
,CASE WHEN TipOrd='CPP' AND NumOrden>0 THEN TipOrd ELSE TipDoc END,CASE WHEN TipOrd='CPP' AND NumOrden>0 THEN NumOrden ELSE Documento END,CASE WHEN TipOrd='CPP' AND NumOrden>0 THEN IdCiaOrd ELSE IdCia END
,VrBase,CdMoneda,VrTasaCamb,'REM',Remision,IdCiaRem,Referencia2,FecOrden,0,0,0,'',0,Rec_Costo,MgenCont,Cotizacion,IdCiaCot,VrImvCosto,TarifaIco,VrImpCon,CantObseq,VrIvaObseq,BaseIvaCom,ImpCarbono,IngBaseCom
,TarifaStc,SobtasaCons,CodTarIco,BaseIvp,TarifaIvp,IvaIngProd
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmMgenCont,tmCotizacion,tmCiaCotiza,tmVrImvCosto,tmTarifaIco,tmVrImpCon,tmCantObseq,tmIvaObseq
,tmIvaComb,tmImpCarb,tmIngCombo,tmTarifaStc,tmSobtasaCons,tmCodTarIco,tmBaseIvp,tmTarifaIvp,tmIvaIngProd
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmMgenCont
,tmCotizacion,tmCiaCotiza,tmVrImvCosto,tmTarifaIco,tmVrImpCon,tmCantObseq,tmIvaObseq,tmIvaComb,tmImpCarb,tmIngCombo,tmTarifaStc,tmSobtasaCons,tmCodTarIco,tmBaseIvp,tmTarifaIvp,tmIvaIngProd)
SELECT @pmtmNumero,Item,K.IdProducto,K.IdBodega,CdTanque,CASE WHEN CantObseq>0 AND (TipDoc='DVE' OR TipDoc='DEI' OR SUBSTRING(TipDoc,1,2)='FC' OR SUBSTRING(TipDoc,1,2)='FO') THEN Salidas-CantObseq ELSE Salidas END
,CASE WHEN CantObseq>0 AND (TipDoc='ENT' OR TipDoc='COM' OR SUBSTRING(TipDoc,1,2)='DF' OR SUBSTRING(TipDoc,1,2)='DO') THEN Entradas-CantObseq ELSE Entradas END
,K.IdUnd,VrUnitario,VrPrecio,VrCostProm,0,TarifaIva,CASE WHEN CantObseq>0 AND VrIvaObseq>0 THEN (VrIvaEnt+VrIvaSal)-VrIvaObseq ELSE VrIvaEnt+VrIvaSal END,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
,CASE WHEN TipOrd='CPP' AND NumOrden>0 THEN 'OPP' ELSE 'REM' END
,Remision,IdCiaRem,Referencia2,FecOrden,galsbruto,galsneto,Temperatura,UmTemp,Densidad,Rec_Costo,MgenCont,Cotizacion,IdCiaCot,VrImvCosto,TarifaIco,VrImpCon,CantObseq,VrIvaObseq
,BaseIvaCom,ImpCarbono,IngBaseCom,TarifaStc,SobtasaCons,CodTarIco,BaseIvp,TarifaIvp,IvaIngProd
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmtmMgenCont  DECIMAL(14,4),@pmtmCotizacion INT,@pmtmCiaCotiza CHAR(2),@pmtmVrImvCosto DECIMAL(16,4),@pmtmTarifaIco DECIMAL(14,4),@pmtmVrImpCon DECIMAL(16,4)
,@pmtmCantObseq DECIMAL(14,4),@pmtmIvaObseq MONEY,@pmtmIvaComb DECIMAL(16,4),@pmtmImpCarb DECIMAL(14,4),@pmtmIngCombo DECIMAL(16,4),@pmtmTarifaStc DECIMAL(14,4),@pmtmSobtasaCons DECIMAL(16,4),@pmtmCodTarIco VARCHAR(4),@pmtmBaseIvp MONEY,@pmtmTarifaIvp DECIMAL(14,4),@pmtmIvaIngProd MONEY
 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,tmMgenCont,tmCotizacion,tmCiaCotiza,tmVrImvCosto,tmTarifaIco,tmVrImpCon,tmCantObseq,tmIvaObseq,tmIvaComb,tmImpCarb,tmIngCombo,tmTarifaStc,tmSobtasaCons,tmCodTarIco
,tmBaseIvp,tmTarifaIvp,tmIvaIngProd)
 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,@pmtmMgenCont,@pmtmCotizacion,@pmtmCiaCotiza,@pmtmVrImvCosto,@pmtmTarifaIco,@pmtmVrImpCon,@pmtmCantObseq,@pmtmIvaObseq
,@pmtmIvaComb,@pmtmImpCarb,@pmtmIngCombo,@pmtmTarifaStc,@pmtmSobtasaCons,@pmtmCodTarIco,@pmtmBaseIvp,@pmtmTarifaIvp,@pmtmIvaIngProd)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
--entradas con flete al costo
,SUM(tmEntradas*(tmVrUnitario+tmNumInicial)) AS SCOSENTFLE
,SUM(tmSalidas*(tmVrUnitario+tmNumInicial)) AS SCOSSALFLE
,SUM(tmCantObseq) AS CANTOBSEQ
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmMgenCont,tmCotizacion,tmCiaCotiza,tmVrImvCosto,tmTarifaIco,tmVrImpCon,tmCantObseq,tmIvaObseq,tmIvaComb,tmImpCarb,tmIngCombo,tmTarifaStc,tmSobtasaCons,tmCodTarIco,tmBaseIvp,tmTarifaIvp,tmIvaIngProd)
SELECT @pmtmNumero,Item,K.IdProducto,K.IdBodega,CdTanque,CASE WHEN CantObseq>0 AND (TipDoc='ENT' OR TipDoc='COM' OR SUBSTRING(TipDoc,1,2)='DF' OR SUBSTRING(TipDoc,1,2)='DO') THEN Entradas-CantObseq ELSE Entradas END
,CASE WHEN CantObseq>0 AND (TipDoc='DVE' OR TipDoc='DEI' OR SUBSTRING(TipDoc,1,2)='FC' OR SUBSTRING(TipDoc,1,2)='FO') THEN Salidas-CantObseq ELSE Salidas END
,K.IdUnd,VrUnitario,VrPrecio,VrCostProm,0,TarifaIva,CASE WHEN CantObseq>0 AND VrIvaObseq>0 THEN (VrIvaEnt+VrIvaSal)-VrIvaObseq ELSE VrIvaEnt+VrIvaSal END
,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,MgenCont,Cotizacion,IdCiaCot,VrImvCosto,TarifaIco,VrImpCon,CantObseq,VrIvaObseq
,BaseIvaCom,ImpCarbono,IngBaseCom,TarifaStc,SobtasaCons,CodTarIco,BaseIvp,TarifaIvp,IvaIngProd
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,Tallaje
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexOcc] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmCdTanque,tmSalidas,tmVrPrecio,(tmSalidas*tmVrPrecio)-tmVrDcto AS VrSubTotal
,tmIvaComb*tmSalidas AS BaseIva,tmTarifaIva,tmVrIva,tmBaseIvp*tmSalidas AS BaseIngProd,tmTarifaIvp,tmIvaIngProd
,((tmSalidas*tmVrPrecio)-tmVrDcto)+tmVrIva+tmIvaIngProd AS VrTotal,tmComptmntos,tmSobretasa,tmImpGlobal,tmSoldicom
,tmTasaNac,tmTasaDep,tmTasaMun,tmImpCarb,tmRec_Costo,tmMgenCont,tmUnidades,tmReferencia,tmDescripcion,tmListaPrec,tmVrBruto,tmVrUnitario,tmVrBase
,tmServcios,Tanques,tmEsCombo,tmCdMoneda,tmVrTasaCamb,tmCdLocal,tmIdVend,tmComision,tmCodTarCom,tmpVehiculo
,tmReferencia2,tmIdBodega,tmIvaComb,tmBaseIvp
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexOrd] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmTipDoc,tmDocumento,tmIdCia,tmTipRem,tmRemision,tmIdCiaRem,count(tmItem) AS CantItm
,SUM(CASE tmNoVentas WHEN -1 THEN 1 ELSE 0 END) AS SODCCIE
,SUM(CASE WHEN tmNoVentas>=0 AND tmServcios=0 THEN 1 ELSE 0 END) AS SODCNOCIE
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero  AND (tmDocumento>0 OR tmRemision>0)
 GROUP BY  tmTipDoc,tmDocumento,tmIdCia,tmTipRem,tmRemision,tmIdCiaRem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Kdex_NtsDev] @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,tmMgenCont
,tmCotizacion,tmCiaCotiza,tmVrImvCosto,tmTarifaIco,tmVrImpCon,tmCantObseq,tmIvaObseq,tmIvaComb,tmImpCarb,tmIngCombo,tmTarifaStc,tmSobtasaCons,tmCodTarIco,tmBaseIvp,tmTarifaIvp,tmIvaIngProd)
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
,CASE WHEN TipOrd='CPP' AND NumOrden>0 THEN TipOrd ELSE TipDoc END,CASE WHEN TipOrd='CPP' AND NumOrden>0 THEN NumOrden ELSE Documento END,CASE WHEN TipOrd='CPP' AND NumOrden>0 THEN IdCiaOrd ELSE IdCia END
,VrBase,CdMoneda,VrTasaCamb,'REM',Remision,IdCiaRem,Referencia2,FecOrden,0,0,0,'',0,Rec_Costo,MgenCont,Cotizacion,IdCiaCot,VrImvCosto,TarifaIco,VrImpCon,CantObseq,VrIvaObseq,BaseIvaCom,ImpCarbono,IngBaseCom,TarifaStc,SobtasaCons,CodTarIco
,BaseIvp,TarifaIvp,IvaIngProd
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,SUM(tmVrImvCosto) AS SIVAMVC,SUM(tmIvaObseq) AS SIVAOBSEQ
,SUM(tmVrImpCon) AS SIMPCON,SUM(tmSobtasaCons) AS SSOBCON
 FROM  tm_Kdex
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmMgenCont,tmCotizacion,tmCiaCotiza,tmVrImvCosto,tmTarifaIco,tmVrImpCon,tmCantObseq,tmIvaObseq,tmIvaComb,tmImpCarb,tmIngCombo
,tmTarifaStc,tmSobtasaCons,tmCodTarIco,tmBaseIvp,tmTarifaIvp,tmIvaIngProd
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY  tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexSubOpe] @pmtmNumero VARCHAR(5)
 AS 
 SELECT IdSubgrupo,tmIdProducto,DescripProd,TipoRef,Combo,tmEsProdBase,IdProv,ExcluidoImp,Electrocomb,IvaDetCombo,tmCdOperario
,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
,SUM(tmMgenCont*tmEntradas) AS SMGENENT,SUM(tmMgenCont*tmSalidas) AS SMGENSAL
,SUM(tmVrImvCosto) AS SIMVCOS,SUM(tmVrImpCon) AS SVICO
,SUM(CASE WHEN tmCantObseq>0 THEN tmIvaObseq ELSE 0 END) AS SIVAOBSQ
,SUM(tmCantObseq*tmVrUnitario) AS SBASCOSOBSQ,SUM(tmCantObseq*tmVrPrecio) AS SBASEOBSQ
,SUM(tmIvaComb*tmEntradas) AS BASEIVAENT,SUM(tmIvaComb*tmSalidas) AS BASEIVASAL 
,SUM(tmImpCarb*tmEntradas) AS IMPCARBENT,SUM(tmImpCarb*tmSalidas) AS IMPCARBSAL 
,SUM(CASE WHEN (tmTarifaIva>0 AND P.TipoZonaFront='F') THEN ((tmEntradas*tmIvaComb)*tmTarifaIva)/100 ELSE 0 END) AS SIVAZFENT
,SUM(CASE WHEN (tmTarifaIva>0 AND P.TipoZonaFront='F') THEN ((tmSalidas*tmIvaComb)*tmTarifaIva)/100 ELSE 0 END) AS SIVAZFSAL
,SUM(CASE WHEN Combo=0 AND tmEsProdBase=1 THEN tmIvaComb ELSE 0 END) AS BASEIVACOM
,SUM(CASE WHEN Combo=1 AND IvaDetCombo=1 THEN tmIngCombo ELSE 0 END) AS INGBASECOM
,SUM(CASE WHEN Combo=0 AND tmEsProdBase=1 THEN tmIngCombo ELSE 0 END) AS SBASEING
,SUM(tmSobtasaCons) AS SOBTCON,SUM(tmBaseIvp*tmEntradas) AS BASEIVPENT,SUM(tmBaseIvp*tmSalidas) AS BASEIVAING,SUM(tmIvaIngProd) AS SIVAINGP,MAX(tmTarifaIvp) AS TARIVAING
 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,ExcluidoImp,Electrocomb,IvaDetCombo,tmCdOperario
 ORDER BY IdSubgrupo,tmIdProducto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Kdex_Ord] @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,tmMgenCont,tmCotizacion,tmCiaCotiza,tmVrImvCosto,tmTarifaIco,tmVrImpCon,tmCantObseq,tmIvaObseq,tmIvaComb,tmImpCarb,tmIngCombo,tmTarifaStc,tmSobtasaCons,tmCodTarIco,tmBaseIvp,tmTarifaIvp,tmIvaIngProd)
SELECT @pmtmNumero,Item,K.IdProducto,K.IdBodega,CdTanque,CASE WHEN CantObseq>0 AND (TipDoc='ENT' OR TipDoc='COM' OR SUBSTRING(TipDoc,1,2)='DF' OR SUBSTRING(TipDoc,1,2)='DO') THEN Entradas-CantObseq ELSE Entradas END
,CASE WHEN CantObseq>0 AND (TipDoc='DVE' OR TipDoc='DEI' OR SUBSTRING(TipDoc,1,2)='FC' OR SUBSTRING(TipDoc,1,2)='FO') THEN Salidas-CantObseq 
 WHEN (TipDoc='PED' AND TipDocDev='PPD' AND Unidades>0) THEN Unidades ELSE Salidas END
,K.IdUnd,VrUnitario,VrPrecio,VrCostProm,0,TarifaIva,CASE WHEN CantObseq>0 AND VrIvaObseq>0 THEN (VrIvaEnt+VrIvaSal)-VrIvaObseq ELSE VrIvaEnt+VrIvaSal END
,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,MgenCont,Cotizacion,IdCiaCot,VrImvCosto,TarifaIco,VrImpCon,CantObseq,VrIvaObseq
,BaseIvaCom,ImpCarbono,IngBaseCom,TarifaStc,SobtasaCons,CodTarIco,BaseIvp,TarifaIvp,IvaIngProd
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmMgenCont,tmCotizacion,tmCiaCotiza,tmVrImvCosto,tmTarifaIco,tmVrImpCon,tmCantObseq,tmIvaObseq,tmIvaComb,tmImpCarb,tmIngCombo,tmTarifaStc,tmSobtasaCons,tmCodTarIco,tmBaseIvp,tmTarifaIvp,tmIvaIngProd)
SELECT @pmtmNumero,Item,K.IdProducto,K.IdBodega,CdTanque,CASE WHEN CantObseq>0 AND (TipDoc='DVE' OR TipDoc='DEI') THEN Salidas-CantObseq ELSE Salidas END
,CASE WHEN CantObseq>0 AND (TipDoc='ENT' OR TipDoc='COM') THEN Entradas-CantObseq ELSE Entradas END
,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,CASE WHEN CantObseq>0 AND VrIvaObseq>0 THEN (VrIvaEnt+VrIvaSal)-VrIvaObseq ELSE VrIvaEnt+VrIvaSal END
,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,MgenCont,Cotizacion,IdCiaCot,VrImvCosto,TarifaIco,VrImpCon,CantObseq,VrIvaObseq
,BaseIvaCom,ImpCarbono,IngBaseCom,TarifaStc,SobtasaCons,CodTarIco,BaseIvp,TarifaIvp,IvaIngProd
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexCom] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm
,tmVrBruto,tmVrDcto,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,IvaDetCombo,LtaBaseIva
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmCantObseq,tmIvaObseq,tmOtroImpto AS AjuCostoDve
--variables de ProMcias
,DescripProd,TipoRef,IdSubgrupo,IdBodega,IdUbic,VrCostAnt,VrCosto,VrCostPmd,ExtciaMin,ExtciaMax,ExtciaAct
,Seriales,Lotes,Combo,Tanques,Tallaje
--,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
 ,SUM(tmVrImvCosto) AS SIVAMVC,SUM(tmVrImpCon) AS SIMPCON,SUM(tmSobtasaCons) AS SSOBCON
  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
    ,SUM(tmVrImvCosto) AS SIVAMVC,SUM(tmVrImpCon) AS SIMPCON,SUM(tmSobtasaCons) AS SSOBCON
    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
    ,SUM(tmVrImvCosto) AS SIVAMVC,SUM(tmVrImpCon) AS SIMPCON,SUM(tmSobtasaCons) AS SSOBCON
    FROM  tm_Kdex
    WHERE tmNumero=@pmtmNumero AND  tmVrIca>0
   END
 END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,SUM(tmImpCarb*tmEntradas) AS SICARENT,SUM(tmImpCarb*tmSalidas) AS SICARSAL
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmCantObseq
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexSub] @pmtmNumero VARCHAR(5)
 AS 
 SELECT IdSubgrupo,tmIdProducto,DescripProd,TipoRef,Combo,tmEsProdBase,IdProv,ExcluidoImp,Electrocomb,IvaDetCombo
,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
,SUM(tmMgenCont*tmEntradas) AS SMGENENT,SUM(tmMgenCont*tmSalidas) AS SMGENSAL
,SUM(tmVrImvCosto) AS SIMVCOS,SUM(tmVrImpCon) AS SVICO
,SUM(CASE WHEN tmCantObseq>0 THEN tmIvaObseq ELSE 0 END) AS SIVAOBSQ
,SUM(tmCantObseq*tmVrUnitario) AS SBASCOSOBSQ,SUM(tmCantObseq*tmVrPrecio) AS SBASEOBSQ
,SUM(tmIvaComb*tmEntradas) AS BASEIVAENT,SUM(tmIvaComb*tmSalidas) AS BASEIVASAL 
,SUM(tmImpCarb*tmEntradas) AS IMPCARBENT,SUM(tmImpCarb*tmSalidas) AS IMPCARBSAL 
,SUM(CASE WHEN (tmTarifaIva>0 AND P.TipoZonaFront='F') THEN ((tmEntradas*tmIvaComb)*tmTarifaIva)/100 ELSE 0 END) AS SIVAZFENT
,SUM(CASE WHEN (tmTarifaIva>0 AND P.TipoZonaFront='F') THEN ((tmSalidas*tmIvaComb)*tmTarifaIva)/100 ELSE 0 END) AS SIVAZFSAL
,SUM(CASE WHEN Combo=0 AND tmEsProdBase=1 THEN tmIvaComb ELSE 0 END) AS BASEIVACOM
,SUM(CASE WHEN Combo=1 AND IvaDetCombo=1 THEN tmIngCombo ELSE 0 END) AS INGBASECOM
,SUM(CASE WHEN Combo=0 AND tmEsProdBase=1 THEN tmIngCombo ELSE 0 END) AS SBASEING
,SUM(tmSobtasaCons) AS SOBTCON
,SUM(tmBaseIvp*tmEntradas) AS BASEIVPENT,SUM(tmBaseIvp*tmSalidas) AS BASEIVAING,SUM(tmIvaIngProd) AS SIVAINGP,MAX(tmTarifaIvp) AS TARIVAING
 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,ExcluidoImp,Electrocomb,IvaDetCombo
 ORDER BY IdSubgrupo,tmIdProducto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_KdexCc] @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16)
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCodTarIca,tmCodTarRet
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmMgenCont,tmCotizacion,tmCiaCotiza,tmVrImvCosto
,tmTarifaIco,tmVrImpCon,tmCantObseq,tmIvaObseq,tmIvaComb,tmImpCarb,tmIngCombo
,tmgalsbruto,tmTemperatura,tmUmTemp,tmDensidad
--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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,SUM(tmVrImpCon) AS SIMPCON,SUM(tmIvaObseq) AS SIVAOBSEQ,SUM(tmVrImvCosto) AS SIVAMVC
 FROM  tm_Kdex
 WHERE tmNumero=@pmtmNumero AND tmEsProdBase=0 AND tmEsCombo=0
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmtmProdCia BIT
,@pmtmUltCostoPre MONEY,@pmtmCostoAntPre MONEY,@pmtmBaseMargen VARCHAR(10),@pmtmTarifaIva DECIMAL(14,4)
,@pmtmPrecioBas1 MONEY,@pmtmPrecioBas2 MONEY,@pmtmPrecioBas3 MONEY,@pmtmPrecioBas4 MONEY,@pmtmPrecioBas5 MONEY
,@pmtmMargen1 DECIMAL(14,4),@pmtmMargen2 DECIMAL(14,4),@pmtmMargen3 DECIMAL(14,4),@pmtmMargen4 DECIMAL(14,4),@pmtmMargen5 DECIMAL(14,4)
,@pmtmDcto1 DECIMAL(14,4),@pmtmDcto2 DECIMAL(14,4),@pmtmDcto3 DECIMAL(14,4),@pmtmDcto4 DECIMAL(14,4),@pmtmDcto5 DECIMAL(14,4)
,@pmtmIncIva1 BIT,@pmtmIncIva2 BIT,@pmtmIncIva3 BIT,@pmtmIncIva4 BIT,@pmtmIncIva5 BIT
,@pmtmPrecioVta1 MONEY,@pmtmPrecioVta2 MONEY,@pmtmPrecioVta3 MONEY,@pmtmPrecioVta4 MONEY,@pmtmPrecioVta5 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,tmProdCia
,tmUltCostoPre,tmCostoAntPre,tmBaseMargen,tmTarifaIva,tmPrecioBas1,tmPrecioBas2,tmPrecioBas3,tmPrecioBas4,tmPrecioBas5,tmMargen1,tmMargen2,tmMargen3,tmMargen4,tmMargen5
,tmDcto1,tmDcto2,tmDcto3,tmDcto4,tmDcto5,tmIncIva1,tmIncIva2,tmIncIva3,tmIncIva4,tmIncIva5,tmPrecioVta1,tmPrecioVta2,tmPrecioVta3,tmPrecioVta4,tmPrecioVta5)
 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,@pmtmProdCia
,@pmtmUltCostoPre,@pmtmCostoAntPre,@pmtmBaseMargen,@pmtmTarifaIva,@pmtmPrecioBas1,@pmtmPrecioBas2,@pmtmPrecioBas3,@pmtmPrecioBas4,@pmtmPrecioBas5,@pmtmMargen1,@pmtmMargen2,@pmtmMargen3,@pmtmMargen4,@pmtmMargen5
,@pmtmDcto1,@pmtmDcto2,@pmtmDcto3,@pmtmDcto4,@pmtmDcto5,@pmtmIncIva1,@pmtmIncIva2,@pmtmIncIva3,@pmtmIncIva4,@pmtmIncIva5,@pmtmPrecioVta1,@pmtmPrecioVta2,@pmtmPrecioVta3,@pmtmPrecioVta4,@pmtmPrecioVta5)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_AuxInv] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_AuxInv
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraRemCum] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipRem VARCHAR(3),@pmtmRemesa INT,@pmtmIdCiaRem CHAR(2),@pmtmItemRem INT,@pmtmIdMercancia VARCHAR(16),@pmtmDescripMcias VARCHAR(250),@pmtmCantidad DECIMAL(14,4),@pmtmPesoNeto DECIMAL(14,4),@pmtmUndMed VARCHAR(10),@pmtmVolumen DECIMAL(14,4),@pmtmUndVol VARCHAR(10),@pmtmCases INT,@pmtmCajas INT,@pmtmPalets INT,@pmtmTarifClie MONEY
,@pmtmTarifPago MONEY,@pmtmUndTarif VARCHAR(10),@pmtmUndTarifPago VARCHAR(10),@pmtmCantCargue DECIMAL(14,4),@pmtmPesoCargue DECIMAL(14,4),@pmtmVolCargue DECIMAL(14,4),@pmtmCasesCargue INT,@pmtmCajasCargue INT,@pmtmPaletsCargue INT,@pmtmEstadoCump INT,@pmtmRemision DECIMAL(18,2),@pmtmDocCliente VARCHAR(30),@pmtmReferencia1 VARCHAR(50),@pmtmReferencia2 VARCHAR(50),@pmtmReferencia3 VARCHAR(50),@pmtmDetalle VARCHAR(250)
,@pmtmNitRemite VARCHAR(16),@pmtmRemitente VARCHAR(250),@pmtmDirOrigen VARCHAR(250),@pmtmIdOrigen VARCHAR(8),@pmtmNitDestntario VARCHAR(16),@pmtmDestinatario VARCHAR(250),@pmtmDirDestino VARCHAR(250),@pmtmIdDestino VARCHAR(8),@pmtmTarifTabla MONEY,@pmtmTipoCum VARCHAR(3),@pmtmMotivoSusp VARCHAR(3),@pmtmHoraLlegaCargue SMALLDATETIME,@pmtmHoraEntraCargue SMALLDATETIME
,@pmtmHoraSaleCargue SMALLDATETIME,@pmtmHoraLlegaDescargue SMALLDATETIME,@pmtmHoraEntraDescargue SMALLDATETIME,@pmtmHoraSaleDescargue SMALLDATETIME,@pmtmCdCCosto VARCHAR(16),@pmtmCdSubCos VARCHAR(16),@pmtmNitCliente VARCHAR(16)
 AS 
 INSERT INTO tm_TraRemCum (tmNumero,tmItem,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed,tmVolumen,tmUndVol,tmCases,tmCajas,tmPalets,tmTarifClie,tmTarifPago,tmUndTarif,tmUndTarifPago,tmCantCargue,tmPesoCargue,tmVolCargue,tmCasesCargue,tmCajasCargue,tmPaletsCargue,tmEstadoCump,tmRemision,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmDetalle,tmNitRemite,tmRemitente
,tmDirOrigen,tmIdOrigen,tmNitDestntario,tmDestinatario,tmDirDestino,tmIdDestino,tmTarifTabla,tmTipoCum,tmMotivoSusp,tmHoraLlegaCargue,tmHoraEntraCargue,tmHoraSaleCargue,tmHoraLlegaDescargue,tmHoraEntraDescargue,tmHoraSaleDescargue,tmCdCCosto,tmCdSubCos,tmNitCliente)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipRem,@pmtmRemesa,@pmtmIdCiaRem,@pmtmItemRem,@pmtmIdMercancia,@pmtmDescripMcias,@pmtmCantidad,@pmtmPesoNeto,@pmtmUndMed,@pmtmVolumen,@pmtmUndVol,@pmtmCases,@pmtmCajas,@pmtmPalets,@pmtmTarifClie,@pmtmTarifPago,@pmtmUndTarif,@pmtmUndTarifPago,@pmtmCantCargue,@pmtmPesoCargue,@pmtmVolCargue,@pmtmCasesCargue,@pmtmCajasCargue,@pmtmPaletsCargue
,@pmtmEstadoCump,@pmtmRemision,@pmtmDocCliente,@pmtmReferencia1,@pmtmReferencia2,@pmtmReferencia3,@pmtmDetalle,@pmtmNitRemite,@pmtmRemitente,@pmtmDirOrigen,@pmtmIdOrigen,@pmtmNitDestntario,@pmtmDestinatario,@pmtmDirDestino,@pmtmIdDestino,@pmtmTarifTabla,@pmtmTipoCum,@pmtmMotivoSusp,@pmtmHoraLlegaCargue,@pmtmHoraEntraCargue,@pmtmHoraSaleCargue,@pmtmHoraLlegaDescargue,@pmtmHoraEntraDescargue,@pmtmHoraSaleDescargue,@pmtmCdCCosto,@pmtmCdSubCos,@pmtmNitCliente)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpDiarios] @pmIdDiario VARCHAR(4),@pmDiario VARCHAR(50),@pmCodTransac VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE Diarios SET Diario=@pmDiario,CodTransac=@pmCodTransac,Inactivo=@pmInactivo
 WHERE IdDiario=@pmIdDiario
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelDiarios] @pmIdDiario VARCHAR(4)
 AS 
 DELETE FROM Diarios
 WHERE IdDiario=@pmIdDiario
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsDiarios] @pmIdDiario VARCHAR(4),@pmDiario VARCHAR(50),@pmCodTransac VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO Diarios (IdDiario,Diario,Inactivo,CodTransac)
 VALUES (@pmIdDiario,@pmDiario,@pmInactivo,@pmCodTransac)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDiarios] @pmIdDiario VARCHAR(4)
 AS 
 IF @pmIdDiario IS NULL
  SELECT IdDiario,Diario,CodTransac
  FROM Diarios
  WHERE Inactivo=0
  ORDER BY IdDiario
 ELSE
  SELECT IdDiario,Diario,CodTransac,Inactivo
  FROM Diarios
  WHERE IdDiario=@pmIdDiario
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPuntBases] @pmIdProgrma INT
 AS 
 DELETE FROM PuntBases
 WHERE IdProgrma=@pmIdProgrma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelUOpciones] @pmIdUsuario VARCHAR(11),@pmNomOpcion VARCHAR(20)=Null
 AS 
 DELETE FROM adm_UOpciones
 WHERE IdUsuario=@pmIdUsuario AND NomOpcion LIKE ISNULL(@pmNomOpcion,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsUOpciones] @pmIdUsuario VARCHAR(11),@pmNomOpcion VARCHAR(20),@pmValor VARCHAR(50)
 AS 
 INSERT INTO adm_UOpciones (IdUsuario,NomOpcion,Valor)
 VALUES (@pmIdUsuario,@pmNomOpcion,@pmValor)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraFijos] @pmNumero INT,@pmIdConcepto VARCHAR(4),@pmDescConc VARCHAR(250),@pmTipoConc VARCHAR(10)
,@pmTarifa DECIMAL(14,4),@pmTipoTarif CHAR(1),@pmTipoAfilVeh VARCHAR(10),@pmCdTipoVeh VARCHAR(4),@pmAnulado BIT,@pmTipoTerc CHAR(1)
,@pmCdCiuOrigen VARCHAR(8),@pmCdCiuDestino VARCHAR(8),@pmpVehiculo VARCHAR(10),@pmCedConductor VARCHAR(16),@pmCdRuta VARCHAR(4)
,@pmTipoVigencia CHAR(1),@pmFechaInicial SMALLDATETIME,@pmFechaFinal SMALLDATETIME,@pmNitCliente VARCHAR(16),@pmCdMercancia VARCHAR(16)
 AS 
 INSERT INTO TraFijos (Numero,IdConcepto,DescConc,TipoConc,Tarifa,TipoTarif,TipoAfilVeh,CdTipoVeh,TipoTerc,Anulado,CdCiuOrigen,CdCiuDestino,pVehiculo,CedConductor,CdRuta,TipoVigencia,FechaInicial,FechaFinal,NitCliente,CdMercancia)
 VALUES (@pmNumero,@pmIdConcepto,@pmDescConc,@pmTipoConc,@pmTarifa,@pmTipoTarif,@pmTipoAfilVeh,@pmCdTipoVeh,@pmTipoTerc
,@pmAnulado,@pmCdCiuOrigen,@pmCdCiuDestino,@pmpVehiculo,@pmCedConductor,@pmCdRuta,@pmTipoVigencia,@pmFechaInicial,@pmFechaFinal,@pmNitCliente,@pmCdMercancia)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraFijos] @pmNumero INT,@pmIdConcepto VARCHAR(4),@pmDescConc VARCHAR(250),@pmTipoConc VARCHAR(10),@pmTarifa DECIMAL(14,4),@pmTipoTarif CHAR(1),@pmTipoAfilVeh VARCHAR(10)
,@pmCdTipoVeh VARCHAR(4),@pmAnulado BIT,@pmTipoTerc CHAR(1),@pmCdCiuOrigen VARCHAR(8),@pmCdCiuDestino VARCHAR(8),@pmpVehiculo VARCHAR(10),@pmCedConductor VARCHAR(16),@pmCdRuta VARCHAR(4)
,@pmTipoVigencia CHAR(1),@pmFechaInicial SMALLDATETIME,@pmFechaFinal SMALLDATETIME,@pmNitCliente VARCHAR(16),@pmCdMercancia VARCHAR(16)
 AS 
 UPDATE TraFijos SET IdConcepto=@pmIdConcepto,DescConc=@pmDescConc,TipoConc=@pmTipoConc,Tarifa=@pmTarifa,TipoTarif=@pmTipoTarif,TipoAfilVeh=@pmTipoAfilVeh,CdTipoVeh=@pmCdTipoVeh
,Anulado=@pmAnulado,TipoTerc=@pmTipoTerc,CdCiuOrigen=@pmCdCiuOrigen,CdCiuDestino=@pmCdCiuDestino,pVehiculo=@pmpVehiculo,CedConductor=@pmCedConductor,CdRuta=@pmCdRuta
,TipoVigencia=@pmTipoVigencia,FechaInicial=@pmFechaInicial,FechaFinal=@pmFechaFinal,NitCliente=@pmNitCliente,CdMercancia=@pmCdMercancia
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraFijos] @pmNumero INT
 AS 
 SELECT Numero,IdConcepto,DescConc,TipoConc,Tarifa,TipoTarif,TipoAfilVeh,CdTipoVeh,TipoTerc,Anulado
,CdCiuOrigen,CdCiuDestino,pVehiculo,CedConductor,CdRuta,TipoVigencia,FechaInicial,FechaFinal,NitCliente,CdMercancia
 FROM TraFijos
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelRangosPeso] @pmIdRango VARCHAR(4)
 AS 
 DELETE FROM RangosPeso
 WHERE IdRango=@pmIdRango
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsFinClieSaldos_Uni] @pmIdClie VARCHAR(16),@pmIdClieDos VARCHAR(16),@pmNewClie VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdClie FROM FinClieSaldos WHERE IdClie=@pmIdClie OR IdClie=@pmIdClieDos)
  INSERT INTO FinClieSaldos (IdClie,CdAgencia,IdCia,VrSaldo)
  SELECT @pmNewClie,CdAgencia,IdCia,SUM(VrSaldo)
  FROM FinClieSaldos  
  WHERE  IdClie=@pmIdClie OR IdClie=@pmIdClieDos
  GROUP BY CdAgencia,IdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposComAgenc] @pmIdSubAgenc VARCHAR(4)
 AS 
 DELETE FROM TiposComAgenc
 WHERE IdSubAgenc=@pmIdSubAgenc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposComAgenc] @pmIdSubAgenc VARCHAR(4)
 AS 
 SELECT IdSubAgenc,SubTipoAgenc,IdTipAgenc,Inactivo
 FROM TiposComAgenc
 WHERE IdSubAgenc=@pmIdSubAgenc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelEstratos] @pmIdEstrato VARCHAR(4)
 AS 
 DELETE FROM Estratos
 WHERE IdEstrato=@pmIdEstrato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpEstratos] @pmIdEstrato VARCHAR(4),@pmEstrato VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE Estratos SET Estrato=@pmEstrato,Inactivo=@pmInactivo
 WHERE IdEstrato=@pmIdEstrato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsEstratos] @pmIdEstrato VARCHAR(4),@pmEstrato VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO Estratos (IdEstrato,Estrato,Inactivo)
 VALUES (@pmIdEstrato,@pmEstrato,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelRequisicionOdt] @pmTipOdt VARCHAR(3),@pmNumOrden INT,@pmIdCiaOdt CHAR(2)
 AS 
 IF EXISTS (SELECT * FROM Trn_MttoOrdenReq WHERE TipOdt=@pmTipOdt AND NumOrden=@pmNumOrden AND IdCiaOdt=@pmIdCiaOdt)
  BEGIN 
   DECLARE @TrnNombre VARCHAR(30)
   SET @TrnNombre = 'Trn_MttoOrdenReq'
   BEGIN TRANSACTION @TrnNombre
   DELETE FROM Trn_ReqDetalle FROM Trn_ReqDetalle INNER JOIN Trn_MttoOrdenReq ON Trn_ReqDetalle.TipDoc=Trn_MttoOrdenReq.TipReq
   AND Trn_ReqDetalle.Requisicion=Trn_MttoOrdenReq.Requisicion AND Trn_ReqDetalle.IdCia=Trn_MttoOrdenReq.IdCia
   WHERE Trn_MttoOrdenReq.TipOdt=@pmTipOdt AND Trn_MttoOrdenReq.NumOrden=@pmNumOrden AND Trn_MttoOrdenReq.IdCiaOdt=@pmIdCiaOdt
   DELETE FROM Trn_Requisicion FROM Trn_Requisicion INNER JOIN Trn_MttoOrdenReq ON Trn_Requisicion.TipDoc=Trn_MttoOrdenReq.TipReq
   AND Trn_Requisicion.Requisicion=Trn_MttoOrdenReq.Requisicion AND Trn_Requisicion.IdCia=Trn_MttoOrdenReq.IdCia
   WHERE Trn_MttoOrdenReq.TipOdt=@pmTipOdt AND Trn_MttoOrdenReq.NumOrden=@pmNumOrden AND Trn_MttoOrdenReq.IdCiaOdt=@pmIdCiaOdt
   COMMIT TRANSACTION  @TrnNombre
  END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMttoOrdenReq] @pmTipReq VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipoItem VARCHAR(10),@pmIdProducto VARCHAR(16),@pmDescripcion VARCHAR(250),@pmCantidad DECIMAL(14,4),@pmIdUnd VARCHAR(4),@pmVrUnitario DECIMAL(16,6),@pmCdBodega VARCHAR(4)
,@pmTipOdt VARCHAR(3),@pmNumOrden INT,@pmIdCiaOdt CHAR(2),@pmEstadoReq INT,@pmIdOperario VARCHAR(16),@pmCdCenServ VARCHAR(4),@pmNitTercero VARCHAR(16),@pmNumVehic VARCHAR(10),@pmNumParte VARCHAR(20),@pmTipSal VARCHAR(3),@pmNumSalida INT,@pmIdCiaSal CHAR(2),@pmCantSalida DECIMAL(14,4)
,@pmObservacion VARCHAR(1000),@pmCdConcServ VARCHAR(4),@pmVrPrecioUnd MONEY,@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_MttoOrdenReq (TipReq,Requisicion,IdCia,Item,TipoItem,IdProducto,Descripcion,Cantidad,IdUnd,VrUnitario,CdBodega,TipOdt,NumOrden,IdCiaOdt,EstadoReq,IdOperario,CdCenServ,NitTercero,NumVehic,NumParte,TipSal,NumSalida,IdCiaSal,CantSalida,Observacion,FechaCrea,IdUsuario,CdConcServ,VrPrecioUnd)
 VALUES (@pmTipReq,@pmRequisicion,@pmIdCia,@pmItem,@pmTipoItem,@pmIdProducto,@pmDescripcion,@pmCantidad,@pmIdUnd,@pmVrUnitario,@pmCdBodega,@pmTipOdt,@pmNumOrden,@pmIdCiaOdt,@pmEstadoReq,@pmIdOperario,@pmCdCenServ,@pmNitTercero,@pmNumVehic,@pmNumParte,@pmTipSal,@pmNumSalida,@pmIdCiaSal,@pmCantSalida
,@pmObservacion,@pmFechaCrea,@pmIdUsuario,@pmCdConcServ,@pmVrPrecioUnd)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposCon] @pmIdTipCon VARCHAR(4),@pmTipoContrato VARCHAR(100)
,@pmDuracion INT,@pmVincLabUgpp VARCHAR(250),@pmCodDIAN VARCHAR(3),@pmInactivo BIT
 AS 
 INSERT INTO TiposCon (IdTipCon,TipoContrato,Duracion,Inactivo,VincLabUgpp,CodDIAN)
 VALUES (@pmIdTipCon,@pmTipoContrato,@pmDuracion,@pmInactivo,@pmVincLabUgpp,@pmCodDIAN)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTiposCon] @pmIdTipCon VARCHAR(4)
 AS 
 IF @pmIdTipCon IS NULL
  SELECT IdTipCon,TipoContrato,Duracion,VincLabUgpp,CodDIAN
  FROM TiposCon
  WHERE Inactivo=0
  ORDER BY IdTipCon
 ELSE
  SELECT IdTipCon,TipoContrato,Duracion,VincLabUgpp,CodDIAN,Inactivo
  FROM TiposCon
  WHERE IdTipCon=@pmIdTipCon
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposCon] @pmIdTipCon VARCHAR(4)
 AS 
 DELETE FROM TiposCon
 WHERE IdTipCon=@pmIdTipCon
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposCon] @pmIdTipCon VARCHAR(4),@pmTipoContrato VARCHAR(100)
,@pmDuracion INT,@pmVincLabUgpp VARCHAR(250),@pmCodDIAN VARCHAR(3),@pmInactivo BIT
 AS 
 UPDATE TiposCon SET TipoContrato=@pmTipoContrato,Duracion=@pmDuracion,Inactivo=@pmInactivo
,VincLabUgpp=@pmVincLabUgpp,CodDIAN=@pmCodDIAN
 WHERE IdTipCon=@pmIdTipCon
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTercCndTarjetasItm] @pmIdConductor VARCHAR(16)
 AS 
 SELECT MAX(Item) AS MXITM
 FROM TercCndTarjetas
 WHERE IdConductor=@pmIdConductor
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilMediciones] @pmTipDoc VARCHAR(3),@pmNumInvent INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumInvent,IdCia,Fecha,FecProceso,IdTurno,IdOperario,NomOperario,Modalidad,TipDocAju,AjuInvSob,AjuInvFal,IdCiaAin,Anulado,NumDev,FecDev,Observacion,IdEstado,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario
 FROM Trn_OilMediciones
 WHERE TipDoc=@pmTipDoc AND NumInvent=@pmNumInvent AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOilMediciones] @pmTipDoc VARCHAR(3),@pmNumInvent INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecProceso SMALLDATETIME,@pmIdTurno VARCHAR(4),@pmIdOperario VARCHAR(16),@pmNomOperario VARCHAR(150),@pmAjuInvSob INT,@pmAjuInvFal INT,@pmIdCiaAin CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmModalidad VARCHAR(10),@pmTipDocAju VARCHAR(3),@pmFechaCrea SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmOrigenAdd VARCHAR(10),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_OilMediciones (TipDoc,NumInvent,IdCia,Fecha,FecProceso,IdTurno,IdOperario,NomOperario,Modalidad,TipDocAju,AjuInvSob,AjuInvFal,IdCiaAin,Anulado,NumDev,FecDev,Observacion,IdEstado,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario)
 VALUES (@pmTipDoc,@pmNumInvent,@pmIdCia,@pmFecha,@pmFecProceso,@pmIdTurno,@pmIdOperario,@pmNomOperario,@pmModalidad,@pmTipDocAju,@pmAjuInvSob,@pmAjuInvFal,@pmIdCiaAin,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmFechaCrea,@pmIdCiaCrea,@pmOrigenAdd,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpOilMediciones] @pmTipDoc VARCHAR(3),@pmNumInvent INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecProceso SMALLDATETIME,@pmIdTurno VARCHAR(4),@pmIdOperario VARCHAR(16),@pmNomOperario VARCHAR(150),@pmAjuInvSob INT,@pmAjuInvFal INT,@pmIdCiaAin CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmModalidad VARCHAR(10),@pmTipDocAju VARCHAR(3)
 AS 
 UPDATE Trn_OilMediciones SET Fecha=@pmFecha,FecProceso=@pmFecProceso,IdTurno=@pmIdTurno,IdOperario=@pmIdOperario,NomOperario=@pmNomOperario,AjuInvSob=@pmAjuInvSob,AjuInvFal=@pmAjuInvFal,IdCiaAin=@pmIdCiaAin,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
,Modalidad=@pmModalidad,TipDocAju=@pmTipDocAju
 WHERE TipDoc=@pmTipDoc AND NumInvent=@pmNumInvent AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_LotID] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_Lot 
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncProdConsInsu] (@pmTipDoc VARCHAR(3),@pmNumCons INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16),@pmIdInsumo VARCHAR(16))
RETURNS BIT
 AS  
 BEGIN
  DECLARE @Insumos BIT
  IF @pmIdInsumo IS NULL
   SET @Insumos=1
  ELSE
   BEGIN 
    IF EXISTS (SELECT 1 FROM Trn_ProdConsInsu WHERE TipDoc=@pmTipDoc AND NumCons=@pmNumCons AND IdCia=@pmIdCia AND IdProducto=@pmIdProducto AND IdInsumo LIKE @pmIdInsumo)
     BEGIN
      SET @Insumos=1
     END
    ELSE
     BEGIN
      SET @Insumos=0
     END
   END
 RETURN @Insumos
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTercSedes_Sel] @pmIdTercero VARCHAR(16),@pmNewTercero VARCHAR(16)
 AS 
 INSERT INTO TercSedes (IdTercero,IdSede,NomSede,Principal,IdLocSede)
 SELECT @pmNewTercero,IdSede,NomSede,Principal,IdLocSede
 FROM  TercSedes 
 WHERE IdTercero=@pmIdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTercSedes_Uni] @pmIdTercero VARCHAR(16),@pmIdTerceroDos VARCHAR(16),@pmNewTercero VARCHAR(16)
AS 
 BEGIN
 DELETE FROM TercSedes WHERE IdTercero=@pmIdTerceroDos 
 AND EXISTS ( SELECT * FROM TercSedes AS TS1 WHERE TS1.IdTercero=@pmIdTercero AND TS1.IdSede=TercSedes.IdSede)
 --unificar
 IF EXISTS (SELECT * FROM TercSedes WHERE IdTercero=@pmIdTercero OR IdTercero=@pmIdTerceroDos)
  INSERT INTO TercSedes (IdTercero,IdSede,NomSede,Principal,IdLocSede)
  SELECT @pmNewTercero,IdSede,NomSede,Principal,IdLocSede
  FROM  TercSedes 
  WHERE IdTercero=@pmIdTercero OR IdTercero=@pmIdTerceroDos
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraRemesa] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 BEGIN
  DECLARE @TrnNombre VARCHAR(20)
  SET @TrnNombre = 'TrnRemesa'
   BEGIN TRANSACTION @TrnNombre
   DELETE FROM Trn_TraRemAnexo  WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
   DELETE FROM Trn_TraRemesa  WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
  COMMIT TRANSACTION  @TrnNombre
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraRemAnexo] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmIdVend VARCHAR(16),@pmIdTarifCom VARCHAR(4),@pmNomCliente VARCHAR(150),@pmNomRemite VARCHAR(150),@pmNomDestino VARCHAR(150),@pmLugarCargue VARCHAR(150),@pmLugarDescargue VARCHAR(150)
,@pmNomContacto VARCHAR(150),@pmTelContacto VARCHAR(30),@pmemlContacto VARCHAR(100),@pmContacDestino VARCHAR(150),@pmTelContacDest VARCHAR(30),@pmemlContacDest VARCHAR(100),@pmPolizaSeguro VARCHAR(50),@pmNitCiaPoliza VARCHAR(16),@pmNomCiaPoliza VARCHAR(150),@pmFecVencePol SMALLDATETIME
,@pmManifMintrans DECIMAL(14,2),@pmRemMintrans DECIMAL(14,2),@pmTipSal VARCHAR(3),@pmNumSalida INT,@pmIdCiaSal CHAR(2),@pmNumInvent INT,@pmIdCiaInv CHAR(2),@pmTomadorPoliza CHAR(1),@pmTipoRuta VARCHAR(10),@pmTipoMintrans VARCHAR(10),@pmNitEmpTransp VARCHAR(16),@pmNumFactEmp VARCHAR(20)
,@pmMvoAnulacion VARCHAR(3),@pmPactoCargue BIT,@pmHorasPactoCargue INT,@pmMinPactoCargue INT,@pmHoraCitaCargue SMALLDATETIME,@pmHoraLlegaCargue SMALLDATETIME,@pmHoraEntraCargue SMALLDATETIME,@pmHoraSaleCargue SMALLDATETIME
,@pmPactoDescargue BIT,@pmHorasPactoDescargue INT,@pmMinPactoDescargue INT,@pmHoraCitaDescargue SMALLDATETIME,@pmHoraLlegaDescargue SMALLDATETIME,@pmHoraEntraDescargue SMALLDATETIME,@pmHoraSaleDescargue SMALLDATETIME
,@pmTipoDevRemesa VARCHAR(3),@pmCodCiuTransb VARCHAR(8),@pmMvoTransbordo VARCHAR(3),@pmCantViajes INT,@pmTipOrdMud VARCHAR(3),@pmItemOsm INT
 AS 
 INSERT INTO Trn_TraRemAnexo (TipDoc,NumOrden,IdCia,IdVend,IdTarifCom,NomCliente,NomRemite,NomDestino,LugarCargue,LugarDescargue,NomContacto,TelContacto,emlContacto,ContacDestino,TelContacDest,emlContacDest,TomadorPoliza,PolizaSeguro,NitCiaPoliza,NomCiaPoliza,FecVencePol,ManifMintrans,RemMintrans
,TipSal,NumSalida,IdCiaSal,NumInvent,IdCiaInv,TipoRuta,TipoMintrans,NitEmpTransp,NumFactEmp,MvoAnulacion,PactoCargue,HorasPactoCargue,MinPactoCargue,HoraCitaCargue,HoraLlegaCargue,HoraEntraCargue,HoraSaleCargue,PactoDescargue,HorasPactoDescargue,MinPactoDescargue,HoraCitaDescargue
,HoraLlegaDescargue,HoraEntraDescargue,HoraSaleDescargue,TipoDevRemesa,CodCiuTransb,MvoTransbordo,CantViajes,TipOrdMud,ItemOsm)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmIdVend,@pmIdTarifCom,@pmNomCliente,@pmNomRemite,@pmNomDestino,@pmLugarCargue,@pmLugarDescargue,@pmNomContacto,@pmTelContacto,@pmemlContacto,@pmContacDestino,@pmTelContacDest,@pmemlContacDest,@pmTomadorPoliza,@pmPolizaSeguro,@pmNitCiaPoliza,@pmNomCiaPoliza
,@pmFecVencePol,@pmManifMintrans,@pmRemMintrans,@pmTipSal,@pmNumSalida,@pmIdCiaSal,@pmNumInvent,@pmIdCiaInv,@pmTipoRuta,@pmTipoMintrans,@pmNitEmpTransp,@pmNumFactEmp,@pmMvoAnulacion,@pmPactoCargue,@pmHorasPactoCargue,@pmMinPactoCargue,@pmHoraCitaCargue,@pmHoraLlegaCargue,@pmHoraEntraCargue
,@pmHoraSaleCargue,@pmPactoDescargue,@pmHorasPactoDescargue,@pmMinPactoDescargue,@pmHoraCitaDescargue,@pmHoraLlegaDescargue,@pmHoraEntraDescargue,@pmHoraSaleDescargue,@pmTipoDevRemesa,@pmCodCiuTransb,@pmMvoTransbordo,@pmCantViajes,@pmTipOrdMud,@pmItemOsm)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraRemAnexo] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmIdVend VARCHAR(16),@pmIdTarifCom VARCHAR(4),@pmNomCliente VARCHAR(150),@pmNomRemite VARCHAR(150),@pmNomDestino VARCHAR(150),@pmLugarCargue VARCHAR(150),@pmLugarDescargue VARCHAR(150),@pmNomContacto VARCHAR(150),@pmTelContacto VARCHAR(30)
,@pmemlContacto VARCHAR(100),@pmContacDestino VARCHAR(150),@pmTelContacDest VARCHAR(30),@pmemlContacDest VARCHAR(100),@pmPolizaSeguro VARCHAR(50),@pmNitCiaPoliza VARCHAR(16),@pmNomCiaPoliza VARCHAR(150),@pmFecVencePol SMALLDATETIME,@pmManifMintrans DECIMAL(14,2),@pmRemMintrans DECIMAL(14,2),@pmTipSal VARCHAR(3),@pmNumSalida INT,@pmIdCiaSal CHAR(2)
,@pmNumInvent INT,@pmIdCiaInv CHAR(2),@pmTomadorPoliza CHAR(1),@pmTipoRuta VARCHAR(10),@pmTipoMintrans VARCHAR(10),@pmNitEmpTransp VARCHAR(16),@pmNumFactEmp VARCHAR(20)
,@pmMvoAnulacion VARCHAR(3),@pmPactoCargue BIT,@pmHorasPactoCargue INT,@pmMinPactoCargue INT,@pmHoraCitaCargue SMALLDATETIME,@pmHoraLlegaCargue SMALLDATETIME,@pmHoraEntraCargue SMALLDATETIME,@pmHoraSaleCargue SMALLDATETIME
,@pmPactoDescargue BIT,@pmHorasPactoDescargue INT,@pmMinPactoDescargue INT,@pmHoraCitaDescargue SMALLDATETIME,@pmHoraLlegaDescargue SMALLDATETIME,@pmHoraEntraDescargue SMALLDATETIME,@pmHoraSaleDescargue SMALLDATETIME,@pmTipoDevRemesa VARCHAR(3),@pmCodCiuTransb VARCHAR(8),@pmMvoTransbordo VARCHAR(3),@pmCantViajes INT,@pmTipOrdMud VARCHAR(3),@pmItemOsm INT
 AS 
 UPDATE Trn_TraRemAnexo SET IdVend=@pmIdVend,IdTarifCom=@pmIdTarifCom,NomCliente=@pmNomCliente,NomRemite=@pmNomRemite,NomDestino=@pmNomDestino,LugarCargue=@pmLugarCargue,LugarDescargue=@pmLugarDescargue,NomContacto=@pmNomContacto,TelContacto=@pmTelContacto,emlContacto=@pmemlContacto,ContacDestino=@pmContacDestino,TelContacDest=@pmTelContacDest
,emlContacDest=@pmemlContacDest,PolizaSeguro=@pmPolizaSeguro,NitCiaPoliza=@pmNitCiaPoliza,NomCiaPoliza=@pmNomCiaPoliza,FecVencePol=@pmFecVencePol,ManifMintrans=@pmManifMintrans,RemMintrans=@pmRemMintrans,TipSal=@pmTipSal,NumSalida=@pmNumSalida,IdCiaSal=@pmIdCiaSal,NumInvent=@pmNumInvent,IdCiaInv=@pmIdCiaInv,TomadorPoliza=@pmTomadorPoliza
,TipoRuta=@pmTipoRuta,TipoMintrans=@pmTipoMintrans,NitEmpTransp=@pmNitEmpTransp,NumFactEmp=@pmNumFactEmp,MvoAnulacion=@pmMvoAnulacion,PactoCargue=@pmPactoCargue,HorasPactoCargue=@pmHorasPactoCargue,MinPactoCargue=@pmMinPactoCargue
,HoraCitaCargue=@pmHoraCitaCargue,HoraLlegaCargue=@pmHoraLlegaCargue,HoraEntraCargue=@pmHoraEntraCargue,HoraSaleCargue=@pmHoraSaleCargue,PactoDescargue=@pmPactoDescargue,HorasPactoDescargue=@pmHorasPactoDescargue,MinPactoDescargue=@pmMinPactoDescargue
,HoraCitaDescargue=@pmHoraCitaDescargue,HoraLlegaDescargue=@pmHoraLlegaDescargue,HoraEntraDescargue=@pmHoraEntraDescargue,HoraSaleDescargue=@pmHoraSaleDescargue,TipoDevRemesa=@pmTipoDevRemesa,CodCiuTransb=@pmCodCiuTransb,MvoTransbordo=@pmMvoTransbordo,CantViajes=@pmCantViajes,TipOrdMud=@pmTipOrdMud,ItemOsm=@pmItemOsm
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraRemAnexo] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraRemAnexo
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraRemAnexo] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumOrden,IdCia,IdVend,IdTarifCom,NomCliente,NomRemite,NomDestino,LugarCargue,LugarDescargue,NomContacto,TelContacto,emlContacto,ContacDestino
,TelContacDest,emlContacDest,TomadorPoliza,PolizaSeguro,NitCiaPoliza,NomCiaPoliza,FecVencePol,ManifMintrans,RemMintrans,TipSal,NumSalida,IdCiaSal,NumInvent,IdCiaInv
,TipoRuta,TipoMintrans,NitEmpTransp,NumFactEmp,MvoAnulacion,PactoCargue,HorasPactoCargue,MinPactoCargue,HoraCitaCargue,HoraLlegaCargue,HoraEntraCargue,HoraSaleCargue
,PactoDescargue,HorasPactoDescargue,MinPactoDescargue,HoraCitaDescargue,HoraLlegaDescargue,HoraEntraDescargue,HoraSaleDescargue,TipoDevRemesa,CodCiuTransb,MvoTransbordo,CantViajes,TipOrdMud,ItemOsm
 FROM Trn_TraRemAnexo
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasAux_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum VARCHAR(4),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuFisCuentasAux (nAnno,nMes,IdCia,nReg,IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,SubAuxiliar,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos,TipoAfiVehic)
 SELECT @pmnAnno,@pmnMes,S.IdCia,0,S.IdCuenta,S.IdTercero,S.CodAgncia,S.IdCCosto,S.IdSubCos,S.SubAuxiliar,S.TipoAcum,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0,S.TipoAfiVehic
 FROM AcuFisCuentasAux AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt AND S.TipoAcum=@pmTipoAcum
 AND NOT EXISTS (SELECT * FROM AcuFisCuentasAux AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes 
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdTercero=S.IdTercero AND AC.CodAgncia=S.CodAgncia AND AC.IdCCosto=S.IdCCosto AND AC.IdSubCos=S.IdSubCos
 AND AC.SubAuxiliar=S.SubAuxiliar AND AC.TipoAcum=S.TipoAcum AND AC.TipoAfiVehic=S.TipoAfiVehic)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,VrReteCREE,TarifaRtc,CodTarRtc,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrBomberil,VrIvaIngProd,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,CodTarRetFle,CodTarIcaFle,FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos,IdPlazo,CxPagar,DocEquiv
,BaseImp,BaseRet,BaseIca,BaseRiv,BaseIvaIgp,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,TarifaBom,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,CodTarifBom,BaseIvaObsq,VrIvaObsequio,VrImpCarbono,ImpAviTab,TarifaAvta,CodTarifAvta
,CdCCosto,CdSubCos,TipEgr,Egreso,IdCiaEgr,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,VrImpuCons,VrSobtasaCons,Modalidad,TipoSA,NumSalAlm,CdCiaSA,TipSop,NumDocSop,CdCiaSop,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,ImgFoto,ImgFirma,ImagenDoc1,ImagenDoc2,ImagenDoc3,FechaNac,IdEstOper,IdLocUbic,DescUbicac,CdGrupoTerc,RegimenFE
,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM Terceros
 WHERE Codigo  LIKE ISNULL(@pmCodigo,'%') AND Telefono LIKE ISNULL(@pmTelefono,'%')
 ORDER BY RazonSocial
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmImgFoto VARCHAR(30),@pmImgFirma VARCHAR(30)
,@pmImagenDoc1 VARCHAR(50),@pmImagenDoc2 VARCHAR(50),@pmImagenDoc3 VARCHAR(50),@pmFechaNac SMALLDATETIME,@pmIdEstOper VARCHAR(4),@pmIdLocUbic VARCHAR(8),@pmDescUbicac VARCHAR(150),@pmCdGrupoTerc VARCHAR(4),@pmRegimenFE 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,ImgFoto=@pmImgFoto,ImgFirma=@pmImgFirma,ImagenDoc1=@pmImagenDoc1,ImagenDoc2=@pmImagenDoc2,ImagenDoc3=@pmImagenDoc3,FechaNac=@pmFechaNac
,IdEstOper=@pmIdEstOper,IdLocUbic=@pmIdLocUbic,DescUbicac=@pmDescUbicac,CdGrupoTerc=@pmCdGrupoTerc,RegimenFE=@pmRegimenFE
 WHERE IdTercero=@pmIdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,ImgFoto,ImgFirma,ImagenDoc1,ImagenDoc2,ImagenDoc3,FechaNac,IdEstOper,IdLocUbic,DescUbicac,CdGrupoTerc,RegimenFE,FechaAdd,FechaUpdate,IdUsuario
 FROM Terceros
 WHERE IdTercero=@pmIdTercero OR Codigo=@pmCodigo
 ORDER BY  IdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,ImgFoto,ImgFirma,ImagenDoc1,ImagenDoc2,ImagenDoc3,FechaNac,IdEstOper,IdLocUbic,DescUbicac,CdGrupoTerc,RegimenFE)
 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,ImgFoto,ImgFirma,ImagenDoc1,ImagenDoc2,ImagenDoc3,FechaNac,IdEstOper,IdLocUbic,DescUbicac,CdGrupoTerc,RegimenFE
 FROM  Terceros 
 WHERE IdTercero=@pmIdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,ImgFoto,ImgFirma,ImagenDoc1,ImagenDoc2,ImagenDoc3,FechaNac,IdEstOper,IdLocUbic,DescUbicac,CdGrupoTerc,RegimenFE,FechaAdd,FechaUpdate,IdUsuario
 FROM Terceros
 WHERE IdTercero=@pmIdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,TdOrden,NumOrden,CdCiaOrden,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NivelAprob
,NomContacto,TelsContacto,EmailContacto,Num_Vehic,Num_Trailer,TipoVigencia,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 TipDoc='REQ' AND 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CodTarRiv,VrReteIVA,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,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,NivelAprob
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,IndFactura
,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,IndFactura,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrReteCREE,TarifaRtc,CodTarRtc,VrDevFact,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmImgFoto VARCHAR(30),@pmImgFirma VARCHAR(30)
,@pmImagenDoc1 VARCHAR(50),@pmImagenDoc2 VARCHAR(50),@pmImagenDoc3 VARCHAR(50),@pmFechaNac SMALLDATETIME,@pmIdEstOper VARCHAR(4),@pmIdLocUbic VARCHAR(8),@pmDescUbicac VARCHAR(150),@pmCdGrupoTerc VARCHAR(4),@pmRegimenFE 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,ImgFoto,ImgFirma,ImagenDoc1,ImagenDoc2,ImagenDoc3,FechaNac,IdEstOper,IdLocUbic,DescUbicac,CdGrupoTerc,RegimenFE)
 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,@pmImgFoto,@pmImgFirma,@pmImagenDoc1,@pmImagenDoc2,@pmImagenDoc3,@pmFechaNac,@pmIdEstOper,@pmIdLocUbic,@pmDescUbicac,@pmCdGrupoTerc,@pmRegimenFE)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrImpCarbono,VrIvaIngProd,VrNeto,Cantidad,BaseIvaIgp,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,NumCotizac,CdCiaCotizac,NumAutSicom,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc,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,BaseIvaObsq,VrIvaObsequio
,TarifaArf,VrAutRetFte,TarifaAri,VrAutRetIca,CdMotDev,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsFinPrestGarant] @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipoGarant VARCHAR(20),@pmDescGarant VARCHAR(250),@pmCdInmueble VARCHAR(4),@pmCdTipoVeh VARCHAR(4),@pmVrGarantia MONEY,@pmVrAvaluo MONEY,@pmPesoBruto DECIMAL(16,6),@pmPesoNeto DECIMAL(16,6),@pmPropietario VARCHAR(250),@pmDirInmueble VARCHAR(250),@pmCdCiudad VARCHAR(8),@pmMatricula VARCHAR(50),@pmEscritura VARCHAR(50),@pmNumNotaria VARCHAR(20),@pmMarcaVeh VARCHAR(20),@pmModelo VARCHAR(4)
,@pmCdBanco VARCHAR(4),@pmFecCheque SMALLDATETIME,@pmNumCheque VARCHAR(20),@pmOtroTipoVehic VARCHAR(50),@pmOtroInmueble VARCHAR(50),@pmCedFmnte1 VARCHAR(16),@pmNomFmnte1 VARCHAR(150),@pmCedFmnte2 VARCHAR(16),@pmNomFmnte2 VARCHAR(150),@pmCedFmnte3 VARCHAR(16),@pmNomFmnte3 VARCHAR(150),@pmCedFmnte4 VARCHAR(16),@pmNomFmnte4 VARCHAR(150),@pmCedFmnte5 VARCHAR(16),@pmNomFmnte5 VARCHAR(150),@pmCedFmnte6 VARCHAR(16),@pmNomFmnte6 VARCHAR(150),@pmArchivoImg VARCHAR(500),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinPrestGarant (TipDoc,IdPrestamo,IdCia,Item,TipoGarant,DescGarant,CdInmueble,CdTipoVeh,VrGarantia,VrAvaluo,PesoBruto,PesoNeto,IdUsuario,Propietario,DirInmueble,CdCiudad,Matricula,Escritura,NumNotaria,MarcaVeh,Modelo,CdBanco,FecCheque,NumCheque,OtroTipoVehic,OtroInmueble,CedFmnte1,NomFmnte1,CedFmnte2,NomFmnte2,CedFmnte3,NomFmnte3,CedFmnte4,NomFmnte4,CedFmnte5,NomFmnte5,CedFmnte6,NomFmnte6,ArchivoImg)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmItem,@pmTipoGarant,@pmDescGarant,@pmCdInmueble,@pmCdTipoVeh,@pmVrGarantia,@pmVrAvaluo,@pmPesoBruto,@pmPesoNeto,@pmIdUsuario,@pmPropietario,@pmDirInmueble,@pmCdCiudad,@pmMatricula,@pmEscritura,@pmNumNotaria,@pmMarcaVeh,@pmModelo,@pmCdBanco,@pmFecCheque,@pmNumCheque,@pmOtroTipoVehic,@pmOtroInmueble,@pmCedFmnte1,@pmNomFmnte1,@pmCedFmnte2,@pmNomFmnte2,@pmCedFmnte3
,@pmNomFmnte3,@pmCedFmnte4,@pmNomFmnte4,@pmCedFmnte5,@pmNomFmnte5,@pmCedFmnte6,@pmNomFmnte6,@pmArchivoImg)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinPrestGarant] @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,IdPrestamo,IdCia,Item,TipoGarant,DescGarant,CdInmueble,CdTipoVeh,VrGarantia,VrAvaluo,PesoBruto,PesoNeto,IdUsuario,Propietario,DirInmueble,CdCiudad,Matricula,Escritura,NumNotaria,MarcaVeh,Modelo,CdBanco,FecCheque,NumCheque,OtroTipoVehic,OtroInmueble
,CedFmnte1,NomFmnte1,CedFmnte2,NomFmnte2,CedFmnte3,NomFmnte3,CedFmnte4,NomFmnte4,CedFmnte5,NomFmnte5,CedFmnte6,NomFmnte6,ArchivoImg
 FROM Trn_FinPrestGarant
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpFinPrestGarant] @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipoGarant VARCHAR(20),@pmDescGarant VARCHAR(250),@pmCdInmueble VARCHAR(4),@pmCdTipoVeh VARCHAR(4),@pmVrGarantia MONEY,@pmVrAvaluo MONEY,@pmPesoBruto DECIMAL(16,6),@pmPesoNeto DECIMAL(16,6),@pmPropietario VARCHAR(250),@pmDirInmueble VARCHAR(250),@pmCdCiudad VARCHAR(8),@pmMatricula VARCHAR(50),@pmEscritura VARCHAR(50),@pmNumNotaria VARCHAR(20),@pmMarcaVeh VARCHAR(20),@pmModelo VARCHAR(4)
,@pmCdBanco VARCHAR(4),@pmFecCheque SMALLDATETIME,@pmNumCheque VARCHAR(20),@pmOtroTipoVehic VARCHAR(50),@pmOtroInmueble VARCHAR(50),@pmCedFmnte1 VARCHAR(16),@pmNomFmnte1 VARCHAR(150),@pmCedFmnte2 VARCHAR(16),@pmNomFmnte2 VARCHAR(150),@pmCedFmnte3 VARCHAR(16),@pmNomFmnte3 VARCHAR(150),@pmCedFmnte4 VARCHAR(16),@pmNomFmnte4 VARCHAR(150),@pmCedFmnte5 VARCHAR(16),@pmNomFmnte5 VARCHAR(150),@pmCedFmnte6 VARCHAR(16),@pmNomFmnte6 VARCHAR(150),@pmArchivoImg VARCHAR(500)
 AS 
 UPDATE Trn_FinPrestGarant SET TipoGarant=@pmTipoGarant,DescGarant=@pmDescGarant,CdInmueble=@pmCdInmueble,CdTipoVeh=@pmCdTipoVeh,VrGarantia=@pmVrGarantia,VrAvaluo=@pmVrAvaluo,PesoBruto=@pmPesoBruto,PesoNeto=@pmPesoNeto,Propietario=@pmPropietario,DirInmueble=@pmDirInmueble,CdCiudad=@pmCdCiudad,Matricula=@pmMatricula,Escritura=@pmEscritura,NumNotaria=@pmNumNotaria,MarcaVeh=@pmMarcaVeh,Modelo=@pmModelo,CdBanco=@pmCdBanco,FecCheque=@pmFecCheque,NumCheque=@pmNumCheque,OtroTipoVehic=@pmOtroTipoVehic
,OtroInmueble=@pmOtroInmueble,CedFmnte1=@pmCedFmnte1,NomFmnte1=@pmNomFmnte1,CedFmnte2=@pmCedFmnte2,NomFmnte2=@pmNomFmnte2,CedFmnte3=@pmCedFmnte3,NomFmnte3=@pmNomFmnte3,CedFmnte4=@pmCedFmnte4,NomFmnte4=@pmNomFmnte4,CedFmnte5=@pmCedFmnte5,NomFmnte5=@pmNomFmnte5,CedFmnte6=@pmCedFmnte6,NomFmnte6=@pmNomFmnte6,ArchivoImg=@pmArchivoImg
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_AfDepPartes] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdActivo VARCHAR(30),@pmtmIdParte INT,@pmtmCostoIni MONEY,@pmtmMejoras MONEY,@pmtmDisminucion MONEY,@pmtmAcuAjustes MONEY,@pmtmDepreciaHis MONEY,@pmtmDepreciaMes MONEY
,@pmtmAjustesInf MONEY,@pmtmAjustesDep MONEY,@pmtmDepreciaAcum MONEY,@pmtmAjuDepAcum MONEY,@pmtmTarifa DECIMAL(14,4),@pmtmVidaUtil INT,@pmtmMetodo VARCHAR(10),@pmtmFechaIni SMALLDATETIME,@pmtmEstadoReg INT,@pmtmObservacion VARCHAR(150)
,@pmtmSalvamento MONEY,@pmtmUnidades INT,@pmtmCantidad INT,@pmtmDepMejoras MONEY
 AS 
 INSERT INTO tm_AfDepPartes (tmNumero,tmItem,tmIdActivo,tmIdParte,tmCostoIni,tmMejoras,tmDisminucion,tmAcuAjustes,tmDepreciaHis,tmDepreciaMes,tmAjustesInf,tmAjustesDep,tmDepreciaAcum,tmAjuDepAcum,tmTarifa,tmVidaUtil,tmMetodo,tmFechaIni,tmEstadoReg,tmObservacion,tmSalvamento,tmUnidades,tmCantidad,tmDepMejoras)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdActivo,@pmtmIdParte,@pmtmCostoIni,@pmtmMejoras,@pmtmDisminucion,@pmtmAcuAjustes,@pmtmDepreciaHis,@pmtmDepreciaMes,@pmtmAjustesInf,@pmtmAjustesDep,@pmtmDepreciaAcum,@pmtmAjuDepAcum,@pmtmTarifa,@pmtmVidaUtil
,@pmtmMetodo,@pmtmFechaIni,@pmtmEstadoReg,@pmtmObservacion,@pmtmSalvamento,@pmtmUnidades,@pmtmCantidad,@pmtmDepMejoras)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCodEmpresa VARCHAR(20),@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,CodEmpresa=@pmCodEmpresa,FechaUpdate=@pmFechaUpdate
 WHERE NumPoliza=@pmNumPoliza
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCodEmpresa VARCHAR(20),@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Polizas (NumPoliza,Fecha,Vigencia,NitCompania,Compania,NitTomador,Tomador,NitAseg,Asegurado,NitBenef,Beneficiario,DescFirma,Inactivo,FechaAdd,IdUsuario,CodEmpresa)
 VALUES (@pmNumPoliza,@pmFecha,@pmVigencia,@pmNitCompania,@pmCompania,@pmNitTomador,@pmTomador,@pmNitAseg,@pmAsegurado
,@pmNitBenef,@pmBeneficiario,@pmDescFirma,@pmInactivo,@pmFechaAdd,@pmIdUsuario,@pmCodEmpresa)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryPolizas] @pmNumPoliza VARCHAR(30)
 AS 
 SELECT NumPoliza,Fecha,Vigencia,NitCompania,Compania,NitTomador,Tomador,NitAseg,Asegurado
,NitBenef,Beneficiario,Inactivo,CodEmpresa,FechaAdd,FechaUpdate,IdUsuario,DescFirma
 FROM Polizas
 WHERE NumPoliza=@pmNumPoliza
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryPolizasLta]
 AS 
 SELECT NumPoliza,Fecha,Vigencia,NitCompania,Compania,NitTomador,Tomador,NitAseg,Asegurado
,NitBenef,Beneficiario,DescFirma,CodEmpresa,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpConceptosTrib] @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(250),@pmCodigoConc VARCHAR(10)
,@pmIdClase VARCHAR(4),@pmCdTarifa VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE ConceptosTrib SET Concepto=@pmConcepto,CodigoConc=@pmCodigoConc,IdClase=@pmIdClase,CdTarifa=@pmCdTarifa,Inactivo=@pmInactivo
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelConceptosTrib] @pmIdConcepto VARCHAR(4)
 AS 
 DELETE FROM ConceptosTrib WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsConceptosTrib] @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(250),@pmCodigoConc VARCHAR(10)
,@pmIdClase VARCHAR(4),@pmCdTarifa VARCHAR(4),@pmInactivo BIT
 AS 
 INSERT INTO ConceptosTrib (IdConcepto,Concepto,CodigoConc,IdClase,CdTarifa,Inactivo)
 VALUES (@pmIdConcepto,@pmConcepto,@pmCodigoConc,@pmIdClase,@pmCdTarifa,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryConceptosTribLta] @pmIdClase VARCHAR(4)=Null
 AS 
 SELECT IdConcepto,Concepto,CodigoConc,C.IdClase AS CdClase,NomClase,CdTarifa,Tarifa 
 FROM ConceptosTrib AS C INNER JOIN ClaseTar AS CL ON C.IdClase=CL.IdClase
 LEFT JOIN Tablapor AS T ON C.CdTarifa=T.IdTarifa
 WHERE C.IdClase LIKE ISNULL(@pmIdClase,'%') AND C.Inactivo=0
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryConceptosTribDso] @pmIdClase VARCHAR(4)
 AS 
 SELECT IdConcepto,Concepto,Concepto+' '+CodigoConc AS DsCon
 FROM ConceptosTrib
 WHERE IdClase LIKE ISNULL(@pmIdClase,'%') AND Inactivo=0
 ORDER BY Concepto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryConceptosTrib] @pmIdConcepto VARCHAR(4)
 AS 
 SELECT IdConcepto,Concepto,CodigoConc,IdClase,CdTarifa,Inactivo
 FROM ConceptosTrib WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryReportGru]
AS
 SELECT TipoRep,NomRep,count(IdRep) AS Cuenta
 FROM   Sys_Report
 GROUP BY  TipoRep,NomRep
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelReport] @pmTipoRep VARCHAR(3),@pmIdRep CHAR(2)=Null
 AS 
 DELETE FROM Sys_Report
 WHERE TipoRep=@pmTipoRep AND IdRep LIKE ISNULL(@pmIdRep,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAfAmtComp] @pmNumero INT,@pmIdCia CHAR(2),@pmTipCom VARCHAR(3),@pmComprobante INT
,@pmTipoCompNiif VARCHAR(3),@pmNumCompNiif INT,@pmTipoCompFis VARCHAR(3),@pmNumCompFis INT
 AS 
 INSERT INTO Trn_AfAmtComp (Numero,IdCia,TipCom,Comprobante,TipoCompNiif,NumCompNiif,TipoCompFis,NumCompFis)
 VALUES (@pmNumero,@pmIdCia,@pmTipCom,@pmComprobante,@pmTipoCompNiif,@pmNumCompNiif,@pmTipoCompFis,@pmNumCompFis)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelAfAmtComp] @pmNumero INT
 AS 
 DELETE FROM Trn_AfAmtComp
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposAdm] @pmIdAdmon VARCHAR(4),@pmTipoAdmon VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposAdm SET TipoAdmon=@pmTipoAdmon,Inactivo=@pmInactivo
 WHERE IdAdmon=@pmIdAdmon
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposAdm] @pmIdAdmon VARCHAR(4),@pmTipoAdmon VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposAdm (IdAdmon,TipoAdmon,Inactivo)
 VALUES (@pmIdAdmon,@pmTipoAdmon,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposAdm] @pmIdAdmon VARCHAR(4)
 AS 
 DELETE FROM TiposAdm
 WHERE IdAdmon=@pmIdAdmon
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelEstadoVeh] @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM EstadoVeh
 WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrySecurity] @pmIdLla INT
 AS 
 SELECT IdLla,Descripcion,Pwd
 FROM Sys_Security
 WHERE IdLla=ISNULL(@pmIdLla,IdLla)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsSecurity] @pmIdLla INT,@pmDescripcion VARCHAR(50),@pmPwd VARCHAR(10)
 AS 
 INSERT INTO Sys_Security (IdLla,Descripcion,Pwd)
 VALUES (@pmIdLla,@pmDescripcion,@pmPwd)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpSecurity] @pmIdLla INT,@pmDescripcion VARCHAR(50),@pmPwd VARCHAR(10)
 AS 
 UPDATE Sys_Security SET Descripcion=@pmDescripcion,Pwd=@pmPwd
 WHERE IdLla=@pmIdLla
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelSecurity] @pmIdLla INT
 AS 
 DELETE FROM Sys_Security
 WHERE IdLla=@pmIdLla
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrReteCREE,TarifaRtc,CodTarRtc,VrDevFact
,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Recibos
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmVrDevFact MONEY,@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,VrReteCREE,TarifaRtc,CodTarRtc,VrDevFact)
 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,@pmVrReteCREE,@pmTarifaRtc,@pmCodTarRtc,@pmVrDevFact)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmVrDevFact MONEY,@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,VrReteCREE=@pmVrReteCREE,TarifaRtc=@pmTarifaRtc,CodTarRtc=@pmCodTarRtc,VrDevFact=@pmVrDevFact,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdOrden] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumOrden,IdCia,Fecha,TipCons,NumCons,IdCiaCons,Modalidad,IdTipProc,IdTaller,CostoTotal,ConPago,EdoOrden,Num_Entrega,CdCiaEnt,FecEntrega
,NitTercero,NomContacto,TelContacto,ProcFinal,Anulado,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_ProdOrden
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpProdOrden] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmTipCons VARCHAR(3),@pmNumCons INT,@pmIdCiaCons CHAR(2),@pmModalidad VARCHAR(10),@pmIdTipProc VARCHAR(4),@pmIdTaller VARCHAR(4),@pmCostoTotal MONEY,@pmConPago BIT,@pmEdoOrden INT
,@pmFecEntrega SMALLDATETIME,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmNitTercero VARCHAR(16),@pmNomContacto VARCHAR(150),@pmTelContacto VARCHAR(30),@pmProcFinal BIT,@pmNum_Entrega INT,@pmCdCiaEnt CHAR(2),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_ProdOrden SET Fecha=@pmFecha,TipCons=@pmTipCons,NumCons=@pmNumCons,IdCiaCons=@pmIdCiaCons,Modalidad=@pmModalidad,IdTipProc=@pmIdTipProc,IdTaller=@pmIdTaller,CostoTotal=@pmCostoTotal,ConPago=@pmConPago,EdoOrden=@pmEdoOrden,FecEntrega=@pmFecEntrega
,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,NitTercero=@pmNitTercero,NomContacto=@pmNomContacto,TelContacto=@pmTelContacto,ProcFinal=@pmProcFinal,Num_Entrega=@pmNum_Entrega,CdCiaEnt=@pmCdCiaEnt,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsProdOrden] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmTipCons VARCHAR(3),@pmNumCons INT,@pmIdCiaCons CHAR(2),@pmModalidad VARCHAR(10),@pmIdTipProc VARCHAR(4),@pmIdTaller VARCHAR(4),@pmCostoTotal MONEY,@pmConPago BIT,@pmEdoOrden INT
,@pmFecEntrega SMALLDATETIME,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmNitTercero VARCHAR(16),@pmNomContacto VARCHAR(150),@pmTelContacto VARCHAR(30),@pmProcFinal BIT,@pmNum_Entrega INT,@pmCdCiaEnt CHAR(2)
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_ProdOrden (TipDoc,NumOrden,IdCia,Fecha,TipCons,NumCons,IdCiaCons,Modalidad,IdTipProc,IdTaller,CostoTotal,ConPago,EdoOrden,FecEntrega,NitTercero,NomContacto,TelContacto,ProcFinal,Anulado,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario,Num_Entrega,CdCiaEnt)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmFecha,@pmTipCons,@pmNumCons,@pmIdCiaCons,@pmModalidad,@pmIdTipProc,@pmIdTaller,@pmCostoTotal,@pmConPago,@pmEdoOrden,@pmFecEntrega,@pmNitTercero,@pmNomContacto,@pmTelContacto,@pmProcFinal,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmNum_Entrega,@pmCdCiaEnt)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[paDelPreCuotasCau] @pmCausacionID INT
 AS
 DELETE FROM  Trn_PreCuotas 
 WHERE CausacionID=@pmCausacionID
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraAnt] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipAnt VARCHAR(3),@pmtmAnticipo INT,@pmtmIdCia CHAR(2)
,@pmtmValor MONEY,@pmtmVrAnticipo MONEY,@pmtmTotalAbonos MONEY,@pmtmDetalle VARCHAR(100),@pmtmCdCueAnt VARCHAR(16)
 AS 
 INSERT INTO tm_TraAnt (tmNumero,tmItem,tmTipAnt,tmAnticipo,tmIdCia,tmValor,tmVrAnticipo,tmTotalAbonos,tmDetalle,tmCdCueAnt)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipAnt,@pmtmAnticipo,@pmtmIdCia,@pmtmValor,@pmtmVrAnticipo,@pmtmTotalAbonos,@pmtmDetalle,@pmtmCdCueAnt)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTarifasPaq] @pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmTarifaUnd DECIMAL(14,4),@pmTiempoEnt DECIMAL(14,4)
,@pmTarifaMin DECIMAL(14,4),@pmFormaPago VARCHAR(20),@pmInactivo BIT
 AS 
 INSERT INTO TarifasPaq (IdOrigen,IdDestino,TarifaUnd,TiempoEnt,TarifaMin,FormaPago,Inactivo)
 VALUES (@pmIdOrigen,@pmIdDestino,@pmTarifaUnd,@pmTiempoEnt,@pmTarifaMin,@pmFormaPago,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTarifasPaq] @pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmTarifaUnd DECIMAL(14,4),@pmTiempoEnt DECIMAL(14,4)
,@pmTarifaMin DECIMAL(14,4),@pmFormaPago VARCHAR(20),@pmInactivo BIT
 AS 
 UPDATE TarifasPaq SET TarifaUnd=@pmTarifaUnd,TiempoEnt=@pmTiempoEnt,TarifaMin=@pmTarifaMin,FormaPago=@pmFormaPago,Inactivo=@pmInactivo
 WHERE IdOrigen=@pmIdOrigen AND IdDestino=@pmIdDestino
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraManifInt] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecEmision SMALLDATETIME,@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmIdVehiculo VARCHAR(10),@pmnRemolque VARCHAR(10),@pmIdConductor VARCHAR(16),@pmIdCondRelev VARCHAR(16),@pmNatCarga VARCHAR(50),@pmCdAduana VARCHAR(4),@pmCdAduanaDest VARCHAR(4)
,@pmIdMoneda VARCHAR(5),@pmValorTotal MONEY,@pmPesoTotal DECIMAL(14,4),@pmNumMuc INT,@pmCdCiaMuc CHAR(2),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2500),@pmIdEstado VARCHAR(4),@pmNegociacion VARCHAR(1000),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraManifInt (TipDoc,Manifiesto,IdCia,Fecha,FecEmision,IdOrigen,IdDestino,IdVehiculo,nRemolque,IdConductor,IdCondRelev,NatCarga,CdAduana,CdAduanaDest,IdMoneda,ValorTotal,PesoTotal,NumMuc,CdCiaMuc,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,Negociacion)
 VALUES (@pmTipDoc,@pmManifiesto,@pmIdCia,@pmFecha,@pmFecEmision,@pmIdOrigen,@pmIdDestino,@pmIdVehiculo,@pmnRemolque,@pmIdConductor,@pmIdCondRelev,@pmNatCarga,@pmCdAduana,@pmCdAduanaDest,@pmIdMoneda,@pmValorTotal,@pmPesoTotal,@pmNumMuc,@pmCdCiaMuc,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmNegociacion)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraManifInt] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Manifiesto,IdCia,Fecha,FecEmision,IdOrigen,IdDestino,IdVehiculo,nRemolque,IdConductor,IdCondRelev,NatCarga,CdAduana,CdAduanaDest,IdMoneda,ValorTotal,PesoTotal,NumMuc,CdCiaMuc,OrigenAdd,Anulado,FecDev,Observacion
,Negociacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraManifInt
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraManifInt] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecEmision SMALLDATETIME,@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmIdVehiculo VARCHAR(10),@pmnRemolque VARCHAR(10),@pmIdConductor VARCHAR(16),@pmIdCondRelev VARCHAR(16),@pmNatCarga VARCHAR(50),@pmCdAduana VARCHAR(4),@pmCdAduanaDest VARCHAR(4)
,@pmIdMoneda VARCHAR(5),@pmValorTotal MONEY,@pmPesoTotal DECIMAL(14,4),@pmNumMuc INT,@pmCdCiaMuc CHAR(2),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2500),@pmIdEstado VARCHAR(4),@pmNegociacion VARCHAR(1000),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraManifInt SET Fecha=@pmFecha,FecEmision=@pmFecEmision,IdOrigen=@pmIdOrigen,IdDestino=@pmIdDestino,IdVehiculo=@pmIdVehiculo,nRemolque=@pmnRemolque,IdConductor=@pmIdConductor,IdCondRelev=@pmIdCondRelev,NatCarga=@pmNatCarga,CdAduana=@pmCdAduana,CdAduanaDest=@pmCdAduanaDest,IdMoneda=@pmIdMoneda,ValorTotal=@pmValorTotal,PesoTotal=@pmPesoTotal
,NumMuc=@pmNumMuc,CdCiaMuc=@pmCdCiaMuc,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,Negociacion=@pmNegociacion
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraManifInt] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraManifInt
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelClieContado] @pmIdCliente VARCHAR(16)
 AS 
 DELETE FROM ClieContado
 WHERE IdCliente=@pmIdCliente
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuNiifCuentasCcSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuNiifCuentasCc SET AcuNiifCuentasCc.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuNiifCuentasCc INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuNiifCuentasCc WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt AND TipoAcum=@pmTipoAcum) AS SA ON AcuNiifCuentasCc.IdCia=SA.IdCia AND AcuNiifCuentasCc.IdCuenta=SA.IdCuenta
 AND AcuNiifCuentasCc.IdCCosto=SA.IdCCosto AND AcuNiifCuentasCc.IdSubCos=SA.IdSubCos AND AcuNiifCuentasCc.TipoAcum=SA.TipoAcum
 WHERE AcuNiifCuentasCc.nAnno=@pmnAnno AND AcuNiifCuentasCc.nMes=@pmnMes AND AcuNiifCuentasCc.TipoAcum=@pmTipoAcum
 AND (@pmIdCia IS NULL OR AcuNiifCuentasCc.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuNiifCuentasCc.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasCc_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuNiifCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdCCosto,S.IdSubCos,S.TipoAcum,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuNiifCuentasCc AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt AND S.TipoAcum=@pmTipoAcum
 AND NOT EXISTS (SELECT * FROM AcuNiifCuentasCc AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes AND AC.TipoAcum=@pmTipoAcum
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdCCosto=S.IdCCosto AND AC.IdSubCos=S.IdSubCos)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasCc] @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 AcuNiifCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdCCosto,@pmIdSubCos
,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasCc_Uni]  @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuNiifCuentasCc WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
  INSERT INTO AcuNiifCuentasCc (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 AcuNiifCuentasCc WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
  GROUP BY nAnno,nMes,IdCia,IdCCosto,IdSubCos,TipoAcum
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpUsuariosPwd]  @pmPwdLog VARCHAR(30),@pmFechaClave SMALLDATETIME,@pmIdUsuario VARCHAR(11)
AS
UPDATE adm_Usuarios SET PwdLog=@pmPwdLog,FechaClave=@pmFechaClave
WHERE IdUsuario=@pmIdUsuario
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposTerc] @pmIdTipoTerc VARCHAR(4),@pmTipoTercero VARCHAR(100),@pmInactivo BIT
 AS 
 UPDATE TiposTerc SET TipoTercero=@pmTipoTercero,Inactivo=@pmInactivo
 WHERE IdTipoTerc=@pmIdTipoTerc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposTerc] @pmIdTipoTerc VARCHAR(4),@pmTipoTercero VARCHAR(100),@pmInactivo BIT
 AS 
 INSERT INTO TiposTerc (IdTipoTerc,TipoTercero,Inactivo)
 VALUES (@pmIdTipoTerc,@pmTipoTercero,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposTerc] @pmIdTipoTerc VARCHAR(4)
 AS 
 DELETE FROM TiposTerc
 WHERE IdTipoTerc=@pmIdTipoTerc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelJurTiposRec] @pmIdTipoRec VARCHAR(4)
 AS 
 DELETE FROM JurTiposRec
 WHERE IdTipoRec=@pmIdTipoRec
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryJurTiposRec] @pmIdTipoRec VARCHAR(4)
 AS 
 IF @pmIdTipoRec IS NULL 
  SELECT IdTipoRec,TipReclamante
  FROM JurTiposRec
  WHERE Inactivo=0
 ORDER BY IdTipoRec
 ELSE
  SELECT IdTipoRec,TipReclamante,Inactivo
  FROM JurTiposRec
  WHERE IdTipoRec=@pmIdTipoRec
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsJurTiposRec] @pmIdTipoRec VARCHAR(4),@pmTipReclamante VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO JurTiposRec (IdTipoRec,TipReclamante,Inactivo)
 VALUES (@pmIdTipoRec,@pmTipReclamante,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpJurTiposRec] @pmIdTipoRec VARCHAR(4),@pmTipReclamante VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE JurTiposRec SET TipReclamante=@pmTipReclamante,Inactivo=@pmInactivo
 WHERE IdTipoRec=@pmIdTipoRec
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuCuentasCcSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuCuentasCc SET AcuCuentasCc.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuCuentasCc INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuCuentasCc WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt AND TipoAcum=@pmTipoAcum) AS SA ON AcuCuentasCc.IdCia=SA.IdCia AND AcuCuentasCc.IdCuenta=SA.IdCuenta
 AND AcuCuentasCc.IdCCosto=SA.IdCCosto AND AcuCuentasCc.IdSubCos=SA.IdSubCos AND AcuCuentasCc.TipoAcum=SA.TipoAcum
 WHERE AcuCuentasCc.nAnno=@pmnAnno AND AcuCuentasCc.nMes=@pmnMes AND AcuCuentasCc.TipoAcum=@pmTipoAcum
 AND (@pmIdCia IS NULL OR AcuCuentasCc.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuCuentasCc.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuCuentasCc_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdCCosto,S.IdSubCos,S.TipoAcum,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuCuentasCc AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt AND S.TipoAcum=@pmTipoAcum
 AND NOT EXISTS (SELECT * FROM AcuCuentasCc AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes AND AC.TipoAcum=@pmTipoAcum
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdCCosto=S.IdCCosto AND AC.IdSubCos=S.IdSubCos)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmtmDiasSan DECIMAL(14,4),@pmtmDiasDnrPen INT,@pmtmDiasDnrSal INT,@pmtmNovDsr INT,@pmtmFecNovIng SMALLDATETIME,@pmtmFecNovRet SMALLDATETIME,@pmtmFecNovVsp SMALLDATETIME,@pmtmFecIniSln SMALLDATETIME,@pmtmFecFinSln SMALLDATETIME
,@pmtmFecIniIge SMALLDATETIME,@pmtmFecFinIge SMALLDATETIME,@pmtmFecIniLma SMALLDATETIME,@pmtmFecFinLma SMALLDATETIME,@pmtmFecIniVac SMALLDATETIME,@pmtmFecFinVac SMALLDATETIME,@pmtmFecIniVct SMALLDATETIME,@pmtmFecFinVct SMALLDATETIME,@pmtmFecIniIrl SMALLDATETIME,@pmtmFecFinIrl SMALLDATETIME,@pmtmVrIBCparaf MONEY,@pmtmCantHoras INT,@pmtmTipSalario VARCHAR(10)
 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,tmDiasSan,tmDiasDnrPen,tmDiasDnrSal,tmNovDsr,tmFecNovIng,tmFecNovRet,tmFecNovVsp,tmFecIniSln,tmFecFinSln,tmFecIniIge,tmFecFinIge,tmFecIniLma,tmFecFinLma,tmFecIniVac,tmFecFinVac,tmFecIniVct,tmFecFinVct,tmFecIniIrl,tmFecFinIrl,tmVrIBCparaf,tmCantHoras,tmTipSalario)
 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,@pmtmDiasSan,@pmtmDiasDnrPen,@pmtmDiasDnrSal
,@pmtmNovDsr,@pmtmFecNovIng,@pmtmFecNovRet,@pmtmFecNovVsp,@pmtmFecIniSln,@pmtmFecFinSln,@pmtmFecIniIge,@pmtmFecFinIge,@pmtmFecIniLma,@pmtmFecFinLma,@pmtmFecIniVac,@pmtmFecFinVac,@pmtmFecIniVct,@pmtmFecFinVct,@pmtmFecIniIrl,@pmtmFecFinIrl,@pmtmVrIBCparaf,@pmtmCantHoras,@pmtmTipSalario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_AportesItm] @pmtmEst VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM,COUNT(tmItem) AS SCANITM
 FROM tm_Aportes 
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_AportesEmp] @pmtmEst VARCHAR(5)
 AS 
 SELECT COUNT(DISTINCT tmIdEmpleado) AS Cantidad
 FROM tm_Aportes
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmtmDiasSan DECIMAL(14,4),@pmtmDiasDnrPen INT,@pmtmDiasDnrSal INT,@pmtmNovDsr INT,@pmtmFecNovIng SMALLDATETIME,@pmtmFecNovRet SMALLDATETIME,@pmtmFecNovVsp SMALLDATETIME,@pmtmFecIniSln SMALLDATETIME,@pmtmFecFinSln SMALLDATETIME
,@pmtmFecIniIge SMALLDATETIME,@pmtmFecFinIge SMALLDATETIME,@pmtmFecIniLma SMALLDATETIME,@pmtmFecFinLma SMALLDATETIME,@pmtmFecIniVac SMALLDATETIME,@pmtmFecFinVac SMALLDATETIME,@pmtmFecIniVct SMALLDATETIME,@pmtmFecFinVct SMALLDATETIME,@pmtmFecIniIrl SMALLDATETIME,@pmtmFecFinIrl SMALLDATETIME,@pmtmVrIBCparaf MONEY,@pmtmCantHoras INT,@pmtmTipSalario VARCHAR(10)
 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,tmDiasSan=@pmtmDiasSan,tmDiasDnrPen=@pmtmDiasDnrPen,tmDiasDnrSal=@pmtmDiasDnrSal,tmNovDsr=@pmtmNovDsr,tmFecNovIng=@pmtmFecNovIng,tmFecNovRet=@pmtmFecNovRet,tmFecNovVsp=@pmtmFecNovVsp,tmFecIniSln=@pmtmFecIniSln,tmFecFinSln=@pmtmFecFinSln,tmFecIniIge=@pmtmFecIniIge,tmFecFinIge=@pmtmFecFinIge,tmFecIniLma=@pmtmFecIniLma,tmFecFinLma=@pmtmFecFinLma,tmFecIniVac=@pmtmFecIniVac,tmFecFinVac=@pmtmFecFinVac 
,tmFecIniVct=@pmtmFecIniVct,tmFecFinVct=@pmtmFecFinVct,tmFecIniIrl=@pmtmFecIniIrl,tmFecFinIrl=@pmtmFecFinIrl,tmVrIBCparaf=@pmtmVrIBCparaf,tmCantHoras=@pmtmCantHoras,tmTipSalario=@pmtmTipSalario
 WHERE tmEst=@pmtmEst AND tmItem=@pmtmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmtmDiasSan DECIMAL(14,4)
,@pmtmDiasDnrPen INT,@pmtmDiasDnrSal INT,@pmtmNovDsr INT,@pmtmFecNovIng SMALLDATETIME,@pmtmFecNovRet SMALLDATETIME,@pmtmFecNovVsp SMALLDATETIME,@pmtmFecIniSln SMALLDATETIME,@pmtmFecFinSln SMALLDATETIME
,@pmtmFecIniIge SMALLDATETIME,@pmtmFecFinIge SMALLDATETIME,@pmtmFecIniLma SMALLDATETIME,@pmtmFecFinLma SMALLDATETIME,@pmtmFecIniVac SMALLDATETIME,@pmtmFecFinVac SMALLDATETIME,@pmtmFecIniVct SMALLDATETIME
,@pmtmFecFinVct SMALLDATETIME,@pmtmFecIniIrl SMALLDATETIME,@pmtmFecFinIrl SMALLDATETIME
 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,tmDiasSan=@pmtmDiasSan,tmDiasDnrPen=@pmtmDiasDnrPen,tmDiasDnrSal=@pmtmDiasDnrSal,tmNovDsr=@pmtmNovDsr,tmFecNovIng=@pmtmFecNovIng,tmFecNovRet=@pmtmFecNovRet
,tmFecNovVsp=@pmtmFecNovVsp,tmFecIniSln=@pmtmFecIniSln,tmFecFinSln=@pmtmFecFinSln,tmFecIniIge=@pmtmFecIniIge,tmFecFinIge=@pmtmFecFinIge,tmFecIniLma=@pmtmFecIniLma,tmFecFinLma=@pmtmFecFinLma,tmFecIniVac=@pmtmFecIniVac
,tmFecFinVac=@pmtmFecFinVac,tmFecIniVct=@pmtmFecIniVct,tmFecFinVct=@pmtmFecFinVct,tmFecIniIrl=@pmtmFecIniIrl,tmFecFinIrl=@pmtmFecFinIrl
 WHERE tmEst=@pmtmEst AND tmItem=@pmtmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmDiasSan,tmDiasDnrPen,tmDiasDnrSal
,tmNovDsr,tmFecNovIng,tmFecNovRet,tmFecNovVsp,tmFecIniSln,tmFecFinSln,tmFecIniIge,tmFecFinIge,tmFecIniLma,tmFecFinLma,tmFecIniVac,tmFecFinVac,tmFecIniVct,tmFecFinVct,tmFecIniIrl,tmFecFinIrl,tmVrIBCparaf,tmCantHoras,tmTipSalario
 FROM tm_Aportes
 WHERE tmEst=@pmtmEst
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPuntFormasItm] @pmIdProgrma INT,@pmIdForma VARCHAR(4)
 AS 
 DELETE FROM PuntFormas
 WHERE IdProgrma=@pmIdProgrma AND IdForma LIKE ISNULL(@pmIdForma,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPuntFormas] @pmIdProgrma INT
 AS 
 DELETE FROM PuntFormas
 WHERE IdProgrma=@pmIdProgrma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsPuntFormas] @pmIdProgrma INT,@pmIdForma VARCHAR(4),@pmPuntos DECIMAL(14,4)
 AS 
 INSERT INTO PuntFormas (IdProgrma,IdForma,Puntos)
 VALUES (@pmIdProgrma,@pmIdForma,@pmPuntos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpPuntFormas] @pmIdProgrma INT,@pmIdForma VARCHAR(4),@pmPuntos DECIMAL(14,4)
 AS 
 UPDATE PuntFormas SET Puntos=@pmPuntos
 WHERE IdProgrma=@pmIdProgrma AND IdForma=@pmIdForma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasNit_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuFisCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdTercero,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuFisCuentasNit AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt 
 AND NOT EXISTS (SELECT * FROM AcuFisCuentasNit AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes  
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdTercero=S.IdTercero)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasNit] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdTercero VARCHAR(16),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuFisCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdTercero,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuFisCuentasNitSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuFisCuentasNit SET AcuFisCuentasNit.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuFisCuentasNit INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuFisCuentasNit WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt) AS SA ON AcuFisCuentasNit.IdCia=SA.IdCia AND AcuFisCuentasNit.IdCuenta=SA.IdCuenta
 AND AcuFisCuentasNit.IdTercero=SA.IdTercero
 WHERE AcuFisCuentasNit.nAnno=@pmnAnno AND AcuFisCuentasNit.nMes=@pmnMes
 AND (@pmIdCia IS NULL OR AcuFisCuentasNit.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuFisCuentasNit.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasNit_Uni] @pmIdTercero VARCHAR(16),@pmIdTerceroDos VARCHAR(16),@pmNewTercero VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdTercero FROM AcuFisCuentasNit WHERE IdTercero=@pmIdTercero OR IdTercero=@pmIdTerceroDos)
  INSERT INTO AcuFisCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT nAnno,nMes,IdCia,IdCuenta,@pmNewTercero,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  FROM AcuFisCuentasNit WHERE IdTercero=@pmIdTercero OR IdTercero=@pmIdTerceroDos
  GROUP BY nAnno,nMes,IdCia,IdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasNit_Unf]  @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuFisCuentasNit WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
  INSERT INTO AcuFisCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT nAnno,nMes,IdCia,@pmNewCuenta,IdTercero,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  FROM AcuFisCuentasNit WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
  GROUP BY nAnno,nMes,IdCia,IdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposCar] @pmIdCrceria VARCHAR(4)
 AS 
 DELETE FROM TiposCar
 WHERE IdCrceria=@pmIdCrceria
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposCar] @pmIdCrceria VARCHAR(4),@pmTipoCar VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposCar SET TipoCar=@pmTipoCar,Inactivo=@pmInactivo
 WHERE IdCrceria=@pmIdCrceria
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposCar] @pmIdCrceria VARCHAR(4),@pmTipoCar VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposCar (IdCrceria,TipoCar,Inactivo)
 VALUES (@pmIdCrceria,@pmTipoCar,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoSistemas] @pmIdSistema VARCHAR(4)
 AS 
 IF @pmIdSistema IS NULL
  BEGIN
   SELECT IdSistema,Sistema,Seccion FROM MttoSistemas WHERE Inactivo=0
  END
 ELSE
  BEGIN
   SELECT IdSistema,Sistema,Seccion,Inactivo FROM MttoSistemas WHERE IdSistema=@pmIdSistema
  END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMttoSistemas] @pmIdSistema VARCHAR(4),@pmSistema VARCHAR(50),@pmSeccion CHAR(1),@pmInactivo BIT
 AS 
 UPDATE MttoSistemas SET Sistema=@pmSistema,Seccion=@pmSeccion,Inactivo=@pmInactivo
 WHERE IdSistema=@pmIdSistema
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMttoSistemas] @pmIdSistema VARCHAR(4),@pmSistema VARCHAR(50),@pmSeccion CHAR(1),@pmInactivo BIT
 AS 
 INSERT INTO MttoSistemas (IdSistema,Sistema,Seccion,Inactivo)
 VALUES (@pmIdSistema,@pmSistema,@pmSeccion,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEgrFacturaTnit]  @pmIdProveedor VARCHAR(16)
 AS 
 SELECT SUM(VrAbono) AS STOTABO
 FROM Trn_EgrFactura
 WHERE IdProveedor=@pmIdProveedor
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelVehPolizas] @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4)
 AS 
 DELETE FROM VehPolizas
 WHERE IdVehiculo=@pmIdVehiculo AND IdClase LIKE ISNULL(@pmIdClase,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_Caja_Ant] @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,Anticipo,IdCia,Fecha,IdConcepto,IdPoseedor,'0',VrAnticipo,0,0,0,0,0,0,0,0,VrAnticipo AS ValNeto,CASE TipoPago WHEN 'EFECTIVO' THEN VrAnticipo ELSE 0 END
,0,0,0,0,0,0,0,CASE TipoPago WHEN 'EFECTIVO' THEN 1 ELSE 0 END,CASE TipoPago WHEN 'EFECTIVO' THEN 'EFE' ELSE 'CHE' END,NumCheque,IdBanco,SUBSTRING(Beneficiario,1,150),A.IdCta,FecCheque,IdConductor,0,'0',IdUsuario,'0','ANTICIPOS',''
,Modalidad,IdVehiculo,TipMuc,Manifiesto,IdCiaMuc,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_TraAnticipos AS A LEFT JOIN CtasCorrientes AS CT ON A.IdCta=CT.IdCta
 WHERE A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND A.TipoPago='EFECTIVO'
 AND A.IdCia LIKE ISNULL(@pmIdCia,'%%') AND A.IdUsuario LIKE ISNULL(@pmIdCajero,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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=1 AND Anticipo=0
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdCajero,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_Caja] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_Caja
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCedContparte VARCHAR(16)
 AS 
 INSERT INTO Trn_JurCasosHis (TipDoc,CasoID,IdCia,ActualizaID,FechaUp,Historial,EstadoAct,CierreProc,ArchivoProc,NombreCparte,DirCparte,TelCparte,DanosCParte,DanosEmp,ValorDeducible,Aseguradora,AuxilioMutuo,IdEstado,IdUsuario,CedContparte)
 VALUES (@pmTipDoc,@pmCasoID,@pmIdCia,@pmActualizaID,@pmFechaUp,@pmHistorial,@pmEstadoAct,@pmCierreProc,@pmArchivoProc,@pmNombreCparte,@pmDirCparte,@pmTelCparte,@pmDanosCParte,@pmDanosEmp
,@pmValorDeducible,@pmAseguradora,@pmAuxilioMutuo,@pmIdEstado,@pmIdUsuario,@pmCedContparte)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCedContparte VARCHAR(16)
 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,CedContparte=@pmCedContparte
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia AND ActualizaID=@pmActualizaID
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CedContparte,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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,IndFactura,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Causacion
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmIndFactura INT,@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,IndFactura=@pmIndFactura,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmIndFactura INT,@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,IndFactura)
 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,@pmIndFactura)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_NiifGen] @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_NiifGen (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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposNov] @pmIdNov VARCHAR(4)
 AS 
 DELETE FROM TiposNov
 WHERE IdNov=@pmIdNov
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTiposNovTra] 
 AS 
  SELECT IdNov,Novedad,Admon  FROM TiposNov
  WHERE  IdNov IN ('TAE','TAP')
  ORDER BY IdNov
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpEdsUnion] @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmCdTanque VARCHAR(4),@pmCapacidad DECIMAL(14,4),@pmCantidad DECIMAL(14,4),@pmNivAgua DECIMAL(14,4)
,@pmTipoTrasl VARCHAR(3),@pmNumTraslado INT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_EdsUnion SET Fecha=@pmFecha,CdTanque=@pmCdTanque,Capacidad=@pmCapacidad,Cantidad=@pmCantidad,NivAgua=@pmNivAgua,TipoTrasl=@pmTipoTrasl,NumTraslado=@pmNumTraslado
,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryEdsUnion] @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumNota,IdCia,Fecha,CdTanque,Capacidad,Cantidad,NivAgua,TipoTrasl,NumTraslado,OrigenAdd,Anulado,FecDev,Observacion,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_EdsUnion
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsEdsUnion] @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmCdTanque VARCHAR(4),@pmCapacidad DECIMAL(14,4),@pmCantidad DECIMAL(14,4),@pmNivAgua DECIMAL(14,4)
,@pmTipoTrasl VARCHAR(3),@pmNumTraslado INT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_EdsUnion (TipDoc,NumNota,IdCia,Fecha,CdTanque,Capacidad,Cantidad,NivAgua,TipoTrasl,NumTraslado,OrigenAdd,Anulado,FecDev,Observacion,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmNumNota,@pmIdCia,@pmFecha,@pmCdTanque,@pmCapacidad,@pmCantidad,@pmNivAgua,@pmTipoTrasl,@pmNumTraslado,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelEdsUnion] @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_EdsUnion WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsGruposPro] @pmIdGrupo VARCHAR(4),@pmGrupoProp VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO GruposPro (IdGrupo,GrupoProp,Inactivo)
 VALUES (@pmIdGrupo,@pmGrupoProp,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpGruposPro] @pmIdGrupo VARCHAR(4),@pmGrupoProp VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE GruposPro SET GrupoProp=@pmGrupoProp,Inactivo=@pmInactivo
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelGruposPro] @pmIdGrupo VARCHAR(4)
 AS 
 DELETE FROM GruposPro
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,TipoContable,VrSalvamento,DepPartes,UndsProd,VUExtend
 FROM Activos
 WHERE FecActivacion<=@pmFechaFin 
 AND (IdEstado<>'9999'  OR (IdEstado='9999' AND FecRetiro>@pmFechaFin))
 ORDER BY IdActivo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 DATETIME,@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,@pmTipoContable VARCHAR(10),@pmVrSalvamento MONEY,@pmDepPartes BIT
,@pmUndsProd INT,@pmTipComp VARCHAR(3),@pmNumComp INT,@pmCdCiaComp CHAR(2),@pmVUExtend INT,@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,TipoContable,VrSalvamento,DepPartes,UndsProd,TipComp,NumComp,CdCiaComp,VUExtend)
 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,@pmTipoContable,@pmVrSalvamento,@pmDepPartes,@pmUndsProd,@pmTipComp,@pmNumComp,@pmCdCiaComp,@pmVUExtend)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmNumParte)
 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,NumParte
 FROM Trn_AfMovRetiro AS R INNER JOIN  Activos AS A ON R.IdActivo=A.IdActivo
 WHERE R.IdMov=@pmIdMov AND R.Numero=@pmNumero AND R.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryActivosMar] 
 AS 
 SELECT Marca,COUNT(IdActivo)
 FROM Activos
 WHERE IdEstado<>'9999'
 GROUP BY  Marca
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,TipoContable,VrSalvamento,DepPartes,UndsProd,TipComp,NumComp,CdCiaComp,VUExtend,FecCreacion,IdUsuario
 FROM Activos
 WHERE IdActivo=@pmIdActivo OR (CodBarras=@pmCodBarras AND IdEstado<>'9999')
 ORDER BY  IdActivo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,TipoContable,VrSalvamento,DepPartes,UndsProd,TipComp,NumComp,CdCiaComp,VUExtend,FecCreacion,IdUsuario
 FROM Activos
 WHERE IdActivo=@pmIdActivo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 DATETIME,@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,@pmTipoContable VARCHAR(10),@pmVrSalvamento MONEY,@pmDepPartes BIT,@pmUndsProd INT,@pmTipComp VARCHAR(3),@pmNumComp INT,@pmCdCiaComp CHAR(2),@pmVUExtend INT
 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
,TipoContable=@pmTipoContable,VrSalvamento=@pmVrSalvamento,DepPartes=@pmDepPartes,UndsProd=@pmUndsProd,TipComp=@pmTipComp,NumComp=@pmNumComp,CdCiaComp=@pmCdCiaComp,VUExtend=@pmVUExtend
 WHERE IdActivo=@pmIdActivo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,NivelAprob,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_OCompra
 WHERE TipDoc=@pmTipDoc AND OCompra=@pmOCompra AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmNivelAprob INT,@pmVrIvaMvc MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc 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,NivelAprob,IdEstado,TimeSys,IdCiaCrea,IdUsuario,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc)
 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,@pmNivelAprob,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmVrIvaMvc,@pmVrReteCREE,@pmTarifaRtc,@pmCodTarRtc)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpOCompraApb] @pmTipDoc VARCHAR(3),@pmOCompra INT,@pmIdCia CHAR(2)
,@pmObservacion VARCHAR(250),@pmNumAprob INT,@pmFecAprob SMALLDATETIME,@pmCdUsuAprob VARCHAR(11),@pmIdEstado VARCHAR(4)
,@pmNivelAprob INT
 AS 
 UPDATE Trn_OCompra SET Observacion=@pmObservacion,NumAprob=@pmNumAprob,FecAprob=@pmFecAprob
,CdUsuAprob=@pmCdUsuAprob,IdEstado=@pmIdEstado,NivelAprob=@pmNivelAprob
 WHERE TipDoc=@pmTipDoc AND OCompra=@pmOCompra AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmNivelAprob INT,@pmVrIvaMvc MONEY
,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc 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,NivelAprob=@pmNivelAprob,VrIvaMvc=@pmVrIvaMvc
,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,FecUpdate=@pmFecUpdate,VrReteIVA=@pmVrReteIVA,CodTarRiv=@pmCodTarRiv,VrReteCREE=@pmVrReteCREE,TarifaRtc=@pmTarifaRtc,CodTarRtc=@pmCodTarRtc
 WHERE TipDoc=@pmTipDoc AND OCompra=@pmOCompra AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCodFEDian VARCHAR(20)
,@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO FinConcFactura (IdConcepto,Concepto,TipoConc,Gravado,CodTarIva,CodTarRet,CodTarIca,IdCuenta,CdCueIva,CdCueRet,CdCueIca,Inactivo,CodFEDian,FechaAdd,IdUsuario)
 VALUES (@pmIdConcepto,@pmConcepto,@pmTipoConc,@pmGravado,@pmCodTarIva,@pmCodTarRet,@pmCodTarIca,@pmIdCuenta
,@pmCdCueIva,@pmCdCueRet,@pmCdCueIca,@pmInactivo,@pmCodFEDian,@pmFechaAdd,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCodFEDian VARCHAR(20),@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,CodFEDian=@pmCodFEDian,FechaUpdate=@pmFechaUpdate
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmNumParte INT
 AS 
 INSERT INTO Trn_AfMovRetiro (IdMov,Numero,IdCia,Item,IdActivo,IdCausal,NitTercero,NomTercero,VrCostoIni,VrMejoras,VrDisminucion,VrAcuAjustes,VrDepAcum,VrAjuDepAcum,VrIngreso,FechaRet,IdCueDeb,IdCueIng,Concepto,NumParte)
 VALUES (@pmIdMov,@pmNumero,@pmIdCia,@pmItem,@pmIdActivo,@pmIdCausal,@pmNitTercero,@pmNomTercero,@pmVrCostoIni,@pmVrMejoras,@pmVrDisminucion,@pmVrAcuAjustes
,@pmVrDepAcum,@pmVrAjuDepAcum,@pmVrIngreso,@pmFechaRet,@pmIdCueDeb,@pmIdCueIng,@pmConcepto,@pmNumParte)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTiposCol] @pmIdColor VARCHAR(4)
 AS 
 IF @pmIdColor IS NULL
 SELECT IdColor,NomColor,NColor,CodigoMT
 FROM TiposCol
 WHERE Inactivo=0
 ORDER BY NomColor
 ELSE
 SELECT IdColor,NomColor,NColor,CodigoMT,Inactivo
 FROM TiposCol
 WHERE IdColor=@pmIdColor
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposCol] @pmIdColor VARCHAR(4),@pmNomColor VARCHAR(50),@pmNColor INT,@pmCodigoMT VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO TiposCol (IdColor,NomColor,NColor,CodigoMT,Inactivo)
 VALUES (@pmIdColor,@pmNomColor,@pmNColor,@pmCodigoMT,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposCol] @pmIdColor VARCHAR(4),@pmNomColor VARCHAR(50),@pmNColor INT,@pmCodigoMT VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE TiposCol SET NomColor=@pmNomColor,NColor=@pmNColor,CodigoMT=@pmCodigoMT,Inactivo=@pmInactivo
 WHERE IdColor=@pmIdColor
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraDecAduana] @pmTipDoc VARCHAR(3),@pmNumDeclara INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraDecAduana
 WHERE TipDoc=@pmTipDoc AND NumDeclara=@pmNumDeclara AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraDecAduana] @pmTipDoc VARCHAR(3),@pmNumDeclara INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecEmision SMALLDATETIME,@pmIdOrigen VARCHAR(8),@pmIdAduCarga VARCHAR(4),@pmIdDestino VARCHAR(8),@pmIdAduOrigen VARCHAR(4),@pmIdAduDestino VARCHAR(4),@pmIdAduFrontera VARCHAR(4),@pmIdDeclarante VARCHAR(16),@pmDeclarante VARCHAR(150),@pmDirDeclara VARCHAR(250),@pmCdLocalDec VARCHAR(8),@pmIdRemitente VARCHAR(16),@pmRemitente VARCHAR(150),@pmDirOrigen VARCHAR(250),@pmCdLocalRem VARCHAR(8),@pmIdDestinatario VARCHAR(16),@pmDestinatario VARCHAR(150),@pmDirDestino VARCHAR(250)
,@pmCdLocalDest VARCHAR(8),@pmIdConsig VARCHAR(16),@pmConsignatario VARCHAR(150),@pmDirConsig VARCHAR(250),@pmCdLocalConsig VARCHAR(8),@pmIdMoneda VARCHAR(5),@pmValorTotal MONEY,@pmPesoTotal DECIMAL(14,4),@pmDocAnexos VARCHAR(500),@pmVehiculos VARCHAR(250),@pmRemolques VARCHAR(250),@pmManifiestos VARCHAR(100),@pmContenedores VARCHAR(500),@pmPrecintos VARCHAR(250),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2500),@pmIdEstado VARCHAR(4),@pmNegociacion VARCHAR(1000),@pmCondicPago VARCHAR(1000),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraDecAduana SET Fecha=@pmFecha,FecEmision=@pmFecEmision,IdOrigen=@pmIdOrigen,IdAduCarga=@pmIdAduCarga,IdDestino=@pmIdDestino,IdAduOrigen=@pmIdAduOrigen,IdAduDestino=@pmIdAduDestino,IdAduFrontera=@pmIdAduFrontera,IdDeclarante=@pmIdDeclarante,Declarante=@pmDeclarante,DirDeclara=@pmDirDeclara,CdLocalDec=@pmCdLocalDec,IdRemitente=@pmIdRemitente,Remitente=@pmRemitente,DirOrigen=@pmDirOrigen,CdLocalRem=@pmCdLocalRem,IdDestinatario=@pmIdDestinatario,Destinatario=@pmDestinatario,DirDestino=@pmDirDestino,CdLocalDest=@pmCdLocalDest,IdConsig=@pmIdConsig,Consignatario=@pmConsignatario,DirConsig=@pmDirConsig
,CdLocalConsig=@pmCdLocalConsig,IdMoneda=@pmIdMoneda,ValorTotal=@pmValorTotal,PesoTotal=@pmPesoTotal,DocAnexos=@pmDocAnexos,Vehiculos=@pmVehiculos,Remolques=@pmRemolques,Manifiestos=@pmManifiestos,Contenedores=@pmContenedores,Precintos=@pmPrecintos,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,Negociacion=@pmNegociacion,CondicPago=@pmCondicPago
 WHERE TipDoc=@pmTipDoc AND NumDeclara=@pmNumDeclara AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDecAduana] @pmTipDoc VARCHAR(3),@pmNumDeclara INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumDeclara,IdCia,Fecha,FecEmision,IdOrigen,IdAduCarga,IdDestino,IdAduOrigen,IdAduDestino,IdAduFrontera,IdDeclarante,Declarante,DirDeclara,CdLocalDec,IdRemitente,Remitente,DirOrigen,CdLocalRem,IdDestinatario,Destinatario,DirDestino,CdLocalDest,IdConsig,Consignatario,DirConsig,CdLocalConsig,IdMoneda,ValorTotal,PesoTotal
,DocAnexos,Vehiculos,Remolques,Manifiestos,Contenedores,Precintos,OrigenAdd,Anulado,FecDev,Observacion,Negociacion,CondicPago,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraDecAduana
 WHERE TipDoc=@pmTipDoc AND NumDeclara=@pmNumDeclara AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraDecAduana] @pmTipDoc VARCHAR(3),@pmNumDeclara INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecEmision SMALLDATETIME,@pmIdOrigen VARCHAR(8),@pmIdAduCarga VARCHAR(4),@pmIdDestino VARCHAR(8),@pmIdAduOrigen VARCHAR(4),@pmIdAduDestino VARCHAR(4),@pmIdAduFrontera VARCHAR(4),@pmIdDeclarante VARCHAR(16),@pmDeclarante VARCHAR(150),@pmDirDeclara VARCHAR(250),@pmCdLocalDec VARCHAR(8),@pmIdRemitente VARCHAR(16),@pmRemitente VARCHAR(150),@pmDirOrigen VARCHAR(250),@pmCdLocalRem VARCHAR(8),@pmIdDestinatario VARCHAR(16),@pmDestinatario VARCHAR(150),@pmDirDestino VARCHAR(250)
,@pmCdLocalDest VARCHAR(8),@pmIdConsig VARCHAR(16),@pmConsignatario VARCHAR(150),@pmDirConsig VARCHAR(250),@pmCdLocalConsig VARCHAR(8),@pmIdMoneda VARCHAR(5),@pmValorTotal MONEY,@pmPesoTotal DECIMAL(14,4),@pmDocAnexos VARCHAR(500),@pmVehiculos VARCHAR(250),@pmRemolques VARCHAR(250),@pmManifiestos VARCHAR(100),@pmContenedores VARCHAR(500),@pmPrecintos VARCHAR(250),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2500),@pmIdEstado VARCHAR(4),@pmNegociacion VARCHAR(1000),@pmCondicPago VARCHAR(1000),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraDecAduana (TipDoc,NumDeclara,IdCia,Fecha,FecEmision,IdOrigen,IdAduCarga,IdDestino,IdAduOrigen,IdAduDestino,IdAduFrontera,IdDeclarante,Declarante,DirDeclara,CdLocalDec,IdRemitente,Remitente,DirOrigen,CdLocalRem,IdDestinatario,Destinatario,DirDestino,CdLocalDest,IdConsig,Consignatario,DirConsig,CdLocalConsig,IdMoneda,ValorTotal,PesoTotal,DocAnexos,Vehiculos,Remolques,Manifiestos,Contenedores,Precintos,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,Negociacion,CondicPago)
 VALUES (@pmTipDoc,@pmNumDeclara,@pmIdCia,@pmFecha,@pmFecEmision,@pmIdOrigen,@pmIdAduCarga,@pmIdDestino,@pmIdAduOrigen,@pmIdAduDestino,@pmIdAduFrontera,@pmIdDeclarante,@pmDeclarante,@pmDirDeclara,@pmCdLocalDec,@pmIdRemitente,@pmRemitente,@pmDirOrigen,@pmCdLocalRem,@pmIdDestinatario,@pmDestinatario,@pmDirDestino,@pmCdLocalDest,@pmIdConsig,@pmConsignatario,@pmDirConsig,@pmCdLocalConsig,@pmIdMoneda,@pmValorTotal,@pmPesoTotal,@pmDocAnexos,@pmVehiculos,@pmRemolques,@pmManifiestos,@pmContenedores,@pmPrecintos,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmNegociacion,@pmCondicPago)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposComNiif] 
AS
 SELECT IdCom,TipoCom,IdCom + ' '+TipoCom+' ('+ISNULL(ClaseNiif,'')+')' AS DsTip
 FROM TiposCom 
 WHERE ISNULL(ClaseNiif,'') IN ('NIIF','FISCAL') AND Inactivo=0
 ORDER BY IdCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTiposCom] @pmIdCom VARCHAR(3)
 AS 
 SELECT IdCom,TipoCom,IdDiario,EsEgreso,ConsUnico,Inactivo,FechaAdd,FechaUpdate,ClaseNiif,Doc_Soporte
 FROM TiposCom
 WHERE IdCom=@pmIdCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposCom] @pmIdCom VARCHAR(3),@pmTipoCom VARCHAR(50),@pmIdDiario VARCHAR(4)
,@pmEsEgreso BIT,@pmConsUnico BIT,@pmInactivo BIT,@pmClaseNiif VARCHAR(10),@pmDoc_Soporte BIT,@pmFechaAdd SMALLDATETIME
 AS 
 INSERT INTO TiposCom (IdCom,TipoCom,IdDiario,EsEgreso,ConsUnico,Inactivo,FechaAdd,ClaseNiif,Doc_Soporte)
 VALUES (@pmIdCom,@pmTipoCom,@pmIdDiario,@pmEsEgreso,@pmConsUnico,@pmInactivo,@pmFechaAdd,@pmClaseNiif,@pmDoc_Soporte)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposCom] @pmIdCom VARCHAR(3),@pmTipoCom VARCHAR(50),@pmIdDiario VARCHAR(4)
,@pmEsEgreso BIT,@pmConsUnico BIT,@pmInactivo BIT,@pmClaseNiif VARCHAR(10),@pmDoc_Soporte BIT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TiposCom SET TipoCom=@pmTipoCom,IdDiario=@pmIdDiario,EsEgreso=@pmEsEgreso
,ConsUnico=@pmConsUnico,Inactivo=@pmInactivo,FechaUpdate=@pmFechaUpdate,ClaseNiif=@pmClaseNiif,Doc_Soporte=@pmDoc_Soporte
 WHERE IdCom=@pmIdCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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))
 AND (ISNULL(ClaseNiif,'')='CONTAB' OR LEN(ISNULL(ClaseNiif,''))=0) 
 ORDER BY IdCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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)
,@pmDocSop BIT,@pmTipSop VARCHAR(3),@pmNumSoporte INT,@pmIdCiaSop CHAR(2),@pmTipoImp VARCHAR(4),@pmCodTarImp VARCHAR(4),@pmItemBase INT
 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,DocSop,TipSop,NumSoporte,IdCiaSop,TipoImp,CodTarImp,ItemBase)
 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
,@pmDocSop,@pmTipSop,@pmNumSoporte,@pmIdCiaSop,@pmTipoImp,@pmCodTarImp,@pmItemBase)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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
,DocSop,TipSop,NumSoporte,IdCiaSop,TipoImp,CodTarImp,ItemBase
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelFijos_Cla] @pmIdCargo VARCHAR(4),@pmIdClase VARCHAR(4)
AS
 DELETE FROM  Fijos_Cla   WHERE IdCargo=@pmIdCargo AND IdClase LIKE ISNULL(@pmIdClase,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPuntPremios] @pmIdProgrma INT
 AS 
 DELETE FROM PuntPremios
 WHERE IdProgrma=@pmIdProgrma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPuntPremiosItm] @pmIdProgrma INT,@pmItem INT
 AS 
 DELETE FROM PuntPremios
 WHERE IdProgrma=@pmIdProgrma
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPuntPremiosItm] @pmIdProgrma INT
 AS 
 SELECT MAX(Item) AS MXITM
 FROM PuntPremios
 WHERE IdProgrma=@pmIdProgrma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelAfPignora] @pmNumero INT
 AS 
 DELETE FROM Trn_AfPignora
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryAfPignora] @pmNumero INT
 AS 
 SELECT Numero,IdActivo,Fecha,TipoAccion,NomTercero,Comentarios,Garantia,IdUsuario
 FROM Trn_AfPignora
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelEstadoDoc] @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM EstadoDoc
 WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE FUNCTION [dbo].[FuncMayAgencias]  (@pmNContrato INT,@pmIdCia CHAR(2))
RETURNS INT
 AS  
BEGIN 
 DECLARE @CantAgenc  INT
 SET  @CantAgenc=(SELECT COUNT(*) FROM Trn_MayAgencias WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia)
 RETURN @CantAgenc
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOilMedicionesTq] @pmTipDoc VARCHAR(3),@pmNumInvent INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdTanque VARCHAR(4),@pmIdProducto VARCHAR(16),@pmNivelVol DECIMAL(16,6),@pmCantidad DECIMAL(16,6),@pmUndMed VARCHAR(10),@pmTipoMed INT,@pmAlturaTq DECIMAL(16,6)
,@pmUndAforoC DECIMAL(16,6),@pmUndAforoF DECIMAL(16,6),@pmTemperatura DECIMAL(16,6),@pmUnidTemp CHAR(1),@pmValorAPI DECIMAL(16,6),@pmAPI_Corregido DECIMAL(16,6),@pmDensidad DECIMAL(16,6),@pmCantSaldo DECIMAL(16,6),@pmCantAjuste DECIMAL(16,6),@pmVrUnitario DECIMAL(16,6)
 AS 
 INSERT INTO Trn_OilMedicionesTq (TipDoc,NumInvent,IdCia,Item,IdTanque,IdProducto,NivelVol,Cantidad,UndMed,TipoMed,AlturaTq,UndAforoC,UndAforoF,Temperatura,UnidTemp,ValorAPI,API_Corregido,Densidad,CantSaldo,CantAjuste,VrUnitario)
 VALUES (@pmTipDoc,@pmNumInvent,@pmIdCia,@pmItem,@pmIdTanque,@pmIdProducto,@pmNivelVol,@pmCantidad,@pmUndMed,@pmTipoMed,@pmAlturaTq,@pmUndAforoC,@pmUndAforoF,@pmTemperatura,@pmUnidTemp,@pmValorAPI,@pmAPI_Corregido,@pmDensidad,@pmCantSaldo,@pmCantAjuste,@pmVrUnitario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncAuxInvNivelMax] (@pmIdProducto VARCHAR(16),@pmIdCia CHAR(2))
RETURNS DECIMAL(14,4)
 AS  
 BEGIN
  DECLARE @NivelMax DECIMAL(14,4)
  IF (@pmIdCia='00' OR @pmIdCia='0')
   BEGIN
    SET @NivelMax=(SELECT ExtciaMax FROM ProdMcias WHERE IdProducto=@pmIdProducto)
   END
  ELSE
   BEGIN 
    SET @NivelMax=(SELECT NivelMax FROM ProdNiveles WHERE IdProducto=@pmIdProducto AND IdCia=@pmIdCia)
   END
  RETURN @NivelMax
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncAuxInvNivelMin] (@pmIdProducto VARCHAR(16),@pmIdCia CHAR(2))
RETURNS DECIMAL(14,4)
 AS  
 BEGIN
  DECLARE @NivelMin DECIMAL(14,4)
  IF (@pmIdCia='00' OR @pmIdCia='0')
   BEGIN
    SET @NivelMin=(SELECT ExtciaMin FROM ProdMcias WHERE IdProducto=@pmIdProducto)
   END
  ELSE
   BEGIN 
    SET @NivelMin=(SELECT NivelMin FROM ProdNiveles WHERE IdProducto=@pmIdProducto AND IdCia=@pmIdCia)
   END
  RETURN @NivelMin
 END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposNom] @pmIdNom VARCHAR(4),@pmTipoNomina VARCHAR(50),@pmAsistencia BIT,@pmNoDevenga BIT
,@pmTipoLiquida CHAR(1),@pmFechaActual SMALLDATETIME,@pmInactivo BIT,@pmCdCiaInt CHAR(2),@pmLiqEspSalud BIT,@pmDctoSalud DECIMAL(14,4)
,@pmPagoSalud DECIMAL(14,4),@pmNomDiario BIT
 AS 
 INSERT INTO TiposNom (IdNom,TipoNomina,Asistencia,NoDevenga,TipoLiquida,FechaActual,Inactivo,CdCiaInt,LiqEspSalud,DctoSalud,PagoSalud,NomDiario)
 VALUES (@pmIdNom,@pmTipoNomina,@pmAsistencia,@pmNoDevenga,@pmTipoLiquida,@pmFechaActual,@pmInactivo,@pmCdCiaInt,@pmLiqEspSalud,@pmDctoSalud,@pmPagoSalud,@pmNomDiario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposNom] @pmIdNom VARCHAR(4),@pmTipoNomina VARCHAR(50),@pmAsistencia BIT,@pmNoDevenga BIT
,@pmTipoLiquida CHAR(1),@pmFechaActual SMALLDATETIME,@pmInactivo BIT,@pmCdCiaInt CHAR(2)
,@pmLiqEspSalud BIT,@pmDctoSalud DECIMAL(14,4),@pmPagoSalud DECIMAL(14,4),@pmNomDiario BIT
 AS 
 UPDATE TiposNom SET TipoNomina=@pmTipoNomina,Asistencia=@pmAsistencia,NoDevenga=@pmNoDevenga,TipoLiquida=@pmTipoLiquida
,FechaActual=@pmFechaActual,Inactivo=@pmInactivo,CdCiaInt=@pmCdCiaInt,LiqEspSalud=@pmLiqEspSalud,DctoSalud=@pmDctoSalud,PagoSalud=@pmPagoSalud,NomDiario=@pmNomDiario
 WHERE IdNom=@pmIdNom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposNom] @pmIdNom VARCHAR(4)
 AS 
 IF @pmIdNom IS NULL
  SELECT IdNom,TipoNomina,Asistencia,NoDevenga,TipoLiquida,FechaActual,CdCiaInt,LiqEspSalud,DctoSalud,PagoSalud,NomDiario
  FROM TiposNom WHERE Inactivo=0
  ORDER BY IdNom
ELSE
  SELECT IdNom,TipoNomina,Asistencia,NoDevenga,TipoLiquida,FechaActual,CdCiaInt,LiqEspSalud,DctoSalud,PagoSalud,NomDiario,Inactivo
  FROM TiposNom
  WHERE IdNom=@pmIdNom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut,NiifTipo,NiifNumero,FisTipo,FisNumero
,DocSoporte,CodRes,DescServicio 
 FROM Trn_Comprobantes
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento  AND IdCiaDoc=@pmIdCiaDoc
 ORDER BY  TipCom,IdCia, Comprobante
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND EsEgreso=1 AND Anticipo=0
 AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND  IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 GROUP BY TipCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@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)
,@pmCdSustento VARCHAR(4),@pmTipo_Pago VARCHAR(10),@pmEstablec VARCHAR(20),@pmPEmision VARCHAR(20),@pmAutorizac VARCHAR(50),@pmFechaAut SMALLDATETIME,@pmNiifTipo VARCHAR(3),@pmNiifNumero INT,@pmFisTipo VARCHAR(3),@pmFisNumero INT,@pmDocSoporte INT,@pmCodRes VARCHAR(4),@pmDescServicio VARCHAR(500),@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,CdSustento=@pmCdSustento,Tipo_Pago=@pmTipo_Pago,Establec=@pmEstablec,PEmision=@pmPEmision,Autorizac=@pmAutorizac,FechaAut=@pmFechaAut,NiifTipo=@pmNiifTipo,NiifNumero=@pmNiifNumero,FisTipo=@pmFisTipo,FisNumero=@pmFisNumero,DocSoporte=@pmDocSoporte,CodRes=@pmCodRes,DescServicio=@pmDescServicio
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@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),@pmCdSustento VARCHAR(4),@pmTipo_Pago VARCHAR(10),@pmEstablec VARCHAR(20),@pmPEmision VARCHAR(20),@pmAutorizac VARCHAR(50),@pmFechaAut SMALLDATETIME,@pmNiifTipo VARCHAR(3),@pmNiifNumero INT,@pmFisTipo VARCHAR(3),@pmFisNumero INT,@pmDocSoporte INT,@pmCodRes VARCHAR(4),@pmDescServicio VARCHAR(500)
,@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,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut,NiifTipo,NiifNumero,FisTipo,FisNumero,DocSoporte,CodRes,DescServicio)
 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,@pmCdSustento,@pmTipo_Pago,@pmEstablec,@pmPEmision,@pmAutorizac,@pmFechaAut,@pmNiifTipo,@pmNiifNumero,@pmFisTipo,@pmFisNumero,@pmDocSoporte,@pmCodRes,@pmDescServicio)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut,NiifTipo,NiifNumero,FisTipo,FisNumero
,DocSoporte,CodRes,DescServicio
 FROM Trn_Comprobantes
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryComprobantesCaj] @pmFecha SMALLDATETIME,@pmIdCia CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 SELECT  TipCom,IdCia,IdUsuario,SUM(VrTotal) AS STOTAL
 FROM Trn_Comprobantes
 WHERE Fecha=@pmFecha AND EsEgreso=1 AND EnEfectivo=1 AND TipDoc<>'DVA' AND TipDoc<>'DAO'
 AND TipDoc<>'DAE' AND TipDoc<>'ANE'
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND  IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 GROUP BY TipCom,IdCia,IdUsuario 
 UNION ALL
 SELECT 'DEG',IdCia,IdUsuario,SUM(VrTotal) AS STOTAL
 FROM Trn_Comprobantes
 WHERE Fecha=@pmFecha AND EsEgreso=1 AND EnEfectivo=1 AND TipDoc IN ('DVA','DAO') AND Integrado=1
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 GROUP BY IdCia,IdUsuario 
 ORDER BY IdCia,IdUsuario
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_CxCobrarId] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_CxCobrar
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUptm_CxCobrarAbo] @pmtmNumero VARCHAR(5),@pmtmItem INT
,@pmtmTotAbono MONEY
 AS 
 UPDATE tm_CxCobrar SET tmTotAbono=@pmtmTotAbono
 WHERE  tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTercClieGarant] @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdLinea VARCHAR(4),@pmItem INT
 AS 
 SELECT IdClie,IdAgencia,IdLinea,Item,TipoGarant,CdInmueble,CdTipoVeh,VrGarant,VrAvaluo,Propietario,DirInmueble,CdCiudad,Matricula,Escritura
,NumNotaria,MarcaVeh,Modelo,CdBanco,FecCheque,NumCheque,OtroTipoVehic,OtroInmueble,CedFmnte1,NomFmnte1,CedFmnte2,NomFmnte2
,CedFmnte3,NomFmnte3,CedFmnte4,NomFmnte4,CedFmnte5,NomFmnte5,CedFmnte6,NomFmnte6
 FROM TercClieGarant
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia AND IdLinea=@pmIdLinea AND Item=@pmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTercClieGarant] @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdLinea VARCHAR(4),@pmItem INT,@pmTipoGarant VARCHAR(20),@pmCdInmueble VARCHAR(4),@pmCdTipoVeh VARCHAR(4),@pmVrGarant MONEY,@pmVrAvaluo MONEY,@pmPropietario VARCHAR(250),@pmDirInmueble VARCHAR(250),@pmCdCiudad VARCHAR(8),@pmMatricula VARCHAR(50),@pmEscritura VARCHAR(50)
,@pmNumNotaria VARCHAR(20),@pmMarcaVeh VARCHAR(20),@pmModelo VARCHAR(4),@pmCdBanco VARCHAR(4),@pmFecCheque SMALLDATETIME,@pmNumCheque VARCHAR(20),@pmOtroTipoVehic VARCHAR(50),@pmOtroInmueble VARCHAR(50),@pmCedFmnte1 VARCHAR(16),@pmNomFmnte1 VARCHAR(150),@pmCedFmnte2 VARCHAR(16),@pmNomFmnte2 VARCHAR(150),@pmCedFmnte3 VARCHAR(16),@pmNomFmnte3 VARCHAR(150)
,@pmCedFmnte4 VARCHAR(16),@pmNomFmnte4 VARCHAR(150),@pmCedFmnte5 VARCHAR(16),@pmNomFmnte5 VARCHAR(150),@pmCedFmnte6 VARCHAR(16),@pmNomFmnte6 VARCHAR(150)
 AS 
 INSERT INTO TercClieGarant (IdClie,IdAgencia,IdLinea,Item,TipoGarant,CdInmueble,CdTipoVeh,VrGarant,VrAvaluo,Propietario,DirInmueble,CdCiudad,Matricula,Escritura,NumNotaria,MarcaVeh,Modelo,CdBanco,FecCheque,NumCheque,OtroTipoVehic,OtroInmueble,CedFmnte1,NomFmnte1,CedFmnte2,NomFmnte2,CedFmnte3,NomFmnte3,CedFmnte4,NomFmnte4,CedFmnte5,NomFmnte5,CedFmnte6,NomFmnte6)
 VALUES (@pmIdClie,@pmIdAgencia,@pmIdLinea,@pmItem,@pmTipoGarant,@pmCdInmueble,@pmCdTipoVeh,@pmVrGarant,@pmVrAvaluo,@pmPropietario,@pmDirInmueble,@pmCdCiudad,@pmMatricula,@pmEscritura,@pmNumNotaria,@pmMarcaVeh,@pmModelo,@pmCdBanco,@pmFecCheque,@pmNumCheque,@pmOtroTipoVehic,@pmOtroInmueble,@pmCedFmnte1,@pmNomFmnte1,@pmCedFmnte2,@pmNomFmnte2,@pmCedFmnte3
,@pmNomFmnte3,@pmCedFmnte4,@pmNomFmnte4,@pmCedFmnte5,@pmNomFmnte5,@pmCedFmnte6,@pmNomFmnte6)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTercClieGarant] @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdLinea VARCHAR(4),@pmItem INT,@pmTipoGarant VARCHAR(20),@pmCdInmueble VARCHAR(4),@pmCdTipoVeh VARCHAR(4),@pmVrGarant MONEY,@pmVrAvaluo MONEY,@pmPropietario VARCHAR(250),@pmDirInmueble VARCHAR(250),@pmCdCiudad VARCHAR(8),@pmMatricula VARCHAR(50),@pmEscritura VARCHAR(50),@pmNumNotaria VARCHAR(20),@pmMarcaVeh VARCHAR(20),@pmModelo VARCHAR(4),@pmCdBanco VARCHAR(4),@pmFecCheque SMALLDATETIME,@pmNumCheque VARCHAR(20)
,@pmOtroTipoVehic VARCHAR(50),@pmOtroInmueble VARCHAR(50),@pmCedFmnte1 VARCHAR(16),@pmNomFmnte1 VARCHAR(150),@pmCedFmnte2 VARCHAR(16),@pmNomFmnte2 VARCHAR(150),@pmCedFmnte3 VARCHAR(16),@pmNomFmnte3 VARCHAR(150),@pmCedFmnte4 VARCHAR(16),@pmNomFmnte4 VARCHAR(150),@pmCedFmnte5 VARCHAR(16),@pmNomFmnte5 VARCHAR(150),@pmCedFmnte6 VARCHAR(16),@pmNomFmnte6 VARCHAR(150)
 AS 
 UPDATE TercClieGarant SET TipoGarant=@pmTipoGarant,CdInmueble=@pmCdInmueble,CdTipoVeh=@pmCdTipoVeh,VrGarant=@pmVrGarant,VrAvaluo=@pmVrAvaluo,Propietario=@pmPropietario,DirInmueble=@pmDirInmueble,CdCiudad=@pmCdCiudad,Matricula=@pmMatricula,Escritura=@pmEscritura,NumNotaria=@pmNumNotaria,MarcaVeh=@pmMarcaVeh,Modelo=@pmModelo,CdBanco=@pmCdBanco,FecCheque=@pmFecCheque,NumCheque=@pmNumCheque,OtroTipoVehic=@pmOtroTipoVehic,OtroInmueble=@pmOtroInmueble
,CedFmnte1=@pmCedFmnte1,NomFmnte1=@pmNomFmnte1,CedFmnte2=@pmCedFmnte2,NomFmnte2=@pmNomFmnte2,CedFmnte3=@pmCedFmnte3,NomFmnte3=@pmNomFmnte3,CedFmnte4=@pmCedFmnte4,NomFmnte4=@pmNomFmnte4,CedFmnte5=@pmCedFmnte5,NomFmnte5=@pmNomFmnte5,CedFmnte6=@pmCedFmnte6,NomFmnte6=@pmNomFmnte6
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia AND IdLinea=@pmIdLinea AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsClaseSub] @pmIdClase VARCHAR(4),@pmNomClase VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ClaseSub (IdClase,NomClase,Inactivo)
 VALUES (@pmIdClase,@pmNomClase,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpClaseSub] @pmIdClase VARCHAR(4),@pmNomClase VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ClaseSub SET NomClase=@pmNomClase,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelClaseSub] @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseSub
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_MttoDetalle] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmFecha SMALLDATETIME,@pmtmIdConc VARCHAR(4),@pmtmDescripcion VARCHAR(1000),@pmtmCantidad DECIMAL(14,4),@pmtmVrUnitario DECIMAL(16,6)
,@pmtmUndMed VARCHAR(50),@pmtmCdMtto VARCHAR(4),@pmtmIdEstado VARCHAR(4),@pmtmIdOperario VARCHAR(16),@pmtmNumVehic VARCHAR(10),@pmtmNumParte VARCHAR(20),@pmtmReqRep BIT,@pmtmReqMO BIT,@pmtmTipoServ VARCHAR(10)
,@pmtmCdCentro VARCHAR(4),@pmtmNitTercero VARCHAR(16)
 AS 
 INSERT INTO tm_MttoDetalle (tmNumero,tmItem,tmFecha,tmIdConc,tmDescripcion,tmCantidad,tmVrUnitario,tmUndMed,tmCdMtto,tmIdEstado,tmIdOperario,tmNumVehic,tmNumParte,tmReqRep,tmReqMO,tmTipoServ,tmCdCentro,tmNitTercero)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmFecha,@pmtmIdConc,@pmtmDescripcion,@pmtmCantidad,@pmtmVrUnitario,@pmtmUndMed,@pmtmCdMtto,@pmtmIdEstado,@pmtmIdOperario,@pmtmNumVehic,@pmtmNumParte,@pmtmReqRep,@pmtmReqMO,@pmtmTipoServ,@pmtmCdCentro,@pmtmNitTercero)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsNomDotacion] @pmIdCia CHAR(2),@pmNumero INT,@pmFecha SMALLDATETIME,@pmTipoDotac VARCHAR(10),@pmTipoEntrega INT,@pmPlazoEntrega INT
,@pmFecVence SMALLDATETIME,@pmTipoReg INT,@pmAnulado BIT,@pmObservacion VARCHAR(1000),@pmCritFiltros VARCHAR(250),@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomDotacion (IdCia,Numero,Fecha,TipoDotac,TipoEntrega,PlazoEntrega,FecVence,TipoReg,Anulado,FechaCrea,IdUsuario,Observacion,CritFiltros)
 VALUES (@pmIdCia,@pmNumero,@pmFecha,@pmTipoDotac,@pmTipoEntrega,@pmPlazoEntrega,@pmFecVence,@pmTipoReg,@pmAnulado,@pmFechaCrea,@pmIdUsuario,@pmObservacion,@pmCritFiltros)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpNomDotacion] @pmIdCia CHAR(2),@pmNumero INT,@pmFecha SMALLDATETIME,@pmTipoDotac VARCHAR(10),@pmTipoEntrega INT,@pmPlazoEntrega INT
,@pmFecVence SMALLDATETIME,@pmTipoReg INT,@pmAnulado BIT,@pmObservacion VARCHAR(1000),@pmCritFiltros VARCHAR(250)
 AS 
 UPDATE Trn_NomDotacion SET Fecha=@pmFecha,TipoDotac=@pmTipoDotac,TipoEntrega=@pmTipoEntrega,PlazoEntrega=@pmPlazoEntrega,FecVence=@pmFecVence,TipoReg=@pmTipoReg
,Anulado=@pmAnulado,Observacion=@pmObservacion,CritFiltros=@pmCritFiltros
 WHERE IdCia=@pmIdCia AND Numero=@pmNumero
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNomDotacion] @pmIdCia CHAR(2),@pmNumero INT
 AS 
 SELECT IdCia,Numero,Fecha,TipoDotac,TipoEntrega,PlazoEntrega,FecVence,TipoReg,Anulado,FechaCrea,IdUsuario,Observacion,CritFiltros
 FROM Trn_NomDotacion
 WHERE IdCia=@pmIdCia AND Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TarifZfe,RngFecha,FecInicial,FecFinal,LtaPre,Inactivo,FecUpdate)
 VALUES (@pmIdProducto,@pmIdClase,@pmItem,@pmTarifa,@pmSimbTfa,@pmCdTarifa,@pmTarifZfe,@pmRngFecha,@pmFecInicial,@pmFecFinal,@pmLtaPre,@pmInactivo,@pmFecUpdate)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEmp_CentrosLta] @pmNContrato INT
 AS 
 SELECT EC.NContrato AS NumContrato,C.IdEmpleado AS IdEmplado,Apellidos,Nombres,FecIngreso,FecRetiro,Indefinido
,EC.IdCCosto AS CdCCosto,CCosto,CodSubCosto,SubCosto,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
 LEFT JOIN SubCentros AS SC ON EC.CodSubCosto=SC.IdSubCos
 WHERE (EC.NContrato>=ISNULL(@pmNContrato,0)  AND EC.NContrato<=ISNULL(@pmNContrato,2147483647)) 
 ORDER BY EC.NContrato,EC.IdCCosto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsGruposRot] @pmIdGrupo VARCHAR(4),@pmGrupoRot VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO GruposRot (IdGrupo,GrupoRot,Inactivo)
 VALUES (@pmIdGrupo,@pmGrupoRot,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpGruposRot] @pmIdGrupo VARCHAR(4),@pmGrupoRot VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE GruposRot SET GrupoRot=@pmGrupoRot,Inactivo=@pmInactivo
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelGruposRot] @pmIdGrupo VARCHAR(4)
 AS 
 DELETE FROM GruposRot
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryRutasTramos] @pmIdRuta VARCHAR(4),@pmNumTramo INT
 AS 
 SELECT IdRuta,NumTramo,IdLocal,kmstramo,kmstotal
 FROM RutasTramos
 WHERE IdRuta=@pmIdRuta AND NumTramo=@pmNumTramo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraPorteInt] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecEmision SMALLDATETIME,@pmFecCargue SMALLDATETIME,@pmFecEntrega SMALLDATETIME,@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmIdNotificado VARCHAR(16),@pmNotificado VARCHAR(150),@pmIdRemitente VARCHAR(16),@pmRemitente VARCHAR(150),@pmDirOrigen VARCHAR(250)
,@pmIdDestinatario VARCHAR(16),@pmDestinatario VARCHAR(150),@pmDirDestino VARCHAR(250),@pmIdConsig VARCHAR(16),@pmConsignatario VARCHAR(150),@pmDirConsig VARCHAR(250),@pmInsTransporte VARCHAR(1000),@pmDocRemitente VARCHAR(500),@pmIdMoneda VARCHAR(5),@pmValorTotal MONEY,@pmPesoTotal DECIMAL(14,4),@pmManifiesto INT,@pmCdCiaManif CHAR(2),@pmAnulado BIT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(2500),@pmIdEstado VARCHAR(4),@pmNegociacion VARCHAR(1000),@pmCondic_Pago VARCHAR(1000),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraPorteInt (TipDoc,NumOrden,IdCia,Fecha,FecEmision,FecCargue,FecEntrega,IdOrigen,IdDestino,IdNotificado,Notificado,IdRemitente,Remitente,DirOrigen,IdDestinatario,Destinatario,DirDestino,IdConsig,Consignatario,DirConsig,InsTransporte,DocRemitente,IdMoneda,ValorTotal,PesoTotal,Manifiesto,CdCiaManif,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,Negociacion,Condic_Pago)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmFecha,@pmFecEmision,@pmFecCargue,@pmFecEntrega,@pmIdOrigen,@pmIdDestino,@pmIdNotificado,@pmNotificado,@pmIdRemitente,@pmRemitente,@pmDirOrigen,@pmIdDestinatario,@pmDestinatario,@pmDirDestino,@pmIdConsig,@pmConsignatario,@pmDirConsig,@pmInsTransporte,@pmDocRemitente,@pmIdMoneda,@pmValorTotal,@pmPesoTotal,@pmManifiesto,@pmCdCiaManif,@pmOrigenAdd
,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmNegociacion,@pmCondic_Pago)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraPorteInt] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraPorteInt
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraPorteInt] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecEmision SMALLDATETIME,@pmFecCargue SMALLDATETIME,@pmFecEntrega SMALLDATETIME,@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmIdNotificado VARCHAR(16),@pmNotificado VARCHAR(150),@pmIdRemitente VARCHAR(16),@pmRemitente VARCHAR(150),@pmDirOrigen VARCHAR(250)
,@pmIdDestinatario VARCHAR(16),@pmDestinatario VARCHAR(150),@pmDirDestino VARCHAR(250),@pmIdConsig VARCHAR(16),@pmConsignatario VARCHAR(150),@pmDirConsig VARCHAR(250),@pmInsTransporte VARCHAR(1000),@pmDocRemitente VARCHAR(500),@pmIdMoneda VARCHAR(5),@pmValorTotal MONEY,@pmPesoTotal DECIMAL(14,4),@pmManifiesto INT,@pmCdCiaManif CHAR(2),@pmAnulado BIT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(2500),@pmIdEstado VARCHAR(4),@pmNegociacion VARCHAR(1000),@pmCondic_Pago VARCHAR(1000),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraPorteInt SET Fecha=@pmFecha,FecEmision=@pmFecEmision,FecCargue=@pmFecCargue,FecEntrega=@pmFecEntrega,IdOrigen=@pmIdOrigen,IdDestino=@pmIdDestino,IdNotificado=@pmIdNotificado,Notificado=@pmNotificado,IdRemitente=@pmIdRemitente,Remitente=@pmRemitente,DirOrigen=@pmDirOrigen,IdDestinatario=@pmIdDestinatario,Destinatario=@pmDestinatario,DirDestino=@pmDirDestino,IdConsig=@pmIdConsig
,Consignatario=@pmConsignatario,DirConsig=@pmDirConsig,InsTransporte=@pmInsTransporte,DocRemitente=@pmDocRemitente,IdMoneda=@pmIdMoneda,ValorTotal=@pmValorTotal,PesoTotal=@pmPesoTotal,Manifiesto=@pmManifiesto,CdCiaManif=@pmCdCiaManif,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,Negociacion=@pmNegociacion,Condic_Pago=@pmCondic_Pago
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraPorteInt] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumOrden,IdCia,Fecha,FecEmision,FecCargue,FecEntrega,IdOrigen,IdDestino,IdNotificado,Notificado,IdRemitente,Remitente,DirOrigen,IdDestinatario,Destinatario,DirDestino,IdConsig,Consignatario,DirConsig,InsTransporte,DocRemitente
,IdMoneda,ValorTotal,PesoTotal,Manifiesto,CdCiaManif,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,Negociacion,Condic_Pago,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraPorteInt
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_cheqItm] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_cheq
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_cheqTot] @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS STOTCAN,SUM(tmVrForma) AS STOTCHE
 FROM tm_cheq
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_TraConc] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmDescripcion VARCHAR(250),@pmtmTarifa DECIMAL(14,4),@pmtmTipoConc VARCHAR(10),@pmtmRubro VARCHAR(20)
,@pmtmCantidad DECIMAL(14,4),@pmtmVrUnitario MONEY,@pmtmTarifIva DECIMAL(14,4),@pmtmIdConcepto VARCHAR(4),@pmtmCdCuenta VARCHAR(16),@pmtmNitTercero VARCHAR(16),@pmtmVrBase MONEY
,@pmtmTipoTarif CHAR(1),@pmtmRefConc VARCHAR(50),@pmtmFijos BIT,@pmtmBaseRet INT
 AS 
 INSERT INTO tm_TraConc (tmNumero,tmItem,tmDescripcion,tmTarifa,tmTipoConc,tmRubro,tmCantidad,tmVrUnitario,tmTarifIva,tmIdConcepto,tmCdCuenta,tmNitTercero,tmVrBase,tmTipoTarif,tmRefConc,tmFijos,tmBaseRet)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmDescripcion,@pmtmTarifa,@pmtmTipoConc,@pmtmRubro,@pmtmCantidad,@pmtmVrUnitario,@pmtmTarifIva,@pmtmIdConcepto,@pmtmCdCuenta
,@pmtmNitTercero,@pmtmVrBase,@pmtmTipoTarif,@pmtmRefConc,@pmtmFijos,@pmtmBaseRet)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsClaseBan] @pmIdClase VARCHAR(4),@pmClaseBanco VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ClaseBan (IdClase,ClaseBanco,Inactivo)
 VALUES (@pmIdClase,@pmClaseBanco,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpClaseBan] @pmIdClase VARCHAR(4),@pmClaseBanco VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ClaseBan SET ClaseBanco=@pmClaseBanco,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelClaseBan] @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseBan
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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)
,@pmtmCantDev DECIMAL(14,4),@pmtmCantFact DECIMAL(14,4),@pmtmCdOperario VARCHAR(16),@pmtmComsOper DECIMAL(14,4)
 AS 
 INSERT INTO tm_ForItems (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCantidad,tmVrPrecio,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmVrBruto,tmDescripcion,tmServcios,tmListaPrec,tmCantDev,tmCantFact,tmCdOperario,tmComsOper)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdProducto,@pmtmIdBodega,@pmtmCantidad,@pmtmVrPrecio,@pmtmTarifaIva,@pmtmVrIva,@pmtmTarifaDct,@pmtmVrDcto,@pmtmVrBruto,@pmtmDescripcion,@pmtmServcios,@pmtmListaPrec,@pmtmCantDev,@pmtmCantFact,@pmtmCdOperario,@pmtmComsOper)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,SUM(tmCantDev) AS SCANTDEV,SUM(tmCantFact) AS SCANTFACT
 FROM tm_ForItems
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_ForItemsItm] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM 
 FROM tm_ForItems
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_ForItems] @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCantidad,tmVrPrecio,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto
,tmVrBruto,tmDescripcion,tmServcios,tmListaPrec,tmCantDev,tmCantFact,tmCdOperario,tmComsOper
 FROM tm_ForItems
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposCot] @pmIdCot VARCHAR(4)
 AS 
 DELETE FROM TiposCot
 WHERE IdCot=@pmIdCot
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTiposCotDso]
 AS 
  SELECT IdCot,Cotizante,IdCot+ ' '+Cotizante AS DsCot
  FROM TiposCot
  WHERE Inactivo=0
  ORDER BY IdCot
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposCot] @pmIdCot VARCHAR(4),@pmCotizante VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposCot SET Cotizante=@pmCotizante,Inactivo=@pmInactivo
 WHERE IdCot=@pmIdCot
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposCot] @pmIdCot VARCHAR(4),@pmCotizante VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposCot (IdCot,Cotizante,Inactivo)
 VALUES (@pmIdCot,@pmCotizante,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmTdOrden VARCHAR(3),@pmCdCiaOrden CHAR(2),@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,TdOrden=@pmTdOrden,CdCiaOrden=@pmCdCiaOrden
,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Traslado=@pmTraslado AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmTdOrden VARCHAR(3),@pmCdCiaOrden CHAR(2),@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,TdOrden,CdCiaOrden)
 VALUES (@pmTipDoc,@pmTraslado,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdTercero,@pmVrSubTotal,@pmCantidad,@pmNumDcmto,@pmNumOrden,@pmIdCCosto,@pmIdSubCos,@pmModalidad,@pmOrigenAdd
,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmTdOrden,@pmCdCiaOrden)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraslados] @pmTipDoc VARCHAR(3),@pmTraslado INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Traslado,IdCia,Fecha,IdConcepto,IdTercero,VrSubTotal,Cantidad,NumDcmto,NumOrden,IdCCosto,IdSubCos,Modalidad
,TdOrden,CdCiaOrden,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Traslados
 WHERE TipDoc=@pmTipDoc AND Traslado=@pmTraslado AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpNiifComp] @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 INT,@pmCedCondtor VARCHAR(16),@pmCodConce VARCHAR(4),@pmBeneficiario VARCHAR(250),@pmVrDivisa1 DECIMAL(16,6),@pmVrDivisa2 DECIMAL(16,6),@pmVrDivisa3 DECIMAL(16,6)
,@pmPermEditar BIT,@pmIntegrado BIT,@pmEsEgreso BIT,@pmAnticipo BIT,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4),@pmTipoNiif VARCHAR(10),@pmCdSustento VARCHAR(4),@pmTipo_Pago VARCHAR(10),@pmEstablec VARCHAR(20),@pmPEmision VARCHAR(20),@pmAutorizac VARCHAR(50),@pmFechaAut SMALLDATETIME
,@pmCtbTipo VARCHAR(3),@pmCtbNumero INT,@pmFisTipo VARCHAR(3),@pmFisNumero INT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NiifComp 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,VrDivisa1=@pmVrDivisa1,VrDivisa2=@pmVrDivisa2,VrDivisa3=@pmVrDivisa3,PermEditar=@pmPermEditar,Integrado=@pmIntegrado,EsEgreso=@pmEsEgreso,Anticipo=@pmAnticipo,Observacion=@pmObservacion
,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,TipoNiif=@pmTipoNiif,CdSustento=@pmCdSustento,Tipo_Pago=@pmTipo_Pago,Establec=@pmEstablec,PEmision=@pmPEmision,Autorizac=@pmAutorizac,FechaAut=@pmFechaAut,CtbTipo=@pmCtbTipo,CtbNumero=@pmCtbNumero,FisTipo=@pmFisTipo,FisNumero=@pmFisNumero
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelNiifComp] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_NiifComp
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsNiifComp] @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 INT,@pmCedCondtor VARCHAR(16),@pmCodConce VARCHAR(4),@pmBeneficiario VARCHAR(250),@pmVrDivisa1 DECIMAL(16,6),@pmVrDivisa2 DECIMAL(16,6),@pmVrDivisa3 DECIMAL(16,6)
,@pmPermEditar BIT,@pmIntegrado BIT,@pmEsEgreso BIT,@pmAnticipo BIT,@pmObservacion VARCHAR(1000),@pmIdEstado VARCHAR(4),@pmTipoNiif VARCHAR(10),@pmCdSustento VARCHAR(4),@pmTipo_Pago VARCHAR(10),@pmEstablec VARCHAR(20),@pmPEmision VARCHAR(20),@pmAutorizac VARCHAR(50),@pmFechaAut SMALLDATETIME
,@pmCtbTipo VARCHAR(3),@pmCtbNumero INT,@pmFisTipo VARCHAR(3),@pmFisNumero INT,@pmPropddes VARCHAR(50),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NiifComp (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,IdCiaCrea,IdUsuario,TipoNiif,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut,CtbTipo,CtbNumero,FisTipo,FisNumero)
 VALUES (@pmTipCom,@pmComprobante,@pmIdCia,@pmFecha,@pmIdTercero,@pmVrTotal,@pmIdCta,@pmEnEfectivo,@pmNumCheque,@pmFecCheque,@pmTipDoc,@pmDocumento,@pmIdCiaDoc,@pmAnulado,@pmNumDev,@pmFecDev,@pmTipEgr,@pmNEgreso,@pmpVehiculo,@pmVehPropio,@pmCedCondtor,@pmCodConce,@pmBeneficiario
,@pmVrDivisa1,@pmVrDivisa2,@pmVrDivisa3,@pmPermEditar,@pmIntegrado,@pmEsEgreso,@pmAnticipo,@pmObservacion,@pmIdEstado,@pmPropddes,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmTipoNiif,@pmCdSustento,@pmTipo_Pago,@pmEstablec,@pmPEmision,@pmAutorizac,@pmFechaAut,@pmCtbTipo,@pmCtbNumero,@pmFisTipo,@pmFisNumero)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNiifComp] @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,TipoNiif,CdSustento,Tipo_Pago,Establec,PEmision,Autorizac,FechaAut,CtbTipo,CtbNumero,FisTipo,FisNumero
 FROM Trn_NiifComp
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpNiifCompAnu] @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2), @pmAnulado BIT, @pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmIdEstado VARCHAR(4),@pmPermEditar BIT,@pmObservacion VARCHAR(1000)
AS
 UPDATE Trn_NiifComp SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,IdEstado=@pmIdEstado
,Observacion=@pmObservacion,PermEditar=@pmPermEditar
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[paInsFijos_Cue] @pmIdCargo VARCHAR(4), @pmIdAdmon VARCHAR(4),@pmIdCuentaDeb VARCHAR(16), @pmIdCuentaCre VARCHAR(16)
, @pmIdCueDebVp VARCHAR(16),@pmIdCueCreVp VARCHAR(16),@pmIdCuentaIva VARCHAR(16),@pmIdCuentaRet VARCHAR(16),@pmIdCuentaIca VARCHAR(16)
AS
 INSERT INTO Fijos_Cue (IdCargo,IdAdmon,IdCuentaDeb,IdCuentaCre,IdCueDebVp,IdCueCreVp,IdCuentaIva,IdCuentaRet,IdCuentaIca)
 VALUES (@pmIdCargo,@pmIdAdmon,@pmIdCuentaDeb,@pmIdCuentaCre,@pmIdCueDebVp,@pmIdCueCreVp,@pmIdCuentaIva,@pmIdCuentaRet,@pmIdCuentaIca)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE  [dbo].[paDelFijos_Cue]  @pmIdCargo VARCHAR(4), @pmIdAdmon VARCHAR(4)
AS
  DELETE FROM  Fijos_Cue
 WHERE   IdCargo=@pmIdCargo AND  IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryFijos_Cue] @pmIdCargo VARCHAR(4),@pmIdAdmon VARCHAR(4)
 AS 
 SELECT IdCargo,IdAdmon,IdCuentaDeb,IdCuentaCre,IdCueDebVp,IdCueCreVp,IdCuentaIva,IdCuentaRet,IdCuentaIca
 FROM Fijos_Cue
 WHERE IdCargo=@pmIdCargo AND IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[paUpFijos_Cue]  @pmIdCargo VARCHAR(4), @pmIdAdmon VARCHAR(4),@pmIdCuentaDeb VARCHAR(16), @pmIdCuentaCre VARCHAR(16)
,@pmIdCueDebVp VARCHAR(16),@pmIdCueCreVp VARCHAR(16),@pmIdCuentaIva VARCHAR(16),@pmIdCuentaRet VARCHAR(16),@pmIdCuentaIca VARCHAR(16)
AS
 UPDATE Fijos_Cue SET IdCuentaDeb=@pmIdCuentaDeb,IdCuentaCre=@pmIdCuentaCre,IdCueDebVp=@pmIdCueDebVp,IdCueCreVp=@pmIdCueCreVp
,IdCuentaIva=@pmIdCuentaIva,IdCuentaRet=@pmIdCuentaRet,IdCuentaIca=@pmIdCuentaIca
 WHERE IdCargo=@pmIdCargo AND IdAdmon=@pmIdAdmon
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTasasIntmoraDso] 
 AS 
 SELECT IdMora,DescMora,IdMora+' '+DescMora+' ( '+TipoInteres+'  '+STR(TasaEM,7,4)+ ')' AS DsTasa 
 FROM TasasIntmora
 WHERE Inactivo=0 
 ORDER BY IdMora
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelClaseTar] @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseTar
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpClaseTar] @pmIdClase VARCHAR(4),@pmNomClase VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ClaseTar SET NomClase=@pmNomClase,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsClaseTar] @pmIdClase VARCHAR(4),@pmNomClase VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ClaseTar (IdClase,NomClase,Inactivo)
 VALUES (@pmIdClase,@pmNomClase,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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','MGC','MGN','CSE','INC','BIM','BIC','ICO','BIP','IVP','BPC')
  ORDER BY NomClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,ComIndustrial,NumLista,IdEstado,Inactivo,Termicas,CodRetCom,CodCCosto,CodSubCosto,CdDiaEnt,AutoIca
,FechaAdd,FechaUpdate,IdUsuario
 FROM TercCliente
 WHERE IdClie=@pmIdClie
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmComIndustrial INT
,@pmNumLista CHAR(1),@pmTermicas BIT,@pmCodRetCom VARCHAR(4),@pmCodCCosto VARCHAR(16),@pmCodSubCosto VARCHAR(16),@pmCdDiaEnt VARCHAR(4),@pmAutoIca BIT,@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,ComIndustrial=@pmComIndustrial,NumLista=@pmNumLista,Termicas=@pmTermicas,CodRetCom=@pmCodRetCom,CodCCosto=@pmCodCCosto,CodSubCosto=@pmCodSubCosto,CdDiaEnt=@pmCdDiaEnt,AutoIca=@pmAutoIca
 WHERE IdClie=@pmIdClie
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmComIndustrial INT,@pmNumLista CHAR(1),@pmTermicas BIT,@pmCodRetCom VARCHAR(4),@pmCodCCosto VARCHAR(16),@pmCodSubCosto VARCHAR(16),@pmCdDiaEnt VARCHAR(4),@pmAutoIca BIT,@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,ComIndustrial,NumLista,Termicas,CodRetCom,CodCCosto,CodSubCosto,CdDiaEnt,AutoIca)
 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,@pmComIndustrial,@pmNumLista,@pmTermicas,@pmCodRetCom,@pmCodCCosto,@pmCodSubCosto,@pmCdDiaEnt,@pmAutoIca)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,ComIndustrial,NumLista,Termicas,CodRetCom,CodCCosto,CodSubCosto,CdDiaEnt,AutoIca)
  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,ComIndustrial,NumLista,Termicas,CodRetCom,CodCCosto,CodSubCosto,CdDiaEnt,AutoIca
  FROM  TercCliente 
  WHERE IdClie=@pmIdClie
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpmonedasLoc] @pmMneLocal BIT,@pmIdMneda VARCHAR(5)
 AS 
 UPDATE adm_monedas SET MneLocal=@pmMneLocal
 WHERE IdMneda<>@pmIdMneda
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrymonedasLoc]
 AS 
 SELECT IdMneda,Mneda,Simbolo,MneLocal,VrCambio,Inactivo
 FROM adm_monedas
 WHERE Inactivo=0 AND MneLocal<>0
 ORDER BY  IdMneda
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpmonedasTas] @pmIdMneda VARCHAR(5),@pmVrCambio DECIMAL(12,6)
 AS 
 UPDATE adm_monedas SET VrCambio=@pmVrCambio
 WHERE IdMneda=@pmIdMneda
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrymonedasDso]
 AS 
 SELECT IdMneda,Mneda,Mneda+' ('+Simbolo +') '+ IdMneda AS DsMon
 FROM adm_monedas
 WHERE  Inactivo=0
 ORDER BY Mneda
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposFuel] @pmIdCom VARCHAR(4),@pmTipoComb VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposFuel SET TipoComb=@pmTipoComb,Inactivo=@pmInactivo
 WHERE IdCom=@pmIdCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposFuel] @pmIdCom VARCHAR(4)
 AS 
 DELETE FROM TiposFuel
 WHERE IdCom=@pmIdCom
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposFuel] @pmIdCom VARCHAR(4),@pmTipoComb VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposFuel (IdCom,TipoComb,Inactivo)
 VALUES (@pmIdCom,@pmTipoComb,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraOrdenDet] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdMercancia VARCHAR(16),@pmDescripcion VARCHAR(250)
,@pmCantidad DECIMAL(14,4),@pmUndMed VARCHAR(10),@pmVrUnitario MONEY,@pmCantCump DECIMAL(14,4),@pmTarifaCump MONEY,@pmDocCliente VARCHAR(30)
,@pmTarifaPago MONEY,@pmPagoCump MONEY
 AS 
 INSERT INTO Trn_TraOrdenDet (TipDoc,NumOrden,IdCia,Item,IdMercancia,Descripcion,Cantidad,UndMed,VrUnitario,TarifaPago,CantCump,TarifaCump,PagoCump,DocCliente)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmItem,@pmIdMercancia,@pmDescripcion,@pmCantidad,@pmUndMed,@pmVrUnitario,@pmTarifaPago,@pmCantCump,@pmTarifaCump,@pmPagoCump,@pmDocCliente)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_DetCueItm] @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM FROM tm_DetCue
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,tmTarifa,tmTipoTarif,tmCodConc,tmCodTarif
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmtmTarifa DECIMAL(14,4),@pmtmTipoTarif CHAR(1),@pmtmCodConc VARCHAR(4),@pmtmCodTarif VARCHAR(4)
 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,tmTarifa,tmTipoTarif,tmCodConc,tmCodTarif)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmConcepto,@pmtmIdCuenta,@pmtmValor,@pmtmDetalle,@pmtmIdTercero,@pmtmCodAgncia,@pmtmCdCCosto,@pmtmCdSubCos,@pmtmpVehiculo,@pmtmVrBase,@pmtmTipFac,@pmtmFactura,@pmtmIdCiaFac
,@pmtmItemFac,@pmtmFecVence,@pmtmCheque,@pmtmCiuOrigen,@pmtmReferencia,@pmtmTarifa,@pmtmTipoTarif,@pmtmCodConc,@pmtmCodTarif)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@pmTipoTrans INT,@pmTipoOrden VARCHAR(3),@pmTipoModifica VARCHAR(10),@pmNumCotizac INT,@pmCdCiaCotizac CHAR(2),@pmNumAutSicom VARCHAR(50),@pmVrImpCarbono MONEY,@pmBaseIvaIgp MONEY,@pmVrIvaIngProd MONEY,@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,NumCotizac=@pmNumCotizac,CdCiaCotizac=@pmCdCiaCotizac,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,TipoModifica=@pmTipoModifica,IdEstado=@pmIdEstado
,ZonaFrontera=@pmZonaFrontera,TipoTrans=@pmTipoTrans,TipoOrden=@pmTipoOrden, FecUpdate=@pmFecUpdate,NumAutSicom=@pmNumAutSicom,VrImpCarbono=@pmVrImpCarbono,BaseIvaIgp=@pmBaseIvaIgp,VrIvaIngProd=@pmVrIvaIngProd
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrIvaIngProd,VrNeto,Cantidad,BaseIvaIgp,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,NumCotizac,CdCiaCotizac,OrigenAdd,Anulado
,FecDev,ZonaFrontera,TipoTrans,TipoOrden,TipoModifica,NumAutSicom,VrImpCarbono,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Opedido
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 INT,@pmTipoTrans INT,@pmTipoOrden VARCHAR(3),@pmTipoModifica VARCHAR(10),@pmNumCotizac INT,@pmCdCiaCotizac CHAR(2),@pmNumAutSicom VARCHAR(50),@pmVrImpCarbono MONEY,@pmBaseIvaIgp MONEY,@pmVrIvaIngProd MONEY,@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,NumCotizac,CdCiaCotizac,OrigenAdd,ZonaFrontera,TipoTrans,TipoOrden,TipoModifica,Anulado,FecDev,Observacion,IdEstado
,TimeSys,IdCiaCrea,IdUsuario,NumAutSicom,VrImpCarbono,BaseIvaIgp,VrIvaIngProd)
 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,@pmNumCotizac,@pmCdCiaCotizac,@pmOrigenAdd,@pmZonaFrontera,@pmTipoTrans,@pmTipoOrden,@pmTipoModifica,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmNumAutSicom,@pmVrImpCarbono,@pmBaseIvaIgp,@pmVrIvaIngProd)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPuntProgItm] 
 AS 
 SELECT MAX(IdProgrma) AS MXITM  FROM PuntProg
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPuntProg] @pmIdProgrma INT
 AS 
 DELETE FROM PuntProg
 WHERE IdProgrma=@pmIdProgrma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPuntProg] @pmIdProgrma INT
 AS 
 SELECT IdProgrma,FecInicial,FecFinal,MesesInact,MesesVcmto,Observacion,Anulado,TimeSys,FecUpdate,IdUsuario
 FROM PuntProg
 WHERE IdProgrma=@pmIdProgrma
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryCentroCostoCia] @pmCodCias VARCHAR(2)
 AS 
  SELECT IdCCosto,CCosto,CCosto+' '+IdCCosto AS DsCco
  FROM CentroCosto
  WHERE (ISNULL(CodCias,'') LIKE '%'+@pmCodCias+'%' OR LEN(ISNULL(CodCias,''))=0 OR ISNULL(CodCias,'')='0')
  AND Inactivo=0
  ORDER BY CCosto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelCentroCosto] @pmIdCCosto VARCHAR(16)
 AS 
 DELETE FROM CentroCosto
 WHERE IdCCosto=@pmIdCCosto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryCentroCosto] @pmIdCCosto VARCHAR(16)
 AS 
 IF @pmIdCCosto IS NULL
  SELECT IdCCosto,CCosto,CodCias,FechaAdd,FechaUpdate 
  FROM CentroCosto
  WHERE Inactivo=0
  ORDER BY CCosto
ELSE
  SELECT IdCCosto,CCosto,CodCias,FechaAdd,FechaUpdate,Inactivo 
  FROM CentroCosto
  WHERE IdCCosto=@pmIdCCosto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsCentroCosto] @pmIdCCosto VARCHAR(16),@pmCCosto VARCHAR(100),@pmCodCias VARCHAR(500),@pmFechaAdd SMALLDATETIME
,@pmInactivo BIT
 AS 
 INSERT INTO CentroCosto (IdCCosto,CCosto,FechaAdd,Inactivo,CodCias)
 VALUES (@pmIdCCosto,@pmCCosto,@pmFechaAdd,@pmInactivo,@pmCodCias)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpCentroCosto] @pmIdCCosto VARCHAR(16),@pmCCosto VARCHAR(100),@pmCodCias VARCHAR(500)
,@pmFechaUpdate SMALLDATETIME,@pmInactivo BIT
 AS 
 UPDATE CentroCosto SET CCosto=@pmCCosto,FechaUpdate=@pmFechaUpdate,Inactivo=@pmInactivo,CodCias=@pmCodCias
 WHERE IdCCosto=@pmIdCCosto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmtmTipoAfi INT
 AS 
 INSERT INTO tm_EstadoGyp (tmEst,tmIdCuenta,tmItem,tmIdCia,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmCodAgncia,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2,tmTipoAfi)
 VALUES (@pmtmEst,@pmtmIdCuenta,@pmtmItem,@pmtmIdCia,@pmtmIdTercero,@pmtmIdVehiculo,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmCodAgncia,@pmtmTipo,@pmtmSaldoAnterior,@pmtmTotalDebitos,@pmtmTotalCreditos,@pmtmNuevoSaldo
,@pmtmSalAnt2,@pmtmDebitos2,@pmtmCreditos2,@pmtmSaldo2,@pmtmTipoAfi)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_EstadoGypGcue] @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCuenta,COUNT(tmItem) AS SCANT
 FROM tm_EstadoGyp
 WHERE tmEst=@pmtmEst 
 GROUP BY tmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmTipoAfi
 FROM tm_EstadoGyp
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
 AND tmTipo LIKE ISNULL(@pmtmTipo,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_EstadoGyp] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_EstadoGyp
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmtmTipoAfi INT=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,'%')
 AND (tmTipoAfi>=ISNULL(@pmtmTipoAfi,-1) AND tmTipoAfi<=ISNULL(@pmtmTipoAfi,9))
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelMarcasLin] @pmIdLinea VARCHAR(10)
 AS 
 DELETE FROM MarcasLin
 WHERE IdLinea=@pmIdLinea
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMarcasLin] @pmIdLinea VARCHAR(10)
 AS 
 SELECT IdLinea,LineaVeh,IdMarca,CodMT,Inactivo
 FROM MarcasLin
 WHERE IdLinea=@pmIdLinea
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTpapers]
AS
 SELECT Idpapel,Nombre,STR(Idpapel,3,0)+'  '+Nombre AS DsPap FROM Sys_Tpapers ORDER BY IdPapel
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmTarifaRet DECIMAL(14,4),@pmVrRetFte MONEY
 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,TarifaRet,VrRetFte)
 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,@pmTarifaRet,@pmVrRetFte)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasVeh_Unc]  @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuFisCuentasVeh WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
   INSERT INTO AcuFisCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
   SELECT nAnno,nMes,IdCia,@pmNewCuenta,IdVehiculo,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
   FROM AcuFisCuentasVeh WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
   GROUP BY nAnno,nMes,IdCia,IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasVeh_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuFisCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdVehiculo,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuFisCuentasVeh AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt 
 AND NOT EXISTS (SELECT * FROM AcuFisCuentasVeh AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes  
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdVehiculo=S.IdVehiculo)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuFisCuentasVehSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuFisCuentasVeh SET AcuFisCuentasVeh.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuFisCuentasVeh INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuFisCuentasVeh WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt) AS SA ON AcuFisCuentasVeh.IdCia=SA.IdCia AND AcuFisCuentasVeh.IdCuenta=SA.IdCuenta
 AND AcuFisCuentasVeh.IdVehiculo=SA.IdVehiculo
 WHERE AcuFisCuentasVeh.nAnno=@pmnAnno AND AcuFisCuentasVeh.nMes=@pmnMes
 AND (@pmIdCia IS NULL OR AcuFisCuentasVeh.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuFisCuentasVeh.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasVeh] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdVehiculo VARCHAR(10),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuFisCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdVehiculo,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasVeh_Uni] @pmIdVehiculo VARCHAR(10),@pmIdVehiculoDos VARCHAR(10),@pmNewVehiculo VARCHAR(10)
 AS 
 IF EXISTS (SELECT * FROM AcuFisCuentasVeh WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos)
   INSERT INTO AcuFisCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
   SELECT nAnno,nMes,IdCia,IdCuenta,@pmNewVehiculo,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
   FROM AcuFisCuentasVeh WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos
   GROUP BY nAnno,nMes,IdCia,IdCuenta
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraDevCos] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCiaDoc CHAR(2)
,@pmFecDoc SMALLDATETIME,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250)
 AS 
 UPDATE Trn_TraDevCos SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Cumplido=@pmCumplido,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,ModdDev=@pmModdDev
,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevCosDoc] @pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Cumplido,IdCiaDoc,FecDoc,ModdDev
 FROM Trn_TraDevCos
 WHERE TipDoc=@pmTipDoc AND Cumplido=@pmCumplido AND IdCiaDoc=@pmIdCiaDoc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevCos] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Cumplido,IdCiaDoc,FecDoc,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,TimeSys,IdCiaCrea,IdUsuario
 FROM Trn_TraDevCos
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraDevCos] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmCumplido INT,@pmIdCiaDoc CHAR(2)
,@pmFecDoc SMALLDATETIME,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraDevCos (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Cumplido,IdCiaDoc,FecDoc,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmCumplido,@pmIdCiaDoc,@pmFecDoc,@pmModdDev,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraDevCos] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraDevCos WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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)
,@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY,@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,TarifaArf,VrAutRetFte,TarifaAri,VrAutRetIca)
 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,@pmTarifaArf,@pmVrAutRetFte,@pmTarifaAri,@pmVrAutRetIca)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TarifaArf,VrAutRetFte,TarifaAri,VrAutRetIca
,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY
 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,TarifaArf=@pmTarifaArf,VrAutRetFte=@pmVrAutRetFte,TarifaAri=@pmTarifaAri,VrAutRetIca=@pmVrAutRetIca
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasNit_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuNiifCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdTercero,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuNiifCuentasNit AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt 
 AND NOT EXISTS (SELECT * FROM AcuNiifCuentasNit AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes  
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdTercero=S.IdTercero)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasNit_Uni] @pmIdTercero VARCHAR(16),@pmIdTerceroDos VARCHAR(16),@pmNewTercero VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdTercero FROM AcuNiifCuentasNit WHERE IdTercero=@pmIdTercero OR IdTercero=@pmIdTerceroDos)
  INSERT INTO AcuNiifCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT nAnno,nMes,IdCia,IdCuenta,@pmNewTercero,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  FROM AcuNiifCuentasNit WHERE IdTercero=@pmIdTercero OR IdTercero=@pmIdTerceroDos
  GROUP BY nAnno,nMes,IdCia,IdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasNit_Unf]  @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuNiifCuentasNit WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
  INSERT INTO AcuNiifCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT nAnno,nMes,IdCia,@pmNewCuenta,IdTercero,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  FROM AcuNiifCuentasNit WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
  GROUP BY nAnno,nMes,IdCia,IdTercero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuNiifCuentasNitSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuNiifCuentasNit SET AcuNiifCuentasNit.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuNiifCuentasNit INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuNiifCuentasNit WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt) AS SA ON AcuNiifCuentasNit.IdCia=SA.IdCia AND AcuNiifCuentasNit.IdCuenta=SA.IdCuenta
 AND AcuNiifCuentasNit.IdTercero=SA.IdTercero
 WHERE AcuNiifCuentasNit.nAnno=@pmnAnno AND AcuNiifCuentasNit.nMes=@pmnMes
 AND (@pmIdCia IS NULL OR AcuNiifCuentasNit.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuNiifCuentasNit.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasNit] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdTercero VARCHAR(16),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuNiifCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdTercero,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryPuestos] @pmIdPto VARCHAR(4)
 AS 
 SELECT IdPto,PtoControl,TagPuesto,IdLocal,Ubicacion,Contacto,Telefono,TelMovil,Referencia,Inactivo
 FROM Puestos
 WHERE IdPto=@pmIdPto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposSustento] @pmIdSustento VARCHAR(4),@pmSustento VARCHAR(150),@pmInactivo BIT
 AS 
 UPDATE TiposSustento SET Sustento=@pmSustento,Inactivo=@pmInactivo
 WHERE IdSustento=@pmIdSustento
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposSustento] @pmIdSustento VARCHAR(4)
 AS 
IF @pmIdSustento IS NULL 
 SELECT IdSustento,Sustento FROM TiposSustento WHERE Inactivo=0 ORDER BY Sustento
ELSE
 SELECT IdSustento,Sustento,Inactivo FROM TiposSustento WHERE IdSustento=@pmIdSustento
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposSustento] @pmIdSustento VARCHAR(4)
 AS 
 DELETE FROM TiposSustento WHERE IdSustento=@pmIdSustento
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposSustento] @pmIdSustento VARCHAR(4),@pmSustento VARCHAR(150),@pmInactivo BIT
 AS 
 INSERT INTO TiposSustento (IdSustento,Sustento,Inactivo)
 VALUES (@pmIdSustento,@pmSustento,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposSustentoDso] 
AS 
SELECT IdSustento,Sustento,IdSustento+' '+Sustento AS DsSus FROM TiposSustento 
WHERE Inactivo=0
ORDER BY IdSustento
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuCuentasNitSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuCuentasNit SET AcuCuentasNit.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuCuentasNit INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuCuentasNit WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt) AS SA ON AcuCuentasNit.IdCia=SA.IdCia AND AcuCuentasNit.IdCuenta=SA.IdCuenta
 AND AcuCuentasNit.IdTercero=SA.IdTercero
 WHERE AcuCuentasNit.nAnno=@pmnAnno AND AcuCuentasNit.nMes=@pmnMes
 AND (@pmIdCia IS NULL OR AcuCuentasNit.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuCuentasNit.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuCuentasNit_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdTercero,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuCuentasNit AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt 
 AND NOT EXISTS (SELECT * FROM AcuCuentasNit AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes  
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdTercero=S.IdTercero)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraRemCntdor] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraRemCntdor
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraRemCntdor] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdTipoCon VARCHAR(4),@pmNumContdor VARCHAR(50),@pmPesoNeto DECIMAL(14,4),@pmIdTipoPre VARCHAR(4)
,@pmNumPrecinto VARCHAR(30),@pmColorPrecinto VARCHAR(20),@pmSellos VARCHAR(150),@pmPeligroso BIT,@pmPerecedero BIT,@pmToxico BIT,@pmInflamable BIT,@pmDifmanejo BIT,@pmFragil BIT,@pmDevCont BIT
,@pmCdLocCont VARCHAR(8),@pmPatioCont VARCHAR(50),@pmObservacion VARCHAR(150),@pmNumSalidaSello INT,@pmCdCiaSalSello CHAR(2),@pmItemSalSello INT,@pmCdProdSalida VARCHAR(16),@pmNumSerialSello VARCHAR(30)
,@pmNumComodato VARCHAR(50),@pmFecVenceCom SMALLDATETIME
 AS 
 INSERT INTO Trn_TraRemCntdor (TipDoc,NumOrden,IdCia,Item,IdTipoCon,NumContdor,PesoNeto,IdTipoPre,NumPrecinto,ColorPrecinto,Sellos,Peligroso,Perecedero,Toxico,Inflamable,Difmanejo,Fragil,DevCont,CdLocCont,PatioCont
,Observacion,NumSalidaSello,CdCiaSalSello,ItemSalSello,CdProdSalida,NumSerialSello,NumComodato,FecVenceCom)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmItem,@pmIdTipoCon,@pmNumContdor,@pmPesoNeto,@pmIdTipoPre,@pmNumPrecinto,@pmColorPrecinto,@pmSellos,@pmPeligroso,@pmPerecedero,@pmToxico,@pmInflamable
,@pmDifmanejo,@pmFragil,@pmDevCont,@pmCdLocCont,@pmPatioCont,@pmObservacion,@pmNumSalidaSello,@pmCdCiaSalSello,@pmItemSalSello,@pmCdProdSalida,@pmNumSerialSello,@pmNumComodato,@pmFecVenceCom)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraRemCntdor] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumOrden,IdCia,Item,IdTipoCon,NumContdor,PesoNeto,IdTipoPre,NumPrecinto,ColorPrecinto,Sellos,Peligroso,Perecedero,Toxico,Inflamable,Difmanejo,Fragil
,DevCont,CdLocCont,PatioCont,Observacion,NumSalidaSello,CdCiaSalSello,ItemSalSello,CdProdSalida,NumSerialSello,NumComodato,FecVenceCom
 FROM Trn_TraRemCntdor
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia=@pmIdCia
 ORDER BY  Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpClaseCta] @pmIdClase VARCHAR(4),@pmClaseCuenta VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ClaseCta SET ClaseCuenta=@pmClaseCuenta,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelClaseCta] @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseCta
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsClaseCta] @pmIdClase VARCHAR(4),@pmClaseCuenta VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ClaseCta (IdClase,ClaseCuenta,Inactivo)
 VALUES (@pmIdClase,@pmClaseCuenta,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelGruposAR] @pmIdGrupo VARCHAR(4)
 AS 
 DELETE FROM GruposAR
 WHERE IdGrupo=@pmIdGrupo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 VARCHAR(5)
,@pmTallaPan VARCHAR(5),@pmTallaZap VARCHAR(5),@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),@pmFecUltParc SMALLDATETIME,@pmDecRenta INT,@pmNivelEduc VARCHAR(4),@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,FecUltParc=@pmFecUltParc,DecRenta=@pmDecRenta,FechaUpdate=@pmFechaUpdate,NivelEduc=@pmNivelEduc
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,FecUltParc,EsCondtor,pVehiculo,Observacion,IdEstado,CdFonAvp,Inactivo,CdLocCue,FechaAdd,IdUsuario,DecRenta,NivelEduc)
 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,FecUltParc,EsCondtor,pVehiculo,Observacion,IdEstado,CdFonAvp,Inactivo,CdLocCue,@pmFechaAdd,@pmIdUsuario,DecRenta,NivelEduc
 FROM  Empleados WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,FecUltParc,NCAnterior,FecIngAnt,FecRetAnt,FotoEmp,FotoLic,FotoFirma,EsCondtor,pVehiculo,DecRenta,Observacion,NivelEduc,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM Empleados
 WHERE  Codigo=@pmCodigo
 ORDER BY IdEmpleado
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 VARCHAR(5),@pmTallaPan VARCHAR(5),@pmTallaZap VARCHAR(5),@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),@pmFecUltParc SMALLDATETIME,@pmDecRenta INT,@pmNivelEduc VARCHAR(4),@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,FecUltParc,FotoEmp,FotoLic,FotoFirma,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo,Observacion,CdFonAvp,IdEstado,Inactivo,CdLocCue,FechaAdd,IdUsuario,DecRenta,NivelEduc)
 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,@pmFecUltParc,@pmFotoEmp,@pmFotoLic
,@pmFotoFirma,@pmNCAnterior,@pmFecIngAnt,@pmFecRetAnt,@pmEsCondtor,@pmpVehiculo,@pmObservacion,@pmCdFonAvp,@pmIdEstado,@pmInactivo,@pmCdLocCue,@pmFechaAdd,@pmIdUsuario,@pmDecRenta,@pmNivelEduc)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,FecUltParc,NCAnterior,FecIngAnt,FecRetAnt,FotoEmp,FotoLic,FotoFirma,EsCondtor,pVehiculo,DecRenta,Observacion,NivelEduc,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM Empleados
 WHERE IdEmpleado=@pmIdEmpleado or Codigo=@pmCodigo
 ORDER BY IdEmpleado
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,FecUltParc,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo,DecRenta,FotoEmp,FotoLic,FotoFirma,Observacion,NivelEduc,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM Empleados
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelReportFmto] @pmTipoRep VARCHAR(10),@pmFormato VARCHAR(20)
 AS 
 DELETE FROM Sys_ReportFmto
 WHERE TipoRep=@pmTipoRep AND Formato=@pmFormato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsFinAlmacenes] @pmIdAlmacen VARCHAR(4),@pmAlmacen VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO FinAlmacenes (IdAlmacen,Almacen,Inactivo)
 VALUES (@pmIdAlmacen,@pmAlmacen,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpFinAlmacenes] @pmIdAlmacen VARCHAR(4),@pmAlmacen VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE FinAlmacenes SET Almacen=@pmAlmacen,Inactivo=@pmInactivo
 WHERE IdAlmacen=@pmIdAlmacen
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelFinAlmacenes] @pmIdAlmacen VARCHAR(4)
 AS 
 DELETE FROM FinAlmacenes
 WHERE IdAlmacen=@pmIdAlmacen
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposLla] @pmIdTlla VARCHAR(4),@pmTipoLlanta VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposLla SET TipoLlanta=@pmTipoLlanta,Inactivo=@pmInactivo
 WHERE IdTlla=@pmIdTlla
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposLla] @pmIdTlla VARCHAR(4),@pmTipoLlanta VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposLla (IdTlla,TipoLlanta,Inactivo)
 VALUES (@pmIdTlla,@pmTipoLlanta,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposLla] @pmIdTlla VARCHAR(4)
 AS 
 DELETE FROM TiposLla
 WHERE IdTlla=@pmIdTlla
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelNomVehiculos] @pmIdVehiculo VARCHAR(10)
 AS 
 DELETE FROM NomVehiculos
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryNomVehiculos] @pmIdVehiculo VARCHAR(10)
 AS 
 SELECT IdVehiculo,NumVeh,ClaseVeh,TipoAdmon,GrupoProp,VehPropio,CedCond,Conductor
 FROM NomVehiculos
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMayEstados] @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM MayEstados
 WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposCarga] @pmIdTipCarga VARCHAR(4),@pmTipoCarga VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposCarga (IdTipCarga,TipoCarga,Inactivo)
 VALUES (@pmIdTipCarga,@pmTipoCarga,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposCarga] @pmIdTipCarga VARCHAR(4),@pmTipoCarga VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposCarga SET TipoCarga=@pmTipoCarga,Inactivo=@pmInactivo
 WHERE IdTipCarga=@pmIdTipCarga
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposCarga] @pmIdTipCarga VARCHAR(4)
 AS 
 DELETE FROM TiposCarga
 WHERE IdTipCarga=@pmIdTipCarga
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_Ingresos] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_Ingresos  WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,BaseIvaIgp,VrIvaIngProd
,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,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,CodTarifBom,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpCarbono,VrImpuCons,VrSobtasaCons
,TipoSA,NumSalAlm,CdCiaSA,TipSop,NumDocSop,CdCiaSop,ImpAviTab,TarifaAvta,CodTarifAvta
 FROM Trn_Entradas
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmVrIvaMvc MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4)
,@pmCodTarifBom VARCHAR(4),@pmVrBomberil MONEY,@pmTarifaBom DECIMAL(14,4),@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY,@pmVrImpCarbono MONEY,@pmTipoSA VARCHAR(3),@pmNumSalAlm INT,@pmCdCiaSA CHAR(2),@pmVrImpuCons MONEY,@pmVrSobtasaCons MONEY,@pmBaseIvaIgp MONEY,@pmVrIvaIngProd MONEY
,@pmTipSop VARCHAR(3),@pmNumDocSop INT,@pmCdCiaSop CHAR(2),@pmImpAviTab MONEY,@pmTarifaAvta DECIMAL(14,4),@pmCodTarifAvta VARCHAR(4),@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,VrIvaMvc=@pmVrIvaMvc,FecUpdate=@pmFecUpdate,VrReteCREE=@pmVrReteCREE,TarifaRtc=@pmTarifaRtc,CodTarRtc=@pmCodTarRtc,CodTarifBom=@pmCodTarifBom,VrBomberil=@pmVrBomberil,TarifaBom=@pmTarifaBom
,BaseIvaObsq=@pmBaseIvaObsq,VrIvaObsequio=@pmVrIvaObsequio,VrImpCarbono=@pmVrImpCarbono,TipoSA=@pmTipoSA,NumSalAlm=@pmNumSalAlm,CdCiaSA=@pmCdCiaSA,VrImpuCons=@pmVrImpuCons,VrSobtasaCons=@pmVrSobtasaCons,BaseIvaIgp=@pmBaseIvaIgp,VrIvaIngProd=@pmVrIvaIngProd,TipSop=@pmTipSop,NumDocSop=@pmNumDocSop,CdCiaSop=@pmCdCiaSop
,ImpAviTab=@pmImpAviTab,TarifaAvta=@pmTarifaAvta,CodTarifAvta=@pmCodTarifAvta
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmVrIvaMvc MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmCodTarifBom VARCHAR(4),@pmVrBomberil MONEY,@pmTarifaBom DECIMAL(14,4)
,@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY,@pmVrImpCarbono MONEY,@pmTipoSA VARCHAR(3),@pmNumSalAlm INT,@pmCdCiaSA CHAR(2),@pmVrImpuCons MONEY,@pmVrSobtasaCons MONEY,@pmBaseIvaIgp MONEY,@pmVrIvaIngProd MONEY,@pmTipSop VARCHAR(3),@pmNumDocSop INT,@pmCdCiaSop CHAR(2),@pmImpAviTab MONEY,@pmTarifaAvta DECIMAL(14,4),@pmCodTarifAvta VARCHAR(4)
,@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,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,CodTarifBom,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpCarbono,TipoSA,NumSalAlm,CdCiaSA,VrImpuCons,VrSobtasaCons,BaseIvaIgp,VrIvaIngProd,TipSop,NumDocSop,CdCiaSop,ImpAviTab,TarifaAvta,CodTarifAvta)
 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,@pmVrIvaMvc,@pmVrReteCREE,@pmTarifaRtc,@pmCodTarRtc,@pmCodTarifBom,@pmVrBomberil,@pmTarifaBom,@pmBaseIvaObsq,@pmVrIvaObsequio,@pmVrImpCarbono,@pmTipoSA,@pmNumSalAlm,@pmCdCiaSA,@pmVrImpuCons,@pmVrSobtasaCons,@pmBaseIvaIgp,@pmVrIvaIngProd,@pmTipSop,@pmNumDocSop,@pmCdCiaSop,@pmImpAviTab,@pmTarifaAvta,@pmCodTarifAvta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTercClieCia] @pmIdClie VARCHAR(16),@pmIdCia CHAR(2)
 AS 
 INSERT INTO TercClieCia (IdClie,IdCia)
 VALUES (@pmIdClie,@pmIdCia)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTercClieCia] @pmIdClie VARCHAR(16),@pmIdCia CHAR(2)
 AS 
 DELETE FROM TercClieCia
 WHERE IdClie=@pmIdClie AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryCnxextnaLta] @pmCnxTipo VARCHAR(10)=Null
 AS 
 SELECT IdReg,CnxTipo,CnxEngine,CnxDso,CnxDbname,CnxAutentic,CnxUid,CnxOwner,CnxObserv
,CnxPpties,CnxDriver,CnxPort,CnxTerminal,CnxPwd
 FROM Sys_Cnxextna
 WHERE  CnxTipo LIKE ISNULL(@pmCnxTipo,'%')
 ORDER BY IdReg
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryCnxextnaTip] @pmCnxTipo VARCHAR(10)
 AS 
 SELECT IdReg,CnxTipo,CnxEngine,CnxDso,CnxDbname,CnxAutentic,CnxUid,CnxPwd,CnxOwner,CnxObserv
,CnxPpties,CnxDriver,CnxPort,CnxTerminal
 FROM Sys_Cnxextna
 WHERE  CnxTipo=@pmCnxTipo
 ORDER BY IdReg
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCnxTerminal VARCHAR(4)
 AS 
 INSERT INTO Sys_Cnxextna (IdReg,CnxTipo,CnxEngine,CnxDso,CnxDbname,CnxAutentic,CnxUid,CnxPwd,CnxOwner,CnxPpties,CnxObserv,CnxDriver,CnxPort,CnxTerminal)
 VALUES (@pmIdReg,@pmCnxTipo,@pmCnxEngine,@pmCnxDso,@pmCnxDbname,@pmCnxAutentic,@pmCnxUid,@pmCnxPwd,@pmCnxOwner,@pmCnxPpties,@pmCnxObserv,@pmCnxDriver,@pmCnxPort,@pmCnxTerminal)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryCnxextna] @pmIdReg INT
 AS 
 SELECT IdReg,CnxTipo,CnxEngine,CnxDso,CnxDbname,CnxAutentic,CnxUid,CnxPwd
,CnxOwner,CnxObserv,CnxPpties,CnxDriver,CnxPort,CnxTerminal
 FROM Sys_Cnxextna
 WHERE IdReg=@pmIdReg
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCnxTerminal VARCHAR(4)
 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,CnxTerminal=@pmCnxTerminal
 WHERE IdReg=@pmIdReg
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTercClieSaldos] @pmIdClie VARCHAR(16),@pmCdAgencia VARCHAR(16)
 AS 
 DELETE FROM TercClieSaldos
 WHERE IdClie LIKE ISNULL(@pmIdClie,'%') AND CdAgencia LIKE ISNULL(@pmCdAgencia,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,@pmNitTercero VARCHAR(16)
 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,NitTercero)
 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,@pmNitTercero)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NitTercero
 FROM Trn_NomLiqDet
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsPeajesCat] @pmIdCat VARCHAR(4),@pmCatpeaje VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO PeajesCat (IdCat,Catpeaje,Inactivo)
 VALUES (@pmIdCat,@pmCatpeaje,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpPeajesCat] @pmIdCat VARCHAR(4),@pmCatpeaje VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE PeajesCat SET Catpeaje=@pmCatpeaje,Inactivo=@pmInactivo
 WHERE IdCat=@pmIdCat
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPeajesCat] @pmIdCat VARCHAR(4)
 AS 
 DELETE FROM PeajesCat
 WHERE IdCat=@pmIdCat
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPeajesCatDso]
 AS 
 SELECT IdCat,Catpeaje,IdCat+' '+Catpeaje AS DsCat
 FROM PeajesCat
 WHERE Inactivo=0
 ORDER BY  IdCat
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_Contratos] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_Contratos WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,@pmVrRecCaja MONEY,@pmTarifSeguro DECIMAL(14,4),@pmTotalSeguro DECIMAL(14,4)
 AS 
 INSERT INTO Trn_FinPagos (TipDoc,IdPrestamo,IdCia,Item,Numero,Fecha,FecPago,VrCuota,EdoForma,CdBanco,CtaCheque,Detalle,TipRec,Recibo,IdCiaRec,VrLiqMora,FecLiqMora,VrRecCaja,TarifSeguro,TotalSeguro)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmItem,@pmNumero,@pmFecha,@pmFecPago,@pmVrCuota,@pmEdoForma,@pmCdBanco,@pmCtaCheque
,@pmDetalle,@pmTipRec,@pmRecibo,@pmIdCiaRec,@pmVrLiqMora,@pmFecLiqMora,@pmVrRecCaja,@pmTarifSeguro,@pmTotalSeguro)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelEstadoTer] @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM EstadoTer
 WHERE IdEstado=@pmIdEstado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraAnticipos] @pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Anticipo,IdCia,Fecha,IdConcepto,TipMuc,Manifiesto,IdCiaMuc,IdVehiculo,IdPoseedor,IdConductor,VrAnticipo,VrAbonado,Modalidad,TipoPago
,NumCheque,FecCheque,IdCta,IdCuenta,CedBenef,Beneficiario,Cantidad,FechaVence,NumPresAnt,PreAnticipo,NumCargue,CiaCargue,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,Observacion,IdEstado,VrDeduccion,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraAnticipos
 WHERE TipDoc=@pmTipDoc AND Anticipo=@pmAnticipo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraAnticipos] @pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipMuc VARCHAR(3),@pmManifiesto INT,@pmIdCiaMuc CHAR(2),@pmIdVehiculo VARCHAR(10),@pmIdPoseedor VARCHAR(16),@pmIdConductor VARCHAR(16),@pmVrAnticipo MONEY,@pmVrAbonado MONEY,@pmModalidad VARCHAR(10)
,@pmTipoPago VARCHAR(10),@pmNumCheque VARCHAR(20),@pmFecCheque SMALLDATETIME,@pmIdCta VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmBeneficiario VARCHAR(150),@pmCantidad DECIMAL(14,4),@pmFechaVence SMALLDATETIME,@pmNumPresAnt INT, @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmCedBenef VARCHAR(16),@pmPreAnticipo INT,@pmNumCargue INT,@pmCiaCargue CHAR(2),@pmVrDeduccion MONEY,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraAnticipos SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipMuc=@pmTipMuc,Manifiesto=@pmManifiesto,IdCiaMuc=@pmIdCiaMuc,IdVehiculo=@pmIdVehiculo,IdPoseedor=@pmIdPoseedor,IdConductor=@pmIdConductor,VrAnticipo=@pmVrAnticipo,VrAbonado=@pmVrAbonado,Modalidad=@pmModalidad,TipoPago=@pmTipoPago,NumCheque=@pmNumCheque,FecCheque=@pmFecCheque
,IdCta=@pmIdCta,IdCuenta=@pmIdCuenta,Beneficiario=@pmBeneficiario,Cantidad=@pmCantidad,FechaVence=@pmFechaVence,NumPresAnt=@pmNumPresAnt,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,CedBenef=@pmCedBenef
,PreAnticipo=@pmPreAnticipo,FecUpdate=@pmFecUpdate,NumCargue=@pmNumCargue,CiaCargue=@pmCiaCargue,VrDeduccion=@pmVrDeduccion
 WHERE TipDoc=@pmTipDoc AND Anticipo=@pmAnticipo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncMucListaAntcipos] (@pmManifiesto INT,@pmIdCia CHAR(2))
RETURNS VARCHAR(250)
 AS  
BEGIN 
 DECLARE @Anticipos VARCHAR(250)
 DECLARE @Anticipo INT
 DECLARE TraAnticipos CURSOR FOR 
 SELECT Anticipo FROM Trn_TraAnticipos WHERE Manifiesto=@pmManifiesto AND IdCiaMuc=@pmIdCia AND Anulado=0
 SET @Anticipos=''
 OPEN TraAnticipos
 FETCH NEXT FROM TraAnticipos  
 INTO @Anticipo
 WHILE @@FETCH_STATUS=0
  BEGIN
   SET @Anticipos=@Anticipos+CAST(@Anticipo AS VARCHAR(10))+','
   FETCH NEXT FROM TraAnticipos  
   INTO @Anticipo
  END
 CLOSE TraAnticipos
 DEALLOCATE TraAnticipos
 RETURN @Anticipos
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraAnticipos] @pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipMuc VARCHAR(3),@pmManifiesto INT,@pmIdCiaMuc CHAR(2),@pmIdVehiculo VARCHAR(10),@pmIdPoseedor VARCHAR(16),@pmIdConductor VARCHAR(16),@pmVrAnticipo MONEY,@pmVrAbonado MONEY
,@pmModalidad VARCHAR(10),@pmTipoPago VARCHAR(10),@pmNumCheque VARCHAR(20),@pmFecCheque SMALLDATETIME,@pmIdCta VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmBeneficiario VARCHAR(150),@pmCantidad DECIMAL(14,4),@pmFechaVence SMALLDATETIME,@pmNumPresAnt INT, @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT
,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmCedBenef VARCHAR(16),@pmPreAnticipo INT,@pmNumCargue INT,@pmCiaCargue CHAR(2),@pmVrDeduccion MONEY,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraAnticipos (TipDoc,Anticipo,IdCia,Fecha,IdConcepto,TipMuc,Manifiesto,IdCiaMuc,IdVehiculo,IdPoseedor,IdConductor,VrAnticipo,VrAbonado,Modalidad,TipoPago,NumCheque,FecCheque,IdCta,IdCuenta,Beneficiario,CedBenef,Cantidad,FechaVence,NumPresAnt,PreAnticipo,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario,NumCargue,CiaCargue,VrDeduccion)
 VALUES (@pmTipDoc,@pmAnticipo,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipMuc,@pmManifiesto,@pmIdCiaMuc,@pmIdVehiculo,@pmIdPoseedor,@pmIdConductor,@pmVrAnticipo,@pmVrAbonado,@pmModalidad,@pmTipoPago,@pmNumCheque,@pmFecCheque,@pmIdCta,@pmIdCuenta,@pmBeneficiario,@pmCedBenef,@pmCantidad,@pmFechaVence,@pmNumPresAnt,@pmPreAnticipo,@pmTipCom,@pmComprobante
,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmNumCargue,@pmCiaCargue,@pmVrDeduccion)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelFijos_Veh] @pmIdCargo VARCHAR(4),@pmIdVehiculo VARCHAR(10)
AS
  DELETE FROM  Fijos_Veh  WHERE IdCargo=@pmIdCargo AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposFil] @pmIdFiltro VARCHAR(4),@pmTipoFiltro VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposFil SET TipoFiltro=@pmTipoFiltro,Inactivo=@pmInactivo
 WHERE IdFiltro=@pmIdFiltro
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposFil] @pmIdFiltro VARCHAR(4),@pmTipoFiltro VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposFil (IdFiltro,TipoFiltro,Inactivo)
 VALUES (@pmIdFiltro,@pmTipoFiltro,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposFil] @pmIdFiltro VARCHAR(4)
 AS 
 DELETE FROM TiposFil
 WHERE IdFiltro=@pmIdFiltro
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelSubCentros] @pmIdSubCos VARCHAR(16)
 AS 
 DELETE FROM SubCentros
 WHERE IdSubCos=@pmIdSubCos
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrySubCentros] @pmIdSubCos VARCHAR(16)
 AS 
 SELECT IdSubCos,SubCosto,IdCCosto,FecAdd,FecUpdate,Inactivo
 FROM SubCentros
 WHERE IdSubCos=@pmIdSubCos
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPuntTarjetas] @pmNumTarjeta VARCHAR(20)
 AS 
 DELETE FROM PuntTarjetas
 WHERE NumTarjeta=@pmNumTarjeta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPuntTarjetas] @pmNumTarjeta VARCHAR(20)
 AS 
 SELECT NumTarjeta,IdClie,Fecha,TotalPuntos,PuntRedim,FecUltCompra,Inactivo,FechaUpdate,IdUsuario
 FROM PuntTarjetas
 WHERE NumTarjeta=@pmNumTarjeta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpPuntTarjetasAcu] @pmNumTarjeta VARCHAR(20)
,@pmTotalPuntos DECIMAL(14,4),@pmFecUltCompra SMALLDATETIME
 AS 
 UPDATE PuntTarjetas SET TotalPuntos=@pmTotalPuntos,FecUltCompra=@pmFecUltCompra
 WHERE NumTarjeta=@pmNumTarjeta
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpLineasCredCue] @pmIdLinea VARCHAR(4),@pmIdTipoTerc VARCHAR(4),@pmConcepto VARCHAR(10),@pmIdCuentaDb VARCHAR(16),@pmIdCuentaCr VARCHAR(16)
 AS 
 UPDATE LineasCredCue SET IdCuentaDb=@pmIdCuentaDb,IdCuentaCr=@pmIdCuentaCr
 WHERE IdLinea=@pmIdLinea AND IdTipoTerc=@pmIdTipoTerc AND Concepto=@pmConcepto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsLineasCredCue] @pmIdLinea VARCHAR(4),@pmIdTipoTerc VARCHAR(4),@pmConcepto VARCHAR(10),@pmIdCuentaDb VARCHAR(16),@pmIdCuentaCr VARCHAR(16)
 AS 
 INSERT INTO LineasCredCue (IdLinea,IdTipoTerc,Concepto,IdCuentaDb,IdCuentaCr)
 VALUES (@pmIdLinea,@pmIdTipoTerc,@pmConcepto,@pmIdCuentaDb,@pmIdCuentaCr)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposComFmt] @pmIdCom VARCHAR(3),@pmIdForma CHAR(2)
 AS 
 SELECT IdCom,IdForma,Descripcion,Formato,Predtmnado,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,sp_Nombre,Ind_Location
 FROM TiposComFmt
 WHERE IdCom=@pmIdCom AND IdForma=@pmIdForma
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposComFmt] @pmIdCom 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 TiposComFmt 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 IdCom=@pmIdCom AND IdForma=@pmIdForma
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposComFmt] @pmIdCom 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 TiposComFmt (IdCom,IdForma,Descripcion,Formato,Predtmnado,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,sp_Nombre,Ind_Location)
 VALUES (@pmIdCom,@pmIdForma,@pmDescripcion,@pmFormato,@pmPredtmnado,@pmTipoPapel,@pmOrientacion,@pmVistaPrevia,@pmVerSetup,@pmNumCopias,@pmsp_Nombre,@pmInd_Location)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposComFmtPre] @pmIdCom VARCHAR(3),@pmIdForma CHAR(2),@pmPredtmnado BIT
 AS 
 UPDATE TiposComFmt SET Predtmnado=@pmPredtmnado
 WHERE IdCom=@pmIdCom AND IdForma<>@pmIdForma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTiposComFmtDso] @pmIdCom VARCHAR(3)
 AS
  SELECT IdCom,IdForma,Descripcion,Formato,IdForma + ' '+Descripcion AS DsFor
  FROM TiposComFmt
  WHERE IdCom=@pmIdCom
 ORDER BY IdForma
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposComFmt] @pmIdCom VARCHAR(3),@pmIdForma CHAR(2)
 AS 
 DELETE FROM TiposComFmt WHERE IdCom=@pmIdCom AND IdForma=@pmIdForma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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)
,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc 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,VrReteCREE,TarifaRtc,CodTarRtc)
 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,@pmVrReteCREE,@pmTarifaRtc,@pmCodTarRtc)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE  [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc 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
,VrReteCREE=@pmVrReteCREE,TarifaRtc=@pmTarifaRtc,CodTarRtc=@pmCodTarRtc,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,VrReteCREE,TarifaRtc,CodTarRtc,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevRec
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,FspFinmes,FecFinPrueba
,TipoPrendDot,TipoMedSent,BenefPacto,TipoTraspaso,FechaTraspaso,ContratoRef,IdMedPago,FecAdd,FecUpdate,IdUsuario
 FROM Emp_Contrato
 WHERE NContrato=@pmNContrato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,FspFinmes,TipoTraspaso,FechaTraspaso,ContratoRef
,FecAdd,FecUpdate,IdUsuario
 FROM Emp_Contrato
 WHERE IdEmpleado=@pmIdEmpleado AND NContrato<>@pmNContratoExc
 ORDER BY FecIngreso,NContrato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,@pmFspFinmes BIT,@pmFecFinPrueba SMALLDATETIME,@pmTipoPrendDot VARCHAR(10),@pmTipoMedSent VARCHAR(4),@pmBenefPacto VARCHAR(100),@pmTipoTraspaso INT,@pmFechaTraspaso SMALLDATETIME,@pmContratoRef INT,@pmIdMedPago VARCHAR(4),@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,FspFinmes=@pmFspFinmes,FecFinPrueba=@pmFecFinPrueba,TipoPrendDot=@pmTipoPrendDot,TipoMedSent=@pmTipoMedSent,BenefPacto=@pmBenefPacto,TipoTraspaso=@pmTipoTraspaso,FechaTraspaso=@pmFechaTraspaso,ContratoRef=@pmContratoRef,IdMedPago=@pmIdMedPago
 WHERE NContrato=@pmNContrato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmFspFinmes BIT,@pmFecFinPrueba SMALLDATETIME,@pmTipoPrendDot VARCHAR(10),@pmTipoMedSent VARCHAR(4),@pmBenefPacto VARCHAR(100),@pmTipoTraspaso INT,@pmFechaTraspaso SMALLDATETIME,@pmContratoRef INT,@pmIdMedPago VARCHAR(4),@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,FspFinmes,FecFinPrueba,TipoPrendDot,TipoMedSent,BenefPacto,TipoTraspaso,FechaTraspaso,ContratoRef,IdMedPago)
 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,@pmFspFinmes,@pmFecFinPrueba,@pmTipoPrendDot,@pmTipoMedSent,@pmBenefPacto,@pmTipoTraspaso,@pmFechaTraspaso,@pmContratoRef,@pmIdMedPago)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmFecConsig SMALLDATETIME,@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,FecConsig,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,@pmFecConsig,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,FecConsig,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Consigna
 WHERE TipDoc=@pmTipDoc AND Consignacion=@pmConsignacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmFecConsig SMALLDATETIME,@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,FecConsig=@pmFecConsig,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Consignacion=@pmConsignacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCdDpto VARCHAR(8)
 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,CdDpto=@pmCdDpto
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase AND Item=@pmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmCdDpto VARCHAR(8)
 AS 
 INSERT INTO ProdImpFron (IdProducto,IdClase,Item,IdLocal,CdDpto,Tarifa,SimbTfa,CdTarifa,TarifZfe,RngFecha,FecInicial,FecFinal,LtaPre,Inactivo,FecUpdate)
 VALUES (@pmIdProducto,@pmIdClase,@pmItem,@pmIdLocal,@pmCdDpto,@pmTarifa,@pmSimbTfa,@pmCdTarifa,@pmTarifZfe,@pmRngFecha,@pmFecInicial
,@pmFecFinal,@pmLtaPre,@pmInactivo,@pmFecUpdate)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdImpFron] @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT
 AS 
 SELECT IdProducto,IdClase,Item,IdLocal,CdDpto,Tarifa,SimbTfa,CdTarifa,TarifZfe,RngFecha,FecInicial,FecFinal,LtaPre,Inactivo,FecUpdate
 FROM ProdImpFron
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase AND Item=@pmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdImpFronUlt] @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4)
,@pmIdLocal VARCHAR(8)
 AS 
 SELECT IdProducto,IdClase,Item,IdLocal,CdDpto,Tarifa,SimbTfa,CdTarifa,TarifZfe,RngFecha,FecInicial,FecFinal,LtaPre
 FROM ProdImpFron
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase  AND IdLocal=@pmIdLocal
 AND Inactivo=0
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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)
,@pmTipoMargen CHAR(1),@pmIntvExcluido BIT,@pmIntvGlobal BIT,@pmIvaIncluido BIT
 AS 
 INSERT INTO Trn_MayBonif (NContrato,IdCia,Item,TipoProd,VrInicial,VrFinal,Unidad,Tarifa,TipoTarifa,Periodo,CdProducto,CdSubgrupo,TipoMargen,IntvExcluido,IntvGlobal,IvaIncluido)
 VALUES (@pmNContrato,@pmIdCia,@pmItem,@pmTipoProd,@pmVrInicial,@pmVrFinal,@pmUnidad,@pmTarifa,@pmTipoTarifa,@pmPeriodo,@pmCdProducto,@pmCdSubgrupo,@pmTipoMargen,@pmIntvExcluido,@pmIntvGlobal,@pmIvaIncluido)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoMargen,IntvExcluido,IntvGlobal,IvaIncluido
 FROM Trn_MayBonif
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia  
  AND CdSubgrupo LIKE ISNULL(@pmCdSubgrupo,'%')
  AND TipoProd LIKE ISNULL(@pmTipoProd,'%')
 ORDER BY Item
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMayBonifDet] @pmNContrato INT,@pmIdCia CHAR(2),@pmTipoProd VARCHAR(20)=Null
 AS 
 SELECT Item,TipoProd,VrInicial,VrFinal,Unidad,Tarifa,TipoTarifa,Periodo,CdSubgrupo,TipoMargen,IntvExcluido,IntvGlobal,IvaIncluido
 FROM Trn_MayBonif
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia  AND CdSubgrupo<>'0'
 AND TipoProd LIKE ISNULL(@pmTipoProd,'%')
 ORDER BY Item
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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)
,@pmTipoMargen CHAR(1),@pmIntvExcluido BIT,@pmIntvGlobal BIT,@pmIvaIncluido BIT
 AS 
 UPDATE Trn_MayBonif SET TipoProd=@pmTipoProd,VrInicial=@pmVrInicial,VrFinal=@pmVrFinal,Unidad=@pmUnidad,Tarifa=@pmTarifa,TipoTarifa=@pmTipoTarifa
,Periodo=@pmPeriodo,CdProducto=@pmCdProducto,CdSubgrupo=@pmCdSubgrupo,TipoMargen=@pmTipoMargen,IntvExcluido=@pmIntvExcluido,IntvGlobal=@pmIntvGlobal,IvaIncluido=@pmIvaIncluido
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMayBonifCant] @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT  COUNT(Item) AS SCANT
 FROM Trn_MayBonif
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMayBonifItm] @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT MAX(Item) AS MXITM
 FROM Trn_MayBonif
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMayBonif] @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT NContrato,IdCia,Item,TipoProd,VrInicial,VrFinal,Unidad,Tarifa,TipoTarifa,Periodo
,CdProducto,CdSubgrupo,TipoMargen,IntvExcluido,IntvGlobal,IvaIncluido
 FROM Trn_MayBonif
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia  
 AND (Item>=ISNULL(@pmItem,-1)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE FUNCTION [dbo].[FuncMayBonifTarif]  (@pmNContrato INT,@pmIdCia CHAR(2))
RETURNS DECIMAL(16,6)
 AS  
BEGIN 
 DECLARE @MaxTarif   DECIMAL(16,6)
 SET  @MaxTarif=(SELECT  MAX(Tarifa) FROM Trn_MayBonif
 WHERE  NContrato=@pmNContrato AND IdCia=@pmIdCia)
 RETURN @MaxTarif
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTipoFactInt VARCHAR(10),@pmCodTarRtc VARCHAR(4),@pmTarifaRtc DECIMAL(14,4),@pmTipoFE VARCHAR(3),@pmFechaFE SMALLDATETIME,@pmCodTarArf VARCHAR(4),@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmCodTarAri VARCHAR(4),@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY,@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,TipoFactInt=@pmTipoFactInt,TipoFE=@pmTipoFE,FechaFE=@pmFechaFE
,CodTarArf=@pmCodTarArf,TarifaArf=@pmTarifaArf,VrAutRetFte=@pmVrAutRetFte,CodTarAri=@pmCodTarAri,TarifaAri=@pmTarifaAri,VrAutRetIca=@pmVrAutRetIca,CodTarRtc=@pmCodTarRtc,TarifaRtc=@pmTarifaRtc,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTipoFactInt VARCHAR(10),@pmCodTarRtc VARCHAR(4),@pmTarifaRtc DECIMAL(14,4),@pmTipoFE VARCHAR(3),@pmFechaFE SMALLDATETIME
,@pmCodTarArf VARCHAR(4),@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmCodTarAri VARCHAR(4),@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY,@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,TipoFactInt
,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,CodTarRtc,TarifaRtc,TipoFE,FechaFE,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca)
 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,@pmTipoFactInt,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmCodTarRtc,@pmTarifaRtc,@pmTipoFE,@pmFechaFE,@pmCodTarArf,@pmTarifaArf,@pmVrAutRetFte,@pmCodTarAri,@pmTarifaAri,@pmVrAutRetIca)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,TipoFactInt,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado
,CodTarRtc,TarifaRtc,TipoFE,FechaFE,CodTarArf,TarifaArf,VrAutRetFte,CodTarAri,TarifaAri,VrAutRetIca,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinFactint
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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

SET ANSI_NULLS ON
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,@pmTipoContab VARCHAR(10),@pmCdComNiif VARCHAR(3),@pmCdComFiscal VARCHAR(3),@pmCodCiaTdo CHAR(2),@pmTipoConRev INT,@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
,TipoContab=@pmTipoContab,CdComNiif=@pmCdComNiif,CdComFiscal=@pmCdComFiscal,CodCiaTdo=@pmCodCiaTdo,TipoConRev=@pmTipoConRev,FechaUpdate=@pmFechaUpdate
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryConceptos] @pmIdConcepto VARCHAR(4)
 AS 
 SELECT IdConcepto,Concepto,IdDoc,IdCom,Vcmto,Imptos,Retfte,RetIca,Observacion,Pdterminado
,TrfaSanc,RndTotal,Inactivo,DcmtoEquiv,TipoContab,CdComNiif,CdComFiscal,CodCiaTdo,TipoConRev
,FechaAdd,FechaUpdate,IdUsuario
 FROM Conceptos
 WHERE IdConcepto=@pmIdConcepto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmTipoContab VARCHAR(10),@pmCdComNiif VARCHAR(3),@pmCdComFiscal VARCHAR(3),@pmCodCiaTdo CHAR(2),@pmTipoConRev INT
,@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,TipoContab,CdComNiif,CdComFiscal,CodCiaTdo,TipoConRev)
 VALUES (@pmIdConcepto,@pmConcepto,@pmIdDoc,@pmIdCom,@pmVcmto,@pmImptos,@pmRetfte,@pmRetIca,@pmObservacion,@pmPdterminado,@pmTrfaSanc
,@pmRndTotal,@pmDcmtoEquiv,@pmInactivo,@pmFechaAdd,@pmIdUsuario,@pmTipoContab,@pmCdComNiif,@pmCdComFiscal,@pmCodCiaTdo,@pmTipoConRev)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposLub] @pmIdLub VARCHAR(4)
 AS 
 DELETE FROM TiposLub
 WHERE IdLub=@pmIdLub
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposLub] @pmIdLub VARCHAR(4),@pmTipoLub VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposLub (IdLub,TipoLub,Inactivo)
 VALUES (@pmIdLub,@pmTipoLub,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposLub] @pmIdLub VARCHAR(4),@pmTipoLub VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposLub SET TipoLub=@pmTipoLub,Inactivo=@pmInactivo
 WHERE IdLub=@pmIdLub
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposCnd] @pmIdTipoCon VARCHAR(4)
 AS 
 DELETE FROM TiposCnd
 WHERE IdTipoCon=@pmIdTipoCon
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposCnd] @pmIdTipoCon VARCHAR(4),@pmTipoContndor VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposCnd (IdTipoCon,TipoContndor,Inactivo)
 VALUES (@pmIdTipoCon,@pmTipoContndor,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposCnd] @pmIdTipoCon VARCHAR(4),@pmTipoContndor VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposCnd SET TipoContndor=@pmTipoContndor,Inactivo=@pmInactivo
 WHERE IdTipoCon=@pmIdTipoCon
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMayTiposAprob] @pmIdAprob VARCHAR(4),@pmTipoAprob VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO MayTiposAprob (IdAprob,TipoAprob,Inactivo)
 VALUES (@pmIdAprob,@pmTipoAprob,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMayTiposAprob] @pmIdAprob VARCHAR(4),@pmTipoAprob VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE MayTiposAprob SET TipoAprob=@pmTipoAprob,Inactivo=@pmInactivo
 WHERE IdAprob=@pmIdAprob
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMayTiposAprob] @pmIdAprob VARCHAR(4)
 AS 
 DELETE FROM MayTiposAprob
 WHERE IdAprob=@pmIdAprob
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdMciasVal] 
 AS 
 SELECT IdProducto,DescripProd,TipoRef
 FROM ProdMcias
 WHERE  TipoRef='SERVICIO' AND ValesComb<>0  AND Inactivo=0
 ORDER BY  IdProducto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmTipoZonaFront VARCHAR(3),@pmExcluidoImp BIT,@pmElectrocomb BIT,@pmIvaDetCombo BIT,@pmLtaBaseIva INT,@pmCodFEDian VARCHAR(20),@pmTallaje 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,TipoZonaFront=@pmTipoZonaFront,ExcluidoImp=@pmExcluidoImp,Electrocomb=@pmElectrocomb,IvaDetCombo=@pmIvaDetCombo,LtaBaseIva=@pmLtaBaseIva,CodFEDian=@pmCodFEDian,Tallaje=@pmTallaje,FechaUpdate=@pmFechaUpdate
 WHERE IdProducto=@pmIdProducto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,Tallaje,FecUltcom,FecUltVta,CodMcia,DescripLong,Cmntarios,PathFoto,CantTpv,IdEstado,Inactivo,TipoZonaFront,ExcluidoImp,Electrocomb,IvaDetCombo,LtaBaseIva,CodFEDian,FechaAdd,FechaUpdate,IdUsuario
 FROM ProdMcias
 WHERE IdProducto=@pmIdProducto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryKardexSub] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdSubgrupo,K.IdProducto AS tmIdProducto,DescripProd,TipoRef,Combo,EsProdBase AS tmEsProdBase
,IdProv,ExcluidoImp,Electrocomb,IvaDetCombo,COUNT(Item) AS SCANT,SUM(Entradas) AS SENT,SUM(Salidas) AS SSAL
,SUM(CASE TipDoc WHEN 'ENT' THEN VrCostoEnt-(NumInicial*Entradas) WHEN 'COM' THEN VrCostoEnt-(NumInicial*Entradas)
 ELSE VrCostoEnt END) AS SCOSENT
,SUM(CASE TipDoc WHEN 'DVE' THEN VrCostoSal-(NumInicial*Salidas) WHEN 'DEI' THEN VrCostoSal-(NumInicial*Salidas)
 ELSE VrCostoSal END) 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
,SUM(MgenCont*Entradas) AS SMGENENT,SUM(MgenCont*Salidas) AS SMGENSAL,SUM(VrImpCon) AS SVICO
,SUM(CASE WHEN CantObseq>0 THEN VrIvaObseq ELSE 0 END) AS SIVAOBSQ
,SUM(CantObseq*VrUnitario) AS SBASCOSOBSQ,SUM(CantObseq*VrPrecio) AS SBASEOBSQ
,SUM(BaseIvaCom*Entradas) AS BASEIVAENT,SUM(BaseIvaCom*Salidas) AS BASEIVASAL 
,SUM(ImpCarbono*Entradas) AS IMPCARBENT,SUM(ImpCarbono*Salidas) AS IMPCARBSAL 
,SUM(CASE WHEN (TarifaIva>0 AND P.TipoZonaFront='F') THEN ((Entradas*BaseIvaCom)*TarifaIva)/100 ELSE 0 END) AS SIVAZFENT
,SUM(CASE WHEN (TarifaIva>0 AND P.TipoZonaFront='F') THEN ((Salidas*BaseIvaCom)*TarifaIva)/100 ELSE 0 END) AS SIVAZFSAL
--Para los ajustes con cantidad en cero (0)
,SUM(VrUnitario) AS SCOSAJU,SUM(OtroImpto) AS SDVEAJU,SUM(VrImvCosto) AS SIMVCOS
,SUM(CASE WHEN Combo=0 AND EsProdBase=1 THEN BaseIvaCom ELSE 0 END) AS BASEIVACOM
,SUM(CASE WHEN Combo=1 AND IvaDetCombo=1 THEN IngBaseCom ELSE 0 END) AS INGBASECOM
,SUM(CASE WHEN Combo=0 AND EsProdBase=1 THEN IngBaseCom ELSE 0 END) AS SBASEING
,SUM(SobtasaCons) AS SOBTCON
,SUM(BaseIvp*Entradas) AS BASEIVPENT,SUM(BaseIvp*Salidas) AS BASEIVAING,SUM(IvaIngProd) AS SIVAINGP,MAX(TarifaIvp) AS TARIVAING
 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,ExcluidoImp,Electrocomb,IvaDetCombo
 ORDER BY IdSubgrupo,K.IdProducto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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','VCC')
 AND TipoRef<>'SERVICIO' AND Tanques=1 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY K.IdProducto,CdTanque
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryKardexSob] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
 AS 
 SELECT CdLocal,K.IdProducto AS CdProducto,DescripProd,ISNULL(ZonaFrontera,0) AS ZonaFront
,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
 LEFT JOIN (SELECT TipDoc,Factura,IdCia,ZonaFrontera FROM Trn_Facturas WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Modalidad='COMBUSTBLE'
 UNION ALL SELECT TipDev,Devolucion,IdCia,ZonaFrontera FROM Trn_DevFcr WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Modalidad='COMBUSTBLE')
 AS F ON K.TipDoc=F.TipDoc AND K.Documento=F.Factura AND K.IdCia=F.IdCia
 WHERE K.TipDoc IN ('FCR','FC1','FC2','FC3','FC4','FC5','DFC','DF1','DF2','DF3','DF4','DF5','PLA','DVP','PAS','DNP')
 AND  K.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND P.Tanques<>0 AND K.NoVentas>0
 GROUP BY CdLocal,K.IdProducto,DescripProd,ISNULL(ZonaFrontera,0)
 ORDER BY CdLocal,K.IdProducto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryKardexSubOpe] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdSubgrupo,K.IdProducto AS tmIdProducto,DescripProd,TipoRef,Combo,EsProdBase AS tmEsProdBase,IdProv,ExcluidoImp,Electrocomb,IvaDetCombo,CdOperario AS tmCdOperario
,COUNT(Item) AS SCANT,SUM(Entradas) AS SENT,SUM(Salidas) AS SSAL
,SUM(CASE TipDoc WHEN 'ENT' THEN VrCostoEnt-(NumInicial*Entradas) WHEN 'COM' THEN VrCostoEnt-(NumInicial*Entradas)
 ELSE VrCostoEnt END) AS SCOSENT
,SUM(CASE TipDoc WHEN 'DVE' THEN VrCostoSal-(NumInicial*Salidas) WHEN 'DEI' THEN VrCostoSal-(NumInicial*Salidas)
 ELSE VrCostoSal END) 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
,SUM(MgenCont*Entradas) AS SMGENENT,SUM(MgenCont*Salidas) AS SMGENSAL,SUM(VrImpCon) AS SVICO
,SUM(CASE WHEN CantObseq>0 THEN VrIvaObseq ELSE 0 END) AS SIVAOBSQ
,SUM(CantObseq*VrUnitario) AS SBASCOSOBSQ,SUM(CantObseq*VrPrecio) AS SBASEOBSQ
,SUM(BaseIvaCom*Entradas) AS BASEIVAENT,SUM(BaseIvaCom*Salidas) AS BASEIVASAL 
,SUM(ImpCarbono*Entradas) AS IMPCARBENT,SUM(ImpCarbono*Salidas) AS IMPCARBSAL 
,SUM(CASE WHEN (TarifaIva>0 AND P.TipoZonaFront='F') THEN ((Entradas*BaseIvaCom)*TarifaIva)/100 ELSE 0 END) AS SIVAZFENT
,SUM(CASE WHEN (TarifaIva>0 AND P.TipoZonaFront='F') THEN ((Salidas*BaseIvaCom)*TarifaIva)/100 ELSE 0 END) AS SIVAZFSAL
--Para los ajustes con cantidad en cero (0)
,SUM(VrUnitario) AS SCOSAJU,SUM(OtroImpto) AS SDVEAJU,SUM(VrImvCosto) AS SIMVCOS
,SUM(CASE WHEN Combo=0 AND EsProdBase=1 THEN BaseIvaCom ELSE 0 END) AS BASEIVACOM
,SUM(CASE WHEN Combo=1 AND IvaDetCombo=1 THEN IngBaseCom ELSE 0 END) AS INGBASECOM
,SUM(CASE WHEN Combo=0 AND EsProdBase=1 THEN IngBaseCom ELSE 0 END) AS SBASEING
,SUM(SobtasaCons) AS SOBTCON
,SUM(BaseIvp*Entradas) AS BASEIVPENT,SUM(BaseIvp*Salidas) AS BASEIVAING,SUM(IvaIngProd) AS SIVAINGP,MAX(TarifaIvp) AS TARIVAING
 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,ExcluidoImp,Electrocomb,IvaDetCombo,CdOperario
 ORDER BY IdSubgrupo,K.IdProducto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmTipoZonaFront VARCHAR(3),@pmExcluidoImp BIT
,@pmElectrocomb BIT,@pmIvaDetCombo BIT,@pmLtaBaseIva INT,@pmCodFEDian VARCHAR(20),@pmTallaje 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,TipoZonaFront,ExcluidoImp,Electrocomb,IvaDetCombo,LtaBaseIva,CodFEDian,Tallaje)
 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,@pmTipoZonaFront,@pmExcluidoImp,@pmElectrocomb,@pmIvaDetCombo,@pmLtaBaseIva,@pmCodFEDian,@pmTallaje)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,TipoZonaFront,ExcluidoImp,Electrocomb,CodFEDian,Tallaje)
 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,TipoZonaFront,ExcluidoImp,Electrocomb,CodFEDian,Tallaje
 FROM  ProdMcias WHERE IdProducto=@pmIdProducto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryKardexProCc] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdSubgrupo,K.IdProducto AS tmIdProducto,DescripProd,TipoRef,IdProv,CdCCosto,CdSubCos
,CodTarIca,CodTarRet,pVehiculo,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,CodTarRet,pVehiculo
 ORDER BY IdSubgrupo,K.IdProducto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,FCodBodega,FCodMarca,FCodSubGrupo,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_InvFisico
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmFCodBodega VARCHAR(4),@pmFCodMarca VARCHAR(4),@pmFCodSubGrupo VARCHAR(8)
,@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,FCodBodega,FCodMarca,FCodSubGrupo)
 VALUES (@pmTipDoc,@pmnInventario,@pmIdCia,@pmFecha,@pmModalidad,@pmCdBodega,@pmCantFisico,@pmVrTotalFisico,@pmExistencias,@pmVrTotalCosto,@pmOrigenAdd,@pmProcesado,@pmFecProceso,@pmNumAjuSob
,@pmNumAjuFal,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmFCodBodega,@pmFCodMarca,@pmFCodSubGrupo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmFCodBodega VARCHAR(4),@pmFCodMarca VARCHAR(4),@pmFCodSubGrupo VARCHAR(8),@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
,FCodBodega=@pmFCodBodega,FCodMarca=@pmFCodMarca,FCodSubGrupo=@pmFCodSubGrupo,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomAutoliq] @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20)=Null
 AS 
 DELETE FROM Trn_NomAutoliq
 WHERE TipoLiq=@pmTipoLiq AND Numero LIKE ISNULL(@pmNumero,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposPrend] @pmIdPrend VARCHAR(4),@pmTipoPrenda VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposPrend (IdPrend,TipoPrenda,Inactivo)
 VALUES (@pmIdPrend,@pmTipoPrenda,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposPrend] @pmIdPrend VARCHAR(4)
 AS 
 IF @pmIdPrend IS NULL
  SELECT IdPrend,TipoPrenda
  FROM TiposPrend
  WHERE Inactivo=0
 ORDER BY TipoPrenda
 ELSE
  SELECT IdPrend,TipoPrenda,Inactivo
  FROM TiposPrend
  WHERE IdPrend=@pmIdPrend
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposPrend] @pmIdPrend VARCHAR(4)
 AS 
 DELETE FROM TiposPrend
 WHERE IdPrend=@pmIdPrend
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposPrend] @pmIdPrend VARCHAR(4),@pmTipoPrenda VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposPrend SET TipoPrenda=@pmTipoPrenda,Inactivo=@pmInactivo
 WHERE IdPrend=@pmIdPrend
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraDevAos] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraDevAos
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraDevAos] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmVrDevolucion MONEY,@pmCdCta VARCHAR(4),@pmCdCuenta VARCHAR(16),@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_TraDevAos (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Anticipo,IdCiaDoc,FecDoc,VrDevolucion,CdCta,CdCuenta,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmAnticipo,@pmIdCiaDoc,@pmFecDoc,@pmVrDevolucion,@pmCdCta,@pmCdCuenta,@pmModdDev,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevAosDoc] @pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Anticipo,IdCiaDoc,FecDoc,VrDevolucion,ModdDev
 FROM Trn_TraDevAos
 WHERE TipDoc=@pmTipDoc AND Anticipo=@pmAnticipo AND IdCiaDoc=@pmIdCiaDoc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraDevAos] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmAnticipo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmVrDevolucion MONEY,@pmCdCta VARCHAR(4),@pmCdCuenta VARCHAR(16),@pmModdDev VARCHAR(10)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraDevAos SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Anticipo=@pmAnticipo,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,VrDevolucion=@pmVrDevolucion,CdCta=@pmCdCta,CdCuenta=@pmCdCuenta,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevAos] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Anticipo,IdCiaDoc,FecDoc,VrDevolucion,CdCta,CdCuenta,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraDevAos
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMayItemsInv] @pmIdArticulo VARCHAR(16)
 AS 
 DELETE FROM MayItemsInv
 WHERE IdArticulo=@pmIdArticulo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMayItemsInvGru]
 AS 
 SELECT GrupoArt,count(IdArticulo) AS SCANT
 FROM MayItemsInv
 WHERE Inactivo=0
 GROUP BY GrupoArt
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMayItemsInv] @pmIdArticulo VARCHAR(16)
 AS 
 SELECT IdArticulo,Descrpcion,UndMed,GrupoArt,CdProducto,Inactivo
 FROM MayItemsInv
 WHERE IdArticulo=@pmIdArticulo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMercancias] @pmIdMercancia VARCHAR(16),@pmDescripMcia VARCHAR(250),@pmCodigoMcia VARCHAR(16),@pmIdGrupo VARCHAR(10),@pmUndMed VARCHAR(10)
,@pmIdUnd VARCHAR(4),@pmIdEmp VARCHAR(4),@pmIdNat VARCHAR(4),@pmIdMnjo VARCHAR(4),@pmIdTmcia VARCHAR(4),@pmEstadoMcia VARCHAR(20),@pmContenedor BIT
,@pmIdProducto VARCHAR(16),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmUmCapac VARCHAR(10),@pmCodigoUN VARCHAR(30),@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Mercancias (IdMercancia,DescripMcia,CodigoMcia,IdGrupo,UndMed,IdUnd,IdEmp,IdNat,IdMnjo,IdTmcia,EstadoMcia,Contenedor,IdProducto,UmCapac,IdEstado,Inactivo,FechaAdd,IdUsuario,CodigoUN)
 VALUES (@pmIdMercancia,@pmDescripMcia,@pmCodigoMcia,@pmIdGrupo,@pmUndMed,@pmIdUnd,@pmIdEmp,@pmIdNat,@pmIdMnjo,@pmIdTmcia,@pmEstadoMcia,@pmContenedor,@pmIdProducto
,@pmUmCapac,@pmIdEstado,@pmInactivo,@pmFechaAdd,@pmIdUsuario,@pmCodigoUN)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMercancias] @pmIdMercancia VARCHAR(16)
 AS 
 SELECT IdMercancia,DescripMcia,CodigoMcia,IdGrupo,UndMed,IdUnd,IdEmp,IdNat,IdMnjo,IdTmcia,EstadoMcia,Contenedor,IdProducto
,IdEstado,Inactivo,UmCapac,CodigoUN,FechaAdd,FechaUpdate,IdUsuario
 FROM Mercancias
 WHERE IdMercancia=@pmIdMercancia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMercancias] @pmIdMercancia VARCHAR(16),@pmDescripMcia VARCHAR(250),@pmCodigoMcia VARCHAR(16),@pmIdGrupo VARCHAR(10),@pmUndMed VARCHAR(10),@pmIdUnd VARCHAR(4)
,@pmIdEmp VARCHAR(4),@pmIdNat VARCHAR(4),@pmIdMnjo VARCHAR(4),@pmIdTmcia VARCHAR(4),@pmEstadoMcia VARCHAR(20),@pmContenedor BIT,@pmIdProducto VARCHAR(16)
,@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmUmCapac VARCHAR(10),@pmCodigoUN VARCHAR(30),@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
,IdEmp=@pmIdEmp,EstadoMcia=@pmEstadoMcia,UmCapac=@pmUmCapac,CodigoUN=@pmCodigoUN,FechaUpdate=@pmFechaUpdate
 WHERE IdMercancia=@pmIdMercancia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmtmNCuota INT
 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,tmNCuota)
 VALUES (@pmtmEst,@pmtmItem,@pmtmTipoReg,@pmtmTipDoc,@pmtmDocumento,@pmtmIdCia,@pmtmItemDoc,@pmtmFecEmision,@pmtmFecVence,@pmtmValorFactura,@pmtmValorAbono,@pmtmIdPropietario,@pmtmIdVehiculo,@pmtmIdConductor,@pmtmIdCargo,@pmtmIdClase
,@pmtmFactura,@pmtmReferencia,@pmtmDetalle,@pmtmDescuento,@pmtmNCuota)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelNomRetDetEmp] @pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Trn_NomRetDet
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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
,@pmtmUndsEnt DECIMAL(16,4),@pmtmUndsDve DECIMAL(16,4),@pmtmUndsFac DECIMAL(16,4),@pmtmUndsDfc DECIMAL(16,4),@pmtmUndsSal DECIMAL(16,4),@pmtmUndsDvs DECIMAL(16,4),@pmtmUndsOtr DECIMAL(16,4),@pmtmUndsDot DECIMAL(16,4)
 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
,tmUndsEnt,tmUndsDve,tmUndsFac,tmUndsDfc,tmUndsSal,tmUndsDvs,tmUndsOtr,tmUndsDot)
 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
,@pmtmUndsEnt,@pmtmUndsDve,@pmtmUndsFac,@pmtmUndsDfc,@pmtmUndsSal,@pmtmUndsDvs,@pmtmUndsOtr,@pmtmUndsDot)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_MovTanques] @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_MovTanques
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposComRes] @pmIdRes VARCHAR(4),@pmResolucion VARCHAR(50),@pmPrefijo VARCHAR(5),@pmNumInicial INT,@pmNumFinal INT,@pmFechaExpRes SMALLDATETIME,@pmFechaVigencia SMALLDATETIME,@pmEstablecimiento VARCHAR(100)
 AS 
 INSERT INTO TiposComRes (IdRes,Resolucion,Prefijo,NumInicial,NumFinal,FechaExpRes,FechaVigencia,Establecimiento)
 VALUES (@pmIdRes,@pmResolucion,@pmPrefijo,@pmNumInicial,@pmNumFinal,@pmFechaExpRes,@pmFechaVigencia,@pmEstablecimiento)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposComRes] @pmIdRes VARCHAR(4),@pmResolucion VARCHAR(50),@pmPrefijo VARCHAR(5),@pmNumInicial INT,@pmNumFinal INT,@pmFechaExpRes SMALLDATETIME,@pmFechaVigencia SMALLDATETIME,@pmEstablecimiento VARCHAR(100)
 AS 
 UPDATE TiposComRes SET Resolucion=@pmResolucion,Prefijo=@pmPrefijo,NumInicial=@pmNumInicial,NumFinal=@pmNumFinal,FechaExpRes=@pmFechaExpRes,FechaVigencia=@pmFechaVigencia,Establecimiento=@pmEstablecimiento
 WHERE IdRes=@pmIdRes
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposComRes] @pmIdRes VARCHAR(4)
 AS 
 SELECT IdRes,Resolucion,Prefijo,NumInicial,NumFinal,FechaExpRes,FechaVigencia,Establecimiento
 FROM TiposComRes
 WHERE IdRes=@pmIdRes
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmVrImpCons MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY,@pmCdMotDev VARCHAR(4)
,@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY,@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,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc,BaseIvaObsq
,VrIvaObsequio,CdMotDev,TarifaArf,VrAutRetFte,TarifaAri,VrAutRetIca)
 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,@pmVrImpCons,@pmVrReteCREE,@pmTarifaRtc,@pmCodTarRtc,@pmBaseIvaObsq,@pmVrIvaObsequio,@pmCdMotDev
,@pmTarifaArf,@pmVrAutRetFte,@pmTarifaAri,@pmVrAutRetIca)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrImpCons,VrReteCREE,TarifaRtc,CodTarRtc
,BaseIvaObsq,VrIvaObsequio,TarifaArf,VrAutRetFte,TarifaAri,VrAutRetIca,CdMotDev,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevFco
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmVrImpCons MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY,@pmCdMotDev VARCHAR(4),@pmTarifaArf DECIMAL(14,4),@pmVrAutRetFte MONEY,@pmTarifaAri DECIMAL(14,4),@pmVrAutRetIca MONEY,@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,VrImpCons=@pmVrImpCons,VrReteCREE=@pmVrReteCREE,TarifaRtc=@pmTarifaRtc,CodTarRtc=@pmCodTarRtc,BaseIvaObsq=@pmBaseIvaObsq,VrIvaObsequio=@pmVrIvaObsequio,CdMotDev=@pmCdMotDev,TarifaArf=@pmTarifaArf,VrAutRetFte=@pmVrAutRetFte,TarifaAri=@pmTarifaAri,VrAutRetIca=@pmVrAutRetIca,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelVehClientes] @pmIdVehiculo VARCHAR(10)
 AS 
 DELETE FROM VehClientes
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDespachos] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Manifiesto,IdCia,Fecha,FecCargue,FecDespacho,IdOrigen,IdDestino,IdRuta,IdVehiculo,IdConductor,IdPoseedor,NitRemite,Remitente,NitDestntario
,Destinatario,DescMcia,PesoTotal,NumOrden,IdCiaOrd,Remesa,IdCiaRem,Estado,Anulado,FecDev,Observacion,TimeSys,FecUpdate,IdUsuario
 FROM Trn_TraDespachos
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraDespachos] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecCargue SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmIdRuta VARCHAR(4)
,@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmIdPoseedor VARCHAR(16),@pmNitRemite VARCHAR(16),@pmRemitente VARCHAR(250),@pmNitDestntario VARCHAR(16),@pmDestinatario VARCHAR(250),@pmDescMcia VARCHAR(150),@pmPesoTotal DECIMAL(14,4)
,@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmRemesa INT,@pmIdCiaRem CHAR(2),@pmEstado INT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraDespachos SET Fecha=@pmFecha,FecCargue=@pmFecCargue,FecDespacho=@pmFecDespacho,IdOrigen=@pmIdOrigen,IdDestino=@pmIdDestino,IdRuta=@pmIdRuta,IdVehiculo=@pmIdVehiculo,IdConductor=@pmIdConductor,IdPoseedor=@pmIdPoseedor
,NitRemite=@pmNitRemite,Remitente=@pmRemitente,NitDestntario=@pmNitDestntario,Destinatario=@pmDestinatario,DescMcia=@pmDescMcia,PesoTotal=@pmPesoTotal,NumOrden=@pmNumOrden,IdCiaOrd=@pmIdCiaOrd,Remesa=@pmRemesa,IdCiaRem=@pmIdCiaRem
,Estado=@pmEstado,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraDespachos] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraDespachos
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraDespachos] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecCargue SMALLDATETIME,@pmFecDespacho SMALLDATETIME,@pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmIdRuta VARCHAR(4)
,@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmIdPoseedor VARCHAR(16),@pmNitRemite VARCHAR(16),@pmRemitente VARCHAR(250),@pmNitDestntario VARCHAR(16),@pmDestinatario VARCHAR(250),@pmDescMcia VARCHAR(150),@pmPesoTotal DECIMAL(14,4)
,@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmRemesa INT,@pmIdCiaRem CHAR(2),@pmEstado INT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraDespachos (TipDoc,Manifiesto,IdCia,Fecha,FecCargue,FecDespacho,IdOrigen,IdDestino,IdRuta,IdVehiculo,IdConductor,IdPoseedor,NitRemite,Remitente,NitDestntario,Destinatario,DescMcia,PesoTotal,NumOrden,IdCiaOrd,Remesa,IdCiaRem,Estado,Anulado,FecDev,Observacion,TimeSys,IdUsuario)
 VALUES (@pmTipDoc,@pmManifiesto,@pmIdCia,@pmFecha,@pmFecCargue,@pmFecDespacho,@pmIdOrigen,@pmIdDestino,@pmIdRuta,@pmIdVehiculo,@pmIdConductor,@pmIdPoseedor,@pmNitRemite,@pmRemitente,@pmNitDestntario,@pmDestinatario,@pmDescMcia,@pmPesoTotal,@pmNumOrden
,@pmIdCiaOrd,@pmRemesa,@pmIdCiaRem,@pmEstado,@pmAnulado,@pmFecDev,@pmObservacion,@pmTimeSys,@pmIdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTraDespachosAnu] @pmTipDoc VARCHAR(3),@pmManifiesto INT,@pmIdCia CHAR(2)
,@pmEstado INT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250)
 AS 
 UPDATE Trn_TraDespachos SET Estado=@pmEstado,Anulado=@pmAnulado,FecDev=@pmFecDev
,Observacion=@pmObservacion
 WHERE TipDoc=@pmTipDoc AND Manifiesto=@pmManifiesto AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpSobProductos] @pmIdClase INT,@pmIdProducto VARCHAR(16),@pmClaseProd VARCHAR(20)
 AS 
 UPDATE SobProductos SET IdProducto=@pmIdProducto,ClaseProd=@pmClaseProd
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrySobProductosId]
 AS 
 SELECT MAX(IdClase) AS MXITM
 FROM SobProductos
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrySobProductosExi] @pmIdProducto VARCHAR(16),@pmIdClase INT
 AS 
 SELECT IdClase,IdProducto,ClaseProd
 FROM SobProductos
 WHERE IdProducto=@pmIdProducto AND IdClase<>@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelSobProductos] @pmIdClase INT
 AS 
 DELETE FROM SobProductos
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsSobProductos] @pmIdClase INT,@pmIdProducto VARCHAR(16),@pmClaseProd VARCHAR(20)
 AS 
 INSERT INTO SobProductos (IdClase,IdProducto,ClaseProd)
 VALUES (@pmIdClase,@pmIdProducto,@pmClaseProd)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrySobProductos] @pmIdClase INT
 AS 
 SELECT IdClase,IdProducto,ClaseProd
 FROM SobProductos
 WHERE IdClase=@pmIdClase
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCdTarifSeguro VARCHAR(4),@pmTasaIntLinea BIT,@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,CdTarifSeguro=@pmCdTarifSeguro,TasaIntLinea=@pmTasaIntLinea
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia AND IdLinea=@pmIdLinea
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,CdTarifSeguro,EstadoAct,TasaIntLinea,FechaCrea,FechaAct,IdUsuario
 FROM TercClieLineas
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia AND IdLinea=@pmIdLinea
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmCdTarifSeguro VARCHAR(4),@pmTasaIntLinea BIT,@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,CdTarifSeguro,TasaIntLinea)
 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,@pmCdTarifSeguro,@pmTasaIntLinea)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuCuentasVeh_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdVehiculo,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuCuentasVeh AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt 
 AND NOT EXISTS (SELECT * FROM AcuCuentasVeh AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes  
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdVehiculo=S.IdVehiculo)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuCuentasVehSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuCuentasVeh SET AcuCuentasVeh.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuCuentasVeh INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuCuentasVeh WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt) AS SA ON AcuCuentasVeh.IdCia=SA.IdCia AND AcuCuentasVeh.IdCuenta=SA.IdCuenta
 AND AcuCuentasVeh.IdVehiculo=SA.IdVehiculo
 WHERE AcuCuentasVeh.nAnno=@pmnAnno AND AcuCuentasVeh.nMes=@pmnMes
 AND (@pmIdCia IS NULL OR AcuCuentasVeh.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuCuentasVeh.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuCuentasVeh_Pla] @pmIdVehiculo VARCHAR(10),@pmIdVehiculoDos VARCHAR(10),@pmNewVehiculo VARCHAR(10)
 AS 
 IF EXISTS (SELECT * FROM AcuCuentasVeh WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos)
   INSERT INTO AcuCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
   SELECT nAnno,nMes,IdCia,IdCuenta,@pmNewVehiculo,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
   FROM AcuCuentasVeh WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos
   GROUP BY nAnno,nMes,IdCia,IdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTraLiqDetalle] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdConcepto VARCHAR(4),@pmDescConcepto VARCHAR(250),@pmIdCuenta VARCHAR(16),@pmVrDebito MONEY,@pmVrCredito MONEY,@pmIdTercero VARCHAR(16),@pmCodAgncia VARCHAR(16)
,@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmRubroConc VARCHAR(10),@pmVrBase MONEY,@pmTarifBase DECIMAL(14,4),@pmCantComb DECIMAL(14,4),@pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCiaFac CHAR(2),@pmItemFac INT
,@pmFecVence SMALLDATETIME,@pmRefConcep VARCHAR(50),@pmTipSop VARCHAR(3),@pmNumSoporte INT,@pmIdCiaSop CHAR(2),@pmDocSop BIT,@pmTipoImp VARCHAR(4),@pmCodTarImp VARCHAR(4),@pmItemBase INT
 AS 
 INSERT INTO Trn_TraLiqDetalle (TipDoc,Liquidacion,IdCia,Item,IdConcepto,DescConcepto,IdCuenta,VrDebito,VrCredito,IdTercero,CodAgncia,CdCCosto,CdSubCos,pVehiculo,RubroConc,VrBase,TarifBase,CantComb,TipFac,Factura,IdCiaFac,ItemFac,FecVence,RefConcep,TipSop,NumSoporte,IdCiaSop
,DocSop,TipoImp,CodTarImp,ItemBase)
 VALUES (@pmTipDoc,@pmLiquidacion,@pmIdCia,@pmItem,@pmIdConcepto,@pmDescConcepto,@pmIdCuenta,@pmVrDebito,@pmVrCredito,@pmIdTercero,@pmCodAgncia,@pmCdCCosto,@pmCdSubCos,@pmpVehiculo,@pmRubroConc,@pmVrBase,@pmTarifBase
,@pmCantComb,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmItemFac,@pmFecVence,@pmRefConcep,@pmTipSop,@pmNumSoporte,@pmIdCiaSop,@pmDocSop,@pmTipoImp,@pmCodTarImp,@pmItemBase)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelProfesiones] @pmIdProf VARCHAR(4)
 AS 
 DELETE FROM Profesiones
 WHERE IdProf=@pmIdProf
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpProfesiones] @pmIdProf VARCHAR(4),@pmProfesion VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE Profesiones SET Profesion=@pmProfesion,Inactivo=@pmInactivo
 WHERE IdProf=@pmIdProf
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsProfesiones] @pmIdProf VARCHAR(4),@pmProfesion VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO Profesiones (IdProf,Profesion,Inactivo)
 VALUES (@pmIdProf,@pmProfesion,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpEmp_Estudios] @pmIdEmpleado VARCHAR(16),@pmItem INT,@pmEstblecmto VARCHAR(50)
,@pmTitulo VARCHAR(50),@pmFecha SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmIdClase VARCHAR(4),@pmHabilitado BIT
 AS 
 UPDATE Emp_Estudios SET Estblecmto=@pmEstblecmto,Titulo=@pmTitulo,Fecha=@pmFecha,FecVence=@pmFecVence
 ,IdClase=@pmIdClase,Habilitado=@pmHabilitado
 WHERE IdEmpleado=@pmIdEmpleado AND Item=@pmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsEmp_Estudios] @pmIdEmpleado VARCHAR(16),@pmItem INT,@pmEstblecmto VARCHAR(50)
,@pmTitulo VARCHAR(50),@pmFecha SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmIdClase VARCHAR(4),@pmHabilitado BIT
 AS 
 INSERT INTO Emp_Estudios (IdEmpleado,Item,Estblecmto,Titulo,Fecha,FecVence,IdClase,Habilitado)
 VALUES (@pmIdEmpleado,@pmItem,@pmEstblecmto,@pmTitulo,@pmFecha,@pmFecVence,@pmIdClase,@pmHabilitado)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEmp_Estudios] @pmIdEmpleado VARCHAR(16),@pmItem INT
 AS 
 SELECT IdEmpleado,Item,Estblecmto,Titulo,Fecha,FecVence,IdClase,Habilitado
 FROM Emp_Estudios
 WHERE IdEmpleado=@pmIdEmpleado 
 AND (Item>=ISNULL(@pmItem,0) AND Item<=ISNULL(@pmItem,2147483647)) 
 ORDER BY Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMayContDocItm] @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT MAX(Item) AS MXITM
 FROM Trn_MayContDoc
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDeltm_NomHoras]
 AS 
 DELETE FROM tm_NomHoras
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmtmNumParte INT
 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,tmNumParte)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdActivo,@pmtmIdCausal,@pmtmNitTercero,@pmtmNomTercero,@pmtmCostoIni,@pmtmMejoras
,@pmtmDisminucion,@pmtmAcuAjustes,@pmtmDepAcum,@pmtmAjuDepAcum,@pmtmVrIngreso,@pmtmFechaRet,@pmtmIdCueDeb,@pmtmIdCueIng
,@pmtmConcepto,@pmtmIdCia,@pmtmCdCCosto,@pmtmCdSubCos,@pmtmNitRespons,@pmtmNumParte)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,tmNumParte
 FROM tm_AfRetiro
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTraReexpedidoRem] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipRem VARCHAR(3),@pmRemesa INT,@pmIdCiaRem CHAR(2),@pmFecRemesa SMALLDATETIME
,@pmDescripcion VARCHAR(250),@pmUnidades DECIMAL(14,4),@pmPesoNeto DECIMAL(14,4),@pmUndMed VARCHAR(10),@pmVolumen DECIMAL(14,4),@pmUndVol VARCHAR(10),@pmUndTarifa VARCHAR(10),@pmVrUnitario MONEY
,@pmValorTotal MONEY,@pmpVehiculo VARCHAR(10),@pmCedConductor VARCHAR(16)
 AS 
 INSERT INTO Trn_TraReexpedidoRem (TipDoc,Causacion,IdCia,Item,TipRem,Remesa,IdCiaRem,FecRemesa,Descripcion,Unidades,PesoNeto,UndMed,Volumen,UndVol,UndTarifa,VrUnitario,ValorTotal,pVehiculo,CedConductor)
 VALUES (@pmTipDoc,@pmCausacion,@pmIdCia,@pmItem,@pmTipRem,@pmRemesa,@pmIdCiaRem,@pmFecRemesa,@pmDescripcion,@pmUnidades,@pmPesoNeto,@pmUndMed,@pmVolumen,@pmUndVol,@pmUndTarifa
,@pmVrUnitario,@pmValorTotal,@pmpVehiculo,@pmCedConductor)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetalle_Crr] @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraDetalle (tmNumero,tmItem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed,tmdmsAlto,tmdmsAncho,tmdmsLargo,tmVolumen,tmUndVol,tmIdUnd,tmIdEmp,tmIdNat
,tmIdMnjo,tmIdTmcia,tmCdRango,tmCases,tmCajas,tmPalets,tmRemision,tmNumOrden,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen,tmNitDestntario,tmDestinatario,tmDirDestino,tmIdDestino
,tmCdAgencia,tmTipoVehic,tmTarifClie,tmTarifPago,tmTarifTabla,tmVrDeclarado,tmVrSeguro,tmTarifSeguro,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmContenedor1,tmContenedor2,tmUndTarifa
,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmNumMintrans,tmPesoCont,tmUndTarPago,tmCodBodega,tmTipoTraslado,tmTieCargue,tmTieDesc,tmFecIniCargue,tmFecFinCargue,tmFecInidesc,tmFecFindesc
,tmCdTipoVeh,tmTipoServicio,tmSedeRem,tmSedeDest,tmFecLlegaCargue,tmFecLlegaDesc,tmMinCargue,tmMinDescargue)
SELECT @pmtmNumero,Item,'0',Descripcion,Unidades,PesoNeto,UndMed,0,0,0,Volumen,UndVol,'0','0','0','0','0','0',0,0,0,0,0,CedConductor,'0','0','0',CedConductor,'0','0','0','0','TERCEROS',0,VrUnitario,ValorTotal
,0,0,0,pVehiculo,'','',CAST(FecRemesa AS VARCHAR(20)) ,'','',UndTarifa,TipRem,Remesa,IdCiaRem,0,0,0,UndTarifa,'',0,0,0,Null,Null,Null,Null,'','','','',Null,Null,0,0
 FROM  Trn_TraReexpedidoRem
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelJurAutoridades] @pmIdAutoridad VARCHAR(4)
 AS 
 DELETE FROM JurAutoridades
 WHERE IdAutoridad=@pmIdAutoridad
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrySectoresEcoDso] 
 AS 
  SELECT IdSector,SectorEco,IdSector+' '+SectorEco AS DsSector
  FROM SectoresEco
  WHERE Inactivo=0
  ORDER BY IdSector
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelSectoresEco] @pmIdSector VARCHAR(8)
 AS 
 DELETE FROM SectoresEco
 WHERE IdSector=@pmIdSector
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrySectoresEco] @pmIdSector VARCHAR(8)
 AS 
 IF @pmIdSector IS NULL
  SELECT IdSector,SectorEco,CdTarifRtc
  FROM SectoresEco
  WHERE Inactivo=0
 ORDER BY SectorEco
 ELSE
  SELECT IdSector,SectorEco,CdTarifRtc,Inactivo
  FROM SectoresEco
  WHERE IdSector=@pmIdSector
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsSectoresEco] @pmIdSector VARCHAR(8),@pmSectorEco VARCHAR(250),@pmCdTarifRtc VARCHAR(4),@pmInactivo BIT
 AS 
 INSERT INTO SectoresEco (IdSector,SectorEco,Inactivo,CdTarifRtc)
 VALUES (@pmIdSector,@pmSectorEco,@pmInactivo,@pmCdTarifRtc)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpSectoresEco] @pmIdSector VARCHAR(8),@pmSectorEco VARCHAR(250),@pmCdTarifRtc VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE SectoresEco SET SectorEco=@pmSectorEco,Inactivo=@pmInactivo,CdTarifRtc=@pmCdTarifRtc
 WHERE IdSector=@pmIdSector
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryCtasCorrientes] @pmIdCta VARCHAR(4)
 AS 
 SELECT IdCta,NumeroCta,IdBanco,IdCuenta,ConsCheques,RangoCons,ConsActual,Formato,TipoPapel,Orientacion
,VistaPrevia,VerSetup,NumCopias,IdCiaDef,TipoProp,Inactivo
 FROM CtasCorrientes
 WHERE IdCta=@pmIdCta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTipoProp VARCHAR(10)
 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,TipoProp=@pmTipoProp
 WHERE IdCta=@pmIdCta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmTipoProp VARCHAR(10)
 AS 
 INSERT INTO CtasCorrientes (IdCta,NumeroCta,IdBanco,IdCuenta,ConsCheques,RangoCons,ConsActual,Formato,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,IdCiaDef,Inactivo,TipoProp)
 VALUES (@pmIdCta,@pmNumeroCta,@pmIdBanco,@pmIdCuenta,@pmConsCheques,@pmRangoCons,@pmConsActual,@pmFormato,@pmTipoPapel,@pmOrientacion
,@pmVistaPrevia,@pmVerSetup,@pmNumCopias,@pmIdCiaDef,@pmInactivo,@pmTipoProp)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmtmVrSeguro MONEY
,@pmtmVrProrroga MONEY,@pmtmVrCustodia MONEY,@pmtmTasaCustod DECIMAL(16,8),@pmtmTarifIva DECIMAL(14,4),@pmtmVrIva MONEY,@pmtmTarifRet DECIMAL(14,4),@pmtmVrRet MONEY
 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,tmVrSeguro,tmVrProrroga,tmVrCustodia,tmTasaCustod
,tmTarifIva,tmVrIva,tmTarifRet,tmVrRet)
 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,@pmtmVrSeguro,@pmtmVrProrroga,@pmtmVrCustodia,@pmtmTasaCustod,@pmtmTarifIva,@pmtmVrIva,@pmtmTarifRet,@pmtmVrRet)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuFisCuentasVehtSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum VARCHAR(3),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuFisCuentasVeht SET AcuFisCuentasVeht.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuFisCuentasVeht INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,IdVehiculo,TipoAfiVehic,TipoAcum,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuFisCuentasVeht WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt AND TipoAcum=@pmTipoAcum) AS SA ON AcuFisCuentasVeht.IdCia=SA.IdCia AND AcuFisCuentasVeht.IdCuenta=SA.IdCuenta
 AND AcuFisCuentasVeht.IdTercero=SA.IdTercero AND AcuFisCuentasVeht.IdVehiculo=SA.IdVehiculo AND AcuFisCuentasVeht.TipoAfiVehic=SA.TipoAfiVehic AND AcuFisCuentasVeht.TipoAcum=SA.TipoAcum
 WHERE AcuFisCuentasVeht.nAnno=@pmnAnno AND AcuFisCuentasVeht.nMes=@pmnMes AND AcuFisCuentasVeht.TipoAcum=@pmTipoAcum
 AND (@pmIdCia IS NULL OR AcuFisCuentasVeht.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuFisCuentasVeht.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuFisCuentasVeht_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmTipoAcum VARCHAR(3),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuFisCuentasVeht (nAnno,nMes,IdCia,IdCuenta,IdTercero,IdVehiculo,TipoAfiVehic,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdTercero,S.IdVehiculo,S.TipoAfiVehic,S.TipoAcum,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuFisCuentasVeht AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt AND S.TipoAcum=@pmTipoAcum
 AND NOT EXISTS (SELECT * FROM AcuFisCuentasVeht AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes AND AC.TipoAcum=@pmTipoAcum
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdTercero=S.IdTercero AND AC.IdVehiculo=S.IdVehiculo AND AC.TipoAfiVehic=S.TipoAfiVehic)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpAcuNiifCuentasVehSA] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 UPDATE AcuNiifCuentasVeh SET AcuNiifCuentasVeh.SaldoAnterior=ISNULL(SA.NuevoSaldo,0)
 FROM AcuNiifCuentasVeh INNER JOIN (SELECT nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuNiifCuentasVeh WHERE nAnno=@pmAnnoAnt AND nMes=@pmMesAnt) AS SA ON AcuNiifCuentasVeh.IdCia=SA.IdCia AND AcuNiifCuentasVeh.IdCuenta=SA.IdCuenta
 AND AcuNiifCuentasVeh.IdVehiculo=SA.IdVehiculo
 WHERE AcuNiifCuentasVeh.nAnno=@pmnAnno AND AcuNiifCuentasVeh.nMes=@pmnMes
 AND (@pmIdCia IS NULL OR AcuNiifCuentasVeh.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR AcuNiifCuentasVeh.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasVeh_San] @pmnAnno INT,@pmnMes INT,@pmAnnoAnt INT,@pmMesAnt INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS 
 INSERT INTO AcuNiifCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT @pmnAnno,@pmnMes,S.IdCia,S.IdCuenta,S.IdVehiculo,S.SaldoAnterior+S.TotalDebitos-S.TotalCreditos,0,0
 FROM AcuNiifCuentasVeh AS S WHERE S.nAnno=@pmAnnoAnt AND S.nMes=@pmMesAnt 
 AND NOT EXISTS (SELECT * FROM AcuNiifCuentasVeh AS AC WHERE AC.nAnno=@pmnAnno AND AC.nMes=@pmnMes  
 AND AC.IdCia=S.IdCia AND AC.IdCuenta=S.IdCuenta AND AC.IdVehiculo=S.IdVehiculo)
 AND (@pmIdCia IS NULL OR S.IdCia=@pmIdCia) AND (@pmIdCuenta IS NULL OR S.IdCuenta=@pmIdCuenta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasVeh_Unc]  @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuNiifCuentasVeh WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
   INSERT INTO AcuNiifCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
   SELECT nAnno,nMes,IdCia,@pmNewCuenta,IdVehiculo,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
   FROM AcuNiifCuentasVeh WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
   GROUP BY nAnno,nMes,IdCia,IdVehiculo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasVeh] @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdVehiculo VARCHAR(10),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuNiifCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdVehiculo,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsAcuNiifCuentasVeh_Uni] @pmIdVehiculo VARCHAR(10),@pmIdVehiculoDos VARCHAR(10),@pmNewVehiculo VARCHAR(10)
 AS 
 IF EXISTS (SELECT * FROM AcuNiifCuentasVeh WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos)
   INSERT INTO AcuNiifCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
   SELECT nAnno,nMes,IdCia,IdCuenta,@pmNewVehiculo,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
   FROM AcuNiifCuentasVeh WHERE IdVehiculo=@pmIdVehiculo OR IdVehiculo=@pmIdVehiculoDos
   GROUP BY nAnno,nMes,IdCia,IdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposMot] @pmIdTipoMot VARCHAR(4),@pmTipoMotor VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposMot (IdTipoMot,TipoMotor,Inactivo)
 VALUES (@pmIdTipoMot,@pmTipoMotor,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelTiposMot] @pmIdTipoMot VARCHAR(4)
 AS 
 DELETE FROM TiposMot
 WHERE IdTipoMot=@pmIdTipoMot
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposMot] @pmIdTipoMot VARCHAR(4),@pmTipoMotor VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposMot SET TipoMotor=@pmTipoMotor,Inactivo=@pmInactivo
 WHERE IdTipoMot=@pmIdTipoMot
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposEsc] @pmIdTipoEsc VARCHAR(4),@pmTipoEscolta VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposEsc (IdTipoEsc,TipoEscolta,Inactivo)
 VALUES (@pmIdTipoEsc,@pmTipoEscolta,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposEsc] @pmIdTipoEsc VARCHAR(4),@pmTipoEscolta VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposEsc SET TipoEscolta=@pmTipoEscolta,Inactivo=@pmInactivo
 WHERE IdTipoEsc=@pmIdTipoEsc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposEsc] @pmIdTipoEsc VARCHAR(4)
 AS 
 DELETE FROM TiposEsc
 WHERE IdTipoEsc=@pmIdTipoEsc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_TraRemFact] @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmTipoReg,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmDescripcion,tmCantidad,tmVrUnitario,tmVrCosto,tmUndTarifa,tmUndCosto,tmUnidades,tmPesoNeto,tmUndMed,tmVolumen,tmUndVol,tmCases,tmCajas,tmPalets,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDscto
,tmTarifaRet,tmVrRetFte,tmTarifaIca,tmVrReteIca,tmVrFaltante,tmRemision,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmCdMercancia,tmCdConc,tmCdCCosto,tmCdSubCos,tmNitTercero,tmCdAgencia,tmpVehiculo,tmTipoVehic,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen
,tmNitDestntario,tmDestinatario,tmDirDestino,tmIdDestino,tmCantFalt,tmUnidadFalt,tmCdTarRet,tmCdTarIca,tmCdTarDct,tmPedRem,tmCiaPed,tmEtdoFact,tmFecRemesa,tmIntCumplido,tmVrDeclarado,tmTarifSeguro,tmVrSeguro,tmNitAsegurad,tmCostoCump,tmTipoOrd,tmNumOrden,tmIdCiaOrd
,tmCantPago,tmVrAutoIca,tmRemTransb,tmCostoAdic,tmNitMucTbdo
 FROM tm_TraRemFact
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraRemFact] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipoReg INT,@pmtmTipRem VARCHAR(3),@pmtmRemesa INT,@pmtmIdCiaRem CHAR(2),@pmtmItemRem INT,@pmtmDescripcion VARCHAR(250),@pmtmCantidad DECIMAL(14,4),@pmtmVrUnitario MONEY,@pmtmVrCosto MONEY,@pmtmUndTarifa VARCHAR(10),@pmtmUndCosto VARCHAR(10),@pmtmUnidades DECIMAL(14,4),@pmtmPesoNeto DECIMAL(14,4),@pmtmUndMed VARCHAR(10),@pmtmVolumen DECIMAL(14,4)
,@pmtmUndVol VARCHAR(10),@pmtmCases INT,@pmtmCajas INT,@pmtmPalets INT,@pmtmTarifaIva DECIMAL(14,4),@pmtmVrIva MONEY,@pmtmTarifaDct DECIMAL(14,4),@pmtmVrDscto MONEY,@pmtmTarifaRet DECIMAL(14,4),@pmtmVrRetFte MONEY,@pmtmTarifaIca DECIMAL(14,4),@pmtmVrReteIca MONEY,@pmtmVrFaltante MONEY,@pmtmRemision DECIMAL(18,2),@pmtmDocCliente VARCHAR(30),@pmtmReferencia1 VARCHAR(50),@pmtmReferencia2 VARCHAR(50),@pmtmReferencia3 VARCHAR(50)
,@pmtmCdMercancia VARCHAR(16),@pmtmCdConc VARCHAR(4),@pmtmCdCCosto VARCHAR(16),@pmtmCdSubCos VARCHAR(16),@pmtmNitTercero VARCHAR(16),@pmtmCdAgencia VARCHAR(16),@pmtmpVehiculo VARCHAR(10),@pmtmTipoVehic VARCHAR(10),@pmtmNitRemite VARCHAR(16),@pmtmRemitente VARCHAR(250),@pmtmDirOrigen VARCHAR(250),@pmtmIdOrigen VARCHAR(8),@pmtmNitDestntario VARCHAR(16),@pmtmDestinatario VARCHAR(250),@pmtmDirDestino VARCHAR(250)
,@pmtmIdDestino VARCHAR(8),@pmtmCantFalt DECIMAL(14,4),@pmtmUnidadFalt VARCHAR(10),@pmtmCdTarRet VARCHAR(4),@pmtmCdTarIca VARCHAR(4),@pmtmCdTarDct VARCHAR(4),@pmtmPedRem INT,@pmtmCiaPed CHAR(2),@pmtmEtdoFact INT,@pmtmFecRemesa SMALLDATETIME,@pmtmIntCumplido INT,@pmtmVrDeclarado MONEY,@pmtmTarifSeguro DECIMAL(14,4),@pmtmVrSeguro MONEY,@pmtmNitAsegurad VARCHAR(16),@pmtmCostoCump MONEY,@pmtmTipoOrd VARCHAR(3),@pmtmNumOrden INT,@pmtmIdCiaOrd CHAR(2)
,@pmtmCantPago DECIMAL(14,4),@pmtmVrAutoIca MONEY,@pmtmRemTransb INT,@pmtmCostoAdic MONEY,@pmtmNitMucTbdo VARCHAR(16)
 AS 
 INSERT INTO tm_TraRemFact (tmNumero,tmItem,tmTipoReg,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmDescripcion,tmCantidad,tmVrUnitario,tmVrCosto,tmUndTarifa,tmUndCosto,tmUnidades,tmPesoNeto,tmUndMed,tmVolumen,tmUndVol,tmCases,tmCajas,tmPalets,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDscto,tmTarifaRet,tmVrRetFte,tmTarifaIca,tmVrReteIca,tmVrFaltante,tmRemision,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmCdMercancia,tmCdConc,tmCdCCosto,tmCdSubCos,tmNitTercero,tmCdAgencia
,tmpVehiculo,tmTipoVehic,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen,tmNitDestntario,tmDestinatario,tmDirDestino,tmIdDestino,tmCantFalt,tmUnidadFalt,tmCdTarRet,tmCdTarIca,tmCdTarDct,tmPedRem,tmCiaPed,tmEtdoFact,tmFecRemesa,tmIntCumplido,tmVrDeclarado,tmTarifSeguro,tmVrSeguro,tmNitAsegurad,tmCostoCump,tmTipoOrd,tmNumOrden,tmIdCiaOrd,tmCantPago,tmVrAutoIca,tmRemTransb,tmCostoAdic,tmNitMucTbdo)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipoReg,@pmtmTipRem,@pmtmRemesa,@pmtmIdCiaRem,@pmtmItemRem,@pmtmDescripcion,@pmtmCantidad,@pmtmVrUnitario,@pmtmVrCosto,@pmtmUndTarifa,@pmtmUndCosto,@pmtmUnidades,@pmtmPesoNeto,@pmtmUndMed,@pmtmVolumen,@pmtmUndVol,@pmtmCases,@pmtmCajas,@pmtmPalets,@pmtmTarifaIva,@pmtmVrIva,@pmtmTarifaDct,@pmtmVrDscto,@pmtmTarifaRet,@pmtmVrRetFte,@pmtmTarifaIca,@pmtmVrReteIca,@pmtmVrFaltante,@pmtmRemision
,@pmtmDocCliente,@pmtmReferencia1,@pmtmReferencia2,@pmtmReferencia3,@pmtmCdMercancia,@pmtmCdConc,@pmtmCdCCosto,@pmtmCdSubCos,@pmtmNitTercero,@pmtmCdAgencia,@pmtmpVehiculo,@pmtmTipoVehic,@pmtmNitRemite,@pmtmRemitente,@pmtmDirOrigen,@pmtmIdOrigen,@pmtmNitDestntario,@pmtmDestinatario,@pmtmDirDestino,@pmtmIdDestino,@pmtmCantFalt,@pmtmUnidadFalt,@pmtmCdTarRet,@pmtmCdTarIca,@pmtmCdTarDct,@pmtmPedRem,@pmtmCiaPed,@pmtmEtdoFact,@pmtmFecRemesa,@pmtmIntCumplido
,@pmtmVrDeclarado,@pmtmTarifSeguro,@pmtmVrSeguro,@pmtmNitAsegurad,@pmtmCostoCump,@pmtmTipoOrd,@pmtmNumOrden,@pmtmIdCiaOrd,@pmtmCantPago,@pmtmVrAutoIca,@pmtmRemTransb,@pmtmCostoAdic,@pmtmNitMucTbdo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_TraRemFactLta] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmNumero,tmItem,tmTipoReg,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmDescripcion,tmCantidad,tmVrUnitario,tmVrCosto,tmUndTarifa,tmUndCosto,tmUnidades,tmPesoNeto,tmUndMed,tmVolumen,tmUndVol,tmCases,tmCajas,tmPalets,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDscto
,tmTarifaRet,tmVrRetFte,tmTarifaIca,tmVrReteIca,tmVrFaltante,tmRemision,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmCdMercancia,tmCdConc,tmCdCCosto,tmCdSubCos,tmNitTercero,tmCdAgencia,tmpVehiculo,tmTipoVehic,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen
,tmNitDestntario,tmDestinatario,tmDirDestino,tmIdDestino,tmCantFalt,tmUnidadFalt,tmCantPago,tmCdTarRet,tmCdTarIca,tmCdTarDct,tmPedRem,tmCiaPed,tmEtdoFact,tmFecRemesa,tmIntCumplido,tmVrDeclarado,tmTarifSeguro,tmVrSeguro,tmVrAutoIca,tmNitAsegurad,tmCostoCump
,tmTipoOrd,tmNumOrden,tmIdCiaOrd,tmRemTransb,tmCostoAdic,tmNitMucTbdo
 FROM tm_TraRemFact
 WHERE tmNumero=@pmtmNumero
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMayTiposBienes] @pmIdTipBien VARCHAR(4),@pmTipoBien VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO MayTiposBienes (IdTipBien,TipoBien,Inactivo)
 VALUES (@pmIdTipBien,@pmTipoBien,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMayTiposBienes] @pmIdTipBien VARCHAR(4)
 AS 
 DELETE FROM MayTiposBienes
 WHERE IdTipBien=@pmIdTipBien
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMayTiposBienes] @pmIdTipBien VARCHAR(4),@pmTipoBien VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE MayTiposBienes SET TipoBien=@pmTipoBien,Inactivo=@pmInactivo
 WHERE IdTipBien=@pmIdTipBien
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrAboCapital,LiqAboCapital,VrAboOtros,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,VrAboCapital,LiqAboCapital,VrAboOtros,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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)
,@pmVrAboCapital MONEY,@pmLiqAboCapital INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmVrAboOtros MONEY,@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,VrAboCapital,LiqAboCapital,VrAboOtros,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,@pmVrAboCapital,@pmLiqAboCapital,@pmVrAboOtros,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmVrAboCapital MONEY,@pmLiqAboCapital INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmVrAboOtros MONEY,@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,VrAboCapital=@pmVrAboCapital,LiqAboCapital=@pmLiqAboCapital,VrAboOtros=@pmVrAboOtros,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDependencias] @pmIdDep VARCHAR(4)
 AS 
 SELECT IdDep,Dependencia,JefeDep,IdArea,IdSubCos,TipRiesgo,Inactivo
 FROM Dependencias
 WHERE IdDep=@pmIdDep
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposInm] @pmIdTipoInm VARCHAR(4)
 AS 
 DELETE FROM TiposInm
 WHERE IdTipoInm=@pmIdTipoInm
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposInm] @pmIdTipoInm VARCHAR(4),@pmTipoInmueble VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposInm (IdTipoInm,TipoInmueble,Inactivo)
 VALUES (@pmIdTipoInm,@pmTipoInmueble,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposInm] @pmIdTipoInm VARCHAR(4)
 AS 
IF @pmIdTipoInm IS NULL
 SELECT IdTipoInm,TipoInmueble FROM TiposInm WHERE Inactivo=0 ORDER BY IdTipoInm
ELSE  
 SELECT IdTipoInm,TipoInmueble,Inactivo FROM TiposInm WHERE IdTipoInm=@pmIdTipoInm
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposInm] @pmIdTipoInm VARCHAR(4),@pmTipoInmueble VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposInm SET TipoInmueble=@pmTipoInmueble,Inactivo=@pmInactivo
 WHERE IdTipoInm=@pmIdTipoInm
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,@pmReferencia VARCHAR(250),@pmCantSalida DECIMAL(14,4),@pmCantDevSal DECIMAL(14,4)
,@pmCodCCtoDet VARCHAR(16),@pmCodSCtoDet VARCHAR(16),@pmPlacaVehDet VARCHAR(10),@pmCant_Orden DECIMAL(14,4)
AS 
 INSERT INTO Trn_ReqDetalle (TipDoc,Requisicion,IdCia,Item,CdProducto,Descripcion,IdSubgrupo,CdBodega,Cantidad,IdUnd,VrUnitario,TipOrd,NumOrden,IdCiaOrd,TipSal,NSalida,IdCiaSal,FechaSal
,Referencia,CantSalida,CantDevSal,CodCCtoDet,CodSCtoDet,PlacaVehDet,Cant_Orden)
 VALUES (@pmTipDoc,@pmRequisicion,@pmIdCia,@pmItem,@pmCdProducto,@pmDescripcion,@pmIdSubgrupo,@pmCdBodega,@pmCantidad,@pmIdUnd,@pmVrUnitario,@pmTipOrd
,@pmNumOrden,@pmIdCiaOrd,@pmTipSal,@pmNSalida,@pmIdCiaSal,@pmFechaSal,@pmReferencia,@pmCantSalida,@pmCantDevSal,@pmCodCCtoDet,@pmCodSCtoDet,@pmPlacaVehDet,@pmCant_Orden)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,D.Referencia
,D.TipOrd,D.NumOrden,D.IdCiaOrd,D.Cant_Orden,D.CantSalida,D.CantDevSal,D.CodCCtoDet,D.CodSCtoDet,D.PlacaVehDet
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryReqDetalle] @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,CdProducto,Descripcion,Cantidad,IdUnd,IdSubgrupo,VrUnitario
,TipOrd,NumOrden,IdCiaOrd,Cant_Orden,TipSal,NSalida,IdCiaSal,FechaSal,CdBodega,Referencia
,CantSalida,CantDevSal,CodCCtoDet,CodSCtoDet,PlacaVehDet
 FROM Trn_ReqDetalle
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
 ORDER BY  Item
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposZona] @pmIdTipZona VARCHAR(4)
 AS  
 IF @pmIdTipZona IS NULL
  SELECT IdTipZona,TipoZona
  FROM TiposZona
  WHERE Inactivo=0
  ORDER BY TipoZona
 ELSE
  SELECT IdTipZona,TipoZona,Inactivo
  FROM TiposZona
  WHERE IdTipZona=@pmIdTipZona
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTiposZona] @pmIdTipZona VARCHAR(4)
 AS 
 DELETE FROM TiposZona
 WHERE IdTipZona=@pmIdTipZona
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposZona] @pmIdTipZona VARCHAR(4),@pmTipoZona VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposZona (IdTipZona,TipoZona,Inactivo)
 VALUES (@pmIdTipZona,@pmTipoZona,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposZona] @pmIdTipZona VARCHAR(4),@pmTipoZona VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposZona SET TipoZona=@pmTipoZona,Inactivo=@pmInactivo
 WHERE IdTipZona=@pmIdTipZona
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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),@pmNum_Vehic VARCHAR(10),@pmNum_Trailer VARCHAR(10),@pmTipoVigencia VARCHAR(10),@pmNivelAprob INT,@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,Num_Vehic=@pmNum_Vehic,Num_Trailer=@pmNum_Trailer,TipoVigencia=@pmTipoVigencia,NivelAprob=@pmNivelAprob,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpRequisicionApr] @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
,@pmNumAprob INT,@pmFecAprob SMALLDATETIME,@pmCdUsuAprob VARCHAR(11),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmNivelAprob INT
 AS 
 UPDATE Trn_Requisicion SET  NumAprob=@pmNumAprob,FecAprob=@pmFecAprob,CdUsuAprob=@pmCdUsuAprob
,Observacion=@pmObservacion,IdEstado=@pmIdEstado,NivelAprob=@pmNivelAprob
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmNum_Vehic VARCHAR(10),@pmNum_Trailer VARCHAR(10),@pmTipoVigencia VARCHAR(10),@pmNivelAprob INT
,@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
,Num_Vehic,Num_Trailer,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,TipoVigencia,NivelAprob)
 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,@pmNum_Vehic,@pmNum_Trailer,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmTipoVigencia,@pmNivelAprob)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,NivelAprob,NomContacto,TelsContacto,EmailContacto,Num_Vehic,Num_Trailer,TipoVigencia
,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Requisicion
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsNomDotacionDet] @pmIdCia CHAR(2),@pmNumero INT,@pmItem INT,@pmIdEmpleado VARCHAR(16),@pmNContrato INT,@pmIdPrend VARCHAR(4)
,@pmDescripcion VARCHAR(250),@pmCantidad DECIMAL(14,4),@pmUnidad VARCHAR(10),@pmVrUnitario MONEY
 AS 
 INSERT INTO Trn_NomDotacionDet (IdCia,Numero,Item,IdEmpleado,NContrato,IdPrend,Descripcion,Cantidad,Unidad,VrUnitario)
 VALUES (@pmIdCia,@pmNumero,@pmItem,@pmIdEmpleado,@pmNContrato,@pmIdPrend,@pmDescripcion,@pmCantidad,@pmUnidad,@pmVrUnitario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTablapor] @pmIdTarifa VARCHAR(4)
 AS 
 SELECT IdTarifa,Descripcion,IdClase,Tarifa,Simbolo,CdLocdad,Minimo,Maximo,Inactivo
 FROM Tablapor
 WHERE IdTarifa=@pmIdTarifa
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTablapor] @pmIdTarifa VARCHAR(4)
 AS 
 DELETE FROM Tablapor
 WHERE IdTarifa=@pmIdTarifa
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncProdConsProc] (@pmTipDoc VARCHAR(3),@pmNumCons INT,@pmIdCia CHAR(2)
,@pmTipPed VARCHAR(3),@pmPedido INT,@pmIdCiaPed CHAR(2),@pmItemPed INT,@pmIdTipProc VARCHAR(4))
RETURNS BIT
 AS  
 BEGIN
  DECLARE @Procesos BIT
  IF @pmIdTipProc IS NULL
   SET @Procesos=1
  ELSE
   BEGIN 
    IF EXISTS (SELECT 1 FROM Trn_ProdConsProc WHERE TipDoc=@pmTipDoc AND NumCons=@pmNumCons AND IdCia=@pmIdCia
     AND TipPed=@pmTipPed AND Pedido=@pmPedido AND IdCiaPed=@pmIdCiaPed AND ItemPed=@pmItemPed AND IdTipProc=@pmIdTipProc)
     BEGIN
      SET @Procesos=1
     END
    ELSE
     BEGIN
      SET @Procesos=0
     END
   END
 RETURN @Procesos
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FuncProdConsProcFinal] (@pmTipDoc VARCHAR(3),@pmNumCons INT,@pmIdCia CHAR(2)
,@pmTipPed VARCHAR(3),@pmPedido INT,@pmIdCiaPed CHAR(2),@pmItemPed INT,@pmProcFinal BIT)
RETURNS BIT
 AS  
 BEGIN
  DECLARE @Procesos BIT
  IF @pmProcFinal IS NULL
   SET @Procesos=1
  ELSE
   BEGIN 
    IF EXISTS (SELECT 1 FROM Trn_ProdConsProc WHERE TipDoc=@pmTipDoc AND NumCons=@pmNumCons AND IdCia=@pmIdCia
     AND TipPed=@pmTipPed AND Pedido=@pmPedido AND IdCiaPed=@pmIdCiaPed AND ItemPed=@pmItemPed AND ProcFinal=@pmProcFinal)
     BEGIN
      SET @Procesos=1
     END
    ELSE
     BEGIN
      SET @Procesos=0
     END
   END
 RETURN @Procesos
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelPeajesTarf] @pmIdCat VARCHAR(4),@pmIdClase VARCHAR(4)
 AS 
 DELETE FROM PeajesTarf
 WHERE IdCat=@pmIdCat AND IdClase LIKE ISNULL(@pmIdClase,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpPeajesTarf] @pmIdCat VARCHAR(4),@pmIdClase VARCHAR(4),@pmVrTarifa MONEY
 AS 
 UPDATE PeajesTarf SET VrTarifa=@pmVrTarifa
 WHERE IdCat=@pmIdCat AND IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPeajesTarf] @pmIdCat VARCHAR(4),@pmIdClase VARCHAR(4)
 AS 
 SELECT IdCat,IdClase,VrTarifa
 FROM PeajesTarf
 WHERE IdCat=@pmIdCat AND IdClase=@pmIdClase
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsPeajesTarf] @pmIdCat VARCHAR(4),@pmIdClase VARCHAR(4),@pmVrTarifa MONEY
 AS 
 INSERT INTO PeajesTarf (IdCat,IdClase,VrTarifa)
 VALUES (@pmIdCat,@pmIdClase,@pmVrTarifa)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_TraDetalleTot] @pmtmNumero VARCHAR(5)
 AS 
 SELECT SUM(tmCantidad) AS SCANT,SUM(tmPesoNeto) AS SPESO,SUM(tmVolumen) AS SVOLUMEN,SUM(tmVrDeclarado) AS TDECLARA,SUM(tmVrSeguro) AS TSEGURO
,SUM(CASE tmUndTarifa WHEN 'PESO' THEN tmTarifClie*tmPesoNeto WHEN 'UNIDADES' THEN tmTarifClie*tmCantidad  WHEN 'VOLUMEN' THEN  tmTarifClie*tmVolumen 
 WHEN 'CAJAS' THEN tmTarifClie*tmCajas WHEN 'PALETS' THEN  tmTarifClie*tmPalets ELSE tmTarifClie*tmPesoNeto END) AS TCLIENTE
,SUM(CASE  tmUndTarPago  WHEN 'PESO' THEN tmTarifPago*tmPesoNeto WHEN 'UNIDADES' THEN tmTarifPago*tmCantidad  WHEN 'VOLUMEN' THEN  tmTarifPago*tmVolumen 
 WHEN 'CAJAS' THEN tmTarifPago*tmCajas WHEN 'PALETS' THEN  tmTarifPago*tmPalets ELSE tmTarifPago*tmPesoNeto END) AS TPAGO
,SUM(CASE tmUndTarPago WHEN 'PESO' THEN tmTarifTabla*tmPesoNeto WHEN 'UNIDADES' THEN tmTarifTabla*tmCantidad  WHEN 'VOLUMEN' THEN  tmTarifTabla*tmVolumen 
 WHEN 'CAJAS' THEN tmTarifTabla*tmCajas WHEN 'PALETS' THEN  tmTarifTabla*tmPalets ELSE tmTarifTabla*tmPesoNeto END) AS TFLETE
 FROM tm_TraDetalle
 WHERE tmNumero=@pmtmNumero
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_TraDetalle] @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed,tmdmsAlto,tmdmsAncho,tmdmsLargo,tmVolumen,tmUndVol
,tmIdUnd,tmIdEmp,tmIdNat,tmIdMnjo,tmIdTmcia,tmCdRango,tmCases,tmCajas,tmPalets,tmRemision,tmNumOrden
,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen,tmNitDestntario,tmDestinatario,tmDirDestino,tmIdDestino,tmCdAgencia,tmTipoVehic
,tmTarifClie,tmTarifPago,tmTarifTabla,tmVrDeclarado,tmVrSeguro,tmTarifSeguro,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3,tmContenedor1,tmContenedor2,tmUndTarifa
,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmNumMintrans,tmPesoCont,tmUndTarPago,tmCodBodega,tmTipoTraslado,tmTieCargue,tmTieDesc,tmFecIniCargue,tmFecFinCargue,tmFecInidesc
,tmFecFindesc,tmCdTipoVeh,tmTipoServicio,tmSedeRem,tmSedeDest,tmFecLlegaCargue,tmFecLlegaDesc,tmMinCargue,tmMinDescargue
 FROM tm_TraDetalle
 WHERE tmNumero=@pmtmNumero 
  AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDeltm_TraDetalle] @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_TraDetalle
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetalle] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdMercancia VARCHAR(16),@pmtmDescripMcias VARCHAR(250),@pmtmCantidad DECIMAL(14,4),@pmtmPesoNeto DECIMAL(14,4),@pmtmUndMed VARCHAR(10),@pmtmdmsAlto DECIMAL(14,4),@pmtmdmsAncho DECIMAL(14,4),@pmtmdmsLargo DECIMAL(14,4),@pmtmVolumen DECIMAL(14,4),@pmtmUndVol VARCHAR(10),@pmtmIdUnd VARCHAR(4),@pmtmIdEmp VARCHAR(4),@pmtmIdNat VARCHAR(4),@pmtmIdMnjo VARCHAR(4),@pmtmIdTmcia VARCHAR(4)
,@pmtmCdRango VARCHAR(4),@pmtmCases INT,@pmtmCajas INT,@pmtmPalets INT,@pmtmRemision DECIMAL(18,2),@pmtmNumOrden INT,@pmtmNitRemite VARCHAR(16),@pmtmRemitente VARCHAR(250),@pmtmDirOrigen VARCHAR(250),@pmtmIdOrigen VARCHAR(8),@pmtmNitDestntario VARCHAR(16),@pmtmDestinatario VARCHAR(250),@pmtmDirDestino VARCHAR(250),@pmtmIdDestino VARCHAR(8),@pmtmCdAgencia VARCHAR(16),@pmtmTipoVehic VARCHAR(10),@pmtmTarifClie MONEY,@pmtmTarifPago MONEY,@pmtmTarifTabla MONEY,@pmtmVrDeclarado MONEY,@pmtmVrSeguro MONEY
,@pmtmTarifSeguro DECIMAL(14,4),@pmtmDocCliente VARCHAR(30),@pmtmReferencia1 VARCHAR(50),@pmtmReferencia2 VARCHAR(50),@pmtmReferencia3 VARCHAR(50),@pmtmContenedor1 VARCHAR(50),@pmtmContenedor2 VARCHAR(50),@pmtmUndTarifa VARCHAR(10),@pmtmTipRem VARCHAR(3),@pmtmRemesa INT,@pmtmIdCiaRem CHAR(2),@pmtmItemRem INT,@pmtmNumMintrans DECIMAL(14,2),@pmtmPesoCont DECIMAL(14,4),@pmtmUndTarPago VARCHAR(10),@pmtmCodBodega VARCHAR(4),@pmtmTipoTraslado INT
,@pmtmTieCargue DECIMAL(14,4),@pmtmTieDesc DECIMAL(14,4),@pmtmFecIniCargue SMALLDATETIME,@pmtmFecFinCargue SMALLDATETIME,@pmtmFecInidesc SMALLDATETIME,@pmtmFecFindesc SMALLDATETIME,@pmtmCdTipoVeh VARCHAR(4),@pmtmTipoServicio VARCHAR(10),@pmtmSedeRem VARCHAR(10),@pmtmSedeDest VARCHAR(10),@pmtmFecLlegaCargue SMALLDATETIME,@pmtmFecLlegaDesc SMALLDATETIME,@pmtmMinCargue INT,@pmtmMinDescargue INT
 AS 
 INSERT INTO tm_TraDetalle (tmNumero,tmItem,tmIdMercancia,tmDescripMcias,tmCantidad,tmPesoNeto,tmUndMed,tmdmsAlto,tmdmsAncho,tmdmsLargo,tmVolumen,tmUndVol,tmIdUnd,tmIdEmp,tmIdNat,tmIdMnjo,tmIdTmcia,tmCdRango,tmCases,tmCajas,tmPalets,tmRemision,tmNumOrden,tmNitRemite,tmRemitente,tmDirOrigen,tmIdOrigen,tmNitDestntario,tmDestinatario,tmDirDestino,tmIdDestino,tmCdAgencia,tmTipoVehic,tmTarifClie,tmTarifPago,tmTarifTabla,tmVrDeclarado,tmVrSeguro,tmTarifSeguro,tmDocCliente,tmReferencia1,tmReferencia2,tmReferencia3
,tmContenedor1,tmContenedor2,tmUndTarifa,tmTipRem,tmRemesa,tmIdCiaRem,tmItemRem,tmNumMintrans,tmPesoCont,tmUndTarPago,tmCodBodega,tmTipoTraslado,tmTieCargue,tmTieDesc,tmFecIniCargue,tmFecFinCargue,tmFecInidesc,tmFecFindesc,tmCdTipoVeh,tmTipoServicio,tmSedeRem,tmSedeDest,tmFecLlegaCargue,tmFecLlegaDesc,tmMinCargue,tmMinDescargue)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdMercancia,@pmtmDescripMcias,@pmtmCantidad,@pmtmPesoNeto,@pmtmUndMed,@pmtmdmsAlto,@pmtmdmsAncho,@pmtmdmsLargo,@pmtmVolumen,@pmtmUndVol,@pmtmIdUnd,@pmtmIdEmp,@pmtmIdNat,@pmtmIdMnjo,@pmtmIdTmcia,@pmtmCdRango,@pmtmCases,@pmtmCajas,@pmtmPalets,@pmtmRemision,@pmtmNumOrden,@pmtmNitRemite,@pmtmRemitente,@pmtmDirOrigen,@pmtmIdOrigen,@pmtmNitDestntario,@pmtmDestinatario,@pmtmDirDestino,@pmtmIdDestino,@pmtmCdAgencia,@pmtmTipoVehic
,@pmtmTarifClie,@pmtmTarifPago,@pmtmTarifTabla,@pmtmVrDeclarado,@pmtmVrSeguro,@pmtmTarifSeguro,@pmtmDocCliente,@pmtmReferencia1,@pmtmReferencia2,@pmtmReferencia3,@pmtmContenedor1,@pmtmContenedor2,@pmtmUndTarifa,@pmtmTipRem,@pmtmRemesa,@pmtmIdCiaRem,@pmtmItemRem,@pmtmNumMintrans,@pmtmPesoCont,@pmtmUndTarPago,@pmtmCodBodega,@pmtmTipoTraslado,@pmtmTieCargue,@pmtmTieDesc,@pmtmFecIniCargue,@pmtmFecFinCargue,@pmtmFecInidesc,@pmtmFecFindesc,@pmtmCdTipoVeh,@pmtmTipoServicio,@pmtmSedeRem,@pmtmSedeDest
,@pmtmFecLlegaCargue,@pmtmFecLlegaDesc,@pmtmMinCargue,@pmtmMinDescargue)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMudOrdenConc] @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdConcepto VARCHAR(4),@pmDescripcion VARCHAR(500),@pmValorTotal MONEY,@pmTarifaIva DECIMAL(14,4),@pmTipoConc VARCHAR(10),@pmTipoRubro VARCHAR(20)
,@pmTipoReg INT,@pmCdCuenta VARCHAR(16),@pmNitTercero VARCHAR(16),@pmReferencia VARCHAR(50),@pmNumDocRef VARCHAR(20),@pmCodTarIva VARCHAR(4),@pmIndFactItem INT,@pmNumDocFac VARCHAR(20),@pmTarifSeguro DECIMAL(14,4),@pmCodTarSeg VARCHAR(4),@pmPlacaVehic VARCHAR(10),@pmTarifaPago MONEY
,@pmFechaReg SMALLDATETIME,@pmCdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_MudOrdenConc (TipDoc,NumOrden,IdCia,Item,IdConcepto,Descripcion,ValorTotal,TarifaIva,TipoConc,TipoRubro,TipoReg,CdCuenta,NitTercero,Referencia,NumDocRef,CodTarIva,IndFactItem,NumDocFac,TarifSeguro,CodTarSeg,PlacaVehic,TarifaPago,FechaReg,CdUsuario)
 VALUES (@pmTipDoc,@pmNumOrden,@pmIdCia,@pmItem,@pmIdConcepto,@pmDescripcion,@pmValorTotal,@pmTarifaIva,@pmTipoConc,@pmTipoRubro,@pmTipoReg,@pmCdCuenta,@pmNitTercero,@pmReferencia,@pmNumDocRef,@pmCodTarIva,@pmIndFactItem,@pmNumDocFac,@pmTarifSeguro,@pmCodTarSeg,@pmPlacaVehic,@pmTarifaPago,@pmFechaReg,@pmCdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrySobTiposDecDso] 
 AS 
 SELECT IdDec,Declaracion,Declaracion+' '+IdDec AS DsDec
 FROM SobTiposDec
 ORDER BY Declaracion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrySobTiposDecGas] 
 AS 
 SELECT IdDec,Declaracion,Declaracion+' '+IdDec AS DsDec
 FROM SobTiposDec
 WHERE IdDec<>'0010'
 ORDER BY Declaracion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelSobTiposDec] @pmIdDec VARCHAR(4)
 AS 
 DELETE FROM SobTiposDec
 WHERE IdDec=@pmIdDec
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelAfMantnmto] @pmNumero INT
 AS 
 DELETE FROM Trn_AfMantnmto
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryAfMantnmto] @pmNumero INT
 AS 
 SELECT Numero,IdCia,IdActivo,Fecha,NumContrato,NitEmpresa,NomEmpresa,FinContrato,IdUsuario,Diagnostico,Comentarios
 FROM Trn_AfMantnmto
 WHERE Numero=@pmNumero
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelMttoTipos] @pmIdMtto VARCHAR(4)
 AS 
 DELETE FROM MttoTipos WHERE IdMtto=@pmIdMtto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryMttoTipos] @pmIdMtto VARCHAR(4)
 AS 
 IF @pmIdMtto IS NULL
  BEGIN
   SELECT IdMtto,TipoMtto FROM MttoTipos WHERE Inactivo=0
  END
 ELSE
  BEGIN
   SELECT IdMtto,TipoMtto,Inactivo FROM MttoTipos WHERE IdMtto=@pmIdMtto
  END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsMttoTipos] @pmIdMtto VARCHAR(4),@pmTipoMtto VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO MttoTipos (IdMtto,TipoMtto,Inactivo)
 VALUES (@pmIdMtto,@pmTipoMtto,@pmInactivo)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpMttoTipos] @pmIdMtto VARCHAR(4),@pmTipoMtto VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE MttoTipos SET TipoMtto=@pmTipoMtto,Inactivo=@pmInactivo
 WHERE IdMtto=@pmIdMtto
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPucNiifAju] @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,TarifaPred AS Porcntaje,Naturaleza
 ,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referencias AS Referncias,CdTipDoc,Afijos
  FROM  PucNiif
  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,TarifaPred AS Porcntaje,Naturaleza
   ,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referencias AS Referncias,CdTipDoc,Afijos
   FROM PucNiif
   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,TarifaPred AS Porcntaje,Naturaleza
   ,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referencias AS Referncias,CdTipDoc,Afijos
   FROM PucNiif
   WHERE Movimiento<>0 AND Ajustable<>0 AND  IdCuenta BETWEEN @pmCueIni AND @pmCueFin
   ORDER BY IdCuenta
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsPucNiif] @pmIdCuenta VARCHAR(16),@pmNomCuenta VARCHAR(250),@pmMayor BIT,@pmMovimiento BIT,@pmTercero BIT,@pmVehiculo BIT,@pmAgencia BIT,@pmCentroCosto BIT,@pmSubCentro BIT,@pmVencimiento BIT,@pmBaseGravable BIT,@pmTarifaPred DECIMAL(14,4),@pmNaturaleza CHAR(1),@pmAjustable BIT
,@pmCuentaAjuDeb VARCHAR(16),@pmCuentaAjuCre VARCHAR(16),@pmCheques BIT,@pmReferencias BIT,@pmCdTipDoc VARCHAR(3),@pmInactivo BIT,@pmAfijos INT,@pmListarConc VARCHAR(4),@pmReqAutorizac BIT,@pmAntProveedor BIT,@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO PucNiif (IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,TarifaPred,Naturaleza,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referencias,CdTipDoc,FechaCrea,IdUsuario,Inactivo,Afijos,ListarConc,ReqAutorizac,AntProveedor)
 VALUES (@pmIdCuenta,@pmNomCuenta,@pmMayor,@pmMovimiento,@pmTercero,@pmVehiculo,@pmAgencia,@pmCentroCosto,@pmSubCentro,@pmVencimiento,@pmBaseGravable,@pmTarifaPred,@pmNaturaleza,@pmAjustable,@pmCuentaAjuDeb,@pmCuentaAjuCre,@pmCheques,@pmReferencias,@pmCdTipDoc,@pmFechaCrea,@pmIdUsuario,@pmInactivo,@pmAfijos,@pmListarConc,@pmReqAutorizac,@pmAntProveedor)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryPucNiif] @pmIdCuenta VARCHAR(16)
 AS 
 SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,TarifaPred,Naturaleza,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referencias,CdTipDoc,FechaCrea,FechaAct,IdUsuario,Inactivo,Afijos,ListarConc,ReqAutorizac,AntProveedor
 FROM PucNiif
 WHERE IdCuenta=@pmIdCuenta
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsPucNiif_Sel] @pmIdCuenta VARCHAR(16),@pmNewCuenta VARCHAR(16)
AS 
 INSERT INTO PucNiif (IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,TarifaPred,Naturaleza,Ajustable,CuentaAjuDeb,CuentaAjuCre
,Cheques,Referencias,CdTipDoc,FechaCrea,IdUsuario,Inactivo,Afijos,ListarConc,ReqAutorizac,AntProveedor)
SELECT @pmNewCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,TarifaPred,Naturaleza,Ajustable,CuentaAjuDeb,CuentaAjuCre
,Cheques,Referencias,CdTipDoc,FechaCrea,IdUsuario,Inactivo,Afijos,ListarConc,ReqAutorizac,AntProveedor
FROM PucNiif WHERE IdCuenta=@pmIdCuenta
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpPucNiif] @pmIdCuenta VARCHAR(16),@pmNomCuenta VARCHAR(250),@pmMayor BIT,@pmMovimiento BIT,@pmTercero BIT,@pmVehiculo BIT,@pmAgencia BIT,@pmCentroCosto BIT,@pmSubCentro BIT,@pmVencimiento BIT,@pmBaseGravable BIT,@pmTarifaPred DECIMAL(14,4),@pmNaturaleza CHAR(1),@pmAjustable BIT
,@pmCuentaAjuDeb VARCHAR(16),@pmCuentaAjuCre VARCHAR(16),@pmCheques BIT,@pmReferencias BIT,@pmCdTipDoc VARCHAR(3),@pmInactivo BIT,@pmAfijos INT,@pmListarConc VARCHAR(4),@pmReqAutorizac BIT,@pmAntProveedor BIT,@pmFechaAct SMALLDATETIME
 AS 
 UPDATE PucNiif SET NomCuenta=@pmNomCuenta,Mayor=@pmMayor,Movimiento=@pmMovimiento,Tercero=@pmTercero,Vehiculo=@pmVehiculo,Agencia=@pmAgencia,CentroCosto=@pmCentroCosto,SubCentro=@pmSubCentro,Vencimiento=@pmVencimiento,BaseGravable=@pmBaseGravable,TarifaPred=@pmTarifaPred,Naturaleza=@pmNaturaleza
,Ajustable=@pmAjustable,CuentaAjuDeb=@pmCuentaAjuDeb,CuentaAjuCre=@pmCuentaAjuCre,Cheques=@pmCheques,Referencias=@pmReferencias,CdTipDoc=@pmCdTipDoc,FechaAct=@pmFechaAct,Inactivo=@pmInactivo,Afijos=@pmAfijos
,ListarConc=@pmListarConc,ReqAutorizac=@pmReqAutorizac,AntProveedor=@pmAntProveedor
 WHERE IdCuenta=@pmIdCuenta
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOilEntradaProd] @pmTipEnt VARCHAR(3),@pmNumEntrada INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdTanque VARCHAR(4),@pmIdProducto VARCHAR(16),@pmCantidad DECIMAL(16,6),@pmUndMed VARCHAR(10),@pmTemperatura DECIMAL(16,6),@pmUnidTemp CHAR(1),@pmValorAPI DECIMAL(16,6),@pmValorVCW DECIMAL(16,6),@pmAPI_Corregido DECIMAL(16,6),@pmDensidad DECIMAL(16,6),@pmCantCargue DECIMAL(16,6)
,@pmTempCargue DECIMAL(16,6),@pmAPI_Cargue DECIMAL(16,6),@pmVCW_Cargue DECIMAL(16,6),@pmPesoNeto DECIMAL(16,6),@pmVrUnitario DECIMAL(16,6),@pmUMCargue VARCHAR(10)
 AS 
 INSERT INTO Trn_OilEntradaProd (TipEnt,NumEntrada,IdCia,Item,IdTanque,IdProducto,Cantidad,UndMed,Temperatura,UnidTemp,ValorAPI,ValorVCW,API_Corregido,Densidad,CantCargue,TempCargue,API_Cargue,VCW_Cargue,PesoNeto,VrUnitario,UMCargue)
 VALUES (@pmTipEnt,@pmNumEntrada,@pmIdCia,@pmItem,@pmIdTanque,@pmIdProducto,@pmCantidad,@pmUndMed,@pmTemperatura,@pmUnidTemp,@pmValorAPI,@pmValorVCW,@pmAPI_Corregido,@pmDensidad,@pmCantCargue,@pmTempCargue,@pmAPI_Cargue,@pmVCW_Cargue,@pmPesoNeto,@pmVrUnitario,@pmUMCargue)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpRegimenDian] @pmIdRegimen VARCHAR(4),@pmRegimen VARCHAR(50),@pmCodFiscal VARCHAR(20),@pmInactivo BIT
 AS 
 UPDATE RegimenDian SET Regimen=@pmRegimen,CodFiscal=@pmCodFiscal,Inactivo=@pmInactivo
 WHERE IdRegimen=@pmIdRegimen
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsRegimenDian] @pmIdRegimen VARCHAR(4),@pmRegimen VARCHAR(50),@pmCodFiscal VARCHAR(20),@pmInactivo BIT
 AS 
 INSERT INTO RegimenDian (IdRegimen,Regimen,CodFiscal,Inactivo)
 VALUES (@pmIdRegimen,@pmRegimen,@pmCodFiscal,@pmInactivo)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryRegimenDian] @pmIdRegimen VARCHAR(4)
 AS 
 IF @pmIdRegimen IS NULL
  SELECT IdRegimen,Regimen,CodFiscal
  FROM RegimenDian
  WHERE Inactivo=0
 ORDER BY Regimen
 ELSE
  SELECT IdRegimen,Regimen,CodFiscal,Inactivo
  FROM RegimenDian
  WHERE IdRegimen=@pmIdRegimen
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paDelRegimenDian] @pmIdRegimen VARCHAR(4)
 AS 
 DELETE FROM RegimenDian
 WHERE IdRegimen=@pmIdRegimen
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsProdEntrega] @pmTipDoc VARCHAR(3),@pmNumEntrega INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmTipOrden VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrden CHAR(2),@pmFecEntrega SMALLDATETIME,@pmCostoTotal MONEY,@pmDescuento MONEY,@pmCantTotal DECIMAL(14,4),@pmTipoEntrega VARCHAR(10),@pmNitTercero VARCHAR(16)
,@pmNomContacto VARCHAR(150),@pmTelContacto VARCHAR(30),@pmEdoOrden INT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmCodConcepto VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmNumDev INT,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_ProdEntrega (TipDoc,NumEntrega,IdCia,Fecha,TipOrden,NumOrden,IdCiaOrden,FecEntrega,CostoTotal,Descuento,CantTotal,TipoEntrega,NitTercero,NomContacto,TelContacto,EdoOrden,Anulado,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario,CodConcepto,TipCom,Comprobante,IdCiaCom,NumDev)
 VALUES (@pmTipDoc,@pmNumEntrega,@pmIdCia,@pmFecha,@pmTipOrden,@pmNumOrden,@pmIdCiaOrden,@pmFecEntrega,@pmCostoTotal,@pmDescuento,@pmCantTotal,@pmTipoEntrega,@pmNitTercero,@pmNomContacto,@pmTelContacto,@pmEdoOrden,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmCodConcepto,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmNumDev)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdEntrega] @pmTipDoc VARCHAR(3),@pmNumEntrega INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumEntrega,IdCia,Fecha,TipOrden,NumOrden,IdCiaOrden,FecEntrega,CostoTotal,Descuento,CantTotal,TipoEntrega,NitTercero,NomContacto,TelContacto,EdoOrden,Anulado,FecDev,Observacion,IdEstado
,CodConcepto,TipCom,Comprobante,IdCiaCom,NumDev,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_ProdEntrega
 WHERE TipDoc=@pmTipDoc AND NumEntrega=@pmNumEntrega AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpProdEntrega] @pmTipDoc VARCHAR(3),@pmNumEntrega INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmTipOrden VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrden CHAR(2),@pmFecEntrega SMALLDATETIME,@pmCostoTotal MONEY,@pmDescuento MONEY,@pmCantTotal DECIMAL(14,4),@pmTipoEntrega VARCHAR(10),@pmNitTercero VARCHAR(16)
,@pmNomContacto VARCHAR(150),@pmTelContacto VARCHAR(30),@pmEdoOrden INT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmCodConcepto VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmNumDev INT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_ProdEntrega SET Fecha=@pmFecha,TipOrden=@pmTipOrden,NumOrden=@pmNumOrden,IdCiaOrden=@pmIdCiaOrden,FecEntrega=@pmFecEntrega,CostoTotal=@pmCostoTotal,Descuento=@pmDescuento,CantTotal=@pmCantTotal,TipoEntrega=@pmTipoEntrega,NitTercero=@pmNitTercero,NomContacto=@pmNomContacto,TelContacto=@pmTelContacto,EdoOrden=@pmEdoOrden
,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,CodConcepto=@pmCodConcepto,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,NumDev=@pmNumDev,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumEntrega=@pmNumEntrega AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,MgenCont,VrImvCosto,TarifaIco,VrImpCon,CantObseq,VrIvaObseq,BaseIvaCom,ImpCarbono,IngBaseCom,TarifaStc,SobtasaCons,CodTarIco,BaseIvp,TarifaIvp,IvaIngProd
 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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),@pmMgenCont  DECIMAL(14,4),@pmVrImvCosto DECIMAL(16,4),@pmTarifaIco DECIMAL(14,4),@pmVrImpCon DECIMAL(16,4),@pmCantObseq DECIMAL(14,4),@pmVrIvaObseq MONEY,@pmBaseIvaCom DECIMAL(16,4),@pmImpCarbono DECIMAL(14,4),@pmIngBaseCom DECIMAL(16,4)
,@pmTarifaStc DECIMAL(14,4),@pmSobtasaCons DECIMAL(16,4),@pmCodTarIco VARCHAR(4),@pmBaseIvp MONEY,@pmTarifaIvp DECIMAL(14,4),@pmIvaIngProd MONEY 
 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,MgenCont,VrImvCosto,TarifaIco,VrImpCon,CantObseq,VrIvaObseq,BaseIvaCom,ImpCarbono,IngBaseCom,TarifaStc,SobtasaCons,CodTarIco,BaseIvp,TarifaIvp,IvaIngProd)
 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,@pmMgenCont,@pmVrImvCosto,@pmTarifaIco,@pmVrImpCon,@pmCantObseq,@pmVrIvaObseq,@pmBaseIvaCom,@pmImpCarbono,@pmIngBaseCom,@pmTarifaStc,@pmSobtasaCons,@pmCodTarIco,@pmBaseIvp,@pmTarifaIvp,@pmIvaIngProd)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,CodTarRet AS tmCodTarRet
 FROM  Trn_Kardex
 WHERE   TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
  AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
 ORDER BY Item
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraLiquida] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdVehiculo VARCHAR(10),@pmIdPoseedor VARCHAR(16)
,@pmIdConductor VARCHAR(16),@pmVrTotal MONEY, @pmCxPagar BIT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmFecIniCau SMALLDATETIME,@pmFecFinCau SMALLDATETIME,@pmFecIniOdp SMALLDATETIME,@pmFecFinOdp SMALLDATETIME,@pmModLiquida VARCHAR(10)
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_TraLiquida (TipDoc,Liquidacion,IdCia,Fecha,IdConcepto,IdVehiculo,IdPoseedor,IdConductor,VrTotal,CxPagar,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado
,FecIniCau,FecFinCau,FecIniOdp,FecFinOdp,ModLiquida,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmLiquidacion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdVehiculo,@pmIdPoseedor,@pmIdConductor,@pmVrTotal,@pmCxPagar,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion
,@pmIdEstado,@pmFecIniCau,@pmFecFinCau,@pmFecIniOdp,@pmFecFinOdp,@pmModLiquida,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraLiquida] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Liquidacion,IdCia,Fecha,IdConcepto,IdVehiculo,IdPoseedor,IdConductor,VrTotal,CxPagar,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,Observacion,IdEstado,FecIniCau,FecFinCau,FecIniOdp,FecFinOdp,ModLiquida,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraLiquida
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraLiquida] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdVehiculo VARCHAR(10),@pmIdPoseedor VARCHAR(16),@pmIdConductor VARCHAR(16)
,@pmVrTotal MONEY,@pmCxPagar BIT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmFecIniCau SMALLDATETIME,@pmFecFinCau SMALLDATETIME,@pmFecIniOdp SMALLDATETIME,@pmFecFinOdp SMALLDATETIME,@pmModLiquida VARCHAR(10),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_TraLiquida SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdVehiculo=@pmIdVehiculo,IdPoseedor=@pmIdPoseedor,IdConductor=@pmIdConductor,VrTotal=@pmVrTotal,CxPagar=@pmCxPagar,TipCom=@pmTipCom,Comprobante=@pmComprobante
,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecIniCau=@pmFecIniCau,FecFinCau=@pmFecFinCau,FecIniOdp=@pmFecIniOdp,FecFinOdp=@pmFecFinOdp
,ModLiquida=@pmModLiquida,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraDevLiq] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Liquidacion,IdCiaDoc,FecDoc,IdVehiculo,IdPoseedor,IdConductor,VrTotal,ModdDev,OrigenAdd
,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_TraDevLiq
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelTraDevLiq] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_TraDevLiq
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTraDevLiq] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME
,@pmIdVehiculo VARCHAR(10),@pmIdPoseedor VARCHAR(16),@pmIdConductor VARCHAR(16),@pmVrTotal MONEY,@pmModdDev VARCHAR(10),@pmOrigenAdd VARCHAR(10),@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_TraDevLiq (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Liquidacion,IdCiaDoc,FecDoc,IdVehiculo,IdPoseedor,IdConductor,VrTotal,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmLiquidacion,@pmIdCiaDoc,@pmFecDoc,@pmIdVehiculo,@pmIdPoseedor,@pmIdConductor,@pmVrTotal,@pmModdDev,@pmOrigenAdd
,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevLiqDoc] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,FecDoc,IdVehiculo,IdPoseedor,IdConductor,ModdDev
 FROM Trn_TraDevLiq
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCiaDoc=@pmIdCiaDoc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenServOpe] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=NULL
AS
SELECT O.TipDoc AS IdTipDoc,TipoDoc,O.NumOrden AS Num_Orden,O.IdCia AS CdCia,Compania,O.Fecha,T.TipoId AS ClieTipoId,O.IdCliente,T.Dv AS ClieDv,T.RazonSocial AS NomCliente
,O.IdVehiculo AS PlacaVeh,NumVeh,O.TipoAfiVehic,O.Modalidad,TipoOrden,O.VrTotal AS ValorTotal,O.VrCosto AS TotalTercero,O.Cantidad AS CantTotal,O.VrAnticipo
,D.Item,D.IdMercancia AS CdMercancia,D.Descripcion,D.Cantidad,D.UndMed,UM.Unidad,D.VrUnitario,D.TarifaPago,D.DocCliente
,O.IdEstacion AS CdCampo,EstServicio AS NomCampo,O.CdRuta,Ruta,O.IdOrigen AS CdOrigen,LO.Localidad AS NomOrigen,O.IdDestino AS CdDestino,LD.Localidad AS NomDestino,O.IdAgencia AS CdAgencia,Agencia
,O.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor
,NomEstacion,TipoResiduo,O.RemCliente,NomRecibido,O.FecDespacho,O.FecRecibo,O.FecIngCertif,O.FecCertificado,O.NumRemesa,O.CdCiaRem,kmtInicial,kmtFinal,Cantkms,NumAprob,IdCiaApr,FecAprob,UsuAprob,ObservAprob
,O.Anulado,O.FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,ED.Estado,O.TimeSys AS FechaCrea,O.FecUpdate AS FechaModif,O.IdCiaCrea AS CdCiaCrea,O.IdUsuario AS CdUsuario,Usuario
--cumplido
,EstCumplido,CM.Cumplido AS NumCumplido,CM.IdCia AS CiaCump,CM.Fecha AS FechaCump,CM.DescServicio AS CumpServicio,CM.Observacion AS ObservCump
,CM.IdMercancia AS CdMercanciaCump,CM.Descripcion AS DescripCump,CM.Cantidad AS CantCump,CM.UndMed AS UndMedCump,UMC.Unidad AS UnidadCump,CM.VrUnitario AS TarifCump
,CM.TarifaPago AS TarifPagoCump
--liquidacion
,O.EstOrden,O.TipLiq,O.NumLiquida,O.CdCiaLiq,L.Fecha AS FecLiquida,L.VrTotal AS LiqTotal,L.VrAnticipo AS LiqAnticipo,L.VrIva AS LiqIva,L.VrRetencion AS LiqRetencion
,L.VrReteIca,L.VrReteCREE,L.VrReteIva,L.VrOtros,L.VrDescuento,L.VrNeto,L.Cantidad AS LiqCantidad,L.TarifaPago AS LiqTarifaPago,L.TarifaImp,L.TarifaRet,L.TarifaIca,L.TarifaRiv
,L.Referencia,L.VrGastos,dbo.FuncOsaListaAntcipos(O.NumOrden,O.IdCia) AS Anticipos,L.TipEgr,L.Egreso,L.IdCiaEgr,EG.Fecha AS FecEgreso,EG.NumCheque,EGT.TotalEgresos
--Factura
,O.EstFactura,O.TipFact,O.NumFactura,O.CdCiaFact,FactFecha,FactCant,FactTarifClie,FactTotal,FactIva,FactTarifIVA,FactCosto,FactIdCli,TCF.RazonSocial AS FactNomCliente
--Inf vehiculo
,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,LC.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.TelMovil AS TelCelular,T.e_mail AS TercEmail
--Vehiculo
,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdColor AS CdColor,NomColor,Modelo,NumMotor,SerieChasis,NumSerie,NitEmpresa,NE.RazonSocial AS VehNomEmpresa
,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,V.Descripcion AS VehDescripcion
FROM Trn_TraOrdenServ AS O INNER JOIN Trn_TraOrdenDet AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.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 Terceros AS NP ON O.IdPoseedor=NP.IdTercero
INNER JOIN Terceros AS CDT ON O.IdConductor=CDT.IdTercero INNER JOIN EdsServicio AS E ON O.IdEstacion=E.IdEstacion
INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed INNER JOIN Localidades AS LC ON T.IdLocal=LC.IdLocal
INNER JOIN Vehiculos AS V ON O.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 TiposCol AS CL ON V.IdColor=CL.IdColor 
LEFT JOIN Localidades AS LD ON O.IdDestino=LD.IdLocal LEFT JOIN Agencias AS A ON O.IdAgencia=A.IdAgencia
LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
LEFT JOIN Trn_TraOrdenLiq AS L ON O.TipLiq=L.TipDoc AND O.NumLiquida=L.Liquidacion AND O.CdCiaLiq=L.IdCia
LEFT JOIN Localidades AS LO ON O.IdOrigen=LO.IdLocal LEFT JOIN Rutas AS RT ON O.CdRuta=RT.IdRuta
--Subconsulta cumplido
LEFT JOIN (SELECT C.Cumplido,C.IdCia,C.Fecha,C.TipOds,C.NumOrden,C.IdCiaOds,C.FecDespacho,C.FecRecibo,C.FecIngCertif,C.FecCertificado,C.DescServicio
,C.RemCliente,C.Funcionario,C.Observacion,D.IdMercancia,D.Descripcion,D.Item,D.Cantidad,D.UndMed,D.VrUnitario,D.TarifaPago,D.DocCliente
FROM Trn_TraOrdenCum AS C INNER JOIN Trn_TraOrdenCumDet AS D ON C.TipDoc=D.TipDoc AND C.Cumplido=D.Cumplido AND C.IdCia=D.IdCia
WHERE C.Anulado=0) AS CM ON O.TipDoc=CM.TipOds AND O.NumOrden=CM.NumOrden AND O.IdCia=CM.IdCiaOds AND D.Item=CM.Item
LEFT JOIN Sys_Um AS UMC ON CM.UndMed=UMC.UndMed
--Subconsulta factura
LEFT JOIN (SELECT F.TipDoc,F.Factura,F.IdCia,D.TipRem,D.Remesa,D.IdCiaRem,F.IdCliente AS FactIdCli,F.Fecha AS FactFecha,SUM(D.Cantidad) AS FactCant,SUM(D.VrUnitario*D.Cantidad) AS FactTotal
,SUM(D.VrCosto*D.Cantidad) AS FactCosto,SUM(D.VrImpuesto) AS FactIva,MAX(D.TarifaIva) AS FactTarifIVA,MAX(D.VrUnitario) AS FactTarifClie,MAX(D.VrCosto) AS FactTarifTerc
FROM Trn_Facturas AS F INNER JOIN Trn_TraFacRemesas AS D ON F.TipDoc=D.TipDoc AND F.Factura=D.Factura AND F.IdCia=D.IdCia
WHERE F.Anulado=0 GROUP BY F.TipDoc,F.Factura,F.IdCia,D.TipRem,D.Remesa,D.IdCiaRem,F.IdCliente,F.Fecha) AS FS ON O.TipFact=FS.TipDoc AND O.NumFactura=FS.Factura AND O.CdCiaFact=FS.IdCia
AND O.TipDoc=FS.TipRem AND O.NumOrden=FS.Remesa AND O.IdCia=FS.IdCiaRem
LEFT JOIN Terceros AS TCF ON FS.FactIdCli=TCF.IdTercero
LEFT JOIN Trn_Comprobantes AS EG ON L.TipEgr=EG.TipCom AND L.Egreso=EG.Comprobante AND L.IdCiaEgr=EG.IdCia
LEFT JOIN (SELECT EO.TipOdp AS TipOrden,EO.OrdPago AS NumOPago,EO.IdCiaOdp AS CdCiaOpago,SUM(VrAbonado) AS TotalEgresos
FROM Trn_TraEgrOrden AS EO INNER JOIN Trn_Comprobantes AS E ON EO.TipCom=E.TipCom AND EO.NumEgreso=E.Comprobante AND EO.IdCia=E.IdCia
WHERE E.Anulado=0 GROUP BY EO.TipOdp,EO.OrdPago,EO.IdCiaOdp) AS EGT ON O.TipLiq=EGT.TipOrden AND O.NumLiquida=EGT.NumOPago AND O.CdCiaLiq=EGT.CdCiaOpago
WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND O.IdCliente LIKE ISNULL(@pmIdCliente,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryMayContratosRel]  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdTipo VARCHAR(4)=Null
 AS 
 SELECT C.NContrato AS NumContrato,C.IdCia AS CdCia,Compania,Fecha,FechaInicio,FechaFirma,IdCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS CdAgencia,Agencia,ClaseContrato,C.IdTipo AS CdTipo,TipContrato
,DescTipoCont,C.IdBonif AS CdBonif,TipoBonif,ConvBonif,CuotaFija,CondPago,C.DiasGracia AS Dias_Gracia,ModPlazo,C.Plazo AS PlazoContrato,FechaVence,VrCuota,CuotaMin,CuotaBase,ModCuota
,C.IdBandera AS CdBandera,TipoBandera,C.IdVend AS CdVend,V.RazonSocial AS Vendedor,CdAprobNeg,TA.TipoAprob AS Tipo_Aprob,AprobJunta,FechaAprob,ObservAprob,ArchivoAprob,C.Observacion AS Observ,NumInterno
,GarantHipoteca,GarantPrenda,GarantPoliza,GarantBanco,GarantPagare,GarantOtro,CdTipoBien,TipoBien,VrAvaludo,NroEscritura,CdTipoPoliza,TipoPoliza,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
,ModRecursos,VrInversion,VrInventario,InvCombus,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,TBO.TipoBloqueo AS ConTipoBloq,EdoDocmtos,C.DescEdoDoc AS ConDescEstdo,RentabInicial,GraciaBonif
,ActFechaIni,CdZonaCont,TZN.TipoZona AS TipoZonaCont,ContVigente,VolContrac,DescHipotec,VrAvaComerc,FecRegEscritura,NotariaEsc,CdTipPrenda,TipoPrenda,VrAvaPrenda,PolizaPrenda,FecVigPrenda,VrAsegPrenda,ContOperando
,C.IdEstado AS CdEstado,Estado,NColor,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS CdUsuario,Usuario,dbo.FuncMayAgencias(C.NContrato,C.IdCia) AS CantAgencias,dbo.FuncMayBonifTarif(C.NContrato,C.IdCia)  AS TarifBonif
,dbo.FuncMayNomAgencia(C.NContrato,C.IdCia) AS NombreEds
--datos 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.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,CLI.FecUpCupo AS FechaActCupo,Cmntario1,Cmntario2,Cmntario3
,CLI.IdPlazo AS CliCdPlazo,PZ.Plazo AS PlazoCli,CLI.CdPlazoComb AS CdPlazoComb,PZC.Plazo AS PlazoComb,CdTipBloq,TBC.TipoBloqueo AS CliTipoBloq ,CLI.DescEdoDoc AS CliDescEdoDoc,EdoRadicaDoc,FecPlazoDoc
--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.IdPlazo AS AgeCdPlazo,PZA.Plazo AS AgePlazo,A.CdPlazoComb AS AgeCdPlazoComb,PZAC.Plazo AS AgePlazoComb,VrCupoCre,A.FecUpCupo AS AgeFecActCupo,CodSicom,A.FecIngreso AS AgeFecIngreso,Comentarios
,A.TipoZona AS AgeTipoZona,CdTipAgenc,TipoAgencia,CdSubAgenc,SubTipoAgenc,CdGrupAgenc,GrupoAgenc,CdSubGrupAgenc,SubGrupoAgenc,CdFntePago,FuentePago,PlazoSobtasa,DocFinanc,FecVigCond,TasaIntArranq
 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 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 C.IdCliente=CLI.IdClie  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 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 Plazos AS PZ ON CLI.IdPlazo=PZ.IdPlazo 
 LEFT JOIN  TiposBloq AS TBO ON C.CdTipoBloq=TBO.IdTipBloq LEFT JOIN MayTiposAprob AS TA ON C.CdAprobNeg=TA.IdAprob
 LEFT JOIN MayTiposBienes AS TBN ON C.CdTipoBien=TBN.IdTipBien LEFT JOIN MayTiposPoliza AS TPZ ON C.CdTipoPoliza=TPZ.IdTipoPol
 LEFT JOIN TiposPrend AS TPD ON C.CdTipPrenda=TPD.IdPrend LEFT JOIN TiposZona AS TZN ON C.CdZonaCont=TZN.IdTipZona
 LEFT JOIN Plazos AS PZC ON CLI.CdPlazoComb=PZC.IdPlazo LEFT JOIN TiposBloq AS TBC ON CLI.CdTipBloq=TBC.IdTipBloq
 LEFT JOIN  Agencias   AS A ON C.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Plazos AS PZA ON A.IdPlazo=PZA.IdPlazo LEFT JOIN Plazos AS PZAC ON A.CdPlazoComb=PZAC.IdPlazo
 LEFT JOIN TiposAgenc AS TAG ON A.CdTipAgenc=TAG.IdTipAgenc LEFT JOIN TiposComAgenc AS STA ON A.CdSubAgenc=STA.IdSubAgenc
 LEFT JOIN GruposAgenc AS GA ON A.CdGrupAgenc=GA.IdGrupo LEFT JOIN SubGruposAgenc AS SGA ON A.CdSubGrupAgenc=SGA.IdSubAgenc
 LEFT JOIN FuentesPago AS FP ON A.CdFntePago=FP.IdFuente 
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND C.IdTipo LIKE ISNULL(@pmIdTipo,'%') 
 ORDER BY C.NContrato
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_AuxInvUbic] @pmtmEst CHAR(2)
 AS 
 SELECT tmIdProducto,DescripProd,tmIdCia,Compania,tmIdBodega,Bodega,tmIdUbic,Ubicacion
,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,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
,dbo.FuncAuxInvNivelMin(tmIdProducto,tmIdCia) AS ExtciaMin,dbo.FuncAuxInvNivelMax(tmIdProducto,tmIdCia) AS ExtciaMax
,dbo.FuncAuxInvSaldoAct(tmIdProducto,tmIdCia,tmIdBodega) AS ExtciaAct --,ExtciaMin,ExtciaMax,ExtciaAct
,Factor1,Factor2,Factor3,Factor4,Factor5,PM.Inactivo AS ProdInactivo,Pasillo,Nivel,Profdad,Notas
 FROM tm_AuxInvUbic 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 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 LEFT JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 LEFT JOIN BodUbic AS U ON A.tmIdUbic=U.IdUbic
 WHERE tmEst=@pmtmEst
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraRemesaMuc] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
SELECT R.TipDoc AS CdTipDoc,R.NumOrden AS NumRemesa,R.IdCia AS CdCia,Compania,R.Fecha AS FecRemesa,FecDespacho,IdCliente,T.RazonSocial AS NomCliente,R.IdAgencia AS CdAgencia,Agencia
,R.IdVehiculo AS PlacaVeh,R.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,R.TipoAfiVehic AS TipoAfiVeh,R.Modalidad AS TipoRemesa,R.TipDcm AS TipOdc,R.NumDocmto AS nOCargue,R.IdCiaDcm AS CdCiaOdc,R.FechaDcm AS FecOCargue
,EstCumplido,EstFactura,CdConcepto,Concepto,SerieGuia,NumGuia,R.Observacion AS Observ,R.IdEstado AS CdEdstado,RA.TipoRuta,RA.TipoMintrans,RA.TipOrdMud,RA.NumInvent,RA.IdCiaInv AS CdCiaInv,R.nRemolque AS RemRemolque
--detalles
,Item,IdMercancia,DescripMcias,D.Cantidad AS Cant,PesoNeto,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,D.Volumen AS Volmen,UndVol,D.IdUnd AS CdUnd,UM.Unidad AS UndMedida,Cases,Cajas,Palets
,NitRemite,Remitente,NitDestntario,Destinatario,DirOrigen,IdOrigen,LO.Localidad AS CiuOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,DirDestino,IdDestino,LD.Localidad AS CiuDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,TarifClie,D.TarifPago AS RemTarifPago
,D.TarifTabla AS RemTarifTabla,D.VrDeclarado AS ValDeclarado,D.VrSeguro AS ValSeguro,TarifSeguro,UndTarifa,UndTarifPago
,Remision,DocCliente,Referencia1,Referencia2,Referencia3,Cumplido,IdCiaCump,FechaCump,DetalleCump
,CantidadCump,PesoCump,VolCump,CasesCump,CajasCump,PaletsCump,TarifCump,PagoCump,UndTarCump,UndTarPagoCump
--columnas ocultas
--,NumManif,IdCiaManif,TipOdp,NumeroOdp,IdCiaOdp,TarifOdp
--datos del pedido
,NumPedido,IdCiaPed,R.FechaPed AS FecPedido,ModalidadPed
--datos de factura
,TipFac,Factura,IdCiaFac,FechaFac,TarifClieFac,NumFactura,NumDevFact,FacCantidad,FacValorTotal,FacCostoTotal,FacFaltantes,FacPesoNeto
--datos de manifiesto
,TipoMuc,ISNULL(MucNumero,0) AS MucNumero,MucCdCia,FechaMuc,FecDespMuc,MucPlacaVeh,MucCdPoseedor,NP.RazonSocial AS NomPoseedor,MucCdRuta,MucTarifTabla,MucTarifPago,MUC.VrFletes AS VrTotFletes,MUC.VrRetencion AS MucVrRetencion
,MUC.VrReteIca AS MucVrReteIca,MUC.VrDescuento AS MucVrDcto,MUC.VrAnticipo AS MucVrAnticipo,VrAntAdic,MUC.TarifaRet AS MucTarifRet,MUC.TarifaIca AS MucTarifIca
,NumMintrans,EdoMintrans,MucObserv,MucTipOdp,ISNULL(MucOrdPago,0) AS MucOrdPago,MucCdCiaOdp,TipEgr,Egreso,IdCiaEgr,FechaEgr,NumCheque,TotalEgresos
,dbo.FuncMucListaAntcipos(ISNULL(MucNumero,0),MucCdCia) AS Anticipos
--datos del cliente
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,V.IdGrupo AS CdGruProp,GP.GrupoProp
,RA.TipoDevRemesa,RA.CodCiuTransb,LT.Localidad AS CiudadTransb,RA.MvoTransbordo
 FROM Trn_TraRemesa AS R INNER JOIN Trn_TraRemMcias AS D ON R.TipDoc=D.TipDoc AND R.NumOrden=D.NumOrden AND R.IdCia=D.IdCia
 INNER JOIN Companias AS CN ON R.IdCia=CN.IdCia  INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero 
 INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo INNER JOIN Terceros AS CDT ON R.IdConductor=CDT.IdTercero 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 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 LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed
 LEFT JOIN Agencias AS A ON R.IdAgencia=A.IdAgencia LEFT JOIN Conceptos AS C ON R.CdConcepto=C.IdConcepto 
 LEFT JOIN Trn_TraRemAnexo AS RA ON R.TipDoc=RA.TipDoc AND R.NumOrden=RA.NumOrden AND R.IdCia=RA.IdCia
 LEFT JOIN Localidades AS LT ON RA.CodCiuTransb=LT.IdLocal
--==== Consulta de pedidos
 LEFT JOIN (SELECT P.TipDoc AS TipoPed,P.Pedido AS nPedido,P.IdCia AS CdCiaPed,P.FecDespacho AS FechaPed,P.IdVend AS CdVend,P.Modalidad AS ModalidadPed
,Cotizacion,IdCiaCot,NumAprob,IdCiaApr,FecAprob,P.Observacion AS PedObserv,D.IdMercancia AS PedCdMcia,D.DescripMcias AS PedDescMcia
,SUM(D.Cantidad) AS PedCantidad,SUM(D.PesoNeto) AS PedPesoNeto,SUM(D.Volumen) AS PedVolumen,SUM(D.Cases) AS PedCases
,SUM(D.Cajas) AS PedCajas,SUM(D.Palets) AS PedPalets,MAX(D.TarifClie) AS PedTarifClie,MAX(D.TarifPago) AS PedTarifPago
 FROM Trn_TraPedido AS P INNER JOIN Trn_TraPedMcias AS D ON P.TipDoc=D.TipDoc AND P.Pedido=D.Pedido AND P.IdCia=D.IdCia
 WHERE P.TipDoc='PDT' AND P.Anulado=0 GROUP BY P.TipDoc,P.Pedido,P.IdCia,P.FecDespacho,P.IdVend,P.Modalidad,Cotizacion,IdCiaCot
,NumAprob,IdCiaApr,FecAprob,P.Observacion,D.IdMercancia,D.DescripMcias) AS PD ON R.NumPedido=PD.nPedido AND R.IdCiaPed=PD.CdCiaPed AND D.IdMercancia=PD.PedCdMcia
--==== consulta de facturas-detalles
 LEFT JOIN (SELECT TipRem,Remesa,IdCiaRem,ItemRem,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Cantidad*-1 ELSE Cantidad END) AS FacCantidad
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN (Cantidad*VrUnitario)*-1 ELSE Cantidad*VrUnitario END) AS FacValorTotal
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN (CantPago*VrCosto)*-1 ELSE CantPago*VrCosto END) AS FacCostoTotal
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Unidades*-1 ELSE Unidades END) AS FacUnidades
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN PesoNeto*-1 ELSE PesoNeto END) AS FacPesoNeto
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Volumen*-1 ELSE Volumen END) AS FacVolumen
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Cases*-1 ELSE Cases END) AS FacCases
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Cajas*-1 ELSE Cajas END) AS FacCajas
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Palets*-1 ELSE Palets END) AS FacPalets
,SUM(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN VrFaltante*-1 ELSE VrFaltante END) AS FacFaltantes
,MAX(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN 0 ELSE Factura END) AS NumFactura
,MAX(CASE WHEN SUBSTRING(TipDoc,1,2)='DF' THEN Factura ELSE 0 END) AS NumDevFact
FROM Trn_TraFacRemesas GROUP BY TipRem,Remesa,IdCiaRem,ItemRem) AS RF ON R.TipDoc=RF.TipRem AND R.NumOrden=RF.Remesa AND R.IdCia=RF.IdCiaRem AND D.Item=RF.ItemRem
--==== consulta de manifiestos 
 LEFT JOIN (SELECT RM.TipRem AS MucTipoRem,RM.Remesa AS MucNumRemesa,RM.IdCiaRem AS MucCdCiaRem,RM.ItemRem AS MucItemRem,RM.TipDoc AS TipoMuc,RM.Manifiesto AS MucNumero,RM.IdCia AS MucCdCia
,RM.TarifTabla AS MucTarifTabla,RM.TarifPago AS MucTarifPago,RemMintrans,M.Fecha AS FechaMuc,M.FecDespacho AS FecDespMuc,M.IdRuta AS MucCdRuta,M.IdOrigen AS MucCdOrigen,M.IdDestino AS MucCdDestino,M.IdVehiculo AS MucPlacaVeh
,M.IdConductor AS MucCedCond,M.nRemolque AS MucnRemolque,TipoAfiVehic,M.IdPoseedor AS MucCdPoseedor,VrFletes,VrRetencion,VrReteIca,VrDescuento,VrAnticipo,VrAntAdic,VrNeto,VrPagos
,VrCargos,VrDctos,TarifaFlete,TarifaRet,TarifaIca,NumMintrans,EdoMintrans,M.TipOdp AS MucTipOdp,M.OrdPago AS MucOrdPago,M.IdCiaOdp AS MucCdCiaOdp,M.FechaOdp AS MucFecOdp,M.Observacion AS MucObserv
FROM Trn_TraManifRem AS RM INNER JOIN Trn_TraManifiesto AS M ON RM.TipDoc=M.TipDoc AND RM.Manifiesto=M.Manifiesto AND RM.IdCia=M.IdCia
WHERE M.Anulado=0) AS MUC ON R.TipDoc=MUC.MucTipoRem AND R.NumOrden=MUC.MucNumRemesa AND R.IdCia=MUC.MucCdCiaRem AND D.Item=MUC.MucItemRem
--===== Orden de pago
 LEFT JOIN (SELECT M.TipMuc AS OdpTipMuc,M.Manifiesto AS OdpManif,M.IdCiaMuc AS OdpCdCiaMuc,M.TipDoc AS TipOdp,M.OrdPago AS NumOPago,M.IdCia AS OdpCdCia,OP.Fecha AS FechaOdp
,VrTotalFletes,VrDescuento,VrRetencion,VrReteIca,VrAnticipos,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,VrNeto
,TarifaTabla,TarifaFlete,UnidTarifa,PesoTotal,Unidades,Volumen,TarifaRet,TarifaIca,TipoLiq,TipEgr,Egreso,IdCiaEgr,FechaEgr,NumCheque,Referencia,CantFaltante,UnidadFalt,TolFaltNeto,OP.Observacion AS OdpObserv
 FROM Trn_TraOrdenManif AS M INNER JOIN Trn_TraOrdenPago AS OP ON M.TipDoc=OP.TipDoc AND M.OrdPago=OP.OrdPago AND M.IdCia=OP.IdCia
 WHERE OP.Anulado=0) AS ODP ON MUC.MucTipOdp=ODP.TipOdp AND MUC.MucOrdPago=ODP.NumOPago AND MUC.MucCdCiaOdp=ODP.OdpCdCia
 LEFT JOIN Terceros AS NP ON MUC.MucCdPoseedor=NP.IdTercero 
-- Egresos
 LEFT JOIN (SELECT EO.TipOdp AS TipOrden,EO.OrdPago AS NumOPago,EO.IdCiaOdp AS CdCiaOpago,SUM(VrAbonado) AS TotalEgresos
 FROM Trn_TraEgrOrden AS EO INNER JOIN Trn_Comprobantes AS E ON EO.TipCom=E.TipCom AND EO.NumEgreso=E.Comprobante AND EO.IdCia=E.IdCia
 WHERE E.Anulado=0 GROUP BY EO.TipOdp,EO.OrdPago,EO.IdCiaOdp) AS EGT ON MUC.MucTipOdp=EGT.TipOrden AND MUC.MucOrdPago=EGT.NumOPago AND MUC.MucCdCiaOdp=EGT.CdCiaOpago

 WHERE R.TipDoc='RMT' AND R.Fecha BETWEEN @pmFechaIni AND  @pmFechaFin AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND R.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.Anulado=0
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraManifiestoOpe] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS 
 SELECT RM.TipDoc AS TipMuc,RM.Manifiesto AS NumManif,RM.IdCia AS CdCia,Compania,M.Fecha AS MucFecha,M.FecDespacho AS MucFecDespacho
,M.IdVehiculo AS PlacaVeh,M.TipoAfiVehic AS TipoAfiVeh,M.IdConductor AS CedCondutor,CDT.RazonSocial AS NomConductor,M.IdPoseedor AS NitPoseedor,NP.RazonSocial AS NomPoseedor,M.nRemolque AS NumRemolque
,RM.TipRem AS TipRem,RM.Remesa AS NumRemesa,RM.IdCiaRem AS CdCiaRem,R.Fecha AS FechaRem,IdMercancia,DescripMcias,D.Cantidad AS Cant,D.PesoNeto AS PesoNetoRem
,D.UndMed AS CdUmPeso,UMP.Unidad AS UmPeso,D.Volumen AS VolumenRem,UndVol,Cases,Cajas,Palets,D.TarifClie AS TarifaClie,UndTarifa,RM.TarifPago AS TarifaPago,RM.TarifTabla AS TarifaTabla,UndTarifPago
,Remision,DocCliente,Referencia1,Referencia2,Referencia3,D.IdEmp AS CdEmp,Empaque,D.IdUnd AS CdUnd,UM.Unidad AS UndMedida
,D.IdOrigen AS CdCiuOrigen,LO.Localidad AS CiuOrigen,LO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,D.IdDestino AS CdCiuDestino,LD.Localidad AS CiuDestino,LD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,M.IdRuta AS CdRuta,T.TipoId AS ClieTipId,IdCliente,T.Dv AS ClieDv,T.RazonSocial AS NomCliente,R.IdAgencia AS CdAgencia,Agencia,R.Modalidad AS TipoRemesa,NitRemite,Remitente,NitDestntario,Destinatario
,M.VrFletes AS MucVrFletes,dbo.FuncMucListaAntcipos(RM.Manifiesto,RM.IdCia) AS Anticipos,M.VrAnticipo+VrAntAdic AS VrAnticipos,M.VrRetencion AS MucVrRetencion,M.VrReteIca AS MucVrReteIca,M.Observacion AS MucObserv,M.Anulado AS MucAnulado,EstOrden
,M.EstCumplido,D.Cumplido,D.IdCiaCump,D.FechaCump,D.CantidadCump,D.PesoCump,D.TarifCump,D.PagoCump
--datos ordenes de pago
,M.TipOdp AS CdTipOdp,M.OrdPago AS NumOrden,M.IdCiaOdp AS MucCiaOdp,M.FechaOdp AS FecOrden,OP.PesoTotal AS OdpPesoNeto,OP.Unidades AS OdpCantidad,OP.Volumen AS OdpVolumen
,OP.TarifaTabla AS OdpTarifTabla,OP.TarifaFlete AS OdpTarifFlete,OP.UnidTarifa AS OdpUndTarifa,OP.VrTotalFletes AS OdpTotalFletes,OP.VrDescuento AS OdpVrDcto,OP.VrRetencion AS OdpVrRetFte
,OP.VrReteIca AS OdpReteIca,OP.VrAnticipos AS OdpAnticipos,OP.VrFaltantes AS OdpFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,OP.VrNeto AS OdpVrNetoPagar,ODP.Observacion AS OdpObserv
--egresos
,OP.TipEgr,Egreso,IdCiaEgr,EG.Fecha AS FecEgreso,EG.NumCheque,TotalEgresos
--datos remesas
,R.NumPedido AS nPedido,IdCiaPed,FechaPed,R.TipDcm AS TipOdc,R.NumDocmto AS nOCargue,R.IdCiaDcm AS CdCiaOdc,R.FechaDcm AS FecOCargue,R.Observacion AS RemObserv
--Datos del vehic/ poseedor
,NP.TipoId AS TercTipId,NP.Dv AS TercDv,NP.NomCial AS TercNomCial,NP.Direccion AS TercDireccion,NP.IdLocal AS TercCodCiudad,L.Localidad AS NomCiudad,NP.Telefono AS TercTelefono,NP.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,TPR.VehPropio AS VehEsPropio,TipoAfil,NumSoat,FecSoat,VigSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,CT.IdLugar AS CdLugar,LugarLic,FecLicencia,VigLicencia,CDT.Direccion AS CdtDireccion,CDT.Telefono AS CdtTelefono,CDT.e_mail AS CdtEmail,CDT.TelMovil AS CdtCelular
 FROM Trn_TraManifRem AS RM INNER JOIN Trn_TraManifiesto AS M ON RM.TipDoc=M.TipDoc AND RM.Manifiesto=M.Manifiesto AND RM.IdCia=M.IdCia
 INNER JOIN Trn_TraRemMcias AS D ON RM.TipRem=D.TipDoc AND RM.Remesa=D.NumOrden AND RM.IdCiaRem=D.IdCia AND RM.ItemRem=D.Item
 INNER JOIN Trn_TraRemesa AS R ON RM.TipRem=R.TipDoc AND RM.Remesa=R.NumOrden AND RM.IdCiaRem=R.IdCia
 INNER JOIN Companias AS CN ON RM.IdCia=CN.IdCia 
 INNER JOIN Localidades AS LO ON D.IdOrigen=LO.IdLocal INNER JOIN Departamentos AS DPO ON LO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS LD ON D.IdDestino=LD.IdLocal INNER JOIN Departamentos AS DPD ON LD.IdDep=DPD.IdDep
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero
 INNER JOIN Terceros AS CDT ON M.IdConductor=CDT.IdTercero INNER JOIN Terceros AS NP ON M.IdPoseedor=NP.IdTercero 
 INNER JOIN Localidades AS L ON NP.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON M.IdVehiculo=V.IdVehiculo INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor
 INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria  INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero
 LEFT JOIN Trn_TraOrdenPago AS ODP ON M.TipOdp=ODP.TipDoc AND M.OrdPago=ODP.OrdPago AND M.IdCiaOdp=ODP.IdCia
 LEFT JOIN Trn_TraOrdenManif AS OP ON M.TipOdp=OP.TipDoc AND M.OrdPago=OP.OrdPago AND M.IdCiaOdp=OP.IdCia
 LEFT JOIN Agencias AS A ON R.IdAgencia=A.IdAgencia LEFT JOIN Sys_Um AS UMP ON D.UndMed=UMP.UndMed 
 LEFT JOIN Empaques AS EQ ON D.IdEmp=EQ.IdEmp
 LEFT JOIN TercCndtores AS CT ON M.IdConductor=CT.IdConductor LEFT JOIN ExpLicencias AS EL ON CT.IdLugar=EL.IdLugar
 LEFT JOIN Trn_Comprobantes AS EG ON OP.TipEgr=EG.TipCom AND OP.Egreso=EG.Comprobante AND OP.IdCiaEgr=EG.IdCia
 --total de egresos
 LEFT JOIN (SELECT EO.TipOdp AS TipOrden,EO.OrdPago AS NumOPago,EO.IdCiaOdp AS CdCiaOpago,SUM(VrAbonado) AS TotalEgresos
 FROM Trn_TraEgrOrden AS EO INNER JOIN Trn_Comprobantes AS E ON EO.TipCom=E.TipCom AND EO.NumEgreso=E.Comprobante AND EO.IdCia=E.IdCia
 WHERE E.Anulado=0 GROUP BY EO.TipOdp,EO.OrdPago,EO.IdCiaOdp) AS EGT ON M.TipOdp=EGT.TipOrden AND M.OrdPago=EGT.NumOPago AND M.IdCiaOdp=EGT.CdCiaOpago
WHERE RM.TipDoc='MUC' AND M.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
AND  M.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND M.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraCumplidoRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME, @pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null
 AS 
 SELECT CU.TipDoc AS TipCum,CU.Cumplido AS NumCumplido,CU.IdCia AS CdCia,Compania,CU.Fecha AS FechaCum,TipMuc,CU.Manifiesto AS NumManif,IdCiaMuc,CU.IdVehiculo AS PlacaVeh,Modalidad,DiasPlazo,FecPago
,TipoMargen,MargenFalt,UndCalcFalt,TarifFaltPago,TarifFaltCobro,NRadicaDoc,IdCiaRadic,CdCiaOfic,FecRadic,CU.Anulado AS Anuldo,CU.FecDev AS FechaDev,TipoComp,NumComp,NumRadicaMT,CU.Observacion AS Observ,CU.IdEstado AS CdEstado,Estado
,CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,CdRuta,R.Ruta AS DescRuta
,CU.TimeSys AS FechaCrea,CU.FecUpdate AS FechaAct,CU.IdCiaCrea AS CdCiaCrea,CU.IdUsuario AS CdUsuario,Usuario
,M.Fecha AS FecManif,FecDespacho,M.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,nRemolque,TipoAfiVehic,M.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,M.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,VrFletes,VrRetencion,VrReteIca,VrDescuento,VrAnticipo,VrAntAdic,VrNeto,VrPagos,VrCargos,VrDctos,TarifaFlete,M.Cantidad AS CantTotal,PesoTotal
,IdLocFletes,CF.Localidad AS LugarFletes,FechaPago,PagoCargue,PagoDescargue,M.TipOdp AS TipoOdp,M.OrdPago AS NumOrdPago,M.IdCiaOdp AS CdCiaOdp,FechaOdp,EstOrden,M.Observacion AS MucObserv
,MA.TipoRuta,MA.kmsTotal,MA.NomRemite,MA.NomDestino,MA.LugarFletes,MA.NumAnticipo AS NumAnticipo,MA.NumCheque AS Num_Cheque,MA.TipoMintrans,MA.WsSeguro,MA.NumRadSeguro,CU.TipoCumpMT,CU.MotivoSusp,CU.ConsecSusp
,dbo.FuncTraCumplidoCobro(CU.TipDoc,CU.Cumplido,CU.IdCia) AS VrTotalClie,dbo.FuncTraCumplidoPago(CU.TipDoc,CU.Cumplido,CU.IdCia) AS VrTotalPago
--Datos del vehiculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,V.IdGrupo AS CdGrupoPro,GrupoProp
,M.Remesa,M.IdCiaRem,RMT.Fecha AS Fecremesa,RMT.Comprobante AS Cmpremesa,RMT.TipCom AS TipComRmt
 FROM Trn_TraCumplido AS CU INNER JOIN Companias AS CN ON CU.IdCia=CN.IdCia
 INNER JOIN EstadoDoc AS ED ON CU.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON CU.IdUsuario=U.IdUsuario
 INNER JOIN Trn_TraManifiesto AS M ON CU.TipMuc=M.TipDoc AND CU.Manifiesto=M.Manifiesto AND CU.IdCiaMuc=M.IdCia
 INNER JOIN Trn_TraManifAnexo AS MA ON CU.TipMuc=MA.TipDoc AND CU.Manifiesto=MA.Manifiesto AND CU.IdCiaMuc=MA.IdCia
 INNER JOIN Terceros AS  CDT ON M.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS NP ON M.IdPropietario=NP.IdTercero   INNER JOIN Terceros AS T ON M.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON CU.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Localidades AS CO ON CU.CdOrigen=CO.IdLocal LEFT JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 LEFT JOIN Localidades AS CD ON CU.CdDestino=CD.IdLocal LEFT JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 LEFT JOIN Rutas AS R ON CU.CdRuta=R.IdRuta
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 LEFT JOIN (SELECT NumOrden,IdCia,Fecha,TipCom,Comprobante,IdCiaCom FROM Trn_TraRemesa WHERE TipDoc='RMT') AS RMT ON M.Remesa=RMT.NumOrden AND M.IdCiaRem=RMT.IdCia 
 WHERE CU.TipDoc=@pmTipDoc AND CU.Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND  CU.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND  CU.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND M.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%') AND M.IdConductor LIKE ISNULL(@pmIdConductor,'%')
  ORDER BY CU.IdCia,CU.Cumplido
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[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,FecUltParc,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
--Dias parciales disfrutados
,dbo.FuncNomVacParcial(E.IdEmpleado,ISNULL(FecUltVac,FecIngreso),@pmFecVacFin) AS DiasDisf
,dbo.FuncNomVacParcRem(E.IdEmpleado,ISNULL(FecUltVac,FecIngreso),@pmFecVacFin) AS DiasRem
 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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOpedidoOpp] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdInsumo VARCHAR(16)=Null,@pmIdTipProc VARCHAR(4)=Null,@pmProcFinal BIT=Null,@pmModalidad VARCHAR(10)=NuLL,@pmFecOrdenIni SMALLDATETIME=Null,@pmFecOrdenFin SMALLDATETIME=Null
,@pmNumOrdenIni INT=Null,@pmNumOrdenFin INT=Null
AS
--desde @pmIdInsumo: son parametros opcionales para comprobar en funciones IF EXISTS
SELECT O.TipDoc,O.Pedido,O.IdCia,Compania,O.Fecha,O.IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia,Agencia,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,O.TarifaCom
,D.Item,D.IdProducto,DescripProd,D.IdBodega AS CdBodega,B.Bodega,D.Salidas,D.Unidades AS CantAprobado,D.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,D.VrUnitario,D.VrPrecio,D.TarifaIva
,D.VrIvaSal,D.TarifaDct,D.VrDctoSal,D.Descripcion,D.Comptmntos AS DescTallas,D.galsneto AS CantFact,D.TipOrd AS DetTipDoc,D.NumOrden AS DetNumDoc,D.IdCiaOrd AS DetCiaDoc,D.ItemCombo AS ItemDoc
,D.galsbruto AS CantRemision,D.EsProdBase,(CASE WHEN D.Unidades>0 THEN D.Unidades ELSE D.Salidas END) AS Cant_AprobPed
,C.NumCons,C.IdCia AS IdCiaCons,DC.Item AS ItemCons,DC.CantPedido AS CantPedidoCons,DC.CantAprob AS CantAprobCons,DC.CantTdo AS CantTdoCons,DC.CdBodTdo,BT.Bodega AS BodegaTdo
--resumen procesos
,OP.CantProcCons,OP.NumOrdenProc,OP.CiaOrdenProc,OP.Cant_OrdenProc,OP.Cant_Arreglos,OP.NumEntrega,OP.IdCiaEntrega,OP.Cant_Entrega,OP.Cant_Rechazo,OP.CantEntInvent,OP.NumEntInvent,OP.CiaEntInvent,OP.Costo_Entrega,OP.Costo_Arreglos
,IC.Cant_Salidas,IC.Costo_Insumos,OP.Ord_Terminados,OP.Ord_Pendientes
,O.DiasEntraga,O.NitContac AS NitContacto,O.NomContac AS NomContacto,O.TelContac AS TelContacto,O.emlContac AS EmailContacto,O.CargoContac,O.FechaVence
,O.MulPlazos,O.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,O.RefPedido,O.NumAutSicom AS NumOrdenCom,O.TipFac,O.Factura,O.IdCiaFac,O.FechaFact,O.NumAprob,O.IdCiaApr,O.FecAprob,O.DetalleAprob
,O.TipRem,D.Remision,D.IdCiaRem,O.Modalidad,O.Vigencia,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.Anulado,O.TimeSys AS FechaCrea,O.IdUsuario AS CdUsuario,Usuario
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.SitioWeb AS TercSitioWeb
,P.Referencia AS Prod_Referencia,P.TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,P.IdMarca AS CdMarca,Marca,P.Tamano
,P.ExtciaMin,P.ExtciaMax,P.ExtciaAct,P.VrCostAnt,P.VrCosto,P.VrCostPmd,P.FecUltcom,P.FecUltVta,P.Seriales,P.Lotes,P.Combo,P.Tanques,P.Tallaje,P.DescripLong
,P.Precio1,P.Precio2,P.Precio3,P.Precio4,P.Precio5
FROM Trn_Opedido AS O INNER JOIN Trn_Kardex AS D ON O.TipDoc=D.TipDoc AND O.Pedido=D.Documento AND O.IdCia=D.IdCia
INNER JOIN Companias AS CI ON O.IdCia=CI.IdCia INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie
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 ProdMcias AS P ON D.IdProducto=P.IdProducto INNER JOIN Bodegas AS B ON D.IdBodega=B.IdBodega
INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd INNER JOIN SubGrupos AS S ON P.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 P.IdMarca=M.IdMarca
LEFT JOIN Trn_ProdConsDet AS DC ON D.TipDoc=DC.TipPed AND D.Documento=DC.Pedido AND D.IdCia=DC.IdCiaPed AND D.Item=DC.ItemPed
LEFT JOIN Trn_ProdConsolida AS C ON DC.TipDoc=C.TipDoc AND DC.NumCons=C.NumCons AND DC.IdCia=C.IdCia
LEFT JOIN Bodegas AS BT ON DC.CdBodTdo=BT.IdBodega
--PROCESOS
LEFT JOIN (SELECT CP.TipDoc,CP.NumCons,CP.IdCia,CP.TipPed,CP.Pedido,CP.IdCiaPed,CP.ItemPed
,SUM(CASE WHEN CP.ProcFinal=1 THEN CP.Cantidad ELSE 0 END) AS CantProcCons
,MAX(CASE WHEN ISNULL(O.Anulado,0)=0 AND CP.ProcFinal=1 THEN OD.NumOrden ELSE 0 END) AS NumOrdenProc
,MAX(CASE WHEN ISNULL(O.Anulado,0)=0 AND CP.ProcFinal=1 THEN OD.IdCia ELSE Null END) AS CiaOrdenProc
,SUM(CASE WHEN ISNULL(O.Anulado,0)=0 AND CP.ProcFinal=1 THEN OD.Cantidad ELSE 0 END) AS Cant_OrdenProc
,SUM(CASE WHEN ISNULL(O.Anulado,0)=0 AND CP.ProcFinal=1 AND O.Modalidad='ARREGLO' THEN OD.Cantidad ELSE 0 END) AS Cant_Arreglos
,SUM(CASE WHEN ISNULL(O.Anulado,0)=0 AND CP.ProcFinal=1 THEN OD.CantEntrada ELSE 0 END) AS CantEntInvent
,MAX(CASE WHEN ISNULL(O.Anulado,0)=0 AND CP.ProcFinal=1 THEN OD.NumEntrada ELSE 0 END) AS NumEntInvent
,MAX(CASE WHEN ISNULL(O.Anulado,0)=0 AND CP.ProcFinal=1 THEN OD.CdCiaEnt ELSE 0 END) AS CiaEntInvent
,MAX(CASE WHEN ISNULL(E.Anulado,0)=0 AND CP.ProcFinal=1 THEN ED.NumEntrega ELSE 0 END) AS NumEntrega
,MAX(CASE WHEN ISNULL(E.Anulado,0)=0 AND CP.ProcFinal=1 THEN ED.IdCia ELSE Null END) AS IdCiaEntrega
,SUM(CASE WHEN ISNULL(O.Anulado,0)=0 AND CP.ProcFinal=1 AND ISNULL(E.Anulado,0)=0 THEN ED.Cantidad+ED.Defectuoso ELSE 0 END) AS Cant_Entrega
,SUM(CASE WHEN ISNULL(O.Anulado,0)=0 AND CP.ProcFinal=1 AND ISNULL(E.Anulado,0)=0 THEN ED.Rechazado ELSE 0 END) AS Cant_Rechazo
,SUM(CASE WHEN O.Modalidad<>'ARREGLO' AND ISNULL(O.Anulado,0)=0 AND ISNULL(E.Anulado,0)=0 THEN (((ED.Cantidad+ED.Defectuoso)-ED.Rechazado)*ED.CostoUnit)-ED.VrDcto ELSE 0 END) AS Costo_Entrega
,SUM(CASE WHEN O.Modalidad='ARREGLO' AND ISNULL(O.Anulado,0)=0 AND ISNULL(E.Anulado,0)=0 THEN (((ED.Cantidad+ED.Defectuoso)-ED.Rechazado)*ED.CostoUnit)-ED.VrDcto ELSE 0 END) AS Costo_Arreglos
,SUM(CASE WHEN ISNULL(O.Anulado,0)=0 AND ((ISNULL(OD.Terminado,0)+ISNULL(OD.Defectuoso,0))-ISNULL(OD.Rechazado,0))>=ISNULL(OD.Cantidad,0) THEN 1 ELSE 0 END) AS Ord_Terminados
,SUM(CASE WHEN ISNULL(O.Anulado,0)=0 AND ((ISNULL(OD.Terminado,0)+ISNULL(OD.Defectuoso,0))-ISNULL(OD.Rechazado,0))<ISNULL(OD.Cantidad,0) THEN 1 ELSE 0 END) AS Ord_Pendientes
FROM Trn_ProdConsProc AS CP LEFT JOIN Trn_ProdOrdenDet AS OD ON CP.TipDoc=OD.TipCons AND CP.NumCons=OD.NumCons AND CP.IdCia=OD.IdCiaCons AND CP.Item=OD.ItemCons
LEFT JOIN Trn_ProdOrden AS O ON OD.TipDoc=O.TipDoc AND OD.NumOrden=O.NumOrden AND OD.IdCia=O.IdCia 
LEFT JOIN Trn_ProdEntregaDet AS ED ON OD.TipDoc=ED.TipOrden AND OD.NumOrden=ED.NumOrden AND OD.IdCia=ED.IdCiaOrden AND OD.Item=ED.ItemOrden
LEFT JOIN Trn_ProdEntrega AS E ON ED.TipDoc=E.TipDoc AND ED.NumEntrega=E.NumEntrega AND ED.IdCia=E.IdCia
WHERE CP.TipDoc='CPP' GROUP BY CP.TipDoc,CP.NumCons,CP.IdCia,CP.TipPed,CP.Pedido,CP.IdCiaPed,CP.ItemPed)
AS OP ON D.TipDoc=OP.TipPed AND D.Documento=OP.Pedido AND D.IdCia=OP.IdCiaPed AND D.Item=OP.ItemPed AND DC.TipDoc=OP.TipDoc AND DC.NumCons=OP.NumCons AND DC.IdCia=OP.IdCia
--Costo de insumos
LEFT JOIN (SELECT IM.TipDoc,IM.NumCons,IM.IdCia,IM.TipPed,IM.Pedido,IM.IdCiaPed,IM.ItemPed,SUM(IM.Cantidad) AS Cant_Insumos
,SUM(IM.CantSalidas) AS Cant_Salidas,SUM(IM.CantSalidas*IM.CostoUntSai) AS Costo_Insumos
FROM Trn_ProdConsInsu AS IM WHERE IM.TipDoc='CPP' GROUP BY IM.TipDoc,IM.NumCons,IM.IdCia,IM.TipPed,IM.Pedido,IM.IdCiaPed,IM.ItemPed) AS IC ON D.TipDoc=IC.TipPed AND D.Documento=IC.Pedido AND D.IdCia=IC.IdCiaPed AND D.Item=IC.ItemPed AND DC.TipDoc=IC.TipDoc AND DC.NumCons=IC.NumCons AND DC.IdCia=IC.IdCia
WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
AND (O.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND ISNULL(C.Anulado,0)=0
AND dbo.FuncProdPedidoInsu(O.TipDoc,O.Pedido,O.IdCia,D.IdProducto,@pmIdInsumo)=1 
AND dbo.FuncProdConsProc(DC.TipDoc,DC.NumCons,DC.IdCia,O.TipDoc,O.Pedido,O.IdCia,D.Item,@pmIdTipProc)=1
AND dbo.FuncProdConsProcFinal(DC.TipDoc,DC.NumCons,DC.IdCia,O.TipDoc,O.Pedido,O.IdCia,D.Item,@pmProcFinal)=1
AND dbo.FuncProdOrdenTipo(DC.TipDoc,DC.NumCons,DC.IdCia,DC.Item,@pmModalidad)=1
AND dbo.FuncProdOrdenFecha(DC.TipDoc,DC.NumCons,DC.IdCia,DC.Item,@pmFecOrdenIni,@pmFecOrdenFin)=1
AND dbo.FuncProdOrdenNum(DC.TipDoc,DC.NumCons,DC.IdCia,DC.Item,@pmNumOrdenIni,@pmNumOrdenFin)=1
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryProdOrdenLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdInsumo VARCHAR(16)=Null
 AS 
SELECT O.TipDoc,TipoDoc,O.NumOrden,O.IdCia,Compania,O.Fecha,O.IdTipProc,TipoProceso
,D.Item,D.IdProducto AS CdProducto,D.Descripcion,D.Cantidad,D.CostoEst,D.Terminado,D.Defectuoso,D.Rechazado
,D.MotivoDev,D.TipCons,D.NumCons,D.IdCiaCons,D.ItemCons,CD.TipPed,CD.Pedido,CD.IdCiaPed,CD.ItemPed,CD.CantAprob
,CD.Cantidad AS CantConsolida,CD.CantTdo,CD.Tallas,CD.EdoOrden AS EdoConsolida,D.TipEntInv,D.NumEntrada,D.CdCiaEnt,D.ItemKdxEnt,D.CantEntrada
,O.Modalidad,O.ConPago,O.IdTaller AS CdTaller,TL.NomTaller,TL.TelCelular,TL.TipoTaller,O.NitTercero,T.RazonSocial,O.NomContacto,O.TelContacto
,O.EdoOrden,O.ProcFinal,TP.IndOrden AS ProcOrden,O.FecEntrega,O.Num_Entrega AS NumEntArreglo,O.CdCiaEnt AS CiaEntArreglo,EG.FechaUltEnt,EG.MaxNumEntrega AS UltNumEntrega,EG.MaxIdCia AS Cd_CiaEnt
,O.Observacion,O.Anulado,O.FecDev,O.IdEstado,ED.Estado,O.TimeSys AS FechaCrea,O.IdUsuario,U.Usuario
--datos del producto
,P.DescripProd,P.TipoRef,P.Referencia,P.CodBarras,G.IdLinea,Linea,SG.IdGrupo,Grupo,P.IdSubgrupo,Subgrupo,P.IdMarca,Marca,P.Tamano,P.Color
,P.UndMed,UM.Unidad AS DescUndMed,P.IdUnd,UM.Unidad,P.Tallaje,P.Inactivo
--datos del pedidO
,OP.Fecha AS FecPedido,OP.FechaVence,OP.IdCliente,TC.RazonSocial AS NomCliente,OP.IdAgencia,OP.IdVend,VN.RazonSocial AS NomVendedor,OP.DiasEntraga,OP.RefPedido
,OP.TipFac,OP.Factura,OP.IdCiaFac,OP.FechaFact,OP.NumAutSicom as NumOrdenCom
FROM Trn_ProdOrden AS O INNER JOIN Trn_ProdOrdenDet AS D ON O.TipDoc=D.TipDoc AND O.NumOrden=D.NumOrden AND O.IdCia=D.IdCia
INNER JOIN TiposProceso AS TP ON O.IdTipProc=TP.IdTipProc INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
INNER JOIN Companias AS CI ON O.IdCia=CI.IdCia INNER JOIN EstadoDoc  AS ED ON O.IdEstado=ED.IdEstado INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
INNER JOIN Talleres AS TL ON O.IdTaller=TL.IdTaller 
INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto 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 L ON G.IdLinea=L.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
LEFT JOIN Terceros AS T ON O.NitTercero=T.IdTercero
LEFT JOIN Trn_ProdConsProc AS CD ON D.TipCons=CD.TipDoc AND D.NumCons=CD.NumCons AND D.IdCiaCons=CD.IdCia AND D.ItemCons=CD.Item
LEFT JOIN Trn_Opedido AS OP ON CD.TipPed=OP.TipDoc AND CD.Pedido=OP.Pedido AND CD.IdCiaPed=OP.IdCia
LEFT JOIN Terceros AS TC ON OP.IdCliente=TC.IdTercero LEFT JOIN Terceros AS VN ON OP.IdVend=VN.IdTercero
--datos ultima entrega
LEFT JOIN (SELECT TipOrden,NumOrden,IdCiaOrden,MAX(FecEntrega) AS FechaUltEnt,MAX(NumEntrega) AS MaxNumEntrega,MAX(IdCia) AS MaxIdCia
FROM Trn_ProdEntrega WHERE TipDoc='OEP' AND Anulado=0 GROUP BY TipOrden,NumOrden,IdCiaOrden) AS EG ON O.TipDoc=EG.TipOrden AND O.NumOrden=EG.NumOrden AND O.IdCia=EG.IdCiaOrden
WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
AND (O.IdCia=@pmIdCia OR @pmIdCia IS NULL)
AND dbo.FuncProdConsInsu(D.TipCons,D.NumCons,D.IdCiaCons,D.IdProducto,@pmIdInsumo)=1
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTallajesMov] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmAnnoAnt INT,@pmMesAnt INT
 AS  
SELECT T.IdProducto,DescripProd,T.IdCia,CI.Compania,T.IdBodega,Bodega,T.IdUbic,T.NumTalla,T.Cantidad,T.Pedidos,S.SaldoActual
,D.Fecha,D.TipDoc,TD.TipoDoc,D.Documento,D.IdCia AS DetIdCia,CM.Compania AS DetCompania,D.Item,D.Entradas,D.Salidas,D.CantPedido,D.CantRem,D.CantFactura
,dbo.FuncAcuInvTallasSan(@pmAnnoAnt,@pmMesAnt,D.IdCia,D.IdProducto,D.IdBodega,D.IdUbic,D.NumTalla) AS SaldoAnt,D.ItemKdx,D.IdUbic AS DetUbic
--datos del producto 
,P.TipoRef,P.Referencia,P.CodBarras,G.IdLinea,Linea,SG.IdGrupo,Grupo,P.IdSubgrupo,Subgrupo,P.IdMarca,Marca,P.Tamano,P.Color
,P.UndMed,UM.Unidad AS DescUndMed,P.IdUnd,U.Unidad,P.Tallaje,P.Inactivo
FROM Trn_Tallajes AS T INNER JOIN ProdMcias AS P ON T.IdProducto=P.IdProducto
INNER JOIN Bodegas AS B ON T.IdBodega=B.IdBodega INNER JOIN Companias AS CI ON T.IdCia=CI.IdCia
INNER JOIN SubGrupos AS SG ON P.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.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 U ON P.IdUnd=U.IdUnd 
LEFT JOIN Trn_TallasMov AS D ON T.IdProducto=D.IdProducto AND T.NumTalla=D.NumTalla AND T.IdCia=D.IdCia AND T.IdBodega=D.IdBodega
LEFT JOIN Sys_TiposDoc AS TD ON D.TipDoc=TD.IdDoc LEFT JOIN Companias AS CM ON D.IdCia=CM.IdCia
LEFT JOIN Sys_Um AS UM ON P.UndMed=UM.UndMed
LEFT JOIN ProdSaldos AS S ON T.IdProducto=S.IdProducto AND T.IdBodega=S.IdBodega AND T.IdCia=S.IdCia
WHERE ((D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin) OR D.Fecha IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmProdCia
,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
,tmPrecioVta1 AS Precio1,tmPrecioVta2 AS Precio2,tmPrecioVta3 AS Precio3,tmPrecioVta4 AS Precio4,tmPrecioVta5 AS Precio5,IdProv,PV.RazonSocial AS NomProveedor,DescripLong,DescripAbrv
,dbo.FuncAuxInvNivelMin(tmIdProducto,tmIdCia) AS ExtciaMin,dbo.FuncAuxInvNivelMax(tmIdProducto,tmIdCia) AS ExtciaMax
,dbo.FuncAuxInvSaldoAct(tmIdProducto,tmIdCia,tmIdBodega) AS 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 ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[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,tmProdCia
--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
,tmPrecioVta1 AS Precio1,tmPrecioVta2 AS Precio2,tmPrecioVta3 AS Precio3,tmPrecioVta4 AS Precio4,tmPrecioVta5 AS Precio5
,IdProv,PV.RazonSocial AS NomProveedor,DescripLong,DescripAbrv
,dbo.FuncAuxInvNivelMin(tmIdProducto,tmIdCia) AS ExtciaMin,dbo.FuncAuxInvNivelMax(tmIdProducto,tmIdCia) AS ExtciaMax
,dbo.FuncAuxInvSaldoAct(tmIdProducto,tmIdCia,tmIdBodega) AS ExtciaAct --,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 ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_BalComBases] @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16)=Null
,@pmtmIdTercero VARCHAR(16)=Null,@pmtmCodAgncia VARCHAR(16)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
,@pmIdCia CHAR(2)=Null
 AS 
 SELECT BC.tmIdCuenta AS IdCuent,NomCuenta,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos
,tmSaldoAnterior+tmTotalDebitos-tmTotalCreditos AS NuevoSaldo,tmVrPresupto,tmVrCompras
--bases acumuladas del año
,CASE P.BaseGravable WHEN 1 THEN ISNULL(CASE tmTipo WHEN 'N' THEN dbo.FuncComDetalleBasesNit(BC.tmIdCuenta,tmIdTercero,@pmFechaIni,@pmFechaFin,@pmIdCia)
WHEN 'NC' THEN dbo.FuncComDetalleBasesNitCc(BC.tmIdCuenta,tmIdTercero,tmIdCCosto,@pmFechaIni,@pmFechaFin,@pmIdCia)
WHEN 'NCS' THEN dbo.FuncComDetalleBasesNitCcSub(BC.tmIdCuenta,tmIdTercero,tmIdCCosto,tmIdSubCos,@pmFechaIni,@pmFechaFin,@pmIdCia)
WHEN 'CS' THEN dbo.FuncComDetalleBasesCcSub(BC.tmIdCuenta,tmIdCCosto,tmIdSubCos,@pmFechaIni,@pmFechaFin,@pmIdCia)
WHEN 'C' THEN dbo.FuncComDetalleBasesCc(BC.tmIdCuenta,tmIdCCosto,@pmFechaIni,@pmFechaFin,@pmIdCia)
ELSE 
 dbo.FuncComDetalleBasesCue (BC.tmIdCuenta,@pmFechaIni,@pmFechaFin,@pmIdCia)
END,0) ELSE 0 END AS TotalBase,Porcntaje
,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,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
