ALTER TABLE Trn_DetCuentas ADD CodTarif VARCHAR(4)
GO
ALTER TABLE tm_DetCue ADD tmCodTarif VARCHAR(4)
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsDetCuentas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsDetCuentas]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_DetCue]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_DetCue]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_DetCue_Pto]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_DetCue_Pto]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_DetCue_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_DetCue_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDetCuentas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryDetCuentas]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDetCuentas_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryDetCuentas_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_DetCue]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_DetCue]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_DetCueDso]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_DetCueDso]
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_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].[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].[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 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].[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].[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
