/// <summary>
 /// Function to check the privilege
 /// </summary>
 /// <param name="userId"></param>
 /// <param name="formName"></param>
 /// <param name="action"></param>
 /// <returns></returns>
 public static bool PrivilegeCheck(decimal userId, string formName, string action)
 {
     bool isPrivilege = false;
     PrivilegeBll BllPrivilege = new PrivilegeBll();
     if (BllPrivilege.PrivilegeCheck(userId, formName, action))
     {
         isPrivilege = true;
     }
     else
     {
         isPrivilege = false;
     }
     return isPrivilege;
 }
 /// <summary>
 /// Save or edit function
 /// </summary>
 public void SaveOrEditFunction()
 {
     try
     {
         DataTable dtbl = new DataTable();
         PrivilegeBll BllPrivilege = new PrivilegeBll();
         if (cmbRole.Text.Trim() == string.Empty)
         {
             Messages.InformationMessage("Select role");
             cmbRole.Focus();
         }
         else
         {
             decimal decRoleId = Convert.ToDecimal(cmbRole.SelectedValue.ToString());
             if (BllPrivilege.RolePrivilegeSaveCheckExistence(decRoleId) == false)
             {
                 if (PublicVariables.isMessageAdd)
                 {
                     if (Messages.SaveMessage())
                     {
                         SaveFunction();
                         Messages.SavedMessage();
                         cmbRole.SelectedIndex = -1;
                         if (cmbRole.SelectedIndex == 0)
                         {
                             cmbRole.SelectedIndex = -1;
                             AllTabpageClear();
                         }
                     }
                 }
                 else
                 {
                     SaveFunction();
                 }
             }
             else
             {
                 if (PublicVariables.isMessageEdit)
                 {
                     if (Messages.UpdateMessage())
                     {
                         DeleteFunction();
                         SaveFunction();
                         Messages.UpdatedMessage();
                         cmbRole.SelectedIndex = -1;
                         if (cmbRole.SelectedIndex == 0)
                         {
                             cmbRole.SelectedIndex = -1;
                             AllTabpageClear();
                         }
                     }
                 }
                 else
                 {
                     DeleteFunction();
                     SaveFunction();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("RLPRST:10" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
        /// <summary>
        /// Save Function
        /// </summary>
        public void SaveFunction()
        {
            try
            {
                PrivilegeInfo infoPrivilege = new PrivilegeInfo();
                PrivilegeBll BllPrivilege = new PrivilegeBll();
                infoPrivilege.RoleId = Convert.ToDecimal(cmbRole.SelectedValue);
                infoPrivilege.Extra1 = string.Empty;
                infoPrivilege.Extra2 = string.Empty;
                foreach (Control tabPage in tcPrivillage.Controls)
                {
                    string strTabPage = tabPage.Text;
                    if (strTabPage == "Company")
                    {
                        infoPrivilege.FormName = "frm" + strTabPage +"Creation";
                        foreach (Control cbxCompany in tabPage.Controls)
                        {
                            if (cbxCompany is CheckBox)
                            {
                                CheckBox cbx = (CheckBox)cbxCompany;
                                if (cbx.Checked == true)
                                {
                                    string strCbx = cbx.Text.Replace(" ",string.Empty);
                                    if (strCbx == "Edit")
                                    {
                                        infoPrivilege.Action = "Update";

                                    }
                                    else
                                    {
                                        infoPrivilege.Action = strCbx;

                                    }
                                    BllPrivilege.PrivilegeAdd(infoPrivilege);

                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (Control gbxControl in tabPage.Controls)
                        {
                            if (gbxControl is GroupBox)
                            {
                                string strFormName = gbxControl.Text.Replace(" ", string.Empty);
                                foreach (Control cbxControl in gbxControl.Controls)
                                {
                                    CheckBox cbx = (CheckBox)cbxControl;
                                    if (cbx.Checked == true)
                                    {
                                        string strCbx = cbx.Text.Replace(" ", string.Empty);
                                        if (gbxControl.Text == "Transactions" || gbxControl.Text == "Payroll")
                                        {
                                            infoPrivilege.FormName = "frm" + strCbx;
                                            infoPrivilege.Action = "View";
                                            BllPrivilege.PrivilegeAdd(infoPrivilege);
                                        }
                                            /*------------------------------------*/
                                        else
                                        {
                                            if (strCbx != "All" && strCbx != "Register")
                                            {
                                                if (gbxControl.Text == "Price List" || gbxControl.Text == "Standerd Rate")
                                                {
                                                    infoPrivilege.FormName = "frm" + strFormName + "Popup";
                                                    if (strCbx == "Add")
                                                    {
                                                        infoPrivilege.Action = "Save";
                                                    }
                                                    else if (strCbx == "Edit")
                                                    {
                                                        infoPrivilege.Action = "Update";
                                                    }
                                                    else if (strCbx == "Delete")
                                                    {
                                                        infoPrivilege.Action = "Delete";
                                                    }
                                                    BllPrivilege.PrivilegeAdd(infoPrivilege);
                                                }

                                                else
                                                {
                                                    infoPrivilege.FormName = "frm" + strFormName;
                                                    if (strCbx == "Add")
                                                    {
                                                        infoPrivilege.Action = "Save";
                                                    }
                                                    else if (strCbx == "Edit")
                                                    {
                                                        infoPrivilege.Action = "Update";
                                                    }
                                                    else if (strCbx == "Delete")
                                                    {
                                                        infoPrivilege.Action = "Delete";
                                                    }
                                                    else if (strCbx == "View")
                                                    {
                                                        infoPrivilege.Action = "View";
                                                    }
                                                    BllPrivilege.PrivilegeAdd(infoPrivilege);
                                                }
                                            }

                                            else if (strCbx == "Register")
                                            {
                                                string strFormNameCreationRemove = "frm" + strFormName.Replace("Creation", string.Empty) + "Register";
                                                string strFormNameVoucherRemove = "frm" + strFormName.Replace("Voucher", string.Empty) + "Register";
                                                infoPrivilege.FormName = strFormNameCreationRemove;
                                                infoPrivilege.Action = "View";
                                                BllPrivilege.PrivilegeAdd(infoPrivilege);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {

                                if (gbxControl is CheckBox)
                                {
                                    CheckBox cbx = (CheckBox)gbxControl;
                                    if (cbx.Checked == true)
                                    {
                                        string strFormName = "frm" + gbxControl.Text.Replace(" ", string.Empty);
                                        infoPrivilege.FormName = strFormName;
                                        infoPrivilege.Action = "View";
                                        BllPrivilege.PrivilegeAdd(infoPrivilege);
                                    }
                                }
                            }
                        }
                    }
                }
                AllTabpageClear();
            }
            catch (Exception ex)
            {
                MessageBox.Show("RLPRST:9" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// Function to fill the controls
        /// </summary>
        public void FillControls()
        {
            try
            {
                PrivilegeBll BllPrivilege = new PrivilegeBll();
                List<DataTable> listObj = new List<DataTable>();
                List<DataTable> listObjAction = new List<DataTable>();
                decimal decRoleId = Convert.ToDecimal(cmbRole.SelectedValue.ToString());
                listObj = BllPrivilege.PrivilegeSettingsSearch(decRoleId);
                foreach (TabPage tabPage in tcPrivillage.Controls)
                {
                    if (tabPage.Text == "Company")
                    {
                        string strCompany = "frm" + tabPage.Text + "Creation";
                        foreach (DataRow dtCompany in listObj[0].Rows)
                        {
                            foreach (Control cbxCompany in tabPage.Controls)
                            {
                                if (cbxCompany is CheckBox)
                                {
                                    CheckBox cbx = (CheckBox)cbxCompany;
                                    string strCbx = cbx.Text.Replace(" ", string.Empty);
                                    listObjAction = BllPrivilege.PrivilegeActionSearch(strCompany, decRoleId);
                                    foreach (DataRow drAction in listObjAction[0].Rows)
                                    {
                                        if (drAction["formName"].ToString() == strCompany)
                                        {
                                            if (drAction["action"].ToString() == strCbx)
                                            {
                                                cbx.Checked = true;
                                            }
                                            else if (drAction["action"].ToString() == "Update")
                                            {
                                                if (cbx.Text =="Edit")
                                                {
                                                    cbx.Checked = true;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else if (tabPage.Text == "Register" || tabPage.Text == "Reports1" || tabPage.Text == "Reports2")
                    {
                        foreach (Control cbx1 in tabPage.Controls)
                        {
                            if (cbx1 is CheckBox)
                            {
                                CheckBox cbx2 = (CheckBox)cbx1;
                                string strFormName = "frm" + cbx2.Text.Replace(" ", string.Empty);
                                listObjAction = BllPrivilege.PrivilegeActionSearch(strFormName, decRoleId);
                                foreach (DataRow drAction in listObjAction[0].Rows)
                                {
                                    if (drAction["formName"].ToString() == strFormName)
                                    {
                                        if (drAction["action"].ToString() == "View")
                                        {
                                            cbx2.Checked = true;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (cbx1 is GroupBox)
                                {
                                    foreach (Control cbxInGbx in cbx1.Controls)
                                    {
                                        CheckBox cbx3 = (CheckBox)cbxInGbx;
                                        string strFormName = "frm" + cbx3.Text.Replace(" ", string.Empty);
                                        listObjAction = BllPrivilege.PrivilegeActionSearch(strFormName, decRoleId);
                                        foreach (DataRow drFormName in listObjAction[0].Rows)
                                        {
                                            if (drFormName["formName"].ToString() == strFormName)
                                            {
                                                if (drFormName["action"].ToString() == "View")
                                                {
                                                    cbx3.Checked = true;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (Control GbxControle in tabPage.Controls)
                        {
                            if (GbxControle is GroupBox)
                            {
                                foreach (Control CbxControls in GbxControle.Controls)
                                {
                                    if (CbxControls is CheckBox)
                                    {
                                        CheckBox cbx = (CheckBox)CbxControls;
                                        string strFormName = "frm" + GbxControle.Text.Replace(" ", string.Empty);
                                        if (GbxControle.Text == "Price List" || GbxControle.Text == "Standerd Rate")
                                        {
                                            string strPopName = strFormName + "Popup";
                                            listObjAction = BllPrivilege.PrivilegeActionSearch(strPopName, decRoleId);
                                            foreach (DataRow dtaction in listObjAction[0].Rows)
                                            {
                                                if (dtaction["action"].ToString() == "Save")
                                                {
                                                    if (dtaction["formName"].ToString() == strPopName)
                                                    {
                                                        if (cbx.Text =="Add")
                                                        {
                                                            cbx.Checked = true;
                                                        }
                                                    }
                                                }
                                                else if (dtaction["action"].ToString() == "Update")
                                                {
                                                    if (dtaction["formName"].ToString() == strPopName)
                                                    {
                                                        if (cbx.Text =="Edit")
                                                        {
                                                            cbx.Checked = true;
                                                        }
                                                    }
                                                }
                                                else if (dtaction["action"].ToString() == "Delete")
                                                {
                                                    if (dtaction["formName"].ToString() == strPopName)
                                                    {
                                                        if (cbx.Text =="Delete")
                                                        {
                                                            cbx.Checked = true;
                                                        }
                                                    }
                                                }
                                                else if (dtaction["action"].ToString() == "View")
                                                {
                                                    if (dtaction["formName"].ToString() == strPopName)
                                                    {
                                                        if (cbx.Text =="Register")
                                                        {
                                                            cbx.Checked = true;
                                                        }
                                                        else if (dtaction["action"].ToString() == cbx.Text)
                                                        {
                                                            cbx.Checked = true;
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                        else
                                        {

                                            listObjAction = BllPrivilege.PrivilegeActionSearch(strFormName, decRoleId);
                                            foreach (DataRow dtaction in listObjAction[0].Rows)
                                            {
                                                if (dtaction["action"].ToString() == "Save")
                                                {
                                                    if (dtaction["formName"].ToString() == strFormName)
                                                    {
                                                        if (cbx.Text == "Add")
                                                        {
                                                            cbx.Checked = true;
                                                        }
                                                    }
                                                }
                                                else if (dtaction["action"].ToString() == "Update")
                                                {
                                                    if (dtaction["formName"].ToString() == strFormName)
                                                    {
                                                        if (cbx.Text =="Edit")
                                                        {
                                                            cbx.Checked = true;
                                                        }
                                                    }
                                                }
                                                else if (dtaction["action"].ToString() == "Delete")
                                                {
                                                    if (dtaction["formName"].ToString() == strFormName)
                                                    {
                                                        if (cbx.Text =="Delete")
                                                        {
                                                            cbx.Checked = true;
                                                        }
                                                    }
                                                }
                                                else if (dtaction["action"].ToString() == "View")
                                                {
                                                    if (dtaction["formName"].ToString() == strFormName)
                                                    {
                                                        if (cbx.Text =="Register")
                                                        {
                                                            cbx.Checked = true;
                                                        }
                                                        else if (dtaction["action"].ToString() == cbx.Text)
                                                        {
                                                            cbx.Checked = true;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (GbxControle is CheckBox)
                                {
                                    CheckBox cbx = (CheckBox)GbxControle;
                                    string strAction = "frm" + GbxControle.Text.Replace(" ", string.Empty);
                                   listObjAction = BllPrivilege.PrivilegeActionSearch(strAction, decRoleId);
                                   foreach (DataRow dtaction in listObjAction[0].Rows)
                                    {
                                        if (dtaction["formName"].ToString() == strAction)
                                        {
                                            if (dtaction["action"].ToString() == "View")
                                            {
                                                cbx.Checked = true;
                                            }
                                            else if (dtaction["action"].ToString() == "Change")
                                            {
                                                cbx.Checked = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("RLPRST:2" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 /// <summary>
 /// Function to Delete an items
 /// </summary>
 public void DeleteFunction()
 {
     try
     {
         PrivilegeInfo infoPrivilege = new PrivilegeInfo();
         PrivilegeBll BllPrivilege = new PrivilegeBll();
         decimal decRoleId = Convert.ToDecimal(cmbRole.SelectedValue);
         BllPrivilege.PrivilegeDeleteTabel(decRoleId);
     }
     catch (Exception ex)
     {
         MessageBox.Show("RLPRST:8" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }