private void SaveRecord() { string methodName = "SaveRecord"; Log(methodName, string.Format("Enterring {0} ++++++++++++++++++", methodName)); if (comboBoxPayee.SelectedItem == null && comboBoxPayee.Text != "") { // Saving NEW PAYEE Log(methodName, string.Format("Using manually entered Payee: [{0}] instead of [{1}]", comboBoxPayee.Text, mPayee)); mPayee = comboBoxPayee.Text; mPayee = AddPayee(mPayee, ""); // Using the payee return value fixes the case issue (ie: send in ricks == returns Ricks) } else if ((comboBoxPayee.SelectedItem == null && comboBoxPayee.Text == "") || comboBoxPayee.SelectedItem.ToString() == DEFAULT_PAYEE_CAPTION) { Log(methodName, string.Format("\nERROR - Don't have a value for Payee\n")); mPayee = ""; } if (comboBoxCategory.SelectedItem == null && comboBoxCategory.Text != "") { // We have a new Category to add to our collection Log(methodName, string.Format("Using manually entered Category: [{0}] instead of [{1}]", comboBoxCategory.Text, mCategory)); mCategory = comboBoxCategory.Text; mCategory = AddCategory(mCategory); // Using the category return value fixes the case issue (ie: send in clothes == returns Clothes) } else if ((comboBoxCategory.SelectedItem == null && comboBoxCategory.Text != "") || comboBoxCategory.SelectedItem.ToString() == DEFAULT_CATEGORY_CAPTION) { Log(methodName, string.Format("\nERROR - Don't have a value for Category\n")); mCategory = ""; } string line = ""; bool flagError = false; // ASSOCIATE CATEGORY TO PAYEE if (mPayee != "" && mCategory != "") { if (mPayeeDict[mPayee] != mCategory && (chkBoxNoChgAssoc.IsChecked == false)) { string msgBoxMsg = string.Format("You are using a different category: {0} for {1}. Is that ok ?", mCategory, mPayee); MessageBoxResult msgResult = MessageBox.Show(msgBoxMsg, "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question); if (msgResult == MessageBoxResult.No) { line = string.Format(" Alert !! User want to change category association - don't save yet. "); flagError = true; } else { // if category associated to payee is changed AND checkbox "Don't chg Association" is NOT checked, save assoc. Log(methodName, string.Format("Associating category: {0} to payee: {1}", mCategory, mPayee)); mPayeeDict[mPayee] = mCategory; } } } if (mMonthEndingDate == "") { Log(methodName, string.Format("\nERROR - Don't have a Transaction Month selected\n")); } try { mAmount = float.Parse(tbAmt.Text, System.Globalization.CultureInfo.InvariantCulture.NumberFormat); } catch { Log(methodName, string.Format("\nERROR - Invalid Amount: {0}\n", tbAmt.Text)); mAmount = 0; } // DATA VALIDATION if (mPayee == "" || mCategory == "" || mAmount == 0 || mMonthEndingDate == "" || mTransactionDate == "") { if (mPayee == "") line = string.Format(" ERROR !! Payee not selected"); else if (mCategory == "") line = string.Format(" ERROR !! Category not selected"); else if (mAmount == 0) line = string.Format(" ERROR !! Amount not enterred or invalid"); else if (mMonthEndingDate == "") line = string.Format(" ERROR !! Transaction Month not selected"); else if (mTransactionDate == "") line = string.Format(" ERROR !! Transaction Date not selected"); else { line = string.Format(" ERROR !! Payee: {0} Category: {1} Amount: {2}", mPayee.PadRight(25), mCategory.PadRight(20), tbAmt.Text); } flagError = true; } else if(!flagError) // if not set above { line = string.Format(" Saving:: {0} {1} {2,-25} {3,-20} {4} {5}", mTransActnNumber, mTransactionDate, mPayee, mCategory, tbAmt.Text, tbNote.Text); } Log(methodName, string.Format("\n {0}\n", line)); if (!flagError) { Transaction trans = new Transaction(){TransId = mTransActnNumber, Date = mTransactionDate, Payee = mPayee, Category = mCategory, Amount = mAmount, Notes = tbNote.Text, TransMonth = mMonthEndingDate}; // SAVE DATA TO DATABASE if (!SaveTransaction(trans)) { UpdateHistory("ERROR Saving data to database !!"); return; } mTransactionList.Add(trans); UpdateHistoryPanel(mTransActnNumber.ToString(), mTransactionDate, mPayee, mCategory, mAmount.ToString("0.00"), tbNote.Text); UpdateCategoryTotals(mCategory, mAmount); // reset data mPayee = ""; mCategory = ""; mAmount = 0; comboBoxPayee.SelectedItem = DEFAULT_PAYEE_CAPTION; comboBoxCategory.SelectedItem = DEFAULT_CATEGORY_CAPTION; tbAmt.Text = ""; tbNote.Text = ""; mTransActnNumber++; // don't increment this unless we have VALID data that is saved to db tbTrnNum.Text = mTransActnNumber.ToString(); chkBoxNoChgAssoc.IsChecked = false; SoundPlayer sp = new SoundPlayer(); sp.SoundLocation = "C:\\ProgramData\\Rick\\SureBudgetMinder\\MEDIA\\CASHREG.wav"; sp.Load(); sp.Play(); } else { // Reset nothing ?? UpdateHistory(line); SoundPlayer sp = new SoundPlayer(); sp.SoundLocation = "C:\\ProgramData\\Rick\\SureBudgetMinder\\MEDIA\\EXPLODE.wav"; sp.Load(); sp.Play(); } }
private bool SaveTransaction(Transaction tran) { bool success = true; DataStorage data = new DataStorage(); if (!data.SaveData(tran)) success = false; return success; }
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 bool SaveToFile(Transaction tran) { string method = "SaveToFile"; bool success = true; try { StreamWriter swData = new StreamWriter(mTransactionStorageFile, true); int id = tran.TransId; string tranDate = tran.Date; string payee = tran.Payee; string category = tran.Category; float amt = tran.Amount; string notes = tran.Notes; // string tranMonth = tran.TransMonth; if (payee.Contains(mDelimeter)) { Log(method, string.Format("ERROR !! Payee contains invalid chr <->. Payee: {0}", payee)); return false; } if (category.Contains(mDelimeter)) { Log(method, string.Format("ERROR !! Category contains invalid chr <->. Category: {0}", category)); return false; } if (notes.Contains(mDelimeter)) { Log(method, string.Format("ERROR !! Notes contains invalid chr <->. Notes: {0}", notes)); return false; } if (payee.Contains(mDelimeter)) { Log(method, string.Format("ERROR !! Payee contains invalid chr <->. Payee: {0}", payee)); return false; } swData.WriteLine("{0}{7}{1}{7}{2}{7}{3}{7}{4}{7}{5}{7}{6}", id, tranDate, payee, category, amt, notes, tranMonth, mDelimeter); swData.Close(); swData.Dispose(); } catch (Exception e) { // return false; } return success; }
private bool SaveToDatabase(Transaction tran) { bool success = true; // connect to MySQL and save data return success; }
private bool DataMatchesCriteria(Transaction tran) { // query should be a list of query qualifications string delimeted by semicolon // ie: "Payee='Winco';TransMonth='2013-04-21'" string method = "DataMatchesCriteria"; bool matchCriteria = true; Log(method, string.Format("Checking record id: {0}", tran.TransId)); if (tran.TransId.ToString() != mFieldsToQualifyDict["TransId"] && mFieldsToQualifyDict["TransId"] != "*") { Log(method, string.Format("Record id: {0} does NOT match TransId", tran.TransId)); matchCriteria = false; } if (tran.Date != mFieldsToQualifyDict["Date"] && mFieldsToQualifyDict["Date"] != "*") { Log(method, string.Format("Record id: {0} does NOT match Date", tran.TransId)); matchCriteria = false; } if (tran.Payee != mFieldsToQualifyDict["Payee"] && mFieldsToQualifyDict["Payee"] != "*") { Log(method, string.Format("Record id: {0} does NOT match Payee", tran.TransId)); matchCriteria = false; } if (tran.Category != mFieldsToQualifyDict["Category"] && mFieldsToQualifyDict["Category"] != "*") { Log(method, string.Format("Record id: {0} does NOT match Category", tran.TransId)); matchCriteria = false; } if (tran.Amount.ToString() != mFieldsToQualifyDict["Amount"] && mFieldsToQualifyDict["Amount"] != "*") { Log(method, string.Format("Record id: {0} does NOT match Amount", tran.TransId)); matchCriteria = false; } if (tran.Notes != mFieldsToQualifyDict["Notes"] && mFieldsToQualifyDict["Notes"] != "*") { Log(method, string.Format("Record id: {0} does NOT match Notes", tran.TransId)); matchCriteria = false; } if (tran.TransMonth != mFieldsToQualifyDict["TransMonth"] && mFieldsToQualifyDict["TransMonth"] != "*") { Log(method, string.Format("Record id: {0} does NOT match TransMonth", tran.TransId)); matchCriteria = false; } return matchCriteria; }
private Transaction ConvertDataToTransactionObject(string line) { Transaction tran = new Transaction(); string[] parts = line.Split(mDelimeter); if (parts.Length == 7) { tran.TransId = Convert.ToInt16(parts[0]); tran.Date = parts[1]; tran.Payee = parts[2]; tran.Category = parts[3]; tran.Amount = float.Parse(parts[4], System.Globalization.CultureInfo.InvariantCulture.NumberFormat); tran.Notes = parts[5]; tran.TransMonth = parts[6]; } return tran; }
public bool SaveData(Transaction tran) { bool success = true; // for MySQL //success = SaveToDatabase(tran); // for file storage system success = SaveToFile(tran); return success; }