Esempio n. 1
0
        private void RemoveIncomeBtn_Click(object sender, EventArgs e)
        {
            var selectedItem = RemoveIncomeAccountCmbx.SelectedItem;

            if (selectedItem is Account && RemoveIncomeMonthCmbx.SelectedItem != null)
            {
                string selectedIncomeID = RemoveIncomeMonthCmbx.SelectedItem.ToString();
                selectedIncomeID = selectedIncomeID.Substring(0, 2).Trim();
                var selectedAccount = selectedItem as Account;
                var accountID       = selectedAccount.AccountID;
                var incomes         = database.Incomes.ToList();

                DialogResult result = MessageBox.Show($"Do you wish to continue?", "Warning", MessageBoxButtons.YesNo);

                if (result == DialogResult.Yes)
                {
                    var resultIncome = incomes.Where(i => incomes.Any(a => i.AccountID == accountID && i.IncomeID == Convert.ToInt32(selectedIncomeID))).FirstOrDefault();

                    try
                    {
                        database.Incomes.Remove(resultIncome);
                        database.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        database.Dispose();
                        database = new ExpenseTrackerDatabase();
                    }

                    UpdateAllAccountCmbx();
                    button3_Click(sender, e);
                }
            }
        }
Esempio n. 2
0
        private void AddCatgoryBtn_Click(object sender, EventArgs e)
        {
            Category newCategory;

            // newCategory = new Category() and database.Categories.Add(newCategory) must be inside the if block
            // or else you get blank categories and an error
            if (!CheckDuplicateCategory())
            {
                newCategory = new Category();
                newCategory.CategoryName = CategoryTxt.Text;
                database.Categories.Add(newCategory);
            }

            try
            {
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to save to database!" + Environment.NewLine + "Exception Message:" + Environment.NewLine + ex.ToString(), "ERROR!");
                database.Dispose();
                database = new ExpenseTrackerDatabase();
            }

            UpdateAllCategoryCmbx();
            button6_Click(sender, e);
        }
Esempio n. 3
0
        private void RemoveBudgetBtn_Click(object sender, EventArgs e)
        {
            var selectedCategory = RemoveBudgetCmbx.SelectedItem;

            if (selectedCategory is Category && RemoveBudgetMonthCmbx.SelectedItem != null)
            {
                string   selectedMonth  = RemoveBudgetMonthCmbx.SelectedItem.ToString();
                Category asCategory     = selectedCategory as Category;
                var      budgetToRemove = SQLMan.getBudget(database, asCategory, selectedMonth);

                try
                {
                    database.Budgets.Remove(budgetToRemove);
                    database.SaveChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    database.Dispose();
                    database = new ExpenseTrackerDatabase();
                }
            }

            UpdateAllCategoryCmbx();
            RemoveBudgetCategoryCmbx_SelectedIndexChanged(sender, e);
            button5_Click(sender, e);
        }
Esempio n. 4
0
        private void removeButton_Click(object sender, EventArgs e)
        {
            var transactions = database.Transactions.ToList();

            if (selectedIndex >= 0 && selectedIndex < transactions.Count)
            {
                var transaction = transactions[selectedIndex];

                database.Transactions.Remove(transaction);

                try
                {
                    database.SaveChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed to save to database!" + Environment.NewLine + "Exception Message:" + Environment.NewLine + ex.ToString(), "ERROR!");
                    database.Dispose();
                    database = new ExpenseTrackerDatabase();
                }

                //transactions[selectedIndex] = null;
                //transactions.Remove(transactions[selectedIndex]);
                UpdateLabels();
            }
        }
Esempio n. 5
0
        public double?getSumOfIncomesInMonth(ExpenseTrackerDatabase database, string month)
        {
            var incomes    = database.Incomes.ToList();
            var sumInMonth = (from inc in incomes
                              where inc.Month == month.ToUpper()
                              select inc.Amount).Sum();

            return(sumInMonth);
        }
Esempio n. 6
0
        private void AddBudgetAmountBtn_Click(object sender, EventArgs e)
        {
            double budgetAmount = 0.00;

            var      selectedCategory = CategoryCmbx.SelectedItem;
            Category asCategory       = selectedCategory as Category;
            var      selectedMonth    = BudgetMonthCmbx.SelectedItem?.ToString();

            try
            {
                if (double.TryParse(BudgetAmountTxt.Text, out budgetAmount) && CategoryCmbx.SelectedItem != null &&
                    BudgetMonthCmbx.SelectedItem != null)
                {
                    if (!SQLMan.checkBudgetAlreadyCreated(database, asCategory, selectedMonth))
                    {
                        {
                            Budget newBudget = new Budget()
                            {
                                CategoryID = asCategory.CategoryID,
                                Month      = selectedMonth,
                                Amount     = (double?)budgetAmount
                            };

                            database.Budgets.Add(newBudget);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Budget has already been created. Consider updated it instead.");
                    }
                }

                else
                {
                    MessageBox.Show("Please fill in all fields with valid data.", "Missing and or Invalid Input", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed in adding newBudget data." + Environment.NewLine + "Exception Message:" + Environment.NewLine + ex.ToString(), "ERROR!", MessageBoxButtons.OK);
            }

            try
            {
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to save to database!" + Environment.NewLine + "Exception Message:" + Environment.NewLine + ex.ToString(), "ERROR!");
                database.Dispose();
                database = new ExpenseTrackerDatabase();
            }

            UpdateAllCategoryCmbx();
            button5_Click(sender, e);
        }
Esempio n. 7
0
        public List <string> getMonthsPerCategory(ExpenseTrackerDatabase database, Category category)
        {
            var budgets = database.Budgets.ToList();

            var months = (from bud in budgets
                          where bud.CategoryID == category.CategoryID
                          select bud.Month).ToList();

            return(months);
        }
Esempio n. 8
0
        //public double? getAccountBalance(ExpenseTrackerDatabase database, string accountName)
        //{
        //    var transactions = database.Transactions.ToList();
        //    var accounts = database.Accounts.ToList();
        //    double? balance = 0.00;

        //    var accountCurrentBalance = (from acs in accounts
        //                                 where acs.AccountName == accountName
        //                                 select acs.AccountTotal).SingleOrDefault();

        //    var accountTransTotal = (from trans in transactions
        //                             join acs in accounts
        //                             on trans.AccountID equals acs.AccountID
        //                             where acs.AccountName == accountName
        //                             select trans.Amount).Sum();

        //    double currentBalance = 0.00;
        //    double transactionTotal = 0.00;

        //    if (double.TryParse(accountCurrentBalance.ToString(), out currentBalance) && double.TryParse(accountTransTotal.ToString(), out transactionTotal))
        //    {
        //        balance = currentBalance - transactionTotal;
        //    }

        //    return balance;
        //}

        public void updateAccountTotal(ExpenseTrackerDatabase database, int accountID, double amount)
        {
            var accounts = database.Accounts.ToList();

            var result = (from acs in accounts
                          where acs.AccountID == accountID
                          select acs).SingleOrDefault();

            result.AccountTotal += amount;
        }
Esempio n. 9
0
        public List <Income> getIncomeByAccount(ExpenseTrackerDatabase database, int accountID)
        {
            var incomes = database.Incomes.ToList();

            var incomeList = (from inc in incomes
                              where inc.AccountID == accountID
                              select inc).ToList();

            return(incomeList);
        }
Esempio n. 10
0
        public double?getGoalPerMonth(ExpenseTrackerDatabase database, Category category, string month)
        {
            var expenses = database.Budgets.ToList();

            var expenseSumInMonth = (from exp in expenses
                                     where exp.Category.CategoryID == category.CategoryID &&
                                     exp.Month == month
                                     select exp.Amount).Sum();

            return(expenseSumInMonth);
        }
Esempio n. 11
0
        public Budget getBudget(ExpenseTrackerDatabase database, Category category, string month)
        {
            var budgets = database.Budgets.ToList();

            Budget toRemove = (from bud in budgets
                               where bud.CategoryID == category.CategoryID
                               where bud.Month == month
                               select bud).SingleOrDefault();

            return(toRemove);
        }
Esempio n. 12
0
        public double?getRunningTotalPerMonth(ExpenseTrackerDatabase database, Category category, string month)
        {
            int monthAsInt = getMonthAsInt(month);

            var transactions          = database.Transactions.ToList();
            var transactionSumInMonth = (from transaction in transactions
                                         where transaction.Category.CategoryID == category.CategoryID &&
                                         ((DateTime)transaction.Date).Month == monthAsInt
                                         select transaction.Amount).Sum();

            return(transactionSumInMonth);
        }
Esempio n. 13
0
        private void RemoveAccountTypesBtn_Click(object sender, EventArgs e)
        {
            var selectedItem = RemoveAccountTypesCmbx.SelectedItem;

            if (selectedItem is Account)
            {
                var    selectedAccount = selectedItem as Account;
                string accountName     = selectedAccount.AccountName;
                var    accountID       = selectedAccount.AccountID;

                // Scan all Incomes and Transactions
                // If none of them contain accountID, then allow the Account remove
                var incomes      = database.Incomes.ToList();
                var transactions = database.Transactions.ToList();

                DialogResult result = MessageBox.Show($"Warning! This will remove all Incomes and Transactions tied to Account '{accountName}'. Do you wish to continue?", "Warning", MessageBoxButtons.YesNo);

                if (result == DialogResult.Yes)
                {
                    var resultIncomes      = incomes.Where(i => incomes.Any(c => i.AccountID == accountID)).ToList();
                    var resultTransactions = transactions.Where(t => transactions.Any(c => t.AccountID == accountID)).ToList();

                    try
                    {
                        foreach (Transaction transaction in resultTransactions)
                        {
                            database.Transactions.Remove(transaction);
                        }

                        foreach (Income income in resultIncomes)
                        {
                            database.Incomes.Remove(income);
                        }

                        database.Accounts.Remove(selectedAccount);
                        database.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        database.Dispose();
                        database = new ExpenseTrackerDatabase();
                    }

                    UpdateAllAccountCmbx();
                    button3_Click(sender, e);
                }
            }
        }
Esempio n. 14
0
        public Form1()
        {
            InitializeComponent();
            database = new ExpenseTrackerDatabase();
            SQLMan   = new SQLManager();

            UpdateAllCategoryCmbx();
            UpdateAllAccountCmbx();

            transactionViewerForm = new Form_TransactionViewer();

            // Populate Month drop downs
            IncomeMonthCmbx.Items.AddRange(months);
            BudgetMonthCmbx.Items.AddRange(months);
        }
Esempio n. 15
0
        private void AddIncomeBtn_Click(object sender, EventArgs e)
        {
            double incomeAmount = 0.00;

            try
            {
                var selectedMonth   = IncomeMonthCmbx.SelectedItem?.ToString();
                var selectedAccount = IncomeAccountCmbx.SelectedItem;

                if (double.TryParse(IncomeAmountTxt.Text, out incomeAmount) && IncomeAccountCmbx.SelectedItem != null &&
                    IncomeMonthCmbx.SelectedItem != null && incomeAmount > 0.00)
                {
                    var asAccount = selectedAccount as Account;

                    Income newIncome = new Income()
                    {
                        Amount    = incomeAmount,
                        Month     = selectedMonth,
                        AccountID = asAccount.AccountID
                    };

                    database.Incomes.Add(newIncome);
                    SQLMan.updateAccountTotal(database, asAccount.AccountID, incomeAmount);
                }
                else
                {
                    MessageBox.Show("Please fill in all fields with valid data.", "Missing and/or Invalid Input", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed in adding newIncome data." + Environment.NewLine + "Exception Message:" + Environment.NewLine + ex.ToString(), "ERROR!", MessageBoxButtons.OK);
            }

            try
            {
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to save to database!" + Environment.NewLine + "Exception Message:" + Environment.NewLine + ex.ToString(), "ERROR!");
                database.Dispose();
                database = new ExpenseTrackerDatabase();
            }

            UpdateAllAccountCmbx();
            button4_Click(sender, e);
        }
Esempio n. 16
0
        private void RemoveCategoryButton_Click(object sender, EventArgs e)
        {
            // BUG: sometimes when you remove a Category, it can show some weird text
            var selectedItem = RemoveCategoryCmbx.SelectedItem;

            if (selectedItem is Category)
            {
                var    selectedCategory = selectedItem as Category;
                string categoryName     = selectedCategory.CategoryName;
                var    categoryID       = selectedCategory.CategoryID;

                var budgets      = database.Budgets.ToList();
                var transactions = database.Transactions.ToList();

                DialogResult result = MessageBox.Show($"Warning! This will remove all Budgets and Transactions tied to Category '{categoryName}'. Do you wish to continue?", "Warning", MessageBoxButtons.YesNo);

                if (result == DialogResult.Yes)
                {
                    var resultBudgets      = budgets.Where(b => budgets.Any(c => b.CategoryID == categoryID)).ToList();
                    var resultTransactions = transactions.Where(t => transactions.Any(c => t.CategoryID == categoryID)).ToList();

                    try
                    {
                        foreach (Transaction transaction in resultTransactions)
                        {
                            database.Transactions.Remove(transaction);
                        }

                        foreach (Budget budget in resultBudgets)
                        {
                            database.Budgets.Remove(budget);
                        }

                        database.Categories.Remove(selectedCategory);
                        database.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        database.Dispose();
                        database = new ExpenseTrackerDatabase();
                    }

                    UpdateAllCategoryCmbx();
                    button6_Click(sender, e);
                }
            }
        }
Esempio n. 17
0
        public void CheckExceedBudget(ExpenseTrackerDatabase databse, Category category, string month)
        {
            // negate transaction amount to compare it with budget
            double?runningTotal     = -SQLMan.getRunningTotalPerMonth(database, category, month);
            double?budget           = SQLMan.getGoalPerMonth(database, category, month);
            var    selectedCategory = TransactionCategoryCmbx.SelectedItem;
            var    asCategory       = selectedCategory as Category;
            int    categoryID       = asCategory.CategoryID;

            var hasABudget = database.Budgets.FirstOrDefault(b => b.Month == month && b.CategoryID == categoryID);

            // Expenses(Transactions that indicate you spent x amount of money) should be entered by the user as negative
            // need to fix this alert based on that, this condition assumes a postive number being entered is an expense
            if (runningTotal > budget && hasABudget != null)
            {
                MessageBox.Show(month + "'s " + category.CategoryName + " budget has been exceeded by $" + (runningTotal - budget));
            }
        }
Esempio n. 18
0
        public bool checkBudgetAlreadyCreated(ExpenseTrackerDatabase database, Category category, string month)
        {
            var budgets = database.Budgets.ToList();

            var alreadyCreated = (from bud in budgets
                                  where bud.CategoryID == category.CategoryID
                                  where bud.Month == month
                                  select bud);

            if (alreadyCreated.Any())
            {
                return(true);
            }

            else
            {
                return(false);
            }
        }
Esempio n. 19
0
        private void AccountTypeBtn_Click(object sender, EventArgs e)
        {
            double accountTotal;

            try
            {
                //var result = database.Accounts.FirstOrDefault(a => a.AccountName == AccountAmountTxt.Text);

                if (!CheckDuplicateAccount() && Double.TryParse(AccountAmountTxt.Text, out accountTotal))
                {
                    Account newAccount = new Account()
                    {
                        AccountName  = AccountTypeTxt.Text,
                        AccountTotal = accountTotal,
                    };

                    database.Accounts.Add(newAccount);
                }
                else
                {
                    MessageBox.Show("Please fill in all account information with valid data.", "Missing and/or Invalid Input", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed in adding newAccount data." + Environment.NewLine + "Exception Message:" + Environment.NewLine + ex.ToString(), "ERROR!", MessageBoxButtons.OK);
            }

            try
            {
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to save to database!" + Environment.NewLine + "Exception Message:" + Environment.NewLine + ex.ToString(), "ERROR!");
                database.Dispose();
                database = new ExpenseTrackerDatabase();
            }

            UpdateAllAccountCmbx();
            button3_Click(sender, e);
        }
Esempio n. 20
0
        private void AddTransactionBtn_Click(object sender, EventArgs e)
        {
            double   transactionAmount = 0.00;
            var      selectedCategory  = TransactionCategoryCmbx.SelectedItem;
            var      selectedAccount   = TransactionAccountCmbx.SelectedItem;
            var      asCategory        = selectedCategory as Category;
            var      asAccount         = selectedAccount as Account;
            int      categoryID        = asCategory.CategoryID;
            int      accountID         = asAccount.AccountID;
            DateTime dateTime          = TransactionDatePckr.Value;
            string   month             = getDateTimeMonth(dateTime);

            try
            {
                if (double.TryParse(TransactionAmountTxt.Text, out transactionAmount) && selectedCategory != null &&
                    selectedAccount != null)
                {
                    if (SpendingRdb.Checked || ReceivingRdb.Checked)
                    {
                        if (SpendingRdb.Checked && transactionAmount > 0)
                        {
                            transactionAmount = 0 - transactionAmount;
                        }

                        if (ReceivingRdb.Checked && transactionAmount < 0)
                        {
                            throw new Exception("A receiving transaction cannot be negative.");
                        }

                        Transaction newTransaction = new Transaction()
                        {
                            Amount     = transactionAmount,
                            Memo       = TransactionMemoTxt.Text,
                            AccountID  = accountID,
                            CategoryID = categoryID,
                            Date       = dateTime
                        };

                        SQLMan.updateAccountTotal(database, accountID, transactionAmount);
                        database.Transactions.Add(newTransaction);
                    }
                    else
                    {
                        throw new Exception("Please select a transaction type below.");
                    }
                }
                else
                {
                    throw new Exception("Please fill in the category, account, and amount sections with valid data.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Missing and / or Invalid Input", MessageBoxButtons.OK);
            }

            try
            {
                database.SaveChanges();
                CheckExceedBudget(database, asCategory, month);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to save to database!" + Environment.NewLine + "Exception Message:" + Environment.NewLine + ex.ToString(), "ERROR!");
                database.Dispose();
                database = new ExpenseTrackerDatabase();
            }

            PrintGoalTotalEveryMonthEveryCategory();
            button2_Click(sender, e);
        }
Esempio n. 21
0
 public void setDB(ExpenseTrackerDatabase database)
 {
     this.database = database;
 }
Esempio n. 22
0
 public double?actualComparedToRunningTotal(ExpenseTrackerDatabase database, Category category, string month)
 {
     return(getRunningTotalPerMonth(database, category, month) - getGoalPerMonth(database, category, month));
 }