Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        private BudgetSummaryModel AddBudgetSummaryModelSettings(BudgetSummaryModel model, long accountId, bool sharedAccount)
        {
            var categories = CategoryHelper.GetCategories();
            var expenses   = ExpenseRepository.GetAccountExpenses(accountId);
            var settings   = SettingsHelper.GetCategorySettings(accountId);

            foreach (var setting in settings.Settings)
            {
                if (!setting.Data.IsActive)
                {
                    continue;
                }
                if (setting.Data.Unlimited)
                {
                    continue;
                }
                if (setting.Data.Amount <= 0)
                {
                    continue;
                }

                model.AddCategoryTable(setting.Data.Duration, categories.Single(c => c.CategoryId == long.Parse(setting.ContextValue)), setting.Data.Amount, expenses, sharedAccount);
            }

            return(model);
        }
Ejemplo n.º 3
0
        private SummaryModel GetSummaryModel(long accountId, SummaryTimePeriod timePeriod)
        {
            var expenses = ExpenseRepository.GetAccountExpenses(accountId);
            var settings = SettingsHelper.GetCategorySettings(accountId);
            var model    = new SummaryModel(timePeriod, expenses.Min(e => e.SpentDate), expenses.Max(e => e.SpentDate));

            expenses = expenses.Where(e => e.SpentDate >= model.StartDate && e.SpentDate <= model.EndDate.AddDays(1).AddSeconds(-1)).ToList();

            return(model);
        }
Ejemplo n.º 4
0
        public ExpenseSummaryModel GetExpenseSummary(long accountId, ExpenseSummaryTimePeriod timePeriod)
        {
            var expenses         = ExpenseRepository.GetAccountExpenses(accountId);
            var hasSharedAccount = AccountHelper.HasSharedAccount(accountId);

            if (timePeriod == ExpenseSummaryTimePeriod.Default)
            {
                var accountSetting = SettingsHelper.GetExpenseSummarySettings(accountId);
                timePeriod = accountSetting.Settings.Any(s => s.ContextValue == SettingsHelper.TimePeriodSettingValue)
                    ? accountSetting.Settings.Single(s => s.ContextValue == SettingsHelper.TimePeriodSettingValue).Data
                    : ExpenseSummaryTimePeriod.Today;
            }

            var model = new ExpenseSummaryModel(expenses, timePeriod, hasSharedAccount);

            return(model);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        private List <Tuple <DateTime, decimal> > GetOweAmount(long accountId, long sharedAccountId)
        {
            var owedAmount = new List <Tuple <DateTime, decimal> >();

            var expenses = ExpenseRepository.GetAccountExpenses(sharedAccountId);
            var months   = expenses.OrderBy(e => e.SpentDate).Select(e => new DateTime(e.SpentDate.Year, e.SpentDate.Month, 1)).Distinct();

            foreach (var month in months)
            {
                var amount = GetOweAmount(accountId, sharedAccountId, month);

                if (amount != 0)
                {
                    owedAmount.Add(new Tuple <DateTime, decimal>(month, amount));
                }
            }

            return(owedAmount);
        }