
/****** Object:  StoredProcedure [dbo].[a_sel_ter_contacto_sucursal_m]    Script Date: 05/26/2017 16:54:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_ter_contacto_sucursal_m]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sel_ter_contacto_sucursal_m]
GO



CREATE PROCEDURE [dbo].[sel_ter_contacto_sucursal_m]
    @p_eid as varchar (50)='',
    @p_filtro as varchar (8000) = '1=1',
    @p_orden as varchar (8000) = 'id'
     AS
BEGIN 
execute(
'select * from (SELECT    
     ter_contacto_sucursal.id,
     dbo.ter_contacto_sucursal.uid,
     dbo.ter_contacto_sucursal.eid,
     ter_contacto_sucursal.id_usuario_modifico,
     ter_contacto_sucursal.id_usuario_creo
    ,ter_contacto_sucursal.fechaModificacion,
     ter_contacto_sucursal.fechaCreacion,
     ter_contacto_sucursal.active,
     ter_contacto_sucursal.contacto_sucursal,
     ter_contacto_sucursal.id_contacto,
 ter_contacto.contacto AS ter_contacto_contacto,
 ter_contacto.contacto_codigo AS ter_contacto_contacto_codigo,
 ter_contacto.telefono AS ter_contacto_telefono,
 ter_contacto.Fax AS ter_contacto_Fax,
ter_contacto.Email AS ter_contacto_Email,
 ter_contacto.Direccion AS ter_contacto_Direccion,
 ter_contacto.telefonoMovil AS ter_contacto_telefonoMovil,
 ter_contacto.id_tercero AS ter_contacto_id_tercero,
 ter_contacto.id_usuario AS ter_contacto_id_usuario,
 ter_contacto.cargo AS ter_contacto_cargo,
ter_contacto_sucursal.id_sucursal,
     ter_sucursal.sucursal AS ter_sucursal_sucursal,
    ter_sucursal.sucursal_codigo AS ter_sucursal_sucursal_codigo,
     ter_sucursal.contacto AS ter_sucursal_contacto,
     ter_sucursal.telefono AS ter_sucursal_telefono, dbo.ter_sucursal.Fax AS ter_sucursal_Fax,
     ter_sucursal.Email AS ter_sucursal_Email,
     ter_sucursal.Direccion AS ter_sucursal_Direccion,
    ter_sucursal.paraVenta AS ter_sucursal_paraVenta,
     ter_sucursal.paraSoporte AS ter_sucursal_paraSoporte,
    ter_sucursal.paraAlquiler AS ter_sucursal_paraAlquiler,
    ter_sucursal.id_tercero AS ter_sucursal_id_tercero,
    ter_sucursal.id_zona AS ter_sucursal_id_zona,
     ter_sucursal.id_usuario_asesor AS ter_sucursal_id_usuario_asesor,
     ter_sucursal.id_usuario_tecnico AS ter_sucursal_id_usuario_tecnico,
     ter_sucursal.id_sucursal AS ter_sucursal_id_sucursal,
     --ter_sucursal.id_listaPrecio AS ter_sucursal_id_listaPrecio,
     ter_sucursal.id_estadoTercero AS ter_sucursal_id_estadoTercero,
    empresaCodigo.empresa AS multiempresa,
    seg_usuario.usuario AS seg_usuario_usuario
FROM         dbo.ter_contacto_sucursal INNER JOIN
                      seg_usuario AS seg_usuario_modifico ON seg_usuario_modifico.id = ter_contacto_sucursal.id_usuario_modifico INNER JOIN
                      seg_usuario AS seg_usuario_creo ON seg_usuario_creo.id = ter_contacto_sucursal.id_usuario_creo INNER JOIN
                      ter_contacto ON ter_contacto.id = ter_contacto_sucursal.id_contacto INNER JOIN
                      seg_usuario AS seg_usuario ON ter_contacto.id_usuario  =  seg_usuario.id INNER JOIN
                      ter_sucursal ON ter_sucursal.id = ter_contacto_sucursal.id_sucursal INNER JOIN
                     gen_empresa AS empresaCodigo ON ter_contacto_sucursal.eid = empresaCodigo.codigo AND empresaCodigo.active = 1

WHERE  ter_contacto_sucursal.eid like ''' + @p_eid  + '%'' or ' + @p_eid  + '= '''') T0
WHERE ' + @p_filtro+ '
ORDER BY ' + @p_orden )

END
