Example #1
0
        private decimal GetOweAmount(long accountId, long sharedAccountId, DateTime month)
        {
            var expenses = ExpenseRepository.GetAccountExpenses(sharedAccountId);

            expenses = expenses.Where(e => e.SpentDate.Year == month.Year && e.SpentDate.Month == month.Month).ToList();

            var transfers = SharedAccountRepository.GetSharedAccountMoneyTransfer(sharedAccountId);

            transfers = transfers.Where(t => t.ForYear == month.Year && t.ForMonth == month.Month).ToList();

            var yourIncomeTotal    = IncomeRepository.GetAccountIncome(accountId).Where(i => i.IncomeDate.Year == month.Year && i.IncomeDate.Month == month.Month).Sum(i => i.Amount);
            var partnerIncomeTotal = IncomeRepository.GetPartnerIncome(accountId).Where(i => i.IncomeDate.Year == month.Year && i.IncomeDate.Month == month.Month).Sum(i => i.Amount);
            var yourPercentage     = yourIncomeTotal + partnerIncomeTotal == 0 ? 0 : yourIncomeTotal / (yourIncomeTotal + partnerIncomeTotal);

            var sharedSpentTotal     = expenses.Sum(e => e.Amount);
            var yourSharedSpentTotal = expenses.Where(e => e.SpentAccountId == accountId).Sum(e => e.Amount);
            var yourIdealSpentTotal  = yourPercentage * sharedSpentTotal;

            var youPayed    = transfers.Where(t => t.PayerAccountId == accountId).Sum(t => t.Amount);
            var youRecieved = transfers.Where(t => t.PayedAccountId == accountId).Sum(t => t.Amount);

            var finalAmount = Math.Round(yourIdealSpentTotal - yourSharedSpentTotal, 2);

            finalAmount = finalAmount - youPayed + youRecieved;

            return(finalAmount);
        }
Example #2
0
        private DashboardModel GetDashboardModel(long accountId, long?sharedAccountId, bool sharedDashboard)
        {
            var model = new DashboardModel();

            var expenses = sharedDashboard
                ? ExpenseRepository.GetAccountExpenses(sharedAccountId.GetValueOrDefault())
                : ExpenseRepository.GetAccountExpenses(accountId);
            var income = sharedDashboard
                ? IncomeRepository.GetAccountIncome(sharedAccountId.GetValueOrDefault())
                : IncomeRepository.GetAccountIncome(accountId);

            model.SpentToday     = GetTotalSpentAmount(expenses, ExpenseSummaryTimePeriod.Today);
            model.SpentThisWeek  = GetTotalSpentAmount(expenses, ExpenseSummaryTimePeriod.ThisWeek);
            model.SpentThisMonth = GetTotalSpentAmount(expenses, ExpenseSummaryTimePeriod.ThisMonth);
            model.SpentThisYear  = GetTotalSpentAmount(expenses, ExpenseSummaryTimePeriod.ThisYear);

            model.ExpensesLineChart = GetLineChart(expenses);
            model.IncomeLineChart   = GetLineChart(income);
            model.PieChart          = GetPieChart(expenses);

            model.BudgetOverviewList = sharedDashboard
                ? GetBudgetOverviewChart(expenses, sharedAccountId.GetValueOrDefault())
                : GetBudgetOverviewChart(expenses, accountId);

            model.Shared = GetSharedDashboardModel(accountId, sharedAccountId, sharedDashboard);

            if (sharedDashboard || !sharedAccountId.HasValue)
            {
                model.SharedExpensesLineChart.Show = false;
                model.SpentExpensesLineChart.Show  = false;
            }
            else
            {
                var spentExpenses = ExpenseRepository.GetAccountExpensesIncludeShared(accountId);
                spentExpenses.AddRange(GetMoneyTransferAsExpense(accountId, sharedAccountId.GetValueOrDefault()));
                var sharedExpenses = ExpenseRepository.GetAccountExpenses(accountId);
                sharedExpenses.AddRange(ExpenseRepository.GetAccountExpenses(sharedAccountId.GetValueOrDefault()));
                model.SpentExpensesLineChart  = GetLineChart(spentExpenses);
                model.SharedExpensesLineChart = GetLineChart(sharedExpenses);
            }

            return(model);
        }