Example #1
0
        void RefreshItems()
        {
            lvBoughtItems.Items.Clear();
            lvBoughtItems.Groups.Clear();

            Person person = GetSelectedPerson();

            if (person != null)
            {
                foreach (Payment pay in person.Payments)
                {
                    ListViewGroup lvg = new ListViewGroup(pay.Name);
                    lvBoughtItems.Groups.Add(lvg);

                    foreach (Product p in pay.Products)
                    {
                        ListViewItem lvi = lvg.Items.Add(p.Name);
                        lvi.SubItems.Add(p.Price.ToString("C"));
                        lvi.Tag = p;

                        if (p.Receivers.Count == persons.Count)
                        {
                            lvi.SubItems.Add("Everyone");
                        }
                        else
                        {
                            lvi.SubItems.Add(string.Join(", ", p.Receivers));
                        }

                        lvBoughtItems.Items.Add(lvi);
                    }
                }
            }
        }
Example #2
0
        Payment GetDefaultTransaction(Person p)
        {
            Payment pay = p.Payments.FirstOrDefault(t => t.Name == "Default");

            if (pay == null)
            {
                pay = new Payment("Default", p);
            }
            return(pay);
        }
Example #3
0
 void SelectPerson(Person p)
 {
     foreach (ListViewItem item in lvPeople.Items)
     {
         if (item.Tag == p)
         {
             item.Selected = true;
             break;
         }
     }
 }
Example #4
0
        private void btnClearItems_Click(object sender, EventArgs e)
        {
            Person selected = GetSelectedPerson();

            selected.Payments.Clear();

            Recalculate();
            RefreshItems();
            RefreshPersons();
            RefreshButtons();
            RefreshSummary();
            SaveData();
        }
Example #5
0
        public void SetDebt(Person from, Person to, decimal debt)
        {
            Row t = new Row(from, to);

            if (debts.ContainsKey(t))
            {
                debts[t] = debt;
            }
            else
            {
                debts.Add(t, debt);
            }
        }
Example #6
0
 void RemovePerson(Person person)
 {
     foreach (Person per in persons)
     {
         foreach (Payment pay in per.Payments)
         {
             foreach (Product prod in pay.Products)
             {
                 prod.Receivers.Remove(person);
             }
         }
     }
     persons.Remove(person);
 }
Example #7
0
        private void btnAddPerson_Click(object sender, EventArgs e)
        {
            InputBoxResult ibr = InputBox.Show("Name", "New person", "");

            if (ibr.OK)
            {
                Person p = new Person(ibr.Text);
                persons.Add(p);
                RefreshPersons();
                RefreshButtons();
                SelectPerson(p);
                // No need for recalculations as this one doesn't contribute with any money yet
                SaveData();
            }
        }
Example #8
0
 private void btnRemovePerson_Click(object sender, EventArgs e)
 {
     foreach (ListViewItem itm in lvPeople.SelectedItems)
     {
         Person p = itm.Tag as Person;
         if (p != null)
         {
             RemovePerson(p);
         }
         itm.Remove();
     }
     Recalculate();
     RefreshButtons();
     RefreshSummary();
     SaveData();
 }
Example #9
0
        private void btnEditItem_Click(object sender, EventArgs e)
        {
            Product item     = GetSelectedProduct();
            Person  selected = GetSelectedPerson();

            if (item != null)
            {
                item = ItemEditor.Edit(item, persons);
                if (item != null)
                {
                    Recalculate();
                    RefreshPersons();
                    RefreshItems();
                    RefreshSummary();
                    SaveData();

                    SelectPerson(selected);
                    SelectProduct(item);
                }
            }
        }
Example #10
0
        private void btnAddItem_Click(object sender, EventArgs e)
        {
            Person  selected = GetSelectedPerson();
            Payment payment  = GetDefaultTransaction(selected);
            Product product  = new Product("New product", 100, payment, new List <Person>());
            Product edited   = ItemEditor.Edit(product, persons);

            if (edited != null)
            {
                Recalculate();
                RefreshItems();
                RefreshPersons();
                RefreshButtons();
                RefreshSummary();
                SaveData();

                SelectPerson(selected);
                SelectProduct(edited);
            }
            else
            {
                product.Transaction = null;
            }
        }
Example #11
0
        void RefreshSummary()
        {
            lblOweIn.Text    = "People owing you money:";
            lblOweOut.Text   = "People you owe money:";
            lblTotDebt.Text  = "";
            lblTotSpent.Text = "";
            lvIncome.Items.Clear();
            lvOutcome.Items.Clear();

            if (lvPeople.SelectedItems.Count > 0)
            {
                Person person = lvPeople.SelectedItems[0].Tag as Person;
                if (person != null)
                {
                    lblOweIn.Text  = string.Format("People owing {0} money:", person.Name);
                    lblOweOut.Text = string.Format("People {0} owes money:", person.Name);

                    decimal totSpent = person.GetTotalSpent();
                    decimal totDebt  = currentCalculation.GetDebt(person);
                    decimal totSum   = totSpent + totDebt;

                    lblSum.Text      = totSum.ToString("C");
                    lblTotSpent.Text = totSpent.ToString("C");
                    lblTotDebt.Text  = Math.Abs(totDebt).ToString("C");
                    if (totDebt >= 0)
                    {
                        label5.Text = "Total debt:";
                        if (totDebt > 0)
                        {
                            lblTotDebt.ForeColor = Color.Red;
                        }
                        else
                        {
                            lblTotDebt.ForeColor = SystemColors.ControlText;
                        }
                    }
                    else
                    {
                        label5.Text          = "Total income:";
                        lblTotDebt.ForeColor = Color.Green;
                    }


                    foreach (var debtGroup in currentCalculation.GetDebts(person).GroupBy(k => k.Item2))
                    {
                        decimal      debt = debtGroup.Sum(g => g.Item3);
                        ListViewItem lvi  = new ListViewItem(debtGroup.Key.Name);

                        if (debt > 0)
                        {
                            lvi.SubItems.Add(debt.ToString("C")).ForeColor = Color.Red;
                            lvOutcome.Items.Add(lvi);
                        }
                        else if (debt < 0)
                        {
                            lvi.SubItems.Add((-debt).ToString("C")).ForeColor = Color.Green;
                            lvIncome.Items.Add(lvi);
                        }
                    }
                }
            }
        }
Example #12
0
 public decimal GetDebt(Person from, Person to)
 {
     // Take all the records with the right byer and receiver and sum the debt
     return(GetDebts(from).Where(r => r.Item2 == to).Sum(r => r.Item3));
 }
Example #13
0
 public decimal GetDebt(Person from)
 {
     return(GetDebts(from).Sum(t => t.Item3));
 }
Example #14
0
 public IEnumerable <Tuple <Person, Person, decimal> > GetDebts(Person from)
 {
     // Take all the records with the right byer
     return(debts.Where(r => r.Key.Item1 == from).Select(r => new Tuple <Person, Person, decimal>(r.Key.Item1, r.Key.Item2, r.Value)));
 }