コード例 #1
0
        private void yearsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List <string> dates = TransactionItem.GetCollection().FindAll().Select(i => i.TransDate.ToString("yyyy")).Distinct().ToList();

            Comparisons comp = new Comparisons(dates);

            comp.Show();
        }
コード例 #2
0
        private void GetAccounts()
        {
            List <string> accounts = TransactionItem.GetCollection().FindAll().Select(i => i.Account).Distinct().ToList();

            accounts.Insert(0, "All Accounts");
            cmbAccount.DataSource    = accounts;
            cmbAccount.SelectedIndex = 0;
        }
コード例 #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            var selectedList = dgTransactions.SelectedRows.Cast <DataGridViewRow>().Select(r => (TransactionItem)r.DataBoundItem).ToList();
            var selectString = string.Join("\r\n", selectedList.Select(s => s.ToString()));

            if (MessageBox.Show(string.Format("Are you sure you want to delete:\r\n{0}", selectString), "Row delete confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                TransactionItem.GetCollection().Delete(t => selectedList.Exists(s => s.Id == t.Id));
            }

            InitGrids();
        }
コード例 #4
0
        private void cmbDateSelect_SelectedValueChanged(object sender, EventArgs e)
        {
            var list = TransactionItem.GetCollection()
                       .Find(t => t.TransDate.ToString(DATEFORMAT).Equals(cmbDateSelect.SelectedItem) &&
                             (cmbAccount.SelectedItem.ToString() == t.Account || cmbAccount.SelectedItem.ToString() == "All Accounts"))
                       .OrderBy(t => t.TransDate)
                       .ToList();

            dgTransactions.DataSource = list;

            CalculateTotals(lblTotalAmounts, list);
        }
コード例 #5
0
        private void newCSVToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                List <TransactionItem> newItems = new List <TransactionItem>();
                string   account  = string.Empty;
                DateTime lastDate = DateTime.MinValue;

                using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
                {
                    string headerLine = sr.ReadLine();
                    string line       = string.Empty;
                    while (!string.IsNullOrEmpty(line = sr.ReadLine()))
                    {
                        while (line.Where(c => c == '"').Count() % 2 == 1) // Read until all quotes are matched
                        {
                            line += sr.ReadLine();
                        }
                        TransactionItem item = null;
                        try
                        {
                            item = TransactionItem.ImportItem(line, headerLine);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(string.Format("File import aborted: \r\n{0}\r\n{1}", ex.Message, line));
                            return;
                        }
                        if (item.TransDate > lastDate)
                        {
                            lastDate = item.TransDate;
                        }
                        if (string.IsNullOrEmpty(account))
                        {
                            account = item.Account;
                        }
                        if (TransactionItem.GetCollection().Exists(t => t.Equals(item)))
                        {
                            continue;
                        }
                        newItems.Add(item);
                    }
                    TransactionItem.GetCollection().InsertBulk(newItems);
                }

                BackupTransactionFile(openFileDialog1.FileName, account, lastDate);

                InitGrids();
            }
        }
コード例 #6
0
        private void GetDates()
        {
            var dates = TransactionItem.GetCollection()
                        .Find(i => i.Account == cmbAccount.SelectedItem.ToString() || cmbAccount.SelectedItem.ToString() == "All Accounts")
                        .OrderByDescending(i => i.TransDate)
                        .Select(i => i.TransDate.ToString(DATEFORMAT))
                        .Distinct()
                        .ToList();

            if (dates.Count == 0)
            {
                dates.Add(DateTime.Now.ToString(DATEFORMAT));
            }

            cmbDateSelect.DataSource    = dates;
            cmbDateSelect.SelectedIndex = 0;
        }
コード例 #7
0
        private void TxtSearch_TextChanged(object sender, EventArgs e)
        {
            var results = TransactionItem.GetCollection().FindAll().ToList();

            List <string> terms = (txtSearch.Text ?? string.Empty).Split(',').Where(t => !string.IsNullOrEmpty(t)).ToList();

            if (terms.Count > 0)
            {
                results = results.Where(trans => terms.Any(term =>
                                                           FindTerm(term, trans.Category, trans.Description, trans.Memo, trans.Notes, trans.TransType.ToString()))).ToList();
            }

            results.Sort(delegate(TransactionItem t1, TransactionItem t2) { return(t1.TransDate.CompareTo(t2.TransDate)); });

            results = results.Take(100).ToList();

            dgTransactions.DataSource = results;
        }
コード例 #8
0
        private void ImportCategoryMatching()
        {
            if (File.Exists(Environment.CurrentDirectory + "\\CategoryMatching.txt"))
            {
                using (StreamReader sr = new StreamReader(Environment.CurrentDirectory + "\\CategoryMatching.txt"))
                {
                    List <CategoryMatch> matches = new List <CategoryMatch>();
                    string line;
                    while (!string.IsNullOrEmpty(line = sr.ReadLine()))
                    {
                        string[] itemType = line.Split('\t');
                        if (itemType.Length != 2)
                        {
                            continue;
                        }
                        try
                        {
                            matches.Add(new CategoryMatch()
                            {
                                MatchString = itemType[0],
                                MatchType   = (TransactionItem.TransactionTypes)Enum.Parse(typeof(TransactionItem.TransactionTypes), itemType[1])
                            });
                        }
                        catch { MessageBox.Show(line, "Item incorrectly set up: " + line); }
                    }

                    CategoryMatch.GetCollection().InsertBulk(matches);

                    foreach (TransactionItem trans in TransactionItem.GetCollection().Find(t => t.TransType == TransactionItem.TransactionTypes.Unselected))
                    {
                        var newType = CategoryMatch.GetType(trans.ToString());
                        if (newType != trans.TransType)
                        {
                            trans.TransType = newType;
                            TransactionItem.GetCollection().Update(trans);
                        }
                    }
                }

                BackupTransactionFile(Environment.CurrentDirectory + "\\CategoryMatching.txt", "Category Matching", DateTime.Now);
            }
        }
コード例 #9
0
        private List <string> GetDateList(GraphPeriod period, DateTime startDate, DateTime endDate)
        {
            List <TransactionItem> items = TransactionItem.GetCollection().Find(t => t.TransDate >= startDate && t.TransDate <= endDate).ToList();

            switch (period)
            {
            case GraphPeriod.Annual:
                return(items.Select(i => i.TransDate.ToString("yyyy")).Distinct().ToList());

            case GraphPeriod.Monthly:
                return(items.Select(i => i.TransDate.ToString(DATEFORMAT)).Distinct().ToList());

            case GraphPeriod.Weekly:
                DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
                Calendar           cal = dfi.Calendar;
                return(items.Select(i => string.Format("Week {0} {1}", cal.GetWeekOfYear(i.TransDate, dfi.CalendarWeekRule, dfi.FirstDayOfWeek), i.TransDate.ToString("yyyy"))).Distinct().ToList());

            default:
                return(new List <string>());
            }
        }
コード例 #10
0
        private void Match_FormClosed(object sender, FormClosedEventArgs e)
        {
            List <TransactionItem> filteredList = (List <TransactionItem>)dgTransactions.DataSource;

            foreach (TransactionItem trans in TransactionItem.GetCollection().Find(t => t.TransType == TransactionItem.TransactionTypes.Unselected))
            {
                var newType = CategoryMatch.GetType(trans.ToString());
                if (newType != trans.TransType)
                {
                    trans.TransType = newType;
                    TransactionItem.GetCollection().Update(trans);
                }
            }

            foreach (TransactionItem trans in filteredList.Where(f => f.TransType == TransactionItem.TransactionTypes.Unselected))
            {
                trans.TransType = CategoryMatch.GetType(trans.ToString());
            }
            // Reset the view
            dgTransactions.DataSource = filteredList;
            dgTransactions.Refresh();
        }
コード例 #11
0
ファイル: Comparisons.cs プロジェクト: link064/Budget-App
        public Comparisons(List <string> comparators)
        {
            InitializeComponent();

            DataTable dt = new DataTable();

            dt.Columns.Add("Date");
            foreach (TransactionItem.TransactionTypes types in Enum.GetValues(typeof(TransactionItem.TransactionTypes)))
            {
                dt.Columns.Add(types.ToString());
            }
            dt.Columns.Add("Total");

            var items = TransactionItem.GetCollection().FindAll().GroupBy(t => t.TransDate.ToString("MMM yyyy")).ToDictionary(k => k.Key, e => e.ToList());

            foreach (string compare in comparators)
            {
                DataRow row = dt.NewRow();
                row["Date"] = compare;
                decimal total = 0;
                foreach (TransactionItem.TransactionTypes types in Enum.GetValues(typeof(TransactionItem.TransactionTypes)))
                {
                    decimal subtotal = items
                                       .Where(i => i.Key.Contains(compare))
                                       .SelectMany(i => i.Value)
                                       .Where(i => i.TransType == types)
                                       .Sum(i => i.Amount);
                    total += subtotal;
                    row[types.ToString()] = subtotal;
                }
                row["Total"] = total;
                dt.Rows.Add(row);
            }

            dgCompare.DataSource = dt;
        }
コード例 #12
0
 private void dgTransactions_CellEndEdit(object sender, DataGridViewCellEventArgs e)
 {
     CalculateTotals(lblTotalAmounts, (List <TransactionItem>)dgTransactions.DataSource);
     TransactionItem.GetCollection().Update(((TransactionItem)dgTransactions.Rows[e.RowIndex].DataBoundItem));
 }
コード例 #13
0
        public void GeneratePointsLists(GraphPeriod period, bool addTotal = true, bool addTrend = false, DateTime?startDate = null, DateTime?endDate = null)
        {
            if (startDate == null || startDate.Value <= DateTime.MinValue || startDate.Value >= DateTime.MaxValue)
            {
                startDate = TransactionItem.GetCollection().Find(t => t.TransDate > DateTime.MinValue).Min(t => t.TransDate).AddDays(-1);
            }
            if (endDate == null || endDate.Value <= DateTime.MinValue || endDate.Value >= DateTime.MaxValue)
            {
                endDate = DateTime.Today;
            }

            _period = period;
            cmbType.SelectedItem = period;
            _startDate           = startDate.Value;
            _endDate             = endDate.Value;
            dtpStartDate.Value   = startDate.Value;
            dtpEndDate.Value     = endDate.Value;

            Dictionary <string, List <object>[]> data = new Dictionary <string, List <object>[]>();
            List <string> dates = GetDateList(_period, _startDate, _endDate);

            // Make the intervals match up with the number of entries we have
            switch (period)
            {
            case GraphPeriod.Annual:
                break;

            case GraphPeriod.Monthly:
                chart1.ChartAreas[0].AxisX.Interval = Math.Min(3, Math.Ceiling(((_endDate - _startDate).Days / 30) / 12.0d));
                break;

            case GraphPeriod.Weekly:
                chart1.ChartAreas[0].AxisX.Interval = Math.Min(5, Math.Ceiling(((_endDate - _startDate).Days / 7) / 26.0d));
                break;
            }

            chart1.Titles[0].Text = period.ToString();

            // group by account
            var items = TransactionItem.GetCollection().FindAll().GroupBy(i => i.Account);

            foreach (var group in items)
            {
                List <object>[] dateAndAmount = new List <object> [2];
                dateAndAmount[0] = new List <object>();
                dateAndAmount[1] = new List <object>();
                // group by date
                foreach (string date in dates)
                {
                    string  lastDate = string.Empty;
                    decimal balance  = 0.00M;
                    // search transactions
                    foreach (TransactionItem item in group)
                    {
                        // Janky, but the best way we can do this without having both a date and a time for each transaction
                        // is to go through the list until you find the first applicable transaction for a given period and
                        // add it to the list of data points. While this may be inaccurate on an individual by-date level,
                        // it should show a consistent level over a number of data points
                        if (IsDateInPeriod(date, item.TransDate, period))
                        {
                            lastDate = date;
                            balance  = item.Balance;
                        }
                    }

                    if (!string.IsNullOrEmpty(lastDate))
                    {
                        dateAndAmount[0].Add(lastDate);
                        dateAndAmount[1].Add(balance);
                    }
                    else
                    {
                        if (dateAndAmount[1].Count > 0)
                        {
                            dateAndAmount[0].Add(date);
                            dateAndAmount[1].Add(dateAndAmount[1].Last());
                        }
                    }
                }
                data.Add(GetFriendlyAccountNames(group.Key), dateAndAmount);
            }

            // If we want to add a total over all the accounts
            if (addTotal)
            {
                List <object>[] dateAndAmount = new List <object> [2];
                dateAndAmount[0] = new List <object>();
                dateAndAmount[1] = new List <object>();
                // go through each possible date
                foreach (string date in dates)
                {
                    decimal total = 0.00M;
                    // go through each account
                    foreach (KeyValuePair <string, List <object>[]> kvp in data)
                    {
                        // search each row...
                        for (int i = 0; i < kvp.Value[0].Count; i++)
                        {
                            // look for the corresponding date
                            if (((string)kvp.Value[0][i]) == date)
                            {
                                // add to the running total
                                total += (decimal)kvp.Value[1][i];
                                break;
                            }
                        }
                    }
                    dateAndAmount[0].Add(date);
                    dateAndAmount[1].Add(total);
                }
                data.Add("Total", dateAndAmount);
            }

            if (addTrend)
            {
                List <object>[] dateAndAmount = data["Total"];
                if (dateAndAmount != null && dateAndAmount[0].Count > 0)
                {
                    decimal slope     = (((decimal)dateAndAmount[1].Last()) - ((decimal)dateAndAmount[1].First())) / (decimal)dateAndAmount[1].Count;
                    decimal intercept = (decimal)dateAndAmount[1].First();

                    List <object>[] trendLine = new List <object> [2];
                    trendLine[0] = new List <object>();
                    trendLine[1] = new List <object>();

                    for (int i = 0; i < dateAndAmount[0].Count; i++)
                    {
                        trendLine[0].Add(dateAndAmount[0][i]);
                        trendLine[1].Add(intercept + (slope * i));
                    }

                    switch (period)
                    {
                    case GraphPeriod.Monthly:
                        trendLine[0].Add("Next month");
                        trendLine[1].Add(intercept + (slope * dateAndAmount[0].Count));
                        break;

                    case GraphPeriod.Weekly:
                        for (int i = 0; i < 4; i++)
                        {
                            trendLine[0].Add("Next week " + (i + 1).ToString());
                            trendLine[1].Add(intercept + (slope * (dateAndAmount[0].Count + i)));
                        }
                        break;

                    default:
                        break;
                    }

                    data.Add("Trend", trendLine);
                }
            }
            else
            {
                List <object>[] dateAndAmount = data["Total"];
                if (dateAndAmount != null && dateAndAmount[0].Count > 0)
                {
                    List <object>[] trendLine = new List <object> [2];
                    trendLine[0] = new List <object>();
                    trendLine[1] = new List <object>();
                    trendLine[0].Add(dateAndAmount[0][0]);
                    trendLine[1].Add(dateAndAmount[1][0]);

                    data.Add("Trend", trendLine);
                }
            }

            foreach (KeyValuePair <string, List <object>[]> kvp in data)
            {
                AddPoints(kvp.Value[0], kvp.Value[1], kvp.Key);
            }
        }