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

                var exceptionMessage = string.Empty;

                if (string.IsNullOrEmpty(TxtRazonSocial.Text))
                {
                    exceptionMessage += "La razón social de la empresa no puede ser vacío.\n";
                }

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

                if (string.IsNullOrEmpty(TxtTelefono.Text))
                {
                    exceptionMessage += "El telefono de la empresa no puede ser vacío.\n";
                }

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

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

                if (string.IsNullOrEmpty(TxtCiudad.Text))
                {
                    exceptionMessage += "La ciudad de la empresa no puede ser vacío.\n";
                }

                if (string.IsNullOrEmpty(TxtCuit.Text))
                {
                    exceptionMessage += "El CUIT de la empresa no puede ser vacío.\n";
                }

                if (string.IsNullOrEmpty(TxtNombreContacto.Text))
                {
                    exceptionMessage += "El nombre de contacto de la empresa no puede ser vacío.\n";
                }

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



                #endregion

                if (insertMode)
                {
                    if (EmpresaPersistance.GetByBusinessName(TxtRazonSocial.Text, this.currentTransaction) != null)
                    {
                        throw new Exception("Ya existe una empresa con la razón social ingresada.");
                    }

                    if (EmpresaPersistance.GetByCUIT(TxtCuit.Text, this.currentTransaction) != null)
                    {
                        throw new Exception("Ya existe una empresa con el CUIT ingresado.");
                    }

                    #region Inserto la nueva empresa

                    var company = new Empresa();
                    company.IdUsuario      = SessionManager.CurrentUser.ID;
                    company.RazonSocial    = TxtRazonSocial.Text;
                    company.Mail           = TxtMail.Text;
                    company.Telefono       = TxtTelefono.Text;
                    company.Direccion      = TxtDireccion.Text;
                    company.CodigoPostal   = TxtCodigoPostal.Text;
                    company.Ciudad         = TxtCiudad.Text;
                    company.CUIT           = TxtCuit.Text;
                    company.NombreContacto = TxtNombreContacto.Text;
                    company.FechaCreacion  = DtpFechaCreacion.Value;

                    var dialogAnswer = MessageBox.Show("Esta seguro que quiere insertar la nueva empresa?", "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 = company.CUIT;
                            user.Password = SHA256Helper.Encode("temporal");
                            var userIngresado = UsuarioPersistance.InsertUserTemporal(user, this.currentTransaction);

                            company.IdUsuario = userIngresado.ID;
                            EmpresaPersistance.InsertCompany(company, this.currentTransaction);
                            this.currentTransaction.Commit();

                            var rol = RolPersistance.GetByName("Empresa");
                            RolPersistance.InsertUserRole(userIngresado, rol, null);
                        }
                        else
                        {
                            EmpresaPersistance.InsertCompany(company, this.currentTransaction);
                            this.currentTransaction.Commit();
                        }

                        MessageBox.Show("La Empresa ha sido ingresada con éxito.", "Atención!");
                        this.Hide();
                        if (!_abmEmpresa)
                        {
                            var frmHome = new FrmHome();
                            frmHome.ShowDialog();
                        }
                    }

                    #endregion
                }
                else
                {
                    var company = new Empresa();
                    company.IdUsuario      = CurrentEmpresa.IdUsuario;
                    company.RazonSocial    = TxtRazonSocial.Text;
                    company.Mail           = TxtMail.Text;
                    company.Telefono       = TxtTelefono.Text;
                    company.Direccion      = TxtDireccion.Text;
                    company.CodigoPostal   = TxtCodigoPostal.Text;
                    company.Ciudad         = TxtCiudad.Text;
                    company.CUIT           = TxtCuit.Text;
                    company.NombreContacto = TxtNombreContacto.Text;
                    company.FechaCreacion  = DtpFechaCreacion.Value;

                    var dialogAnswer = MessageBox.Show("Esta seguro que quiere modificar la empresa?", "Atencion", MessageBoxButtons.YesNo);
                    if (dialogAnswer == DialogResult.Yes)
                    {
                        EmpresaPersistance.UpdateCompany(company);
                        CompleteAction = true;
                        this.Hide();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Atención");
            }
        }
        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");
            }
        }
예제 #3
0
        private void LblGrabar_Click(object sender, EventArgs e)
        {
            try
            {
                #region Validaciones
                var exceptionMessage = string.Empty;

                if (string.IsNullOrEmpty(TxtUsuario.Text))
                {
                    throw new Exception("El usuario no puede ser vacío.");
                }

                if (string.IsNullOrEmpty(TxtContrasena.Text))
                {
                    throw new Exception("La contraseña no puede ser vacía.");
                }

                if (UsuarioPersistance.GetByUsername(TxtUsuario.Text) != null)
                {
                    exceptionMessage += Environment.NewLine + "Ya existe un usuario con el nombre ingresado.";
                }


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

                #region Inserto el nuevo usuario

                var user = new Usuario();
                user.Username = TxtUsuario.Text;
                user.Password = SHA256Helper.Encode(TxtContrasena.Text);

                var dialogAnswer = MessageBox.Show("Esta seguro que quiere insertar el nuevo usuario?", "Atencion", MessageBoxButtons.YesNo);
                if (dialogAnswer == DialogResult.Yes)
                {
                    using (var transaction = DataBaseManager.Instance().Connection.BeginTransaction(IsolationLevel.Serializable))
                    {
                        if (_abmEmpresa || _abmCliente)
                        {
                            user = UsuarioPersistance.InsertUserTemporal(user, transaction);
                        }
                        else
                        {
                            user = UsuarioPersistance.InsertUser(user, transaction);
                        }

                        Rol selectedRol = (Rol)CboRoles.SelectedItem;
                        RolPersistance.InsertUserRole(user, selectedRol, transaction);

                        if (!_abmEmpresa && !_abmCliente)
                        {
                            //Cargo los datos en sesion
                            SessionManager.CurrentUser = user;
                            SessionManager.CurrentRol  = selectedRol;
                            SessionManager.CurrentRol.Funcionalidades = FuncionalidadPersistance.GetByRole(SessionManager.CurrentRol, transaction);
                        }

                        switch (selectedRol.Descripcion)
                        {
                        case "Cliente":
                            this.Hide();
                            var frmABMInsertUpdateCliente = new FrmABMInsertUpdateCliente(transaction, _abmCliente, user);
                            frmABMInsertUpdateCliente.ShowDialog();
                            break;

                        case "Empresa":
                            this.Hide();
                            var frmABMInsertUpdateEmpresa = new FrmABMInsertUpdateEmpresa(transaction, _abmEmpresa, user);
                            frmABMInsertUpdateEmpresa.ShowDialog();
                            break;

                        default:
                            transaction.Commit();
                            this.Hide();
                            var frmHome = new FrmHome();
                            frmHome.ShowDialog();
                            break;
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Atención");
            }
        }