public async Task <ActionResult <List <Oferta> > > GetValidar([FromForm] OfertaViewModel oferta) { var Validade = oferta.Validade; DateTime DataAtual = DateTime.Now.AddDays(Convert.ToInt32(Validade)); if (Validade != null) { return(await RetornaValidade(oferta, DataAtual)); } }
private async Task <ActionResult <List <Oferta> > > RetornaValidade(OfertaViewModel oferta, DateTime data) { List <Oferta> produtos = new List <Oferta>(); var ofertas = await _contexto.(); foreach (var item in ofertas) { if (item.Quantidade > 0 && item.Validade.Date == data.Date) { produtos.Add(item); } } return(ofertas()); }
public IActionResult Form(OfertaViewModel model) { var oferta = new Oferta { ClienteId = model.ClienteId, OfertaProdutos = model.Produtos.Where(x => x.Checked).Select(x => new OfertaProduto { ProdutoId = x.Id }).ToList() }; var cliente = _clienteRepository.GetById(model.ClienteId); cliente.StatusAtualId = 3; _ofertaRepository.AddOrUpdate(oferta); _clienteRepository.AddOrUpdate(cliente); return(RedirectToAction("Index")); }
public async Task <ActionResult> Put(long id, OfertaViewModel oferta) { if (id != oferta.IdOferta) { return(BadRequest()); } Oferta ofertaBanco = _context.Oferta.Find(id); if (oferta.IdProduto != null) { ofertaBanco.IdProduto = oferta.IdProduto; } if (oferta.IdProduto != null) { ofertaBanco.Titulo = oferta.Titulo; } if (oferta.IdProduto != null) { ofertaBanco.DataOferta = oferta.DataOferta; } if (oferta.IdProduto != null) { ofertaBanco.DataVencimento = oferta.DataVencimento; } if (oferta.Preco != null) { ofertaBanco.Preco = oferta.Preco; } if (oferta.Quantidade != null) { ofertaBanco.Quantidade = oferta.Quantidade; } _context.Entry(ofertaBanco).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { var oferta_valido = await _context.Oferta.FindAsync(id); if (oferta_valido == null) { return(NotFound()); } else { throw; } } return(NoContent()); }
// TELA DE CADASTRO DE OFERTA PARA O CLIENTE public IActionResult Index(string cpf) { try { // VERIFICAR SE JÁ HOUVE UMA OFERTA PARA ESTE CLIENTE var ofertaAlreadyExists = this._ofertaRepository.PesquisarOfertaPorCliente(cpf); if (ofertaAlreadyExists != null) { return(this.Visualizar(ofertaAlreadyExists.Id)); } // REQUISIÇÃO PARA RETORNAR O CLIENTE var clienteOferta = _clienteRepository.ClienteOferta(cpf); if (clienteOferta == null) { ViewBag.Error = "Cliente não encontrado"; return(View("~/Views/Cliente/Index.cshtml")); } // CRIA UMA VIEW MODEL PARA VISUALIZAÇÃO DO CLIENTE EM TELA var clienteViewModel = new ClienteOfertaViewModel { Cpf = clienteOferta.Cpf, Credito = clienteOferta.Credito, Nome = clienteOferta.Nome, Status = new StatusViewModel { Id = clienteOferta.Status.Id, Codigo = clienteOferta.Status.Codigo, Descricao = clienteOferta.Status.Descricao, FinalizaCliente = clienteOferta.Status.FinalizaCliente, ContabilizaVenda = clienteOferta.Status.ContabilizaVenda }, StatusId = clienteOferta.StatusId, Telefone = clienteOferta.Telefone }; // RETORNA LISTA DE PRODUTOS PARA SELEÇÃO var produtos = _produtoRepository.Todos().Select(p => new ProdutoOfertaViewModel { Codigo = p.Codigo, Descricao = p.Descricao, Id = p.Id, IsSelected = false, Preco = p.Preco, Tipo = p.Tipo }); // RETORNA TABELA DE STATUS PARA MODIFICAÇÃO var statusList = _statusRepository.Todos().Select(s => new StatusViewModel { Codigo = s.Codigo, Descricao = s.Descricao, Id = s.Id }); // DADOS DE OFERTA PARA VISUALIZAÇÃO E EDIÇÃO EM TELA var ofertaViewModel = new OfertaViewModel { Cliente = clienteViewModel, Endereco = new EnderecoViewModel(), Produtos = produtos, StatusList = statusList }; ViewBag.Action = "CadastrarOferta"; return(View("Index", ofertaViewModel)); } catch (Exception ex) { ViewBag.Error = ex.InnerException.Message; return(View("~/Views/Cliente/Index.cshtml")); } }
public IActionResult CadastrarOferta(OfertaViewModel oferta) { try { // VALIDAÇÕES NECESSÁRIAS DE CAMPOS E REGRA DE NEGÓCIO if (!this.ValidateCliente(oferta.Cliente)) { return(this.Index(oferta.Cliente.Cpf)); } if (!this.ValidateEndereco(oferta.Endereco, oferta.Produtos)) { return(this.Index(oferta.Cliente.Cpf)); } if (!this.ValidateProdutos(oferta.Produtos, oferta.Cliente.StatusId, oferta.Cliente.Credito)) { return(this.Index(oferta.Cliente.Cpf)); } // UPDATE DO CLIENTE var newCliente = new Cliente { Nome = oferta.Cliente.Nome, Cpf = oferta.Cliente.Cpf.Replace(".", "").Replace("-", "").Trim(), Telefone = oferta.Cliente.Telefone, StatusId = oferta.Cliente.StatusId, Credito = oferta.Cliente.Credito }; var clienteUpdated = this._clienteRepository.Editar(newCliente); // CRIA UM ENDERECO NO BANCO var newEndereco = new Endereco { Bairro = oferta.Endereco.Bairro, CEP = oferta.Endereco.CEP, Cidade = oferta.Endereco.Cidade, Complemento = oferta.Endereco.Complemento, Numero = oferta.Endereco.Numero, Rua = oferta.Endereco.Rua, UF = oferta.Endereco.UF }; var enderecoCreated = this._enderecoRepository.Cadastrar(newEndereco); // CRIA UMA OFERTA COM O ENDEREÇO E CLIENTE var newOferta = new Oferta { ClienteId = clienteUpdated.Id, EnderecoId = enderecoCreated.Id, }; var ofertaCreated = this._ofertaRepository.Cadastrar(newOferta); // CADASTRA OS PRODUTOS DA VENDA foreach (var produto in oferta.Produtos) { if (produto.IsSelected) { var newProduto = new ProdutoOferta { OfertaId = ofertaCreated.Id, ProdutoId = produto.Id }; var produtoCreated = this._produtoOfertaRepository.Cadastrar(newProduto); } } return(this.Visualizar(ofertaCreated.Id)); } catch (Exception ex) { ViewBag.Error = ex.Message; return(this.Index(oferta.Cliente.Cpf)); } }