private void buttonadd_Click(object sender, EventArgs e)
        {
            if (m_action == action_type.create)
            {
                #region Insert Region
                ic.departmentC _dept = new MTOMS.ic.departmentC();
                _dept.dept_name = textBox1.Text.Trim().ToProperCase();
                if (curr_node.Level == 1)
                {
                    _dept.parent_id = curr_node.Parent.Tag == null ? 0 : curr_node.Parent.Tag.ToInt32();
                }
                if (curr_node.Level == 2)
                {
                    _dept.parent_id = curr_node.Parent.Tag == null ? 0 : (curr_node.Parent.Tag as ic.departmentC).dept_id;
                }
                _dept.is_visible = true;
                _dept.level      = curr_node.Level;
                _dept.index      = curr_node.Index;
                string[] _cols = new string[]
                {
                    "exp_type",
                    "is_visible",
                    "lch_id",
                    "fs_time_stamp",
                    "dept_name",
                    "parent_id", "s_level", "s_index"
                };
                object[] _row = new object[]
                {
                    emm.export_type.insert.ToByte(),
                    1,
                         sdata.ChurchID,
                    0,
                         _dept.dept_name,
                         _dept.parent_id, _dept.level, _dept.index
                };
                using (var xd = new xing())
                {
                    if (datam.DuplicateDepartmentName(_dept.dept_name, xd))
                    {
                        MessageBox.Show("The Department Name You Have Entered Already Exists", "Duplicate Department Name");
                        buttonclose.PerformClick();
                        return;
                    }
                    _dept.dept_id  = xd.SingleInsertCommandTSPInt("dept_master_tb_ns", _cols, _row);
                    curr_node.Tag  = _dept;
                    curr_node.Text = _dept.dept_name;
                    curr_node.Name = string.Format("DEPT{0}", _dept.dept_id);
                    if (_dept.parent_id > 0)
                    {
                        _dept.expense_sys_account_id = accn.CreateChildGroupAccount(xd, datam.DATA_DEPARTMENT[_dept.parent_id].expense_sys_account_id, _dept.dept_name).account_id;
                        _dept.cr_sys_account_id      = datam.DATA_DEPARTMENT[_dept.parent_id].cr_sys_account_id;
                        _dept.income_sys_account_id  = datam.DATA_DEPARTMENT[_dept.parent_id].income_sys_account_id;
                    }
                    else
                    {
                        _dept.expense_sys_account_id = accn.CreateChildGroupAccount(xd, -2386, _dept.dept_name).account_id;
                        _dept.income_sys_account_id  = accn.CreateChildGroupAccount(xd, -2370, _dept.dept_name).account_id;
                        _dept.cr_sys_account_id      = accn.CreateChildGroupAccount(xd, -2369, _dept.dept_name).account_id;
                        xd.UpdateFsTimeStamp("accounts_tb");
                        xd.SingleUpdateCommand(string.Format("update accounts_tb set link_id={0},{1},fs_time_stamp={2} where account_id in ({3},{4},{5})", _dept.expense_sys_account_id,
                                                             dbm.ETS, SQLH.UnixStamp, _dept.expense_sys_account_id, _dept.income_sys_account_id, _dept.cr_sys_account_id));
                    }
                    //
                    xd.SingleUpdateCommandALL("dept_master_tb_ns", new string[]
                    {
                        "sys_account_id",
                        "inc_sys_account_id",
                        "cr_sys_account_id",
                        "dept_id"
                    }, new object[] { _dept.expense_sys_account_id, _dept.income_sys_account_id, _dept.cr_sys_account_id, _dept.dept_id }, 1);
                    //
                    ic.expense_accountC _exp = new ic.expense_accountC();
                    _exp.dept_id             = _dept.dept_id;
                    _exp.dept_parent_id      = _dept.parent_id;
                    _exp.dept_sys_account_id = _dept.expense_sys_account_id;
                    _exp.exp_acc_name        = string.Format("GE :: {0}", _dept.dept_name);
                    _exp.exp_acc_status      = em.exp_acc_statusS.valid;
                    _exp.exp_acc_type        = em.exp_acc_typeS.system_department;
                    datam.DATA_DEPARTMENT.Add(_dept.dept_id, _dept);
                    accn.CreateExpenseAccount(_exp, xd);
                    if (_dept.parent_id < 0)
                    {
                        #region
                        ic.accountC _inc_dept = new MTOMS.ic.accountC();
                        _inc_dept.account_name    = string.Format("GI :: {0}", _dept.dept_name);
                        _inc_dept.account_status  = em.account_statusS.Enabled;
                        _inc_dept.opening_balance = 0;
                        _inc_dept.start_date      = sdata.CURR_DATE;
                        _inc_dept.owner_type      = em.AccountOwnerTypeS.DEPARTMENT;
                        _inc_dept.owner_id        = _dept.dept_id;
                        _inc_dept.owner_name      = _dept.dept_name;
                        _inc_dept.PostType        = em.postTypeS.cash_accounts_payable;

                        ic.accountC parent_account = datam.DATA_ACCOUNTS[_dept.income_sys_account_id];
                        _inc_dept.account_dept_type     = parent_account.account_dept_type;
                        _inc_dept.account_dept_category = parent_account.account_dept_category;
                        _inc_dept.account_status        = em.account_statusS.Enabled;
                        _inc_dept.account_type          = em.account_typeS.ActualAccount;
                        _inc_dept.p_account_id          = parent_account.account_id;
                        _inc_dept.a_level = (parent_account.a_level + 1).ToInt16();
                        _inc_dept.a_index = ((datam.DATA_ACCOUNTS.Values.Count(l => l.p_account_id == parent_account.account_id & l.account_type == em.account_typeS.ActualAccount)) + 1).ToInt16();
                        _cols             = new string[]
                        {
                            "acc_d_cat_id",
                            "acc_d_type_id",
                            "account_name",
                            "account_type_id",
                            "account_alias",
                            "a_level",
                            "a_index",
                            "p_account_id",
                            "exp_type",
                            "fs_time_stamp",
                            "edate",
                            "pc_us_id",
                            "account_status_id",
                            "search_alias",
                            "is_sys_account",
                            "post_type_id",
                            "owner_type_id",
                            "owner_id",
                            "owner_name",
                            "start_date",
                            "end_date", "opening_balance",
                            "account_short_name", "account_code", "description", "lch_id", "ex_cg_type_ids", "accounts_ext_purpose"
                        };

                        xd.UpdateFsTimeStamp("accounts_tb");
                        _row = new object[]
                        {
                            _inc_dept.account_dept_category.ToByte(),
                                _inc_dept.account_dept_type.ToByte(),
                                _inc_dept.account_name,
                                _inc_dept.account_type.ToByte(),
                                _inc_dept.account_alias,
                                _inc_dept.a_level,
                                _inc_dept.a_index,
                                _inc_dept.p_account_id,
                                emm.export_type.insert.ToByte(),
                            0,
                                datam.CURR_DATE,
                                datam.PC_US_ID,
                                _inc_dept.account_status.ToByte(),
                                null,                          //search_alias
                            0,                                 // is_sys_account
                                _inc_dept.PostType.ToByte(),   //post_type
                                _inc_dept.owner_type.ToByte(), //owner_type
                                _inc_dept.owner_id,            //owner_id
                                _inc_dept.owner_name,          //owner name
                                _inc_dept.start_date,
                                _inc_dept.end_date,            //end_date
                                _inc_dept.opening_balance,     //opening balance
                                _inc_dept.account_short_name,  //account_short_name
                                _inc_dept.account_code,
                                _inc_dept.description,         //description
                                sdata.ChurchID, null, _inc_dept.extension_purpose.ToByte()
                        };
                        _inc_dept.account_id = xd.SingleInsertCommandTSPInt("accounts_tb", _cols, _row);
                        xd.UpdateFsTimeStamp("accounts_tb");
                        xd.SingleUpdateCommand(string.Format("update accounts_tb set link_id={0},{1},fs_time_stamp={2} where account_id in ({3},{4})", _dept.expense_sys_account_id,
                                                             dbm.ETS, SQLH.UnixStamp, _inc_dept.account_id, _exp.sys_account_id));

                        if (datam.DATA_ACCOUNTS != null)
                        {
                            try
                            {
                                datam.DATA_ACCOUNTS.Add(_inc_dept.account_id, _inc_dept);
                            }
                            catch (Exception)
                            {
                            }
                        }


                        #endregion
                    }
                    switch (curr_node.Level)
                    {
                    case 1:
                    {
                        curr_node.Style = _sb_dept_style;
                        break;
                    }

                    case 2:
                    {
                        curr_node.Style = _sb_dept_style2;
                        break;
                    }
                    }
                    curr_node.ContextMenu = contextMenuFile;
                    xd.CommitTransaction();
                }
                #endregion

                _dept    = null;
                m_action = action_type.none;
                if (!is_edited)
                {
                    is_edited = true;
                }
                sdata.ClearFormCache(em.fm.expense_account_settings.ToInt16());
                sdata.ClearFormCache(em.fm.chart_of_accounts.ToInt16());
                //
                sdata.ClearFormCache(em.fm.income_accounts_settings.ToInt16());
                sdata.ClearFormCache(em.fm.creditors.ToInt16());
                buttonclose.PerformClick();
            }
            if (m_action == action_type.update)
            {
                if (curr_node != null && curr_node.Tag != null)
                {
                    var _dept = curr_node.Tag as ic.departmentC;
                    if (_dept != null)
                    {
                        using (var xd = new xing())
                        {
                            if (datam.DuplicateDepartmentName(textBox1.Text.Trim().ToProperCase(), xd, _dept.dept_id))
                            {
                                MessageBox.Show("The Department Name You Have Entered Already Exists", "Duplicate Department Name");
                                buttonclose.PerformClick();
                                return;
                            }
                            xd.SingleUpdateCommandALL("dept_master_tb_ns", new string[] { "dept_name", "dept_id" }, new object[] { textBox1.Text.Trim().ToProperCase(), _dept.dept_id }, 1);
                            xd.SingleUpdateCommandALL("accounts_tb", new string[] { "account_name", "account_id" }, new object[] { textBox1.Text.Trim().ToProperCase(), _dept.expense_sys_account_id }, 1);
                            xd.CommitTransaction();
                        }
                        _dept.dept_name = textBox1.Text.Trim().ToProperCase();
                        curr_node.Text  = _dept.dept_name;
                        if (!is_edited)
                        {
                            is_edited = true;
                        }
                        sdata.ClearFormCache(em.fm.expense_account_settings.ToInt16());
                        sdata.ClearFormCache(em.fm.chart_of_accounts.ToInt16());
                        buttonclose.PerformClick();
                    }
                }
            }
        }
        private void buttonadd_Click(object sender, EventArgs e)
        {
            if (m_action == action_type.create)
            {
                #region Insert Region
                ic.accountC _account = new MTOMS.ic.accountC();
                _account.account_status = em.account_statusS.Enabled;
                _account.a_index        = curr_node.Index.ToInt16();
                _account.a_level        = curr_node.Level.ToInt16();
                int gp_id = 0;
                var _sect = GetRoot(curr_node);
                _account.account_dept_category = (em.account_d_categoryS)_sect.acc_d_cat_id;
                _account.account_dept_type     = (em.account_d_typeS)_sect.acc_d_type_id;
                if (curr_node.Level > 1)
                {
                    _account.p_account_id = curr_node.Parent == null ? 0 : (curr_node.Parent.Tag as ic.accountC).account_id;
                }
                using (var xd = new xing())
                {
                    switch (m_object)
                    {
                    case object_type.GroupAccount:
                    {
                        #region Add New Types
                        _account.account_type = curr_node.Level == 1 ? em.account_typeS.GroupAccount : em.account_typeS.SubGroupAccount;
                        curr_node.Text        = textBox1.Text.Trim().ToProperCase();
                        _account.account_name = curr_node.Text;
                        curr_node.Style       = _folder_style;
                        curr_node.ContextMenu = contextMenuFolder;
                        //  curr_node.Image = MUTTICO.Properties.Resources.Folder;
                        m_action = action_type.none;
                        if (_account.account_type == em.account_typeS.SubGroupAccount)
                        {
                            gp_id = (curr_node.Parent.Tag as ic.accountC).account_id;
                        }
                        break;
                        #endregion
                    }

                    case object_type.Account:
                    {
                        if (datam.DuplicateAccountName(textBox1.Text.Trim().ToProperCase(), xd))
                        {
                            MessageBox.Show("The Account Name You Have Entered Already Exists", "Duplicate Account Name");
                            buttonclose.PerformClick();
                            return;
                        }
                        #region Add New Types
                        _account.account_type = em.account_typeS.ActualAccount;
                        curr_node.Text        = textBox1.Text.Trim().ToProperCase();
                        _account.account_name = curr_node.Text;
                        curr_node.Style       = _folder_style;
                        curr_node.ContextMenu = contextMenuFile;
                        // curr_node.Image = MUTTICO.Properties.Resources.email;
                        m_action = action_type.none;
                        switch (curr_node.Level)
                        {
                        case 2:
                        case 3:
                        {
                            gp_id = (curr_node.Parent.Tag as ic.accountC).account_id;
                            break;
                        }
                        }
                        break;
                        #endregion
                    }
                    }
                    string[] _cols = new string[]
                    {
                        "account_id",
                        "acc_d_cat_id",
                        "acc_d_type_id",
                        "account_name",
                        "account_type_id",
                        "account_alias",
                        "a_level",
                        "a_index",
                        "p_account_id",
                        "exp_type",
                        "fs_time_stamp",
                        "edate",
                        "pc_us_id",
                        "account_status_id",
                        "search_alias",
                        "is_sys_account",
                        "post_type_id",
                        "owner_type_id",
                        "owner_id",
                        "owner_name",
                        "start_date",
                        "end_date", "opening_balance",
                        "account_short_name", "account_code", "description", "lch_id"
                    };
                    object[] _row = new object[]
                    {
                        _account.account_id = xd.IDCtrlGet("acc_id_xxx", 2013, -500),
                        _account.account_dept_category.ToByte(),
                        _account.account_dept_type.ToByte(),
                        _account.account_name,
                        _account.account_type.ToByte(),
                        _account.account_alias,
                        _account.a_level,
                        _account.a_index,
                        _account.p_account_id = gp_id,
                        emm.export_type.insert.ToByte(),
                        0,
                        datam.CURR_DATE,
                        datam.PC_US_ID,
                        _account.account_status.ToByte(),
                        null, //search_alias
                        0,    // is_sys_account
                        0,    //post_type
                        0,    //owner_type
                        0,    //owner_id
                        null, //owner name
                        sdata.CURR_DATE,
                        null,
                        0,    //opening balance
                        null, //account_short_name
                        null, // account_code
                        null, //description
                        sdata.App_station_id
                    };
                    xd.SingleInsertCommandInt("accounts_tb", _cols, _row);
                    xd.IDCtrlDelete("acc_id_xxx");
                    curr_node.Name = string.Format("Account{0}", _account.account_id);
                    xd.CommitTransaction();
                }
                #endregion
                curr_node.Tag = _account;
                datam.DATA_ACCOUNTS.Add(_account.account_id, _account);
                labelclient.Text = _account.account_name;
                if (_account.account_type == em.account_typeS.ActualAccount)
                {
                    curr_node.Style = _file_style;
                }
                else
                {
                    curr_node.Style = _folder_style;
                }

                _account = null;
                buttonclose.PerformClick();
            }
            if (m_action == action_type.update)
            {
                if (curr_node != null && curr_node.Tag != null)
                {
                    var _folder = curr_node.Tag as ic.accountC;
                    if (_folder != null)
                    {
                        using (var xd = new xing())
                        {
                            if (datam.DuplicateAccountName(textBox1.Text.Trim().ToProperCase(), xd, _folder.account_id))
                            {
                                MessageBox.Show("The Account Name You Have Entered Already Exists", "Duplicate Account Name");
                                buttonclose.PerformClick();
                                return;
                            }
                            xd.SingleUpdateCommandALL("accounts_tb", new string[] { "account_name", "account_id" }, new object[] { textBox1.Text.Trim().ToProperCase(), _folder.account_id }, 1);
                            xd.CommitTransaction();
                        }
                        _folder.account_name = textBox1.Text.Trim().ToProperCase();
                        curr_node.Text       = _folder.account_name;
                        buttonclose.PerformClick();
                    }
                }
            }
        }