public ResultDTO CreateDepartment(BusinessEntities.DepartmentEntity departmentEntity)
        {
            //CodeGenerator code = new CodeGenerator();

            //var res = String.Empty;
            //var result = _unitOfWork.DepartmentRepository.GetMany(x => x.DepartmentId == departmentEntity.DepartmentId);
            //if (result.Count() > 0)
            //{
            //    res = (from c in result
            //           orderby c.DepartmentId descending
            //           select c).Take(1).First().DepartmentCode;
            //}
            //var last = res != null ? code.NumberFromExcelColumn(res) : 0;
            //var DepartmentCode = code.GenerateSequence(last);

            var result = new ResultDTO {
                IsSuccess = false
            };

            var isExist = _unitOfWork.DepartmentRepository.GetManyQueryable(c => c.DepartmentName.ToLower() == departmentEntity.DepartmentName.ToLower() && c.CompanyId == departmentEntity.CompanyId).Count() > 0;

            if (!isExist)
            {
                using (var scope = new TransactionScope())
                {
                    var department = new DataModel.Department
                    {
                        DepartmentName = departmentEntity.DepartmentName,
                        DepartmentCode = departmentEntity.DepartmentCode,
                        CompanyId      = departmentEntity.CompanyId,
                        ParentId       = departmentEntity.ParentId,
                        IsActive       = true,
                        CreatedBy      = departmentEntity.CreatedBy,
                        CreatedOn      = DateTime.Now,
                        ModifiedBy     = departmentEntity.CreatedBy,
                        ModifiedOn     = DateTime.Now,
                    };
                    _unitOfWork.DepartmentRepository.Insert(department);
                    _unitOfWork.Save();
                    scope.Complete();

                    result.IsSuccess = true;
                    result.Message   = "Inserted Department Successfully";
                }
            }
            else
            {
                result.IsSuccess = false;
                result.Message   = "Department name already exist";
            }
            return(result);
        }
        private void SaveNewDepartmentButton_Click_1(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(DepartmentNameTextBox.Text.Trim()))
            {
                DepartmentNameerrorProvider.SetError(DepartmentNameTextBox, "Sorry!Please input department name.");
            }
            else if (DepartmentNameTextBox.Text.Trim() != "")
            {
                DepartmentNameerrorProvider.SetError(DepartmentNameTextBox, "");
            }
            if (string.IsNullOrEmpty(DepartmentManagerTextBox.Text.Trim()))
            {
                DepartmentManagererrorProvider.SetError(DepartmentManagerTextBox, "Sorry!Please input department manager.");
            }
            else if (DepartmentManagerTextBox.Text.Trim() != "")
            {
                DepartmentManagererrorProvider.SetError(DepartmentManagerTextBox, "");
            }
            if (string.IsNullOrEmpty(DescriptionTextBox.Text.Trim()))
            {
                DepartmentManagererrorProvider.SetError(DescriptionTextBox, "Sorry!Please input description.");
            }
            else if (DescriptionTextBox.Text.Trim() != "")
            {
                DepartmentManagererrorProvider.SetError(DescriptionTextBox, "");
            }

            bool dp = new DepartmentService().SearchDepartmentByDepartmentName(DepartmentNameTextBox.Text.Trim());
            bool bl = new DepartmentService().SearchOneStaffId(DepartmentManagerTextBox.Text.Trim());

            if (bl && !dp && DepartmentNameTextBox.Text.Trim() != "" && DepartmentManagerTextBox.Text.Trim() != "" && DescriptionTextBox.Text.Trim() != "")
            {
                DataModel.Department department = new DataModel.Department();
                department.DepartmentName      = DepartmentNameTextBox.Text.Trim();
                department.Description         = DescriptionTextBox.Text.Trim();
                department.DepartmentManagerId = new DepartmentService().GetManagerId(DepartmentManagerTextBox.Text.Trim());
                department.ParentDepartmentId  = Convert.ToInt32(ParentDepartmentcomboBox.SelectedValue.ToString());
                new DepartmentService().AddDepartment(department);

                this.DepartmentDataGridView.Columns.RemoveAt(9);
                this.DepartmentDataGridView.Columns.RemoveAt(8);
                this.DepartmentDataGridView.Columns.RemoveAt(7);
                this.DepartmentDataGridView.Columns.RemoveAt(6);
                this.DepartmentDataGridView.Columns.RemoveAt(5);
                this.DepartmentDataGridView.DataSource = null;
                Bind();

                MessageBox.Show("Add successfully.");
                DepartmentNameLabel.Visible      = false;
                DepartmentNameTextBox.Visible    = false;
                DepartmentNameTextBox.Text       = "";
                DepartmentManagerLabel.Visible   = false;
                DepartmentManagerTextBox.Visible = false;
                DepartmentManagerTextBox.Text    = "";
                DescriptionLable.Visible         = false;
                DescriptionTextBox.Visible       = false;
                DescriptionTextBox.Text          = "";
                ParentDepartmentLable.Visible    = false;
                ParentDepartmentcomboBox.Visible = false;

                this.Size = new System.Drawing.Size(this.Size.Width, 540);
            }
            else if (dp)
            {
                MessageBox.Show("This department already exist.");
                DepartmentNameTextBox.Text = "";
            }
            else if (!bl)
            {
                MessageBox.Show("This manager does not exist.");
                DepartmentManagerTextBox.Text = "";
            }
        }
        private void DepartmentDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int rows         = DepartmentDataGridView.CurrentRow.Index;
            int DepartmentId = Convert.ToInt32(DepartmentDataGridView.Rows[rows].Cells[0].Value);

            try
            {
                if (DepartmentDataGridView.Columns[e.ColumnIndex].HeaderText == "DeleteManagement")
                {
                    foreach (DataGridViewCell cell in DepartmentDataGridView.Rows[rows].Cells)
                    {
                        cell.Style.BackColor = Color.SkyBlue;
                    }

                    bool dp = new DepartmentService().SearchEmployeeIdByDepartment(DepartmentId);
                    if (!dp && DepartmentId != 0 && MessageBox.Show("Do you want to delete it?", "Warning Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.OK)
                    {
                        new DepartmentService().DeleteDepartment(DepartmentId);
                        this.DepartmentDataGridView.Columns.RemoveAt(9);
                        this.DepartmentDataGridView.Columns.RemoveAt(8);
                        this.DepartmentDataGridView.Columns.RemoveAt(7);
                        this.DepartmentDataGridView.Columns.RemoveAt(6);
                        this.DepartmentDataGridView.Columns.RemoveAt(5);
                        this.DepartmentDataGridView.DataSource = null;
                        Bind();
                    }
                    else if (dp)
                    {
                        MessageBox.Show("This department can not be deleted right now.");
                        foreach (DataGridViewCell cell in DepartmentDataGridView.Rows[rows].Cells)
                        {
                            cell.Style.BackColor = Color.Empty;
                        }
                    }
                    else
                    {
                        foreach (DataGridViewCell cell in DepartmentDataGridView.Rows[rows].Cells)
                        {
                            cell.Style.BackColor = Color.Empty;
                        }
                    }
                }
                if (DepartmentDataGridView.Columns[e.ColumnIndex].HeaderText == "EditManagement")
                {
                    if (DepartmentId != 0)
                    {
                        DepartmentDataGridView.Rows[rows].ReadOnly = false;
                        DepartmentDataGridView.Columns[9].Visible  = false;
                        DepartmentDataGridView.Columns[8].Visible  = true;
                        DepartmentDataGridView.Columns[7].Visible  = true;
                        DepartmentDataGridView.Columns[6].Visible  = false;
                        DepartmentDataGridView.Columns[5].Visible  = true;
                        DepartmentDataGridView.Columns[4].Visible  = false;
                        foreach (DataGridViewCell cell in DepartmentDataGridView.Rows[rows].Cells)
                        {
                            cell.Style.BackColor = Color.SkyBlue;
                        }
                    }
                    else
                    {
                        foreach (DataGridViewCell cell in DepartmentDataGridView.Rows[rows].Cells)
                        {
                            cell.Style.BackColor = Color.Empty;
                        }
                    }
                }
                if (DepartmentDataGridView.Columns[e.ColumnIndex].HeaderText == "UpdateManagement")
                {
                    if (DepartmentDataGridView.Rows[rows].Cells[7].Style.BackColor == Color.SkyBlue)
                    {
                        DataModel.Department departmentnew = new DataModel.Department();
                        departmentnew.DepartmentId = Convert.ToInt32(DepartmentDataGridView.Rows[rows].Cells[0].Value);
                        departmentnew.Description  = DepartmentDataGridView.Rows[rows].Cells[2].Value.ToString().Trim();
                        bool stf  = new DepartmentService().SearchOneStaffId(DepartmentDataGridView.Rows[rows].Cells[3].Value.ToString().Trim());
                        bool dept = new DepartmentService().SearchDepartmentByDepartmentName(DepartmentDataGridView.Rows[rows].Cells[1].Value.ToString().Trim());
                        if (!dept)
                        {
                            departmentnew.DepartmentName = DepartmentDataGridView.Rows[rows].Cells[1].Value.ToString().Trim();
                        }
                        else
                        {
                            int deptId = new DepartmentService().GetDepartmentId(DepartmentDataGridView.Rows[rows].Cells[1].Value.ToString().Trim());
                            if (deptId == departmentnew.DepartmentId)
                            {
                                departmentnew.DepartmentName = DepartmentDataGridView.Rows[rows].Cells[1].Value.ToString().Trim();
                            }
                            else
                            {
                                MessageBox.Show("This department already exist.");
                            }
                        }

                        if (DepartmentDataGridView.Rows[rows].Cells[5].Value != null)
                        {
                            departmentnew.ParentDepartmentId = Convert.ToInt32(DepartmentDataGridView.Rows[rows].Cells[5].Value.ToString().Trim());
                        }
                        else
                        {
                            departmentnew.ParentDepartmentId = new DepartmentService().GetParentId(DepartmentDataGridView.Rows[rows].Cells[5].Style.NullValue.ToString().Trim());
                        }

                        if (!stf)
                        {
                            MessageBox.Show("This manager does not exist.");
                        }
                        else if (stf && departmentnew.DepartmentId != 0 && departmentnew.DepartmentName != null && departmentnew.ParentDepartmentId != 0)
                        {
                            departmentnew.DepartmentManagerId = new DepartmentService().GetManagerId(DepartmentDataGridView.Rows[rows].Cells[3].Value.ToString().Trim());
                            new DepartmentService().UpdateDepartment(departmentnew);
                            MessageBox.Show("Update successfully.");
                            foreach (DataGridViewCell cell in DepartmentDataGridView.Rows[rows].Cells)
                            {
                                cell.Style.BackColor = Color.Empty;
                            }
                            this.DepartmentDataGridView.Columns.RemoveAt(9);
                            this.DepartmentDataGridView.Columns.RemoveAt(8);
                            this.DepartmentDataGridView.Columns.RemoveAt(7);
                            this.DepartmentDataGridView.Columns.RemoveAt(6);
                            this.DepartmentDataGridView.Columns.RemoveAt(5);
                            this.DepartmentDataGridView.DataSource = null;
                            Bind();
                        }
                    }
                    else
                    {
                        MessageBox.Show("It is the different row.Please check it again");
                    }
                }
                if (DepartmentDataGridView.Columns[e.ColumnIndex].HeaderText == "CancleManagement")
                {
                    if (DepartmentDataGridView.Rows[rows].Cells[8].Style.BackColor == Color.SkyBlue)
                    {
                        foreach (DataGridViewCell cell in DepartmentDataGridView.Rows[rows].Cells)
                        {
                            cell.Style.BackColor = Color.Empty;
                        }
                        this.DepartmentDataGridView.Columns.RemoveAt(9);
                        this.DepartmentDataGridView.Columns.RemoveAt(8);
                        this.DepartmentDataGridView.Columns.RemoveAt(7);
                        this.DepartmentDataGridView.Columns.RemoveAt(6);
                        this.DepartmentDataGridView.Columns.RemoveAt(5);
                        this.DepartmentDataGridView.DataSource = null;
                        Bind();
                    }
                    else
                    {
                        MessageBox.Show("It is the different row.Please check it again");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "System tips", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Exemple #4
0
        private void DepartmentDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int rows = DepartmentDataGridView.CurrentRow.Index;

            DataModel.Department department = new DataModel.Department();
            try
            {
                if (DepartmentDataGridView.Columns[e.ColumnIndex].HeaderText == "DeleteManagement")
                {
                    department.DepartmentId      = Convert.ToInt32(DepartmentDataGridView.Rows[rows].Cells[0].Value);
                    department.DepartmentName    = DepartmentDataGridView.Rows[rows].Cells[1].Value.ToString();
                    department.DepartmentManager = DepartmentDataGridView.Rows[rows].Cells[2].Value.ToString();

                    foreach (DataGridViewCell cell in DepartmentDataGridView.Rows[rows].Cells)
                    {
                        cell.Style.BackColor = Color.SkyBlue;
                    }
                    new DepartmentService().SearchEmployeeIdByDepartmentName(department);
                    if (department.DepartmentId != null && MessageBox.Show("Do you want to edit it?", "Warning Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.OK)
                    {
                        //SystemConstantDataGridView.Rows[rows].Cells[2].ReadOnly = false;
                        //SystemConstantDataGridView.Columns[5].Visible = true;
                        //SystemConstantDataGridView.Columns[4].Visible = false;
                    }
                    else
                    {
                        foreach (DataGridViewCell cell in SystemConstantDataGridView.Rows[rows].Cells)
                        {
                            cell.Style.BackColor = Color.Empty;
                        }
                    }
                }
                if (SystemConstantDataGridView.Columns[e.ColumnIndex].HeaderText == "UpdateManagement")
                {
                    if (SystemConstantDataGridView.Rows[rows].Cells[5].Style.BackColor == Color.SkyBlue)
                    {
                        SystemConstantDataGridView.Columns[5].Visible = false;
                        systemConstant.ConstantName  = SystemConstantDataGridView.Rows[rows].Cells[1].Value.ToString();
                        systemConstant.ConstantValue = SystemConstantDataGridView.Rows[rows].Cells[2].Value.ToString();
                        systemConstant.Description   = SystemConstantDataGridView.Rows[rows].Cells[3].Value.ToString();
                        new SystemConstantService().UpdateSystemConstant(systemConstant);
                        SystemConstantDataGridView.DataSource = new SystemConstantService().GetSystemConstant();

                        SystemConstantDataGridView.Rows[rows].Cells[2].ReadOnly = true;
                        SystemConstantDataGridView.Columns[4].Visible           = true;
                        MessageBox.Show("Update successfully.");
                        foreach (DataGridViewCell cell in SystemConstantDataGridView.Rows[rows].Cells)
                        {
                            cell.Style.BackColor = Color.Empty;
                        }
                    }
                    else
                    {
                        MessageBox.Show("It is the different row.Please check it again");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "System tips", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }