SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[CompListas](
	[IdCia] [char](2) NOT NULL,
	[Item] [int] NOT NULL CONSTRAINT [DF_CompListas_Item]  DEFAULT ((0)),
	[IdCiaPre] [char](2) NOT NULL,
 CONSTRAINT [PK_CompListas] PRIMARY KEY CLUSTERED 
(
	[IdCia] ASC,
	[Item] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[CompListas]  WITH CHECK ADD  CONSTRAINT [FK_CompListas_Companias] FOREIGN KEY([IdCia])
REFERENCES [dbo].[Companias] ([IdCia])
GO
ALTER TABLE [dbo].[CompListas] CHECK CONSTRAINT [FK_CompListas_Companias]
GO
ALTER TABLE [dbo].[CompListas]  WITH CHECK ADD  CONSTRAINT [CK_CompListasIdCiaPre] CHECK  ((len([IdCiaPre])>(0)))
GO
ALTER TABLE [dbo].[CompListas] CHECK CONSTRAINT [CK_CompListasIdCiaPre]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_EdsFormas_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_EdsFormas_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_EdsFormas_Fac]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_EdsFormas_Fac]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_EdsFormas_Cre]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_EdsFormas_Cre]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paDelCompanias]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paDelCompanias]
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_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 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 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
