Esempio 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);
        }