private void checaExistenciaDePropostasExpiradas() { var pb = new PropostaBusiness(); var propostas = Context.Propostas.Where(x => x.Status != (PropostaStatus)Enum.ToObject(typeof(PropostaStatus), 3) || x.Status != (PropostaStatus)Enum.ToObject(typeof(PropostaStatus), 1) && !x.Excluido); bool salvar = false; foreach (var proposta in propostas) { var usuario = Context.Usuarios.FirstOrDefault(x => x.UsuarioPermissoes.Permissoes.Nivel.Equals(1)); bool valida = (!Context.PropostasHistoricos.Any(x => proposta.Id == x.PropostaId && x.PropostaStatus == (PropostaStatus)3 || x.PropostaStatus == (PropostaStatus)2)); if (valida && pb.validaSePropsotaExpirou(proposta)) { salvar = true; proposta.Status = (PropostaStatus)3; var propostaHistorico = new PropostaHistorico(proposta, usuario); Context.PropostasHistoricos.Add(propostaHistorico); Context.Propostas.Update(proposta); } } if (salvar) { MemoryCache.Remove("propostas"); Context.SaveChanges(); } }
public async Task <IActionResult> Put(string id, [FromBody] PropostaSituacaoModel model) { if (string.IsNullOrEmpty(model.Status.ToString())) { return(BadRequest(new { Response = "Não foi possivel aprovar a proposta" })); } if (string.IsNullOrEmpty(model.UsuarioId.ToString())) { return(BadRequest(new { Response = "Não foi possivel aprovar a proposta" })); } var pb = new PropostaBusiness(Context); Guid propsotaId = Guid.Parse(id); if (!pb.propostaIsValid(propsotaId)) { return(BadRequest(new { Response = "Não foi possivel aprovar a proposta" })); } var proposta = pb.GetPropostaValida(propsotaId); var usuario = await pb.getUsuarioLogado(model.UsuarioId); proposta.Status = (pb.getPermissaoUsuarioLogado().Nivel.Equals(4))? (PropostaStatus)4 : (PropostaStatus)2; var propostaHistorico = new PropostaHistorico(proposta, usuario); await Context.PropostasHistoricos.AddAsync(propostaHistorico); Context.Propostas.Update(proposta); await Context.SaveChangesAsync(); MemoryCache.Remove("propostas"); return(Ok(new { ok = true, Response = "Proposta Aprovada" })); }
public async Task <IActionResult> Put(string id, [FromForm] NovaPropostaModel model) { this._logger.LogInformation("Log.NovaPropostaModel", "update item {ID}", id); if (model == null || string.IsNullOrEmpty(id)) { return(BadRequest()); } var proposta = ConsultaProposta(id); if (proposta == null) { return(NotFound()); } var propost = new Proposta(model.NomeProposta, model.Descricao, model.Valor, ConsultaFornecedor(model.FornecedorID), ConsultaCategoria(model.CategoriaID), (PropostaStatus)Enum.ToObject(typeof(PropostaStatus), model.Status)); var usuario = Context.Usuarios.FirstOrDefault(x => x.Id == Guid.Parse(model.Usuario)); var propostaHistorico = new PropostaHistorico(proposta, usuario); await Context.PropostasHistoricos.AddAsync(propostaHistorico); proposta.Atualizar(propost); if (model.Anexo != null) { using (Stream stream = model.Anexo.OpenReadStream()) { using (var binaryReader = new BinaryReader(stream)) { var anexo = await Context.PropostaAnexos.FirstOrDefaultAsync(x => x.Proposta.Id == Guid.Parse(id)); anexo.Excluido = true; Context.PropostaAnexos.Update(anexo); var fileContent = binaryReader.ReadBytes((int)model.Anexo.Length); var propostaAnexo = new PropostaAnexo(fileContent, model.Anexo.FileName, model.Anexo.ContentType, proposta); await Context.PropostaAnexos.AddAsync(propostaAnexo); } } } Context.Propostas.Update(proposta); await Context.SaveChangesAsync(); MemoryCache.Remove("propostas"); return(Ok(new { Response = "Proposta atualizado com sucesso" })); }
public async Task <IActionResult> Post([FromForm] NovaPropostaModel model) { this._logger.LogInformation("Log.NovaPropostaModel", "Getting item {ID}", model); if (model.NomeProposta == null) { return(BadRequest(new { Erro = "aconteceu algo errado, tenta novamente!" })); } model.Status = 1; var proposta = new Proposta(model.NomeProposta, model.Descricao, model.Valor, ConsultaFornecedor(model.FornecedorID), ConsultaCategoria(model.CategoriaID), (PropostaStatus)Enum.ToObject(typeof(PropostaStatus), model.Status)); await Context.Propostas.AddAsync(proposta); if (model.Anexo != null) { using (Stream stream = model.Anexo.OpenReadStream()) { using (var binaryReader = new BinaryReader(stream)) { var fileContent = binaryReader.ReadBytes((int)model.Anexo.Length); var propostaAnexo = new PropostaAnexo(fileContent, model.Anexo.FileName, model.Anexo.ContentType, proposta); await Context.PropostaAnexos.AddAsync(propostaAnexo); } } } var usuario = Context.Usuarios.FirstOrDefault(x => x.Id == Guid.Parse(model.Usuario)); var propostaHistorico = new PropostaHistorico(proposta, usuario); await Context.PropostasHistoricos.AddAsync(propostaHistorico); await Context.SaveChangesAsync(); MemoryCache.Remove("propostas"); return(Ok(new { ok = "true", Response = "Proposta salvo com sucesso" })); }