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);
        }
Esempio n. 2
0
        private List <Expense> GetMoneyTransferAsExpense(long accountId, long sharedAccountId)
        {
            var transfers = SharedAccountRepository.GetSharedAccountMoneyTransfer(sharedAccountId);
            var expenses  = new List <Expense>();

            foreach (var transfer in transfers)
            {
                if (transfer.PayedAccountId == accountId)
                {
                    expenses.Add(new Expense
                    {
                        AccountId      = accountId,
                        Amount         = transfer.Amount * -1,
                        ExternalGuid   = transfer.ExternalGuid,
                        SpentDate      = transfer.TransferDate,
                        Store          = AccountRepository.GetAccount(transfer.PayerAccountId, false).Name,
                        SpentAccountId = accountId,
                        Category       = "Transfer",
                        ExpenseId      = -1
                    });
                }
                else if (transfer.PayerAccountId == accountId)
                {
                    expenses.Add(new Expense
                    {
                        AccountId      = accountId,
                        Amount         = transfer.Amount,
                        ExternalGuid   = transfer.ExternalGuid,
                        SpentDate      = transfer.TransferDate,
                        Store          = AccountRepository.GetAccount(transfer.PayerAccountId, false).Name,
                        SpentAccountId = accountId,
                        Category       = "Transfer",
                        ExpenseId      = -1
                    });
                }
            }

            return(expenses);
        }