public static CsvImport TransactionsFromCsv(string filePath, DateTime?currentDate, int dateCol, int amountCol, int payeeCol, DirectionBehavior db) { CsvImport ci = new CsvImport(); ci.it = new List <TransactionModel>(); ci.status = CsvImport.successStatus; // open csv file var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(filePath); parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(new string[] { "," }); // for each row int line = 0; while (!parser.EndOfData) { line++; string[] row = parser.ReadFields(); // skip if not enough columns in this row if (row.Length - 1 < Math.Max(Math.Max(dateCol, amountCol), payeeCol)) { ci.it = null; ci.status = $"Not enough columns ({row.Length}) for import on line {line}"; return(ci); } // set date TransactionModel t = new TransactionModel(); t.Payee = ""; t.Category = ""; t.Custom_notes = ""; if (currentDate != null) { t.Date = (DateTime)currentDate; } else { try { t.Date = Convert.ToDateTime(row[dateCol]); } catch { ci.it = null; ci.status = $"Invalid date ({row[dateCol]}) on line {line}"; return(ci); } } // set payee t.Payee = row[payeeCol]; // set amount based off of direction format try { switch (db) { case DirectionBehavior.Inflow: t.Amount = Math.Abs(Convert.ToDecimal(row[amountCol].Replace("$", ""))); break; case DirectionBehavior.Outflow: t.Amount = -Math.Abs(Convert.ToDecimal(row[amountCol].Replace("$", ""))); break; case DirectionBehavior.Both: t.Amount = Convert.ToDecimal(row[amountCol].Replace("$", "")); break; } if (t.Amount == 0) { continue; } } catch { ci.it = null; ci.status = $"Invalid amount ({row[amountCol]}) on line {line}"; return(ci); } ci.it.Add(t); } return(ci); }
// Updates DB when transaction cells are edited by user private void Transactions_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { if (e.EditAction == DataGridEditAction.Commit) { // Need to get the new user-edited value with a switch statement var c = e.Column as DataGridBoundColumn; if (c != null) { TransactionModel editedTransaction = (TransactionModel)e.Row.DataContext; int rowIndex = e.Row.GetIndex(); var el = e.EditingElement as TextBox; var changedColumn = (c.Binding as Binding).Path.Path; try { switch (changedColumn) { case "Date": DateTime d; try { d = Convert.ToDateTime(el.Text); } catch { // error parsing this.Recent_Transactions.CancelEdit(); return; } if (d.CompareTo(System.DateTime.Now) > 0) { // cannot use a future date this.Recent_Transactions.CancelEdit(); return; } editedTransaction.Date = d; break; case "Amount": Decimal a; try { a = Convert.ToDecimal(el.Text.Replace("$", "")); } catch { // error parsing this.Recent_Transactions.CancelEdit(); return; } //Created bug; unable to maintain an outflow transaction on edit //if (a <= 0) //{ // // amount cannot be negative // this.Recent_Transactions.CancelEdit(); // return; //} editedTransaction.Amount = a; break; case "Payee": editedTransaction.Payee = el.Text; if (el.Text == "") { // payee cannot be blank this.Recent_Transactions.CancelEdit(); return; } break; case "Category": editedTransaction.Category = el.Text; break; case "Custom_notes": editedTransaction.Custom_notes = el.Text; break; default: // not allowed to change balance, return now return; } // save edited transaction to DB and refresh UI viewModel.UpdateTransaction(editedTransaction); BalanceLabel.GetBindingExpression(Label.ContentProperty).UpdateTarget(); } catch { return; } } } }