IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'[dbo].[CP_Cbtes_Rem_Generar]') 
                   and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
    DROP PROCEDURE [dbo].[CP_Cbtes_Rem_Generar] 
END
GO

CREATE PROCEDURE [dbo].[CP_Cbtes_Rem_Generar]
@pId_Usuario		INT,			/*Seg_User*/
@pId_Tipo_Cbte		CHAR(4),		/*Cbtes_Tipos*/
@pFecha				DATETIME,
@pId_Cliente		INT,			/*Clientes*/
@pFecha_Estimada	DATETIME,
@pId_Vendedor		VARCHAR(10),	/*Vendedores*/
@pId_Cond_Pago		SMALLINT,		/*Cbtes_Cond_Pago*/
@pId_Cond_Vta		CHAR(10),		/*Cbtes_Cond_Vtas*/
@pId_Centro_Costo	CHAR(10),		/*Centro_Costos*/
@pId_Lista			SMALLINT,		/*Prod_Listas_Precios*/
@pEmail				VARCHAR(8000),
@pId_Puerto			CHAR(3),		/*Tab_Puertos*/
@pNota_Part1		VARCHAR(100),
@pNota_Part2		VARCHAR(100),
@pObservacion		VARCHAR(8000),
@pNro_Perm_Embarque	VARCHAR(20),
@pPersona_Recibe	VARCHAR(100),
@pLote				VARCHAR(20),
@pPedido_Nro		VARCHAR(25),
@pId_Transportista	INT,
@pDir_Entrega		VARCHAR(255)
AS

DECLARE @Trans TABLE (ErrorNumber	INT 
					,ErrorSeverity  INT 
					,ErrorState		INT
					,ErrorProcedure	VARCHAR(100)
					,ErrorLine		INT
					,ErrorMessage	VARCHAR(1000)
					,Id_Trans		INT)

INSERT INTO @Trans
	EXEC CP_Transacciones_Generar @pId_Usuario

IF (SELECT TOP 1 ErrorNumber FROM @Trans) <> 0
BEGIN
	SELECT * FROM @Trans
	RETURN
END

/*Declaracion Variables Locales*/
DECLARE @Id_Trans			INT
DECLARE @Id_Empresa			SMALLINT
DECLARE @Id_Sucursal		SMALLINT
DECLARE @Cbte_Grupo			INT
DECLARE @Comprobante_Nro	INT
DECLARE @Id_Taller			VARCHAR(10)
DECLARE @Id_Proceso			VARCHAR(10)
DECLARE @Id_Proveedor		INT
DECLARE @Signo				SMALLINT
DECLARE @Id_Estado_Cbte		SMALLINT
DECLARE @Id_Dep_Fisico		CHAR(3)
DECLARE @Id_Dep_Logico		CHAR(3)
DECLARE @Porc_DescEspecial	DECIMAL(5, 2)
DECLARE @Porc_DescFin		DECIMAL(5, 2)
DECLARE @Porc_Iva			DECIMAL(5, 2)
DECLARE @Porc_Iva2			DECIMAL(5, 2)
DECLARE @Imp_Neto			DECIMAL(12, 2)
DECLARE @Imp_DescEspecial	DECIMAL(12, 2)
DECLARE @Imp_DescFin		DECIMAL(12, 2)
DECLARE @Imp_Bonificacion	DECIMAL(12, 2)
DECLARE @Imp_Gravado		DECIMAL(12, 2)
DECLARE @Imp_Exento			DECIMAL(12, 2)
DECLARE @Imp_IVA			DECIMAL(12, 2)
DECLARE @Imp_IVA2			DECIMAL(12, 2)
DECLARE @Imp_Percepcion_IB	DECIMAL(12, 2)
DECLARE @Imp_Percepcion_Iva DECIMAL(12, 2)
DECLARE @Imp_Total			DECIMAL(12, 2)
DECLARE @Bultos				SMALLINT
DECLARE @NroImpresion		INT
DECLARE @Exportado			TINYINT
DECLARE @Id_Pais			CHAR(3)
DECLARE @Id_Provincia		CHAR(3)
DECLARE @Id_Localidad		INT
DECLARE @Porc_Iva3			DECIMAL(5, 2)
DECLARE @Imp_Gravado2		DECIMAL(12, 2)
DECLARE @Imp_Gravado3		DECIMAL(12, 2)
DECLARE @Imp_NoGravado		DECIMAL(12, 2)
DECLARE @Imp_IVA3			DECIMAL(12, 2)
DECLARE @Imp_Percep			DECIMAL(12, 2)
DECLARE @Saldo				DECIMAL(12, 2)
DECLARE @Periodo_Iva		DATETIME
DECLARE @Periodo			DATETIME
DECLARE @Id_Cond_Pago_Item	TINYINT
DECLARE @Id_Tipo_Reten		VARCHAR(10)
DECLARE @Cant_Cuotas		SMALLINT
DECLARE @Id_Producto		VARCHAR(14)
DECLARE @Id_Formula			VARCHAR(15)
DECLARE @Cantidad			DECIMAL(12, 4)
DECLARE @Destinatario		VARCHAR(100)
DECLARE @Cuit				VARCHAR(19)
DECLARE @Id_Iva				CHAR(3)
DECLARE @Cant_Batch			SMALLINT
DECLARE @Fecha_Perm_Embar	DATETIME
DECLARE @Saldo_Iva_Recup	DECIMAL(12, 2)
DECLARE @Suc_Destina		SMALLINT
DECLARE @Id_Asiento			INT
DECLARE @Id_Valor			INT
DECLARE @Cai				DECIMAL(14, 0)
DECLARE @Cai_Vence			DATETIME
DECLARE @Des_Anio			TINYINT
DECLARE @Des_Aduana			CHAR(3)
DECLARE @Des_Destina		CHAR(4)
DECLARE @Des_Nro			INT
DECLARE @Des_Verif			CHAR(1)
DECLARE @Id_Contrato		INT
DECLARE @Id_Cobrador		VARCHAR(10)
DECLARE @Nro_Auto_Trib		VARCHAR(14)
DECLARE @Remito_Nro			VARCHAR(25)
DECLARE @Id_Tarjeta			VARCHAR(11)
DECLARE @Tarjeta_Nro		VARCHAR(25)
DECLARE @Cantidad_Saldo		DECIMAL(12, 4)
DECLARE @Saldo_Anterior		DECIMAL(12, 2)
DECLARE @NRO_DJAI			VARCHAR(30)
DECLARE @Cantidad_Equiv		DECIMAL(12, 4)
DECLARE @Id_Trans_Origen	INT
DECLARE @Id_Sede_Origen		SMALLINT
DECLARE @Email				VARCHAR(8000)
DECLARE @Patente_Transporte VARCHAR(7)
DECLARE @Patente_Transporte_Acoplado VARCHAR(7)
DECLARE @Id_Moneda			CHAR(3)
DECLARE @Id_Camp			SMALLINT
DECLARE @Cotizacion			DECIMAL(9,4)
DECLARE @Porc_Bonificacion	DECIMAL(5,2)
DECLARE @Fecha_Venc			DATETIME
DECLARE @Cbte_Nro_Externo	VARCHAR(25)
DECLARE @Flag_Existe_Cliente		TINYINT
DECLARE @Flag_Existe_Cbte_Tipo		TINYINT
DECLARE @Flag_Existe_Moneda			TINYINT
DECLARE @Flag_Existe_Vendedor		TINYINT
DECLARE @Flag_Existe_Cbte_Cond_Pago	TINYINT
DECLARE @Flag_Existe_Cbte_Cond_Vta  TINYINT
DECLARE @Flag_Existe_CRM_Campania	TINYINT
DECLARE @Flag_Existe_Centro_Costo	TINYINT
DECLARE @Flag_Existe_Prod_Lista_Precio TINYINT
DECLARE @Flag_Existe_Tab_Puerto		TINYINT
DECLARE @Flag_Existe_Id_Transportista TINYINT

DECLARE @Plazo_Venc			SMALLINT

DECLARE @Descripcion		VARCHAR(80)

DECLARE @Mensaje			VARCHAR(100)
/*Fin de Declaraciones Variables Locales */

PRINT 'Procedimiento: CP_Cbtes_Rem_Generar'

SET @Id_Trans = (SELECT Id_Trans FROM @Trans)

BEGIN TRY
	BEGIN TRAN

	SET @Flag_Existe_Cliente = ISNULL((SELECT 1 FROM Clientes WHERE Id_Cliente = @pId_Cliente), 0)
	IF (@Flag_Existe_Cliente = 0)
	BEGIN
		SET @Mensaje = 'No existe el Cliente '

		SELECT	2001			AS ErrorNumber 
				,0				AS ErrorSeverity  
				,0				AS ErrorState 
				,0				AS ErrorProcedure  
				,0				AS ErrorLine		
				,@Mensaje		AS ErrorMessage
				,@Id_Trans		AS Id_Trans
		ROLLBACK
		RETURN
	END

	/*************************************************************************************************/

	SET @Flag_Existe_Cliente = ISNULL((SELECT 1 FROM Clientes WHERE Id_Cliente = @pId_Cliente AND ISNULL(Activo, 0) = 1), 0)
	IF (@Flag_Existe_Cliente = 0)
	BEGIN
		SET @Mensaje = 'El Cliente esta Inactivo'

		SELECT	2002			AS ErrorNumber 
				,0				AS ErrorSeverity  
				,0				AS ErrorState 
				,0				AS ErrorProcedure  
				,0				AS ErrorLine		
				,@Mensaje		AS ErrorMessage
				,@Id_Trans		AS Id_Trans
		ROLLBACK
		RETURN
	END

	/*************************************************************************************************/

	SET @Flag_Existe_Cbte_Tipo = ISNULL((SELECT 1 FROM Cbtes_Tipos WHERE Id_Tipo_Cbte = @pId_Tipo_Cbte AND ISNULL(Activo, 0) = 1), 0)
	IF (@Flag_Existe_Cbte_Tipo = 0)
	BEGIN
		SET @Mensaje = 'No existe el Tipo de Cbte o esta inactivo'

		SELECT	2003			AS ErrorNumber 
				,0				AS ErrorSeverity  
				,0				AS ErrorState 
				,0				AS ErrorProcedure  
				,0				AS ErrorLine		
				,@Mensaje		AS ErrorMessage
				,@Id_Trans		AS Id_Trans
		ROLLBACK
		RETURN
	END

	/*************************************************************************************************/

	SET @Id_Moneda = (SELECT ISNULL(Id_Moneda, 'PES') FROM Cbtes_Tipos WHERE Id_Tipo_Cbte = @pId_Tipo_Cbte)

	SET @Flag_Existe_Moneda = ISNULL((SELECT 1 FROM Valores_Monedas WHERE Id_Moneda = @Id_Moneda), 0)
	IF (@Flag_Existe_Moneda = 0)
	BEGIN
		SET @Mensaje = 'No existe el Tipo de Moneda'

		SELECT	2004			AS ErrorNumber 
				,0				AS ErrorSeverity  
				,0				AS ErrorState 
				,0				AS ErrorProcedure  
				,0				AS ErrorLine		
				,@Mensaje		AS ErrorMessage
				,@Id_Trans		AS Id_Trans
		ROLLBACK
		RETURN
	END

	/*************************************************************************************************/

	SET @Flag_Existe_Vendedor = ISNULL((SELECT 1 FROM Vendedores WHERE Id_Vendedor = @pId_Vendedor), 0)
	IF (@Flag_Existe_Vendedor = 0)
	BEGIN
		SET @Mensaje = 'No existe el Vendedor'

		SELECT	2005			AS ErrorNumber 
				,0				AS ErrorSeverity  
				,0				AS ErrorState 
				,0				AS ErrorProcedure  
				,0				AS ErrorLine		
				,@Mensaje		AS ErrorMessage
				,@Id_Trans		AS Id_Trans
		ROLLBACK
		RETURN
	END

	/*************************************************************************************************/

	SET @Flag_Existe_Cbte_Cond_Pago = ISNULL((SELECT 1 FROM Cbtes_Cond_Pago WHERE Id_Cond_Pago = @pId_Cond_Pago), 0)
	IF (@Flag_Existe_Cbte_Cond_Pago = 0)
	BEGIN
		SET @Mensaje = 'No existe la Condicion de Pago'

		SELECT	2006			AS ErrorNumber 
				,0				AS ErrorSeverity  
				,0				AS ErrorState 
				,0				AS ErrorProcedure  
				,0				AS ErrorLine		
				,@Mensaje		AS ErrorMessage
				,@Id_Trans		AS Id_Trans
		ROLLBACK
		RETURN
	END

	/*************************************************************************************************/

	SET @Flag_Existe_Cbte_Cond_Vta = ISNULL((SELECT 1 FROM Cbtes_Cond_Vtas WHERE Id_Cond_Vta = @pId_Cond_Vta), 0)
	IF (@Flag_Existe_Cbte_Cond_Vta = 0)
	BEGIN
		SET @Mensaje = 'No existe la Condicion de Venta'

		SELECT	2007			AS ErrorNumber 
				,0				AS ErrorSeverity  
				,0				AS ErrorState 
				,0				AS ErrorProcedure  
				,0				AS ErrorLine		
				,@Mensaje		AS ErrorMessage
				,@Id_Trans		AS Id_Trans
		ROLLBACK
		RETURN
	END

	/*************************************************************************************************/
	SET @Id_Camp = 0
	
	SET @Flag_Existe_CRM_Campania = ISNULL((SELECT 1 FROM CRM_Campanias WHERE Id_Camp = @Id_Camp), 0)
	IF (@Flag_Existe_CRM_Campania = 0)
	BEGIN
		SET @Mensaje = 'No existe la CRM Campaña'

		SELECT	2008			AS ErrorNumber 
				,0				AS ErrorSeverity  
				,0				AS ErrorState 
				,0				AS ErrorProcedure  
				,0				AS ErrorLine		
				,@Mensaje		AS ErrorMessage
				,@Id_Trans		AS Id_Trans
		ROLLBACK
		RETURN
	END

	/*************************************************************************************************/

	SET @Flag_Existe_Centro_Costo = ISNULL((SELECT 1 FROM Centro_Costos WHERE Id_Centro_Costo = @pId_Centro_Costo), 0)
	IF (@Flag_Existe_Centro_Costo = 0)
	BEGIN
		SET @Mensaje = 'No existe el Centro de Costo'

		SELECT	2009			AS ErrorNumber 
				,0				AS ErrorSeverity  
				,0				AS ErrorState 
				,0				AS ErrorProcedure  
				,0				AS ErrorLine		
				,@Mensaje		AS ErrorMessage
				,@Id_Trans		AS Id_Trans
		ROLLBACK
		RETURN
	END

	/*************************************************************************************************/

	SET @Flag_Existe_Prod_Lista_Precio = ISNULL((SELECT 1 FROM Prod_Listas_Precios WHERE Id_Lista_Precio = @pId_Lista), 0)
	IF (@Flag_Existe_Prod_Lista_Precio = 0)
	BEGIN
		SET @Mensaje = 'No existe la Lista de Precios'

		SELECT	2010			AS ErrorNumber 
				,0				AS ErrorSeverity  
				,0				AS ErrorState 
				,0				AS ErrorProcedure  
				,0				AS ErrorLine		
				,@Mensaje		AS ErrorMessage
				,@Id_Trans		AS Id_Trans
		ROLLBACK
		RETURN
	END

	/*************************************************************************************************/

	SET @Flag_Existe_Tab_Puerto = ISNULL((SELECT 1 FROM Tab_Puertos WHERE Id_Puerto = @pId_Puerto), 0)
	IF (@Flag_Existe_Tab_Puerto = 0)
	BEGIN
		SET @Mensaje = 'No existe el Puerto'

		SELECT	2011			AS ErrorNumber 
				,0				AS ErrorSeverity  
				,0				AS ErrorState 
				,0				AS ErrorProcedure  
				,0				AS ErrorLine		
				,@Mensaje		AS ErrorMessage
				,@Id_Trans		AS Id_Trans
		ROLLBACK
		RETURN
	END

	/*************************************************************************************************/
	
	SET @Flag_Existe_Id_Transportista = ISNULL((SELECT MAX(1) FROM Proveedores WHERE (Id_Proveedor = @pId_Transportista AND Id_Tipo_Pro = (dbo.CF_xParam('Transporte_Id'))) OR @pId_Transportista = 0), 0)
	IF(@Flag_Existe_Id_Transportista = 0)
		BEGIN
		SET @Mensaje = 'No existe el Transportist'

		SELECT	2012			AS ErrorNumber 
				,0				AS ErrorSeverity  
				,0				AS ErrorState 
				,0				AS ErrorProcedure  
				,0				AS ErrorLine		
				,@Mensaje		AS ErrorMessage
				,@Id_Trans		AS Id_Trans
		ROLLBACK
		RETURN
	END
	/*************************************************************************************************/
							
	SET @pFecha				= (SELECT CONVERT(DATE, @pFecha))

	SELECT  @Id_Sucursal	= ISNULL(Id_Sucursal, 0),
			@Cbte_Grupo		= ISNULL(Cbte_Grupo, 0),
			@Id_Estado_Cbte	= ISNULL(Id_Estado_Cbte, 0)
	FROM Cbtes_Tipos 
	WHERE 
	Id_Tipo_Cbte = @pId_Tipo_Cbte

	SELECT 	@Destinatario	= ISNULL(Razon_Social, Apellido + ', ' + Nombre),
			@Id_Pais		= ISNULL(Id_Pais_Cob, ''),
			@Id_Provincia	= ISNULL(Id_Provincia_Cob, ''),
			@Id_Localidad	= ISNULL(Id_Localidad_Cob, 0),
			@Cuit			= ISNULL(Cuit, ''),
			@Id_Iva			= ISNULL(Id_Iva, ''),
			@Id_Dep_Fisico	= ISNULL(Id_Dep_Fisico, ''),
			@Id_Dep_Logico	= ISNULL(Id_Dep_Logico, '')
	FROM Clientes
	WHERE
	Id_Cliente = @pId_Cliente

	IF(ISNULL(@pEmail, '') = '')
	BEGIN
		SET @Email				= (SELECT ISNULL(Email, '') FROM Clientes WHERE Id_Cliente = @pId_Cliente)
	END
	ELSE
	BEGIN
		SET @Email				= @pEmail
	END

	SET @Comprobante_Nro	= 0		
	SET @Plazo_Venc			= (SELECT ISNULL(Plazo_Venc, 0) FROM Cbtes_Cond_Pago WHERE Id_Cond_Pago = @pId_Cond_Pago)	
	SET @Periodo_Iva		= @pFecha
	SET @Periodo			= @pFecha
	SET @Imp_Total			= 0 /*Hay que actualizarlo luego de grabar los items*/
	SET @Saldo				= 0 /*Hay que actualizarlo luego de grabar los items*/
	SET @Imp_Gravado		= 0 /*Hay que actualizarlo luego de grabar los items*/

	/*TO-DO: De aca para abajo por ahora por Default*/
	SET @Id_Taller			= ''
	SET @Id_Proceso			= ''
	SET @Id_Proveedor		= 0
	SET @Signo				= 0
	SET	@Porc_DescFin		= 0
	SET @Porc_DescEspecial	= 0
	SET @Porc_Iva			= dbo.CF_xParam('Tasa_Iva_1')
	SET @Porc_Iva2			= dbo.CF_xParam('Tasa_Iva_2')
	SET @Imp_Neto			= 0
	SET @Imp_DescEspecial	= 0
	SET @Imp_DescFin		= 0
	SET @Imp_Bonificacion	= 0
	SET @Imp_Exento			= 0
	SET @Imp_Iva			= 0
	SET @Imp_Iva2			= 0
	SET @Imp_Percepcion_IB	= 0
	SET @Imp_Percepcion_Iva = 0
	SET @Bultos				= 0
	SET @NroImpresion		= 0
	SET @Exportado			= 0
	SET @Porc_Iva3			= dbo.CF_xParam('Tasa_Iva_3')
	SET @Imp_Gravado2		= 0
	SET @Imp_Gravado3		= 0
	SET @Imp_NoGravado		= 0
	SET @Imp_IVA3			= 0
	SET @Imp_Percep			= 0
	--SET @Id_Cond_Pago_Item	NULL
	SET @Id_Tipo_Reten		= ''
	SET @Cant_Cuotas		= 0
	--SET @Id_Producto		NULL
	--SET @Id_Formula		NULL
	SET @Cantidad = 0
	SET @Cant_Batch = 0
	--SET @Fecha_Perm_Embar
	SET @Saldo_Iva_Recup = 0
	SET @Suc_Destina = 0
	SET @Id_Asiento = 0
	SET @Id_Valor = 0
	SET @Cai = 0
	--SET @Cai_Vence NULL
	SET @Des_Anio = 0
	SET @Des_Aduana = ''
	SET @Des_Destina = ''
	SET @Des_Nro = 0
	SET @Des_Verif = ''
	SET @Id_Contrato = 0
	SET @Id_Cobrador = ''
	SET @Nro_Auto_Trib = ''
	SET @Remito_Nro = ''
	SET @Id_Tarjeta = ''
	SET @Tarjeta_Nro = ''
	SET @Cantidad_Saldo = 0
	SET @Saldo_Anterior	= 0
	SET @NRO_DJAI = ''
	SET @Cantidad_Equiv = 0
	SET @Id_Trans_Origen = 0
	SET @Id_Sede_Origen = 0
	SET @Cotizacion = 1
	SET @Porc_Bonificacion = 0
	SET @Fecha_Venc = DATEADD(dd, 1, @pFecha)
	SET @Cbte_Nro_Externo = '0'
	--SET @Patente_Transporte NULL
	--SET @Patente_Transporte_Acoplado NULL	


	INSERT INTO dbo.Cbtes
	(Id_Trans,			Id_Empresa,			Id_Sucursal,
	Id_Tipo_Cbte,		Cbte_Grupo,			Comprobante_Nro,
	Id_Cliente,			Id_Taller,			Id_Proceso,
	Id_Proveedor,		Signo,				Id_Estado_Cbte,
	Fecha,				Id_Dep_Fisico,		Id_Dep_Logico,
	Porc_DescEspecial,	Porc_DescFin,		Porc_Bonificacion,
	Porc_Iva,			Porc_Iva2,			Imp_Neto,
	Imp_DescEspecial,	Imp_DescFin,		Imp_Bonificacion,
	Imp_Gravado,		Imp_Exento,			Imp_IVA,
	Imp_IVA2,			Imp_Percepcion_IB,	Imp_Percepcion_Iva,
	Imp_Total,			Bultos,				NroImpresion,
	Exportado,			Id_Usuario,			Fecha_Estimada,
	Dir_Entrega,		Id_Pais,			Id_Provincia,
	Id_Localidad,		Persona_Recibe,		Porc_Iva3,
	Imp_Gravado2,		Imp_Gravado3,		Imp_NoGravado,
	Imp_IVA3,			Imp_Percep,			Saldo,
	Periodo_Iva,		Periodo,			Fecha_Venc,
	Observacion,		Id_Cond_Pago_Item,	Id_Cond_Pago,
	Id_Tipo_Reten,		Cant_Cuotas,		Id_Producto,
	Id_Formula,			Cantidad,			Id_Vendedor,
	Id_Transportista,	Id_Lista,			Id_Moneda,
	Cotizacion,			Destinatario,		Cuit,
	Id_Iva,				Cant_Batch,			Nro_Perm_Embarque,
	Fecha_Perm_Embar,	Saldo_Iva_Recup,	Lote,
	Suc_Destina,		Id_Puerto,			Id_Cond_Vta,
	Cbte_Nro_Externo,	Nota_Part1,			Nota_Part2,
	Id_Asiento,			Id_Centro_Costo,	Id_Valor,
	Cai,				Cai_Vence,			Des_Anio,
	Des_Aduana,			Des_Destina,		Des_Nro,
	Des_Verif,			Id_Contrato,		Id_Cobrador,
	Nro_Auto_Trib,		Id_Camp,			Remito_Nro,
	Pedido_Nro,			Id_Tarjeta,			Tarjeta_Nro,
	Cantidad_Saldo,		Saldo_Anterior,		NRO_DJAI,
	Cantidad_Equiv,		Id_Trans_Origen,	Id_Sede_Origen,
	Email,				Patente_Transporte,	Patente_Transporte_Acoplado)
	VALUES
	(@Id_Trans,			@Id_Empresa,		@Id_Sucursal,
	SUBSTRING(@pId_Tipo_Cbte,1,4),	@Cbte_Grupo,		@Comprobante_Nro,
	@pId_Cliente,		@Id_Taller,			@Id_Proceso,
	@Id_Proveedor,		@Signo,				@Id_Estado_Cbte,
	@pFecha,			@Id_Dep_Fisico,		@Id_Dep_Logico,
	@Porc_DescEspecial,	@Porc_DescFin,		@Porc_Bonificacion,
	@Porc_Iva,			@Porc_Iva2,			@Imp_Neto,
	@Imp_DescEspecial,	@Imp_DescFin,		@Imp_Bonificacion,
	@Imp_Gravado,		@Imp_Exento,		@Imp_IVA,
	@Imp_IVA2,			@Imp_Percepcion_IB,	@Imp_Percepcion_Iva,
	@Imp_Total,			@Bultos,			@NroImpresion,
	@Exportado,			@pId_Usuario,		@pFecha_Estimada,
	@pDir_Entrega,		@Id_Pais,			@Id_Provincia,
	@Id_Localidad,		@pPersona_Recibe,	@Porc_Iva3,
	@Imp_Gravado2,		@Imp_Gravado3,		@Imp_NoGravado,
	@Imp_IVA3,			@Imp_Percep,		@Saldo,
	@Periodo_Iva,		@Periodo,			@Fecha_Venc,
	@pObservacion,		@Id_Cond_Pago_Item,	@pId_Cond_Pago,
	@Id_Tipo_Reten,		@Cant_Cuotas,		@Id_Producto,
	@Id_Formula,		@Cantidad,			@pId_Vendedor,
	@pId_Transportista,	@pId_Lista,			@Id_Moneda,
	@Cotizacion,		@Destinatario,		@Cuit,
	@Id_Iva,			@Cant_Batch,		@pNro_Perm_Embarque,
	@Fecha_Perm_Embar,	@Saldo_Iva_Recup,	@pLote,
	@Suc_Destina,		@pId_Puerto,		@pId_Cond_Vta,
	@Cbte_Nro_Externo,	@pNota_Part1,		@pNota_Part2,
	@Id_Asiento,		@pId_Centro_Costo,	@Id_Valor,
	@Cai,				@Cai_Vence,			@Des_Anio,
	@Des_Aduana,		@Des_Destina,		@Des_Nro,
	@Des_Verif,			@Id_Contrato,		@Id_Cobrador,
	@Nro_Auto_Trib,		@Id_Camp,			@Remito_Nro,
	@pPedido_Nro,		@Id_Tarjeta,		@Tarjeta_Nro,
	@Cantidad_Saldo,	@Saldo_Anterior,	@Nro_DJAI,
	@Cantidad_Equiv,	@Id_Trans_Origen,	@Id_Sede_Origen,
	@Email,				@Patente_Transporte,@Patente_Transporte_Acoplado)

	/*****************************************************************************/

	SET @Descripcion = ISNULL((dbo.CF_Cbte_Descrip(@Id_Trans)), '')

	/*****************************************************************************/
	
	UPDATE Transacciones SET Descripcion = Descripcion + ' ' + @Descripcion WHERE Id_Trans = @Id_Trans

	/*****************************************************************************/
	
	COMMIT

	SET @Mensaje = @Descripcion

	SELECT	 0				AS ErrorNumber 
			,0				AS ErrorSeverity  
			,0				AS ErrorState 
			,0				AS ErrorProcedure  
			,0				AS ErrorLine		
			,@Mensaje		AS ErrorMessage
			,@Id_Trans		AS Id_Trans

END TRY  


BEGIN CATCH  
    EXECUTE usp_GetErrorInfo;  
	ROLLBACK
END CATCH

PRINT 'FIN Procedimiento: CP_Cbtes_Rem_Generar'
