private void LblGrabar_Click(object sender, EventArgs e)
        {
            try
            {
                #region Validaciones

                var exceptionMessage = string.Empty;

                if (string.IsNullOrEmpty(TxtNombre.Text))
                {
                    exceptionMessage += "El nombre del cliente no puede ser vacío.\n";
                }

                if (string.IsNullOrEmpty(TxtApellido.Text))
                {
                    exceptionMessage += "El apellido del cliente no puede ser vacío.\n";
                }

                if (string.IsNullOrEmpty(CboTipoDocumento.Text))
                {
                    exceptionMessage += "El tipo de documento del cliente no puede ser vacío.\n";
                }

                if (string.IsNullOrEmpty(TxtDocumento.Text))
                {
                    exceptionMessage += "El número de documento del cliente no puede ser vacío.\n";
                }
                else
                {
                    int nroDoc;
                    if (!int.TryParse(TxtDocumento.Text, out nroDoc))
                    {
                        exceptionMessage += "El número de documento del cliente no es válido.\n";
                    }
                }

                if (string.IsNullOrEmpty(TxtMail.Text))
                {
                    exceptionMessage += "El mail del cliente no puede ser vacío.\n";
                }

                if (string.IsNullOrEmpty(TxtTelefono.Text))
                {
                    exceptionMessage += "El teléfono del cliente no puede ser vacío.\n";
                }

                if (string.IsNullOrEmpty(TxtDireccion.Text))
                {
                    exceptionMessage += "La dirección del cliente no puede ser vacío.\n";
                }

                if (string.IsNullOrEmpty(TxtCodigoPostal.Text))
                {
                    exceptionMessage += "El código postal del cliente no puede ser vacío.\n";
                }

                if (string.IsNullOrEmpty(TxtCuil.Text))
                {
                    exceptionMessage += "El CUIL del cliente no puede ser vacío.\n";
                }
                else
                {
                    if (!TypesHelper.IsCUITValid(TxtCuil.Text))
                    {
                        exceptionMessage += Environment.NewLine + "El CUIL no es válido.\n";
                    }
                }

                if (!string.IsNullOrEmpty(exceptionMessage))
                {
                    throw new Exception(exceptionMessage);
                }

                #endregion

                if (insertMode)
                {
                    //Valido que no se dupliquen los telefonos ni documentos
                    if (ClientePersistance.GetByPhone(TxtTelefono.Text, this.currentTransaction) != null)
                    {
                        throw new Exception("Ya existe un cliente con el teléfono ingresado.");
                    }

                    if (ClientePersistance.GetByDocument((int)CboTipoDocumento.SelectedValue, Int32.Parse(TxtDocumento.Text), this.currentTransaction) != null)
                    {
                        throw new Exception("Ya existe un cliente con el tipo y número de documento ingresados.");
                    }

                    #region Inserto el nuevo cliente

                    var client = new Cliente();
                    client.IdUsuario       = SessionManager.CurrentUser.ID;
                    client.Nombre          = TxtNombre.Text;
                    client.Apellido        = TxtApellido.Text;
                    client.TipoDocumento   = (int)CboTipoDocumento.SelectedValue;
                    client.NroDocumento    = Int32.Parse(TxtDocumento.Text);
                    client.Mail            = TxtMail.Text;
                    client.Telefono        = TxtTelefono.Text;
                    client.Direccion       = TxtDireccion.Text;
                    client.CodigoPostal    = TxtCodigoPostal.Text;
                    client.CUIL            = TxtCuil.Text;
                    client.FechaNacimiento = DtpFechaNacimiento.Value;

                    var dialogAnswer = MessageBox.Show("¿Está seguro que quiere insertar el nuevo cliente?", "Atencion", MessageBoxButtons.YesNo);
                    if (dialogAnswer == DialogResult.Yes)
                    {
                        //Si es el administrador el que hace el Alta, se genera un usuario con password temporal
                        if (insertDefaultUser)
                        {
                            var user = new Usuario();
                            user.Username = client.NroDocumento.ToString();
                            user.Password = SHA256Helper.Encode("temporal");
                            var userIngresado = UsuarioPersistance.InsertUserTemporal(user, this.currentTransaction);

                            client.IdUsuario = userIngresado.ID;
                            ClientePersistance.InsertClient(client, this.currentTransaction);
                            this.currentTransaction.Commit();

                            var rol = RolPersistance.GetByName("Cliente");
                            RolPersistance.InsertUserRole(userIngresado, rol, null);
                        }
                        else
                        {
                            ClientePersistance.InsertClient(client, this.currentTransaction);
                            this.currentTransaction.Commit();
                        }

                        MessageBox.Show("El Cliente ha sido ingresado con éxito.", "Atención!");
                        this.Hide();
                        if (!_abmCliente)
                        {
                            var frmHome = new FrmHome();
                            frmHome.ShowDialog();
                        }
                    }

                    #endregion
                }
                else
                {
                    if (TxtTelefono.Text != CurrentCliente.Telefono)
                    {
                        if (ClientePersistance.GetByPhone(TxtTelefono.Text, this.currentTransaction) != null)
                        {
                            throw new Exception("Ya existe un cliente con el teléfono ingresado.");
                        }
                    }

                    var client = new Cliente();
                    client.IdUsuario       = CurrentCliente.IdUsuario;
                    client.Nombre          = TxtNombre.Text;
                    client.Apellido        = TxtApellido.Text;
                    client.TipoDocumento   = (int)CboTipoDocumento.SelectedValue;
                    client.NroDocumento    = Convert.ToInt32(TxtDocumento.Text);
                    client.Mail            = TxtMail.Text;
                    client.Telefono        = TxtTelefono.Text;
                    client.Direccion       = TxtDireccion.Text;
                    client.CodigoPostal    = TxtCodigoPostal.Text;
                    client.CUIL            = TxtCuil.Text;
                    client.FechaNacimiento = DtpFechaNacimiento.Value;

                    var dialogAnswer = MessageBox.Show("¿Está seguro que quiere modificar el cliente?", "Atención", MessageBoxButtons.YesNo);
                    if (dialogAnswer == DialogResult.Yes)
                    {
                        ClientePersistance.UpdateClient(client);
                        CompleteAction = true;
                        this.Hide();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Atención");
            }
        }
Ejemplo n.º 2
0
        private void LblBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                #region Validations

                var filtersSetted    = false;
                var exceptionMessage = string.Empty;

                if (!TypesHelper.IsEmpty(TxtRazonSocial.Text))
                {
                    filtersSetted = true;
                }

                if (!TypesHelper.IsEmpty(TxtCuit.Text))
                {
                    filtersSetted = true;
                    if (!TypesHelper.IsCUITValid(TxtCuit.Text))
                    {
                        exceptionMessage += Environment.NewLine + "El cuit no es un cuit válido.";
                    }
                }

                if (!TypesHelper.IsEmpty(TxtEmail.Text))
                {
                    filtersSetted = true;
                }

                if (!filtersSetted)
                {
                    exceptionMessage = "No se puede realizar la busqueda ya que no se informó ningún filtro";
                }

                if (!TypesHelper.IsEmpty(exceptionMessage))
                {
                    throw new Exception(exceptionMessage);
                }

                #endregion

                var filters = new EmpresaFilters
                {
                    RazonSocial = (!TypesHelper.IsEmpty(TxtRazonSocial.Text)) ? TxtRazonSocial.Text : null,
                    Cuit        = (!TypesHelper.IsEmpty(TxtCuit.Text)) ? TxtCuit.Text : null,
                    Email       = (!TypesHelper.IsEmpty(TxtEmail.Text)) ? TxtEmail.Text : null
                };

                var empresas = (ChkBusquedaExacta.Checked) ? EmpresaPersistance.GetAllBusinessByParameters(filters) : EmpresaPersistance.GetAllBusinessByParametersLike(filters);

                if (empresas == null || empresas.Count == 0)
                {
                    throw new Exception("No se encontraron empresas según los filtros informados.");
                }

                RefreshSources(empresas);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Atención");
            }
        }