// GET: IntervencaoViewModels/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Intervencao intervencao = db.Intervencoes.Where(x => x.Id == id).Include(x => x.Especialista) .Include(x => x.Paciente).Include(x => x.Procedimento).Include(x => x.Arquivo).FirstOrDefault(); if (intervencao == null) { return(HttpNotFound()); } IntervencaoViewModel intervencaoViewModel = GetIntervencaoViewModel(intervencao); if (intervencaoViewModel == null) { return(HttpNotFound()); } var paymentWays = db.FormasPagamento.ToList(); var pacients = db.Pacientes.OrderBy(x => x.Nome); paymentWays.Insert(0, new FormaPagamento() { Id = -1, Nome = "-" }); ViewBag.EspecialistaId = new SelectList(db.Especialistas, "Id", "Nome", intervencaoViewModel.EspecialistaId); ViewBag.FormaPagamentoId = new SelectList(paymentWays, "Id", "Nome", intervencaoViewModel.FormaPagamentoId); ViewBag.PacienteId = new SelectList(pacients, "Id", "Nome", intervencaoViewModel.PacienteId); ViewBag.ProcedimentoId = new SelectList(db.Procedimentos, "Id", "Nome", intervencaoViewModel.ProcedimentoId); ViewBag.Hora = GetListHour(); ViewBag.Minuto = GetListMinute(); return(View(intervencaoViewModel)); }
// GET: IntervencaoViewModels/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Intervencao intervencao = db.Intervencoes.Where(x => x.Id == id).Include(x => x.Especialista) .Include(x => x.Paciente).Include(x => x.Procedimento).Include(x => x.Arquivo).FirstOrDefault(); if (intervencao == null) { return(HttpNotFound()); } IntervencaoViewModel intervencaoViewModel = GetIntervencaoViewModel(intervencao); if (intervencaoViewModel == null) { return(HttpNotFound()); } return(View(intervencaoViewModel)); }
private IntervencaoViewModel GetIntervencaoViewModel(Intervencao x) { var paymentWay = db.PagamentosProcedimentos.Where(y => y.IntervencaoId == x.Id) .Include(y => y.FormaPagamento).FirstOrDefault(); var intervencaoViewModel = new IntervencaoViewModel(); intervencaoViewModel.Date = x.Date; intervencaoViewModel.Hora = x.Hora; intervencaoViewModel.Minuto = x.Minuto; intervencaoViewModel.Especialista = x.Especialista; intervencaoViewModel.EspecialistaId = x.EspecialistaId; intervencaoViewModel.Id = x.Id; intervencaoViewModel.Observacao = x.Observacao; intervencaoViewModel.ValorPago = x.ValorPago; intervencaoViewModel.Paciente = x.Paciente; intervencaoViewModel.PacienteId = x.PacienteId; intervencaoViewModel.Procedimento = x.Procedimento; intervencaoViewModel.ProcedimentoId = x.ProcedimentoId; intervencaoViewModel.LinkArquivo = x.Arquivo.Path; if (paymentWay != null) { intervencaoViewModel.FormaPagamentoId = paymentWay.FormaPagamento.Id; intervencaoViewModel.FormaPagamento = paymentWay.FormaPagamento; } else { intervencaoViewModel.FormaPagamentoId = -1; intervencaoViewModel.FormaPagamento = new FormaPagamento() { Id = -1, Nome = "-" }; } return(intervencaoViewModel); }
public ActionResult Edit([Bind(Include = "Id,Date,Hora,Minuto,ProcedimentoId,EspecialistaId," + "PacienteId,Observacao,ValorPago,FormaPagamentoId")] IntervencaoViewModel intervencaoViewModel, HttpPostedFileBase file) { if (ModelState.IsValid) { using (var transaction = db.Database.BeginTransaction()) { string path = ""; try { var intervencao = db.Intervencoes.Where(x => x.Id == intervencaoViewModel.Id).FirstOrDefault(); if (intervencao != null) { intervencao.Date = intervencaoViewModel.Date; intervencao.Hora = intervencaoViewModel.Hora; intervencao.Minuto = intervencaoViewModel.Minuto; intervencao.ProcedimentoId = intervencaoViewModel.ProcedimentoId; intervencao.EspecialistaId = intervencaoViewModel.EspecialistaId; intervencao.PacienteId = intervencaoViewModel.PacienteId; intervencao.Observacao = intervencaoViewModel.Observacao; intervencao.ValorPago = intervencaoViewModel.ValorPago; db.SaveChanges(); Arquivo arquivo = db.Arquivos.Where(x => x.Id == intervencao.ArquivoId).FirstOrDefault(); if (arquivo != null) { if (file != null) { if (System.IO.File.Exists(arquivo.Path)) { System.IO.File.Delete(arquivo.Path); } path = Path.Combine(@Server.MapPath(@"~\Files\Clientes"), string.Format("i_{0}_{1}", intervencao.Id, file.FileName)); file.SaveAs(path); arquivo.Path = path; arquivo.OriginalName = file.FileName; } } if (intervencaoViewModel.FormaPagamentoId != -1) { var oldPayment = db.PagamentosProcedimentos.Where(x => x.IntervencaoId == intervencao.Id).FirstOrDefault(); if (oldPayment != null) { oldPayment.FormaPagamentoId = intervencaoViewModel.FormaPagamentoId; } else { db.PagamentosProcedimentos.Add(new PagamentoProcedimento() { IntervencaoId = intervencao.Id, FormaPagamentoId = intervencaoViewModel.FormaPagamentoId }); } } db.SaveChanges(); transaction.Commit(); } } catch (Exception e) { DebugLog.Logar(e.Message); DebugLog.Logar(e.StackTrace); DebugLog.Logar(DebugLog.Details(e)); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } transaction.Rollback(); } } return(RedirectToAction("Index")); } var paymentWays = db.FormasPagamento.ToList(); var pacients = db.Pacientes.OrderBy(x => x.Nome); paymentWays.Insert(0, new FormaPagamento() { Id = -1, Nome = "-" }); ViewBag.EspecialistaId = new SelectList(db.Especialistas, "Id", "Nome", intervencaoViewModel.EspecialistaId); ViewBag.FormaPagamentoId = new SelectList(paymentWays, "Id", "Nome", intervencaoViewModel.FormaPagamentoId); ViewBag.PacienteId = new SelectList(pacients, "Id", "Nome", intervencaoViewModel.PacienteId); ViewBag.ProcedimentoId = new SelectList(db.Procedimentos, "Id", "Nome", intervencaoViewModel.ProcedimentoId); ViewBag.Hora = GetListHour(); ViewBag.Minuto = GetListMinute(); return(View(intervencaoViewModel)); }
public ActionResult Create([Bind(Include = "Id,Date,Hora,Minuto,ProcedimentoId,EspecialistaId," + "PacienteId,Observacao,ValorPago,FormaPagamentoId")] IntervencaoViewModel intervencaoViewModel, [Bind(Include = "Cpf,Nome,DataNascimento")] Paciente formPaciente, string cidade, string bairro, string rua, string numero, string telefone, HttpPostedFileBase file) { if (ModelState.IsValid) { using (var transaction = db.Database.BeginTransaction()) { string path = ""; try { var paciente = db.Pacientes.Where(x => x.Cpf == formPaciente.Cpf).FirstOrDefault(); if (paciente == null || string.IsNullOrEmpty(formPaciente.Cpf)) { var lvtelefone = new Telefone(); lvtelefone.Numero = telefone; db.Telefones.Add(lvtelefone); db.SaveChanges(); var endereco = new Endereco(); endereco.Cidade = cidade; endereco.Bairro = bairro; endereco.Rua = rua; endereco.Numero = numero; db.Enderecos.Add(endereco); db.SaveChanges(); paciente = new Paciente(); paciente.Nome = formPaciente.Nome; paciente.DataNascimento = formPaciente.DataNascimento; paciente.DataCadastro = DateTime.Now; paciente.Cpf = formPaciente.Cpf; paciente.TelefoneId = lvtelefone.Id; paciente.EnderecoId = endereco.Id; db.Pacientes.Add(paciente); db.SaveChanges(); } Arquivo arquivo = new Arquivo(); arquivo.OriginalName = ""; arquivo.Path = "#"; db.Arquivos.Add(arquivo); db.SaveChanges(); var intervencao = new Intervencao(); intervencao.Date = intervencaoViewModel.Date; intervencao.Hora = intervencaoViewModel.Hora; intervencao.Minuto = intervencaoViewModel.Minuto; intervencao.ProcedimentoId = intervencaoViewModel.ProcedimentoId; intervencao.EspecialistaId = intervencaoViewModel.EspecialistaId; intervencao.PacienteId = paciente.Id; intervencao.Observacao = intervencaoViewModel.Observacao; intervencao.ValorPago = intervencaoViewModel.ValorPago; intervencao.ArquivoId = arquivo.Id; db.Intervencoes.Add(intervencao); db.SaveChanges(); if (file != null) { path = Path.Combine(@Server.MapPath(@"~\Files\Clientes"), string.Format("i_{0}_{1}", intervencao.Id, file.FileName)); file.SaveAs(path); arquivo.Path = path; arquivo.OriginalName = file.FileName; } var atendimentoArquivo = new AtendimentoArquivo(); atendimentoArquivo.ArquivoId = arquivo.Id; atendimentoArquivo.Tipo = "Procedimento"; atendimentoArquivo.AtendimentoId = intervencao.Id; db.AtendimentoArquivo.Add(atendimentoArquivo); if (intervencaoViewModel.FormaPagamentoId != -1) { db.PagamentosProcedimentos.Add(new PagamentoProcedimento() { IntervencaoId = intervencao.Id, FormaPagamentoId = intervencaoViewModel.FormaPagamentoId }); } db.SaveChanges(); transaction.Commit(); } catch (Exception e) { DebugLog.Logar(e.Message); DebugLog.Logar(e.StackTrace); DebugLog.Logar(DebugLog.Details(e)); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } transaction.Rollback(); } } return(RedirectToAction("Index")); } var paymentWays = db.FormasPagamento.ToList(); paymentWays.Insert(0, new FormaPagamento() { Id = -1, Nome = "-" }); ViewBag.EspecialistaId = new SelectList(db.Especialistas, "Id", "Nome", intervencaoViewModel.EspecialistaId); ViewBag.FormaPagamentoId = new SelectList(paymentWays, "Id", "Nome", intervencaoViewModel.FormaPagamentoId); ViewBag.PacienteId = new SelectList(db.Pacientes, "Id", "Nome", intervencaoViewModel.PacienteId); ViewBag.ProcedimentoId = new SelectList(db.Procedimentos, "Id", "Nome", intervencaoViewModel.ProcedimentoId); return(View(intervencaoViewModel)); }