private void SaveDataFile()
 {
     DataStorage data = new DataStorage();
     bool success = data.CopyDataFile();
     if (!success)
     {
         MessageBox.Show("Error saving data file to C:\\Users\\Church\\Documents\\ShirleysBudgetMinder_Data.txt - ensure path exists.", "ERROR !!", MessageBoxButton.OK, MessageBoxImage.Asterisk);
     }
 }
        private void PopulateFormWithPreExistingTransaction(string tranNum)
        {
            string methodName = "PopulateFormWithPreExistingTransaction";

            try
            {
                DataStorage data = new DataStorage();
                List<Transaction> tranList = data.GetData(string.Format("TransId={0};TransMonth={1}", tranNum, mMonthEndingDate));

                if (tranList.Count != 1)
                {
                    Log(methodName, "");
                    Log(methodName, "");
                    Log(methodName, string.Format("Unexpected number of transactions found: {0}", tranList.Count));
                    Log(methodName, string.Format("     Displaying all transactions found;", tranList.Count));
                    foreach (Transaction tranFound in tranList)
                    {
                        Log(methodName, string.Format("     TranId: {0} || MonthEnding: {1} || Payee: {2} || Amt: {3} || TranDate: {4}",
                                                        tranFound.TransId, tranFound.TransMonth, tranFound.Payee, tranFound.Amount, tranFound.Date));
                    }
                    Log(methodName, "");
                    Log(methodName, "");
                    MessageBox.Show("Error - Multiple records found but only expected to find one.  See log.");
                    return;
                }
                string recordId = string.Empty;
                //
                // Populate textBoxes with original values & let user modify as needed and then save record normally
                //
                foreach (Transaction oldTran in tranList)
                {
                    //
                    // delete old transaction
                    //
                    data.RemoveRecord(oldTran.TransId.ToString(), oldTran.TransMonth);

                    //
                    // todo - probably should just call LoadInitData() to simply this code AND then reload the textboxes as I'm already doing below.
                    //

                    // Remove transaction from mTransactionList
                    List<Transaction> tmpTranList = new List<Transaction>();
                    foreach (Transaction tran in mTransactionList)
                    {
                        tmpTranList.Add(tran);
                    }
                    foreach (Transaction tran in tmpTranList)
                    {
                        if (tran.TransId == oldTran.TransId)
                        {
                            mTransactionList.Remove(tran);
                        }
                    }

                    Log(methodName, string.Format("Reset hist and category panels and category Dict"));
                    // reset hist and category panels and category Dict
                    wpHistory.Children.Clear();
                    wpCategoryTotals.Children.Clear();
                    mCategoryDict = new Dictionary<string, Category>();
                    mPayeeDict = new Dictionary<string, string>();

                    LoadPayeeAndCategoryComboBtns();

                    Log(methodName, string.Format("ReLoad category totals and TransactionHistory panel"));
                    // Reload category totals and TransactionHistory panel
                    foreach (Transaction tran in mTransactionList)
                    {
                        UpdateCategoryTotals(tran.Category, tran.Amount);
                        UpdateHistoryPanel(tran.TransId.ToString(), tran.Date, tran.Payee, tran.Category, tran.Amount.ToString("0.00"), tran.Notes);
                    }

                    // Load textBoxes, etc with the original data
                    comboBoxPayee.SelectedItem = oldTran.Payee;
                    comboBoxCategory.SelectedItem = oldTran.Category;
                    tbAmt.Text = oldTran.Amount.ToString();
                    //mTransActnNumber = tran.TransId;  // don't set this - let this modified record have the next avail id num
                    tbTranDate.Text = oldTran.Date;  // might need to use calendar dict to get full month & replace MM with MMMM
                    tbNote.Text = oldTran.Notes;
                    //tbMonthEndingDate.Text = oldTran.TransMonth;  // this should not be needed since it should be unchanged
                }
            }
            catch (Exception ex)
            {
                Log(methodName, string.Format("\n {0}   ERROR !! Error msg: {1}     stack trace: {2}", methodName, ex.Message, ex.StackTrace));
                MessageBox.Show("Fatal error !  Rick needs to look at logs - C:\\ProgramData\\Rick\\SureBudgetMinder", "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.None);
            }
        }
        private void LoadPayeeAndCategoryComboBtns()
        {
            string methodName = "LoadPayeeAndCategoryComboBtns";
            Log(methodName, string.Format("Enterring"));

            if (comboBoxPayee.Items.Count != 0)
            {
                comboBoxPayee.Items.Clear();
            }
            if (comboBoxCategory.Items.Count != 0)
            {
                comboBoxCategory.Items.Clear();
            }

            comboBoxPayee.Items.Add(DEFAULT_PAYEE_CAPTION);
            comboBoxCategory.Items.Add(DEFAULT_CATEGORY_CAPTION);

            List<string> payeeList = new List<string>();
            List<string> catList = new List<string>();

            DataStorage data = new DataStorage();
            List<Transaction> tranList = data.GetData("");

            foreach (Transaction tran in tranList)
            {
                if (!payeeList.Contains(tran.Payee))
                {
                    Log(methodName, string.Format("Adding Payee: {0} to Payee ComboBtn", tran.Payee));
                    AddPayee(tran.Payee, tran.Category);
                }
                if (!catList.Contains(tran.Category))
                {
                    Log(methodName, string.Format("Adding Category: {0} to Category ComboBtn", tran.Category));
                    AddCategory(tran.Category);
                }
            }
        }
        void LoadInitData(string monthEndingDate, string sortBy="TransId")
        {
            string methodName = "LoadInitData";
            try
            {
                Log(methodName, string.Format(" Entering . . .    Sort by [{0}]", sortBy));

                // reset / clear History panel, Category panel, and all primary objects
                wpHistory.Children.Clear();
                wpCategoryTotals.Children.Clear();
                mTransactionList = new List<Transaction>();
                mCategoryDict = new Dictionary<string, Category>();
                mPayeeDict = new Dictionary<string, string>();

                List<Transaction> tranList = new List<Transaction>();
                //
                //  GET CURRENT MONTH TRANSACTIONS FROM DATABASE     tranList = Select * from TRANSACTIONS where TransMonth = <transMonthSelected>
                //
                DataStorage data = new DataStorage();
                tranList = data.GetData(string.Format("TransMonth={0}", monthEndingDate));

                if (sortBy == "Date")
                {
                    Log(methodName, string.Format("Sorting by Date before displaying all transactions."));
                    // found this line of code on SO to sort a list of objects
                    tranList = tranList.OrderBy(o => o.Date).ToList();
                }
                else if (sortBy == "TransId")
                {
                    Log(methodName, string.Format("Sorting by TransId before displaying all transactions."));
                    // found this line of code on SO to sort a list of objects
                    tranList = tranList.OrderBy(o => o.TransId).ToList();
                }
                else if (sortBy == "Payee")
                {
                    Log(methodName, string.Format("Sorting by Payee before displaying all transactions."));
                    // found this line of code on SO to sort a list of objects
                    tranList = tranList.OrderBy(o => o.Payee).ToList();
                }

                //if (tranList.Count == 0)
                if (false)
                {
                    // for testing purposes, populate dummy data
                    Transaction tmpTrans = new Transaction() { TransId = 101, Date = "2013-02-13", Payee = "Maverik", Category = "Gas", Amount = (float)32.24, Notes = "86 Honda", TransMonth = "201302" };
                    tranList.Add(tmpTrans);
                    tmpTrans = new Transaction() { TransId = 102, Date = "2013-02-13", Payee = "Winco", Category = "Groceries", Amount = (float)132.24, Notes = "", TransMonth = "201302" };
                    tranList.Add(tmpTrans);
                    tmpTrans = new Transaction() { TransId = 103, Date = "2013-02-17", Payee = "Walmart", Category = "Clothes", Amount = (float)43.19, Notes = "Natalia", TransMonth = "201302" };
                    tranList.Add(tmpTrans);
                    tmpTrans = new Transaction() { TransId = 104, Date = "2013-02-18", Payee = "Maverik", Category = "Gas", Amount = (float)49.02, Notes = "", TransMonth = "201302" };
                    tranList.Add(tmpTrans);
                    tmpTrans = new Transaction() { TransId = 105, Date = "2013-02-21", Payee = "Fiesta Guadalahara", Category = "Dining Out", Amount = (float)132.24, Notes = "", TransMonth = "201302" };
                    tranList.Add(tmpTrans);
                    tmpTrans = new Transaction() { TransId = 106, Date = "2013-02-23", Payee = "Delsa's", Category = "Dining Out", Amount = (float)12.29, Notes = "", TransMonth = "201302" };
                    tranList.Add(tmpTrans);
                    tmpTrans = new Transaction() { TransId = 107, Date = "2013-02-23", Payee = "Schucks", Category = "Automotive Repair", Amount = (float)29.99, Notes = "", TransMonth = "201302" };
                    tranList.Add(tmpTrans);
                    tmpTrans = new Transaction() { TransId = 108, Date = "2013-02-23", Payee = "Guido's", Category = "Dining Out", Amount = (float)14.32, Notes = "", TransMonth = "201302" };
                    tranList.Add(tmpTrans);
                }

                //
                // get payee and category info from database
                //
                LoadPayeeAndCategoryComboBtns();

                int maxId = 0;
                foreach (Transaction trans in tranList)
                {
                    UpdateHistoryPanel(trans.TransId.ToString(), trans.Date, trans.Payee, trans.Category, trans.Amount.ToString("0.00"), trans.Notes);
                    UpdateCategoryTotals(trans.Category, trans.Amount);
                    mTransactionList.Add(trans);
                    if (trans.TransId > maxId) maxId = trans.TransId;
                }

                comboBoxPayee.SelectedItem = DEFAULT_PAYEE_CAPTION;
                comboBoxCategory.SelectedItem = DEFAULT_CATEGORY_CAPTION;

                //
                // get next transaction number from DB  (Select max(TransId) from Transactions)  and then increment by one
                //
                mTransActnNumber = maxId + 1;
                tbTrnNum.Text = mTransActnNumber.ToString();

                lbl_HistoryHdr.Content = " ID        Date                          Payee                               Category                   Amount                 Notes";
                lbl_HistoryHdr.Background = Brushes.Aquamarine;
            }
            catch (Exception ex)
            {
                Log(methodName, string.Format("\n {0}   ERROR !! Error msg: {1}     stack trace: {2}", methodName, ex.Message, ex.StackTrace));
                MessageBox.Show("Fatal error !  Rick needs to look at logs - C:\\ProgramData\\Rick\\SureBudgetMinder", "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.None);
            }
        }
 private void btnMoEndHelp_Click(object sender, RoutedEventArgs e)
 {
     string methodName = "btnMoEndHelp_Click";
     Log(methodName, string.Format("Showing months"));
     DataStorage data = new DataStorage();
     List<Transaction> tranList = data.GetData("");
     string monthEnds = string.Empty;
     foreach (Transaction tran in tranList)
     {
         if (!monthEnds.Contains(tran.TransMonth))
         {
             monthEnds = string.Format("  {0}    {1}  ", monthEnds, tran.TransMonth);
         }
     }
     MessageBox.Show(string.Format("Valid Months: {0}", monthEnds));
 }
        private bool SaveTransaction(Transaction tran)
        {
            bool success = true;
            DataStorage data = new DataStorage();
            if (!data.SaveData(tran)) success = false;

            return success;
        }