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

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

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_ccCupones @pmtmEst CHAR(2),@pmtmItem INT,@pmtmTipoReg INT,@pmtmTipDoc VARCHAR(3),@pmtmDocumento INT,@pmtmIdCia CHAR(2),@pmtmItemDoc INT,@pmtmFecEmision SMALLDATETIME,@pmtmFecVence SMALLDATETIME,@pmtmValorFactura MONEY,@pmtmValorAbono MONEY
,@pmtmIdPropietario VARCHAR(16),@pmtmIdVehiculo VARCHAR(10),@pmtmIdConductor VARCHAR(16),@pmtmIdCargo VARCHAR(4),@pmtmIdClase VARCHAR(4),@pmtmFactura VARCHAR(15),@pmtmReferencia VARCHAR(50),@pmtmDetalle VARCHAR(100),@pmtmDescuento MONEY
 AS 
 INSERT INTO tm_ccCupones (tmEst,tmItem,tmTipoReg,tmTipDoc,tmDocumento,tmIdCia,tmItemDoc,tmFecEmision,tmFecVence,tmValorFactura,tmValorAbono,tmIdPropietario,tmIdVehiculo,tmIdConductor,tmIdCargo,tmIdClase,tmFactura,tmReferencia,tmDetalle,tmDescuento)
 VALUES (@pmtmEst,@pmtmItem,@pmtmTipoReg,@pmtmTipDoc,@pmtmDocumento,@pmtmIdCia,@pmtmItemDoc,@pmtmFecEmision,@pmtmFecVence,@pmtmValorFactura,@pmtmValorAbono,@pmtmIdPropietario,@pmtmIdVehiculo,@pmtmIdConductor,@pmtmIdCargo,@pmtmIdClase
,@pmtmFactura,@pmtmReferencia,@pmtmDetalle,@pmtmDescuento)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ccCupones @pmtmEst CHAR(2) 
 AS 
 SELECT tmItem,tmTipoReg,tmTipDoc,tmDocumento,tmIdCia,Compania,tmItemDoc,tmFecEmision,tmFecVence,tmValorFactura,tmValorAbono
,tmIdPropietario,T.RazonSocial AS Propietario,tmIdVehiculo,NumVeh,tmIdConductor,CD.RazonSocial AS Conductor,tmIdCargo,Concepto,NCuotas,TipoDcto,DctoPpago
,tmIdClase,ClaseCuenta,tmFactura,tmReferencia,tmDetalle,tmDescuento
--datos del tercero
,T.TipoId AS TercTipoId,T.Dv AS TercDv,T.Direccion AS TercDireccion,T.IdLocal AS TercIdCiu,Localidad,T.Telefono AS TercTel,T.Fax AS TercFax,T.TelMovil AS TercMovil,T.e_mail AS TercEmail
 FROM tm_ccCupones AS C INNER JOIN  Companias  AS CI ON C.tmIdCia=CI.IdCia
 INNER JOIN  Terceros AS T ON C.tmIdPropietario=T.IdTercero 
 INNER JOIN Localidades AS LC ON T.IdLocal=LC.IdLocal
 INNER JOIN Vehiculos AS V ON C.tmIdVehiculo=V.IdVehiculo
 INNER JOIN   Terceros AS CD  ON C.tmIdConductor=CD.IdTercero
 LEFT JOIN   Fijos AS  F ON C.tmIdCargo=F.IdCargo  LEFT JOIN  BusClaseCue AS B ON C.tmIdClase=B.IdClase
 WHERE tmEst=@pmtmEst 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

