0
I have the class Clientefirm and the class Archivists one client has a list of tax files (a maximum of 12 per year, one per month) and one Archivist has a Client.
public class ArquivosFiscais
{
public virtual ClienteEmpresa ClienteEmpresa { get; set; }
[ForeignKey("ClienteEmpresa")]
public int IdClienteEmpresa { get; set; }
}
I need to create a routine, which is launched automatically, without user interaction, on the first day of each month. and I need her to be Stardata, just once every month. I need to create a filing cabinet for each client
So I created this method. (I don’t know if the logic is right).
private Contexto db = new Contexto();
public void CriarArquivoPrimeiroDiaDeCadaMes()
{
DateTime data = DateTime.Today;
DateTime primeiroDiaDoMes = new DateTime(data.Year, data.Month, 1);
if (data == new DateTime(data.Year, data.Month, 1))
{
var cliente = db.ClienteEmpresaDb.Where(c => c.StatusCliente == Smc.Dominio.Model.StatusCliente.Ativo || c.StatusCliente == Smc.Dominio.Model.StatusCliente.Bloqueado);
foreach (var item in cliente)
{
using (var sal = new Contexto())
{
ArquivosFiscais af = new ArquivosFiscais();
af.DataAbertura = DateTime.Now;
af.DataDoEnvio = null;
af.IdClienteEmpresa = item.Id;
af.StatusArquivo = Smc.Dominio.Model.ArquivosFiscais.StatusArquivo.NaoEnviado;
af.MesDeReferencia = new DateTime(data.Month);
sal.ArquivosFiscaisDb.Add(af);
sal.SaveChanges();
}
}
}
}
I don’t know if the logic is right. how do I start this method automatically?
As your system is web I advise you to do your routine in the bank through some job, or else use some
schedule
with Hangfire: https://www.hangfire.io/– William
I would also recommend Hangfire
– Leandro Angelo
It was the best option, Hangfire, it’s a hand on the wheel, I didn’t know.
– Rafael Passos