dbo.monitoreos_envio_mails_todos

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

2
Name Data Type Length Description
@yesterday date 3
@si varchar 10

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
--exec monitoreos_envio_mails_todos '2024-12-30' , 'NO'
CREATE OR ALTER PROCEDURE dbo.monitoreos_envio_mails_todos 
@yesterday date = null, 
@si varchar(10) = 'TODOS' 
      
AS
BEGIN
 
  SET NOCOUNT ON;
  declare @cant int
  if (@yesterday is null) begin set @yesterday = GETDATE() - 1 end 
 
        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 

     /* 15/6/2023: 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
       /* 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 
    /**/
       --and a.cliente = 20872528
     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 > @yesterday)
                    where convert(date, mr.fecha) = @yesterday 
                      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
  if (@si = 'SI')
  select distinct cliente, nombre_cliente, email, novedades from @respuesta
  where novedades = 'SI' /* para mandar solo a los clientes que tienen novedades */
  -- or /* si se quiere todos los que tienen novedades y solunion aunque no tenga va con este or, las dos condiciones */
  -- nombre_cliente like 'SOLUNION%'  /* para mandar a solunion */
  
  else 
  select distinct cliente, nombre_cliente, email, novedades from @respuesta 
  
END

GO

Used By

No items found