public ActionResult Edit([Bind(Include = "Id,Date,Hora,Minuto,EspecialidadeId,EspecialistaId,PacienteId," +
                                                 "Observacao,ValorPago,FormaPagamentoId,Tratamento")] ConsultaViewModel consultaViewModel, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    string path = "";

                    try
                    {
                        var consulta = db.Consultas.Where(x => x.Id == consultaViewModel.Id).FirstOrDefault();

                        if (consulta != null)
                        {
                            consulta.Date            = consultaViewModel.Date;
                            consulta.Hora            = consultaViewModel.Hora;
                            consulta.Minuto          = consultaViewModel.Minuto;
                            consulta.EspecialidadeId = consultaViewModel.EspecialidadeId;
                            consulta.EspecialistaId  = consultaViewModel.EspecialistaId;
                            consulta.PacienteId      = consultaViewModel.PacienteId;
                            consulta.Observacao      = consultaViewModel.Observacao;
                            consulta.ValorPago       = consultaViewModel.ValorPago;
                            consulta.Tratamento      = consultaViewModel.Tratamento;
                            db.SaveChanges();

                            Arquivo arquivo = db.Arquivos.Where(x => x.Id == consulta.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("c_{0}_{1}", consulta.Id, file.FileName));
                                    file.SaveAs(path);
                                    arquivo.Path         = path;
                                    arquivo.OriginalName = file.FileName;
                                }
                            }

                            if (consultaViewModel.FormaPagamentoId != -1)
                            {
                                var oldPayment = db.PagamentosConsultas.Where(x => x.ConsultaId == consulta.Id).FirstOrDefault();

                                if (oldPayment != null)
                                {
                                    oldPayment.FormaPagamentoId = consultaViewModel.FormaPagamentoId;
                                }
                                else
                                {
                                    db.PagamentosConsultas.Add(new PagamentoConsulta()
                                    {
                                        ConsultaId       = consulta.Id,
                                        FormaPagamentoId = consultaViewModel.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.EspecialidadeId  = new SelectList(db.Especialidades, "Id", "Nome", consultaViewModel.EspecialidadeId);
            ViewBag.EspecialistaId   = new SelectList(db.Especialistas, "Id", "Nome", consultaViewModel.EspecialistaId);
            ViewBag.FormaPagamentoId = new SelectList(paymentWays, "Id", "Nome", consultaViewModel.FormaPagamentoId);
            ViewBag.PacienteId       = new SelectList(pacients, "Id", "Nome", consultaViewModel.PacienteId);
            ViewBag.Hora             = GetListHour();
            ViewBag.Minuto           = GetListMinute();
            return(View(consultaViewModel));
        }
Beispiel #2
0
        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));
        }