public ActionResult Agendar(CadastrarAgendamentoViewModel model)
        {
            if (!ModelState.IsValid)
            {
                Callback(new Exception("Dados inválidos"));
            }
            else
            {
                try
                {
                    DateTime dataAgendamento = model.DataAgendamento.AddTicks(model.HorarioAgendamento.Ticks);
                    if (dataAgendamento < DateTime.Now)
                    {
                        Callback(new Exception(ExceptionMessages.DataNaoDisponivel));
                        return(RedirectToAction("Index", new { model.DataAgendamento }));
                    }

                    model.IdCliente = GetUserId();
                    agendaService.CadastrarAgendamento(model);
                    Callback();
                }
                catch (Exception ex)
                {
                    Callback(ex);
                }
            }

            return(RedirectToAction("Index", new { model.DataAgendamento }));
        }
Beispiel #2
0
        public async Task <ISaida> CadastrarAgendamento([FromBody, SwaggerParameter("Informações de cadastro do agendamento.", Required = true)] CadastrarAgendamentoViewModel model)
        {
            var cadastrarEntrada = new CadastrarAgendamentoEntrada(
                base.ObterIdUsuarioClaim(),
                model.IdCategoria.Value,
                model.IdConta,
                model.IdCartaoCredito,
                model.TipoMetodoPagamento.Value,
                model.ValorParcela.Value,
                model.DataPrimeiraParcela.Value,
                model.QuantidadeParcelas.Value,
                model.PeriodicidadeParcelas.Value,
                model.IdPessoa,
                model.Observacao);

            return(await _agendamentoServico.CadastrarAgendamento(cadastrarEntrada));
        }
Beispiel #3
0
        public void CadastrarAgendamento(CadastrarAgendamentoViewModel model)
        {
            TB_SERVICOS servico = context.TB_SERVICOS.Single(x => x.Id == model.IdServico);
            TB_SECOES   secao   = null;

            if (model.QuantidadeSessao > 0 && model.QuantidadeSessao <= servico.QT_SESSOES)
            {
                List <TimeSpan> horarios = new List <TimeSpan>
                {
                    model.HorarioAgendamento
                };

                if (model.QuantidadeSessao == 1 && !servico.BT_MASSAGEM)
                {
                    TimeSpan horasServico = TimeSpan.FromMinutes(servico.TP_MINUTOS);

                    if (horasServico.Hours == 1)
                    {
                        TimeSpan maisTrintaMinutos     = model.HorarioAgendamento.Add(new TimeSpan(0, 30, 0));
                        DateTime proximosTrintaMinutos = model.DataAgendamento.AddTicks(maisTrintaMinutos.Ticks);

                        TB_AGENDA proximoHorarioOcupado = context.TB_AGENDA.SingleOrDefault(x => x.DT_AGENDAMENTO == proximosTrintaMinutos);
                        if (proximoHorarioOcupado != null)
                        {
                            throw new Exception(string.Format(ExceptionMessages.ProximoHorarioInvalido, proximosTrintaMinutos.ToLongTimeString(), proximosTrintaMinutos.ToLongDateString()));
                        }

                        horarios.Add(maisTrintaMinutos);
                    }

                    secao = new TB_SECOES
                    {
                        DS_IDENTIFICADOR = Guid.NewGuid().ToString()
                    };
                    context.TB_SECOES.Add(secao);
                    context.SaveChanges();
                }
                else
                {
                    int[] agendamentos = context.TB_AGENDA
                                         .Include(x => x.TB_CLIENTES)
                                         .Include(x => x.TB_SERVICOS)
                                         .Where(x => x.ID_CLIENTE == model.IdCliente && x.ID_SERVICO == model.IdServico)
                                         .Select(x => x.Id)
                                         .ToArray();

                    IEnumerable <IGrouping <string, RL_SECAO_AGENDAMENTO> > sessoes = context.RL_SECAO_AGENDAMENTO
                                                                                      .Include(x => x.TB_SECOES)
                                                                                      .Where(x => agendamentos.Contains(x.TB_AGENDA.Id))
                                                                                      .ToArray()
                                                                                      .GroupBy(x => x.TB_SECOES.DS_IDENTIFICADOR);

                    IEnumerable <RL_SECAO_AGENDAMENTO> rl_secao_agendamento = sessoes.Where(x => x.Count() < servico.QT_SESSOES)
                                                                              .FirstOrDefault();

                    if (rl_secao_agendamento != null && rl_secao_agendamento.Any())
                    {
                        secao = rl_secao_agendamento.FirstOrDefault().TB_SECOES;
                    }

                    if (secao == null)
                    {
                        secao = new TB_SECOES
                        {
                            DS_IDENTIFICADOR = Guid.NewGuid().ToString()
                        };

                        context.TB_SECOES.Add(secao);
                        context.SaveChanges();

                        TimeSpan horasServico = TimeSpan.FromMinutes(servico.TP_MINUTOS);
                        if (horasServico.Hours == 1 && model.QuantidadeSessao == 1)
                        {
                            TimeSpan maisTrintaMinutos     = model.HorarioAgendamento.Add(new TimeSpan(0, 30, 0));
                            DateTime proximosTrintaMinutos = model.DataAgendamento.AddTicks(maisTrintaMinutos.Ticks);

                            TB_AGENDA proximoHorarioOcupado = context.TB_AGENDA.SingleOrDefault(x => x.DT_AGENDAMENTO == proximosTrintaMinutos);
                            if (proximoHorarioOcupado != null)
                            {
                                throw new Exception(string.Format(ExceptionMessages.ProximoHorarioInvalido, proximosTrintaMinutos.ToLongTimeString(), proximosTrintaMinutos.ToLongDateString()));
                            }

                            horarios.Add(maisTrintaMinutos);
                        }
                    }
                    else
                    {
                        model.QuantidadeSessao = servico.QT_SESSOES;
                    }
                }

                foreach (TimeSpan horario in horarios)
                {
                    DateTime  dataAgendamento = model.DataAgendamento.AddTicks(horario.Ticks);
                    TB_AGENDA agendamento     = new TB_AGENDA
                    {
                        DT_AGENDAMENTO         = dataAgendamento,
                        DT_CRIACAO             = DateTime.Now,
                        ID_CLIENTE             = model.IdCliente,
                        ID_SERVICO             = model.IdServico,
                        QT_SESSOES_AGENDAMENTO = model.QuantidadeSessao
                    };

                    context.TB_AGENDA.Add(agendamento);

                    RL_SECAO_AGENDAMENTO secaoAgendamento = new RL_SECAO_AGENDAMENTO
                    {
                        ID_AGENDAMENTO = agendamento.Id,
                        ID_SECAO       = secao.Id
                    };

                    context.RL_SECAO_AGENDAMENTO.Add(secaoAgendamento);
                    context.SaveChanges();
                }
            }
            else
            {
                throw new Exception(string.Format(ExceptionMessages.QuantidadeDeSessoesNaoPermitida, servico.QT_SESSOES));
            }
        }