protected void btn_Salvar_Click(object sender, EventArgs e) { if (oficina != null) { try { int qtdAgendamento = 0; int numeroEndereco = 0; int lenCNPJ = txt_OficinaCNPJ.Text.Replace(".", "").Replace("/", "").Replace("-", "").Length; int lenCEP = txt_CEP.Text.Replace("-", "").Length; qtdAgendamento = int.Parse(txt_CapacidadeAtendimento.Text); numeroEndereco = int.Parse(txt_Numero.Text); if ((txt_HorarioAberturaUtil.Text.Length == 0 || txt_HorarioFechamentoUtil.Text.Length == 0) && (txt_HorarioAberturaSabado.Text.Length == 0 || txt_HorarioFechamentoSabado.Text.Length == 0) && (txt_HorarioAberturaDomingo.Text.Length == 0 || txt_HorarioFechamentoDomingo.Text.Length == 0)) { pnl_AlerSalvar.CssClass = "alert alert-danger"; pnl_AlerSalvar.Visible = true; lblAlertSalvar.Text = "Informe os horários de abertura e fechamento de pelo menos um dia das semana"; } else if (qtdAgendamento <= 0) { pnl_Alert.CssClass = "alert alert-danger"; pnl_Alert.Visible = true; lbl_Alert.Text = "A capacidade de agendamentos precisa ser maior que 0"; } else if (numeroEndereco <= 0) { pnl_Alert.CssClass = "alert alert-danger"; pnl_Alert.Visible = true; lbl_Alert.Text = "O número do endereço precisa ser maior que 0"; } else if (lenCNPJ != 14) { pnl_Alert.CssClass = "alert alert-danger"; pnl_Alert.Visible = true; lbl_Alert.Text = "Favor preencher o CNPJ corretamente"; } else if (lenCEP != 8) { pnl_Alert.CssClass = "alert alert-danger"; pnl_Alert.Visible = true; lbl_Alert.Text = "Favor preencher o CEP corretamente"; } else { using (var context = new DatabaseEntities()) { Endereco endereco = context.Endereco.Where(end => end.cnpjOficina.Equals(oficina.cnpj)).FirstOrDefault(); if (endereco != null) { endereco.cep = txt_CEP.Text.Replace("-", ""); endereco.cidade = txt_Cidade.Text; endereco.cnpjOficina = oficina.cnpj; endereco.complemento = txt_Complemento.Text; endereco.logradouro = txt_Rua.Text; endereco.numero = int.Parse(txt_Numero.Text); endereco.uf = txt_UF.Text; context.SaveChanges(); pnl_AlerSalvar.Visible = true; } else { pnl_AlerSalvar.Visible = true; pnl_AlerSalvar.CssClass = "alert-danger"; lblAlertSalvar.Text = "O endereco nao foi localizado no banco de dados"; } oficina = context.Oficina.Where(o => o.cnpj.Equals(oficina.cnpj)).FirstOrDefault(); if (oficina != null) { oficina.nome = txt_OficinaNome.Text; oficina.telefone = txt_Telefone.Text.Replace("-", "").Replace("(", "").Replace(")", ""); oficina.email = txt_Email.Text; oficina.descricao = txt_Descrição.Text; oficina.capacidadeAgendamentos = int.Parse(txt_CapacidadeAtendimento.Text); oficina.duracaoAtendimento = TimeSpan.Parse(txt_DuracaoAtendimento.Text); context.SaveChanges(); //Alterar data e dia de funcionamento DiaFuncionamento dia; if (txt_HorarioAberturaUtil.Text.Length > 0 && txt_HorarioFechamentoUtil.Text.Length > 0) { dia = context.DiaFuncionamento.Where(d => d.cnpjOficina.Equals(oficina.cnpj) && d.diaSemana.Equals("util")).FirstOrDefault(); if (dia != null) { dia.horaAbertura = TimeSpan.Parse(txt_HorarioAberturaUtil.Text); dia.horaFechamento = TimeSpan.Parse(txt_HorarioFechamentoUtil.Text); } else { dia = new DiaFuncionamento() { diaSemana = "util", cnpjOficina = oficina.cnpj, horaAbertura = TimeSpan.Parse(txt_HorarioAberturaUtil.Text), horaFechamento = TimeSpan.Parse(txt_HorarioFechamentoUtil.Text) }; } context.DiaFuncionamento.Add(dia); context.SaveChanges(); } else { dia = context.DiaFuncionamento.Where(d => d.cnpjOficina.Equals(oficina.cnpj) && d.diaSemana.Equals("util")).FirstOrDefault(); if (dia != null) { context.DiaFuncionamento.Remove(dia); context.SaveChanges(); } } if (txt_HorarioAberturaSabado.Text.Length > 0 && txt_HorarioFechamentoSabado.Text.Length > 0) { dia = context.DiaFuncionamento.Where(d => d.cnpjOficina.Equals(oficina.cnpj) && d.diaSemana.Equals("sabado")).FirstOrDefault(); if (dia != null) { dia.horaAbertura = TimeSpan.Parse(txt_HorarioAberturaSabado.Text); dia.horaFechamento = TimeSpan.Parse(txt_HorarioFechamentoSabado.Text); } else { dia = new DiaFuncionamento() { diaSemana = "sabado", cnpjOficina = oficina.cnpj, horaAbertura = TimeSpan.Parse(txt_HorarioAberturaSabado.Text), horaFechamento = TimeSpan.Parse(txt_HorarioFechamentoSabado.Text) }; } context.DiaFuncionamento.Add(dia); context.SaveChanges(); } else { dia = context.DiaFuncionamento.Where(d => d.cnpjOficina.Equals(oficina.cnpj) && d.diaSemana.Equals("sabado")).FirstOrDefault(); if (dia != null) { context.DiaFuncionamento.Remove(dia); context.SaveChanges(); } } if (txt_HorarioAberturaDomingo.Text.Length > 0 && txt_HorarioFechamentoDomingo.Text.Length > 0) { dia = context.DiaFuncionamento.Where(d => d.cnpjOficina.Equals(oficina.cnpj) && d.diaSemana.Equals("domingo")).FirstOrDefault(); if (dia != null) { dia.horaAbertura = TimeSpan.Parse(txt_HorarioAberturaDomingo.Text); dia.horaFechamento = TimeSpan.Parse(txt_HorarioFechamentoDomingo.Text); } else { dia = new DiaFuncionamento() { diaSemana = "domingo", cnpjOficina = oficina.cnpj, horaAbertura = TimeSpan.Parse(txt_HorarioAberturaDomingo.Text), horaFechamento = TimeSpan.Parse(txt_HorarioFechamentoDomingo.Text) }; } context.DiaFuncionamento.Add(dia); context.SaveChanges(); } else { dia = context.DiaFuncionamento.Where(d => d.cnpjOficina.Equals(oficina.cnpj) && d.diaSemana.Equals("domingo")).FirstOrDefault(); if (dia != null) { context.DiaFuncionamento.Remove(dia); context.SaveChanges(); } } pnl_AlerSalvar.Visible = true; } else { pnl_AlerSalvar.Visible = true; pnl_AlerSalvar.CssClass = "alert-danger"; lblAlertSalvar.Text = "A oficina nao foi localizada no banco de dados"; } } } } catch (Exception ex) { Response.Write("<script>alert('" + ex.Message + "');</script>"); } } }
protected void btn_Cadastrar_Click(object sender, EventArgs e) { int qtdAgendamento = 0; int numeroEndereco = 0; int lenCNPJ = txt_CNPJ.Text.Replace(".", "").Replace("/", "").Replace("-", "").Length; int lenCEP = txt_CEP.Text.Replace("-", "").Length; qtdAgendamento = int.Parse(num_Agendamentos.Text); numeroEndereco = int.Parse(txt_Numero.Text); try { if ((txt_HorarioAberturaUtil.Text.Length == 0 || txt_HorarioFechamentoUtil.Text.Length == 0) && (txt_HorarioAberturaSabado.Text.Length == 0 || txt_HorarioFechamentoSabado.Text.Length == 0) && (txt_HorarioAberturaDomingo.Text.Length == 0 || txt_HorarioFechamentoDomingo.Text.Length == 0)) { pnl_Alert.CssClass = "alert alert-danger"; pnl_Alert.Visible = true; lbl_Alert.Text = "Informe os horários de abertura e fechamento de pelo menos um dia das semana"; } else if (qtdAgendamento <= 0) { pnl_Alert.CssClass = "alert alert-danger"; pnl_Alert.Visible = true; lbl_Alert.Text = "A capacidade de agendamentos precisa ser maior que 0"; } else if (numeroEndereco <= 0) { pnl_Alert.CssClass = "alert alert-danger"; pnl_Alert.Visible = true; lbl_Alert.Text = "O número do endereço precisa ser maior que 0"; } else if (lenCNPJ != 14) { pnl_Alert.CssClass = "alert alert-danger"; pnl_Alert.Visible = true; lbl_Alert.Text = "Favor preencher o CNPJ corretamente"; } else if (lenCEP != 8) { pnl_Alert.CssClass = "alert alert-danger"; pnl_Alert.Visible = true; lbl_Alert.Text = "Favor preencher o CEP corretamente"; } else { using (var context = new DatabaseEntities()) { Oficina o = new Oficina { cnpj = txt_CNPJ.Text.Replace(".", "").Replace("/", "").Replace("-", ""), nome = txt_Nome.Text, telefone = txt_Telefone.Text.Replace("(", "").Replace(")", "").Replace(" ", "").Replace("-", ""), email = txt_Email.Text, capacidadeAgendamentos = int.Parse(num_Agendamentos.Text), statusAssinatura = 1, duracaoAtendimento = TimeSpan.Parse(txt_TempoAtendimento.Text) }; Endereco endereco = new Endereco() { cep = txt_CEP.Text.Replace("-", ""), cidade = txt_Cidade.Text, cnpjOficina = o.cnpj, complemento = txt_Complemento.Text, logradouro = txt_Rua.Text, numero = int.Parse(txt_Numero.Text), uf = txt_UF.Text, bairro = txt_Bairro.Text }; if (context.Oficina.Where(oficina => oficina.cnpj.Equals(o.cnpj)).FirstOrDefault() != null) { pnl_Alert.Visible = true; pnl_Alert.CssClass = "alert alert-danger"; lbl_Alert.Text = "Uma oficina com este CNPJ já existe"; } else { context.Oficina.Add(o); context.Endereco.Add(endereco); DiaFuncionamento dia; if (txt_HorarioAberturaUtil.Text.Length > 0 && txt_HorarioFechamentoUtil.Text.Length > 0) { dia = new DiaFuncionamento() { diaSemana = "util", cnpjOficina = o.cnpj, horaAbertura = TimeSpan.Parse(txt_HorarioAberturaUtil.Text), horaFechamento = TimeSpan.Parse(txt_HorarioFechamentoUtil.Text) }; context.DiaFuncionamento.Add(dia); context.SaveChanges(); } if (txt_HorarioAberturaSabado.Text.Length > 0 && txt_HorarioFechamentoSabado.Text.Length > 0) { dia = new DiaFuncionamento() { diaSemana = "sabado", cnpjOficina = o.cnpj, horaAbertura = TimeSpan.Parse(txt_HorarioAberturaSabado.Text), horaFechamento = TimeSpan.Parse(txt_HorarioFechamentoSabado.Text) }; context.DiaFuncionamento.Add(dia); context.SaveChanges(); } if (txt_HorarioAberturaDomingo.Text.Length > 0 && txt_HorarioFechamentoDomingo.Text.Length > 0) { dia = new DiaFuncionamento() { diaSemana = "domingo", cnpjOficina = o.cnpj, horaAbertura = TimeSpan.Parse(txt_HorarioAberturaDomingo.Text), horaFechamento = TimeSpan.Parse(txt_HorarioFechamentoDomingo.Text) }; context.DiaFuncionamento.Add(dia); context.SaveChanges(); } Funcionario f = new Funcionario { cargo = "Gerente", cpf = c.cpf, cnpjOficina = o.cnpj, }; context.Funcionario.Add(f); context.SaveChanges(); autorizar(o); } } } } catch (Exception ex) { pnl_Alert.CssClass = "alert alert-danger"; pnl_Alert.Visible = true; lbl_Alert.Text = "Erro: " + ex.Message + Environment.NewLine + "Por favor entre em contato com o suporte"; } }