Beispiel #1
0
        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);
        }
        private void ImportCsvBtnClick(object sender, RoutedEventArgs e)
        {
            if (viewModel.Importing)
            {
                return;
            }

            // make sure each column is selected
            int      dateCol = -1;
            int      amountCol;
            int      payeeCol;
            string   directionCol;
            DateTime?currentDate = null;

            try
            {
                ComboBoxItem i;

                if (ImportCsvDateToday.IsChecked.Value == true)
                {
                    currentDate = System.DateTime.Now;
                }
                else
                {
                    i       = (ComboBoxItem)ImportCsvDateCol.SelectedItem;
                    dateCol = Convert.ToInt32(i.Content.ToString()) - 1;
                }

                i         = (ComboBoxItem)ImportCsvAmountCol.SelectedItem;
                amountCol = Convert.ToInt32(i.Content.ToString()) - 1;

                i        = (ComboBoxItem)ImportCsvPayeeCol.SelectedItem;
                payeeCol = Convert.ToInt32(i.Content.ToString()) - 1;

                i            = (ComboBoxItem)ImportCsvDirection.SelectedItem;
                directionCol = i.Content.ToString();

                if ((ImportCsvDateToday.IsChecked.Value == false && dateCol == -1))
                {
                    CsvImportStatus.Content = "Select all columns first";
                    return;
                }
            }
            catch
            {
                CsvImportStatus.Content = "Select all columns first";
                return;
            }

            // open filepicker to csv file
            var dialog = new Microsoft.Win32.OpenFileDialog();

            dialog.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
            dialog.Title            = "Import transactions from...";
            dialog.DefaultExt       = ".csv";
            dialog.Filter           = "csv|*.csv";

            CsvImport results;

            results = new CsvImport();
            if (dialog.ShowDialog() == true)
            {
                string csvPath = dialog.FileName;

                CsvImport.DirectionBehavior db;
                if (directionCol == "Inflow")
                {
                    db = CsvImport.DirectionBehavior.Inflow;
                }
                else if (directionCol == "Outflow")
                {
                    db = CsvImport.DirectionBehavior.Outflow;
                }
                else
                {
                    db = CsvImport.DirectionBehavior.Both;
                }

                try
                {
                    results = CsvImport.TransactionsFromCsv(csvPath, currentDate, dateCol, amountCol, payeeCol, db);
                }
                catch
                {
                    // (Requirement 2.1.3)
                    CsvImportStatus.Content = "Import failed!";
                    return;
                }
            }
            else
            {
                // (Requirement 2.1.3)
                CsvImportStatus.Content = "Invalid import selection";
                return;
            }

            CsvImportStatus.Content = results.status;
            if (results.status == CsvImport.successStatus)
            {
                // show preview of first transaction
                // (Requirement 2.1.2)
                CsvDatePreview.Content   = "Date: " + results.it.First().Date.ToString();
                CsvAmountPreview.Content = "Amount: " + results.it.First().Amount.ToString();
                CsvPayeePreview.Content  = "Payee: " + results.it.First().Payee.ToString();
                CsvNumberPreview.Content = $"and {results.it.Count - 1} more...";
                viewModel.Importing      = true;
                viewModel.Imported       = results.it;
            }
        }