Example #1
0
        internal async Task UpdateExpense(Expense myExpense, SplitExpenseVM viewModel)
        {
            if (viewModel.FriendIds != "")
            {
                try
                {
                    var newPayers = viewModel.FriendIds.Split(',');

                    var payerIds = new List <int>();

                    foreach (var payer in newPayers)
                    {
                        payerIds.Add(Convert.ToInt32(payer));
                    }

                    var oldPayers = PayersForExpense.Where(p => p.ExpenseId == myExpense.Id).ToList();

                    //Radera de oldpayers som inte är med i payerIds
                    foreach (var payer in oldPayers)
                    {
                        if (payerIds.Where(p => p == payer.UserId).Count() == 0)
                        {
                            PayersForExpense.Remove(payer);
                        }
                    }

                    //Lägg till de payerIds som inte fanns i oldPayers.
                    foreach (var payer in payerIds)
                    {
                        if (oldPayers.Where(p => p.UserId == payer).Count() == 0)
                        {
                            PayersForExpense.Add(new PayersForExpense {
                                UserId = payer, ExpenseId = myExpense.Id
                            });
                        }
                    }

                    SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }

            var myEvent = GetEventById(Convert.ToInt32(viewModel.SelectedEvent));

            myExpense.Amount      = Convert.ToDecimal(viewModel.Amount, CultureInfo.InvariantCulture);
            myExpense.Description = viewModel.Description;
            myExpense.CurrencyId  = Convert.ToInt32(viewModel.SelectedCurrency);
            myExpense.Date        = Convert.ToDateTime(viewModel.Date);
            myExpense.EventId     = myEvent.Id;
            myExpense.PurchaserId = viewModel.PurchaserID;
            myExpense.AmountInStandardCurrency = await CalculateStandardCurrencyAmount(Convert.ToDecimal(viewModel.Amount, CultureInfo.InvariantCulture), viewModel.SelectedCurrency, myEvent.StandardCurrencyId, Convert.ToDateTime(viewModel.Date));

            SaveChanges();
        }
Example #2
0
        internal void DeleteExpense(Expense myExpense)
        {
            foreach (var payer in myExpense.PayersForExpense)
            {
                PayersForExpense.Remove(payer);
            }
            SaveChanges();

            Expense.Remove(myExpense);

            SaveChanges();
        }
Example #3
0
        internal Expense GetExpenseById(int id)
        {
            var myExpense = Expense.Where(e => e.Id == id).FirstOrDefault();

            if (myExpense != null)
            {
                var listOfPayers = PayersForExpense.Where(p => p.ExpenseId == myExpense.Id).ToList();
                myExpense.PayersForExpense = listOfPayers;

                foreach (var payer in myExpense.PayersForExpense)
                {
                    payer.User = User.Where(u => u.Id == payer.UserId).FirstOrDefault();
                }
            }
            return(myExpense);
        }
Example #4
0
        internal void CreatePayerForExpense(string payerIds, int expenseId)
        {
            var splitpayers = payerIds.Split(',');

            foreach (var payerId in splitpayers)
            {
                PayersForExpense.Add(new PayersForExpense
                {
                    ExpenseId = expenseId,
                    UserId    = Convert.ToInt32(payerId),
                    Objection = false
                });
            }

            SaveChanges();
        }
Example #5
0
        internal List <Expense> GetExpensesByEvent(int eventId)
        {
            var expenses = Expense.Where(e => e.EventId == eventId).ToList();

            foreach (var item in expenses)
            {
                item.PayersForExpense = PayersForExpense.Where(p => p.ExpenseId == item.Id).ToList();
                foreach (var payer in item.PayersForExpense)
                {
                    payer.User = User.Where(u => u.Id == payer.UserId).First();
                }
                item.Currency = Currency.Where(c => c.Id == item.CurrencyId).First();
            }

            return(expenses);
        }
Example #6
0
        public Event GetEventById(int id)
        {
            var myEvent = Event.Where(e => e.Id == id).First();

            myEvent.ParticipantsInEvent = ParticipantsInEvent.Where(p => p.EventId == myEvent.Id).ToList();

            myEvent.StandardCurrency = Currency.Where(c => c.Id == myEvent.StandardCurrencyId).FirstOrDefault();

            myEvent.Expense = Expense.Where(e => e.EventId == myEvent.Id).ToList();

            foreach (var expense in myEvent.Expense)
            {
                expense.PayersForExpense = PayersForExpense.Where(p => p.ExpenseId == expense.Id).ToList();
            }

            foreach (var person in myEvent.ParticipantsInEvent)
            {
                person.User = User.Where(u => u.Id == person.UserId).First();
            }

            return(myEvent);
        }
Example #7
0
 internal List <PayersForExpense> GetObjectionsInEvent(int id)
 {
     return(PayersForExpense.Where(p => p.Expense.EventId == id && p.Objection == true).ToList());
 }