protected void BtnProcessTransactionItemsClick(object sender, EventArgs e)
        {
            try
            {
                if (!ValidateExpenseItemControls())
                {
                    mpeExpenseItemsPopup.Show();
                    return;
                }

                switch (int.Parse(btnProcessTransactionItems.CommandArgument))
                {
                case 1:     //Add
                    if (!AddExpenseItem())
                    {
                        ConfirmAlertBox1.ShowMessage("Expense Item information could not be added. Please try again soon or contact the Admin.", ConfirmAlertBox.PopupMessageType.Error);
                        return;
                    }
                    ConfirmAlertBox1.ShowSuccessAlert("Expense Item information was Added Successfully.");


                    break;

                case 2:     //Update
                    if (!UpdateExpenseItem())
                    {
                        ConfirmAlertBox1.ShowMessage("Expense Item information could not be updated. Please try again soon or contact the Admin.", ConfirmAlertBox.PopupMessageType.Error);
                        return;
                    }

                    ConfirmAlertBox1.ShowSuccessAlert("Expense Item information was updated Successfully.");

                    break;

                default:
                    ConfirmAlertBox1.ShowMessage("Invalid process call!", ConfirmAlertBox.PopupMessageType.Error);
                    break;
                }

                if (int.Parse((ddlAccountsHeads.SelectedValue)) < 1)
                {
                    LoadAllExpenseItems();
                }

                if (int.Parse((ddlAccountsHeads.SelectedValue)) > 0)
                {
                    LoadFilteredExpenseItems();
                }
            }

            catch (Exception ex)
            {
                ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
                ErrorDisplayProcessTransactionItems.ShowError("An unknown error was encountered. Please try again soon or contact the Admin.");
            }
        }
        private bool UpdateExpenseItem()
        {
            try
            {
                if (Session["_expenseItem"] == null)
                {
                    ConfirmAlertBox1.ShowMessage("Expense Item list is empty or session has expired", ConfirmAlertBox.PopupMessageType.Error);
                    return(false);
                }

                var expenseItem = Session["_expenseItem"] as ExpenseItem;

                if (expenseItem == null || expenseItem.ExpenseItemId < 1)
                {
                    ConfirmAlertBox1.ShowMessage("The requested process failed!", ConfirmAlertBox.PopupMessageType.Error);
                    return(false);
                }

                expenseItem.Description       = txtDescription.Text.Trim();
                expenseItem.Title             = txtTitle.Text.Trim();
                expenseItem.ExpenseCategoryId = int.Parse(ddlExpenseCategory.SelectedValue);
                expenseItem.AccountsHeadId    = int.Parse(ddlAccountsHead.SelectedValue);
                expenseItem.Status            = chkActivateTransactionItem.Checked ? 1 : 0;

                var k = ServiceProvider.Instance().GetExpenseItemServices().UpdateExpenseItemCheckDuplicate(expenseItem);

                if (k < 1)
                {
                    if (k == -3)
                    {
                        ErrorDisplayProcessTransactionItems.ShowError("Expense Item information already exists.");
                        txtTitle.Focus();
                        mpeExpenseItemsPopup.Show();
                        return(false);
                    }

                    ErrorDisplayProcessTransactionItems.ShowError("Expense Item could not be updated.");
                    mpeExpenseItemsPopup.Show();
                    return(false);
                }

                ConfirmAlertBox1.ShowSuccessAlert("ExpenseItem Information Was updated successfully.");
                return(true);
            }
            catch (Exception ex)
            {
                ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
                ConfirmAlertBox1.ShowMessage("An unknown error was encountered. Please try again soon or contact the Admin.", ConfirmAlertBox.PopupMessageType.Error);
                return(false);
            }
        }
        private bool ValidateExpenseItemControls()
        {
            if (int.Parse(ddlExpenseCategory.SelectedValue) < 1)
            {
                ErrorDisplayProcessTransactionItems.ShowError("Invalid selection!");
                ddlExpenseCategory.Focus();
                mpeExpenseItemsPopup.Show();
                return(false);
            }

            if (int.Parse(ddlAccountsHead.SelectedValue) < 1)
            {
                ErrorDisplayProcessTransactionItems.ShowError("Invalid selection!");
                ddlAccountsHead.Focus();
                mpeExpenseItemsPopup.Show();
                return(false);
            }

            if (string.IsNullOrEmpty(txtTitle.Text.Trim()))
            {
                ErrorDisplayProcessTransactionItems.ShowError("Please supply a title for the Expense Item.");
                txtTitle.Focus();
                mpeExpenseItemsPopup.Show();
                return(false);
            }

            if (string.IsNullOrEmpty(txtDescription.Text.Trim()))
            {
                ErrorDisplayProcessTransactionItems.ShowError("Please provide a Description for the Expense Item");
                txtDescription.Focus();
                mpeExpenseItemsPopup.Show();
                return(false);
            }

            //if (RegExValidation.IsNameValid(txtTitle.Text.Trim()))
            //{
            //    ErrorDisplayProcessTransactionItems.ShowError("Invalid entry!");
            //    txtTitle.Focus();
            //    mpeExpenseItemsPopup.Show();
            //    return false;
            //}

            return(true);
        }
        private bool AddExpenseItem()
        {
            ErrorDisplayProcessTransactionItems.ClearError();
            try
            {
                string itemCode;

                var itemsList = ServiceProvider.Instance().GetExpenseItemServices().GetLastInsertedExpenseItem(int.Parse(ddlAccountsHead.SelectedValue));

                if (!itemsList.Any())
                {
                    var accountsHead = ServiceProvider.Instance().GetAccountsHeadServices().GetAccountsHead(int.Parse(ddlAccountsHead.SelectedValue));

                    if (accountsHead == null || accountsHead.AccountsHeadId < 1)
                    {
                        return(false);
                    }

                    var accountsHeadCode = accountsHead.Code + "01";

                    itemCode = accountsHeadCode;
                }

                else
                {
                    var expenseItemCode = long.Parse(itemsList.First().Code) + 1;

                    itemCode = expenseItemCode.ToString(CultureInfo.InvariantCulture);
                }

                var newExpenseItem = new ExpenseItem
                {
                    ExpenseCategoryId = int.Parse(ddlExpenseCategory.SelectedValue),
                    AccountsHeadId    = int.Parse(ddlAccountsHead.SelectedValue),
                    Title             = txtTitle.Text.Trim(),
                    Code        = itemCode,
                    Description = txtDescription.Text,
                    Status      = (chkActivateTransactionItem.Checked) ? 1 : 0
                };

                var k = ServiceProvider.Instance().GetExpenseItemServices().AddExpenseItem(newExpenseItem);

                if (k < 1)
                {
                    if (k == -3)
                    {
                        ErrorDisplayProcessTransactionItems.ShowError("Expense Item information already exists.");
                        txtTitle.Focus();
                        mpeExpenseItemsPopup.Show();
                        return(false);
                    }

                    ErrorDisplayProcessTransactionItems.ShowError("Expense Item could not be added.");
                    mpeExpenseItemsPopup.Show();
                    return(false);
                }

                ErrorDisplayProcessTransactionItems.ShowSuccess("Expense Item information was added successfully.");
                txtDescription.Text              = string.Empty;
                txtTitle.Text                    = string.Empty;
                ddlAccountsHead.SelectedIndex    = 0;
                ddlExpenseCategory.SelectedIndex = 0;
                return(true);
            }
            catch (Exception ex)
            {
                ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
                ErrorDisplayProcessTransactionItems.ShowError("An unknown error was encountered. Please try again soon or contact the Admin.");
                mpeExpenseItemsPopup.Show();
                return(false);
            }
        }