
delimiter //
CREATE procedure GetProductosKit
(
	codigoOraclePadre varchar(200),
	tipoModalidad varchar(64)
)
begin

	select 
		a.*,b.codigoOracle,codigoSiebel,b.descripcionOracle,b.recurrenteSubtotal,b.recurrenteTotal,idTipoModalidadProducto
	FROM ProductoDetalle a inner join Producto b on b.id=a.idProducto 
	inner join ProductosKit pk on pk.codigoOraclePerteneciente=b.codigoOracle and pk.codigoOraclePadre=codigoOraclePadre and idTipoModalidadProducto=tipoModalidad;
end//
delimiter ;







delimiter //
CREATE procedure GetVentaHistorial2
(
	nroPage int ,
    numberOfEntries int ,
    textSearch varchar(250),
	idUsuario int,
	perfil varchar(64),
	prmEstado varchar(50),
	fechaInicio Datetime , 
	fechaFin Datetime
 )
 BEGIN
	declare cantidadTotal int;
    declare inicial int;
    set numberOfEntries =  case when numberOfEntries <= 0 then 0 else numberOfEntries end;
	set cantidadTotal = (select  count(*) from venta v /*inner join usuario u on u.idusuario= v.usuariocreador  inner join categoriaperfil cp on cp.id=u.idCategoriaPerfil*/
						 where cast(v.fechacreacion as date )  between fechaInicio and fechaFin
						 /*and ( @perfil = '00000000-0000-0000-0000-000000000000'	or cp.id= @perfil)*/
						 and ( iFnull(prmEstado,'') = ''	or v.estadoventa = prmEstado) 
						 and v.estado=1 and ( numeroContrato like CONCAT('%', textSearch, '%') or nombreCliente like CONCAT('%', textSearch, '%')  or numeroContizacion like CONCAT('%', @textSearch, '%')));  
	set inicial = numberOfEntries * (nroPage -1 );
    
    select cantidadTotal;

	select 
		v.*
	from venta v
	/*inner join usuario u on u.idusuario= v.usuariocreador  inner join categoriaperfil cp on cp.id=u.idCategoriaPerfil*/
	where 
	cast(v.fechacreacion as date )  between fechaInicio and fechaFin
	/*and ( @perfil = '00000000-0000-0000-0000-000000000000'	or cp.id= @perfil)*/
	and ( iFnull(prmEstado,'') = ''	or v.estadoventa = prmEstado) 
	and  v.estado=1 and ( numeroContrato like CONCAT('%', textSearch, '%') or nombreCliente like CONCAT('%', textSearch, '%') or numeroContizacion like CONCAT('%', textSearch, '%')) 
    ORDER BY v.fechaCreacion desc
	LIMIT inicial, numberOfEntries;
end//
delimiter ;






delimiter //
CREATE procedure FindVenta2
(
	idVenta varchar(64)
)
begin
	select * , (select  descripcionOracle from producto where codigoOracle = vd.idProductoDetalle LIMIT 1) as descripcionOracle from ventadetalle vd 
	 where vd.idVenta= idVenta;
end//
delimiter ;