dbo.monitoreos_envio_mails_cliente

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@cliente int 4
@fecha_desde date 3
@fecha_hasta date 3

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
--exec monitoreos_envio_mails_todos '2019-01-02'
-- [monitoreos_envio_mails_cliente] '1299789112','2021-04-26','2021-05-01'
CREATE OR ALTER PROCEDURE dbo.monitoreos_envio_mails_cliente 
@cliente int, 
@fecha_desde date, 
@fecha_hasta date       
AS
BEGIN
 
  SET NOCOUNT ON;
  declare @cant int
 
        declare @respuesta table (cliente int, nombre_cliente varchar(200), email varchar(200), novedades char(2))
       declare @r_cliente int
  declare @r_user_id int
  declare @r_nombre_cliente varchar(200)
  declare @r_mail varchar(200)

  DECLARE bn CURSOR FOR
      /*
       select distinct a.cliente
         from monitoreos a
         inner join clientes_productos b on b.cliente = a.cliente 
         where a.visible = 1
           and b.id_producto = 16  --> Mantenimiento de cartera
           and b.activo = 1
                      and a.cliente = @cliente
         order by a.cliente
       */



   select distinct a.cliente
     from monitoreos a
     inner join clientes_productos b on b.cliente = a.cliente 

     /* 29/07/2024: controla que el cliente este activo */
     inner join clientes c on c.cliente = a.cliente and c.estado_cliente = 'A'

     /* 29/07/2024: Si el cliente está unificado, veo que el cliente padre esté activo y tenga el producto MAN activo */
     left join clientes_union u on u.cliente = a.cliente 
     left join clientes cu on cu.cliente = u.cliente_union 
     left join clientes_productos cpu on cpu.cliente = u.cliente_union and cpu.id_producto = 16 
     /**/

     where a.visible = 1
       and b.id_producto = 16  --> Mantenimiento de cartera
       and b.activo = 1
                and a.cliente = @cliente
    /* 29/07/2024: Si el cliente está unificado, veo que el cliente padre esté activo y tenga el producto MAN activo */
    and iif(u.cliente_union is not null, iif((cu.estado_cliente = 'A' and cpu.activo = 1),1,0) , 1) = 1 
    /**/
     order by a.cliente




  OPEN bn 
  -- Lectura de la primera fila del cursor
  Fetch next from bn INTO @r_cliente 

  WHILE (@@FETCH_STATUS <> -1 )
  
  BEGIN
   /*set @cant = (select COUNT(*) from monitoreos_resultados
                  where convert(date, fecha) = @yesterday
              and cuit in (select distinct cuit from monitoreos where cliente = @r_cliente and visible = 1)) */


      set @cant = (select COUNT(*) from monitoreos_resultados mr
                    inner join monitoreos m 
                            on m.cliente = @r_cliente
                               and m.cuit =  mr.cuit
                               and (m.seccion_mantenimiento = mr.seccion_mantenimiento or m.seccion_mantenimiento is null) 
          and m.visible = 1
                               and (m.fecha_vigencia_mantenimiento is null or m.fecha_vigencia_mantenimiento >  @fecha_desde )
                    where convert(date, mr.fecha) between @fecha_desde and @fecha_hasta
                      and mr.novedad = 1) 
             
   set @r_nombre_cliente = (select nombre_cliente from clientes where cliente = @r_cliente) 
   
   DECLARE cmail CURSOR FOR
               select distinct email from users 
           where user_id in (select user_id from usuarios_productos where cliente = @r_cliente and id_producto = 16 and activo = 1)
        and email is not null
        and email like '%@%'
   OPEN cmail
     
   Fetch next from cmail INTO  @r_mail
   WHILE (@@FETCH_STATUS <> -1 )
    BEGIN  
     if (@cant = 0) 
     begin 
      insert into @respuesta values (@r_cliente, @r_nombre_cliente, @r_mail, 'NO')
     end 
     else 
     begin
      insert into @respuesta values (@r_cliente, @r_nombre_cliente, @r_mail, 'SI')
     end 
     FETCH next from cmail INTO @r_mail
    END 
    CLOSE cmail
    DEALLOCATE cmail
      
   Fetch next from bn INTO @r_cliente

  END            
  CLOSE bn
  DEALLOCATE bn 

  --select distinct cliente, nombre_cliente + ' -> ' + email as nombre_cliente, 'maria.soria@fidelitas.com.ar' as email, novedades from @respuesta
  select distinct cliente, nombre_cliente, email, novedades from @respuesta
END

GO

Used By

No items found