private void AddDatesAndExpenses(MonthlyExpenseSummary monthlyExpenseSummary)
        {
            DateTime startDate = new DateTime(DateTime.Now.Year, 1, 1);
            DateTime endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

            var expenses = new MakeItClear.Service.Moke.AccountsService().GetMonthlyExpensesDetails().OrderBy(x => x.Date).ToList();

            while (startDate <= endDate)
            {
                var monthlyExpenseItem = new MonthlyExpenseItemModel();
                monthlyExpenseItem.Date = startDate.ToString(DateToMonthYearFormat);
                AddIncomesForOwners(monthlyExpenseItem, monthlyExpenseSummary.AccountOwners);
                AddExpensesForOwners(monthlyExpenseItem, monthlyExpenseSummary.AccountOwners, expenses);
                CalculateCommonExpense(monthlyExpenseItem, monthlyExpenseSummary.AccountOwnersInCalc, expenses);

                monthlyExpenseSummary.MonthlyExpenses.Add(monthlyExpenseItem);
                startDate = startDate.AddMonths(1);
            }
        }
        private void AddIncomesForOwners(MonthlyExpenseItemModel monthlyExpenseItem, IEnumerable<string> accountOwners)
        {
            //Optimize below service call to load only items for the selected MMM/yyyy
            var incomes = new MakeItClear.Service.Moke.AccountsService().GetMonthlyIncomeDetails().OrderBy(x => x.Date);

            foreach (var owner in accountOwners)
            {
                var expItem = new AccountingItemModel();
                expItem.Owner = owner;
                expItem.Amount = incomes.Where(x => x.Date.ToString(DateToMonthYearFormat).Equals(monthlyExpenseItem.Date)
                && x.Owner.Equals(owner)).Sum(x => x.Amount);
                monthlyExpenseItem.Incomes.Add(expItem);
            }
        }
 private void AddExpensesForOwners(MonthlyExpenseItemModel monthlyExpenseItem, IEnumerable<string> accountOwners,
     List<AccountingItemModel> expenses)
 {
     foreach (var owner in accountOwners)
     {
         var expItem = new AccountingItemModel();
         expItem.Owner = owner;
         expItem.Amount = expenses.Where(x => x.Date.ToString(DateToMonthYearFormat).Equals(monthlyExpenseItem.Date)
         && x.Owner.Equals(owner)).Sum(x => x.Amount);
         monthlyExpenseItem.Expenses.Add(expItem);
     }
 }
        private void CalculateCommonExpense(MonthlyExpenseItemModel monthlyExpenseItem, IEnumerable<string> accountOwners,
            List<AccountingItemModel> expenses)
        {
            var commonExpnsForMonth = expenses.Where(x => x.Date.ToString(DateToMonthYearFormat)
                                                .Equals(monthlyExpenseItem.Date) && x.IsCommon);
            var totalCommonExpnsForMonth = commonExpnsForMonth.Sum(x => x.Amount);
            monthlyExpenseItem.TotalCommonExpense = totalCommonExpnsForMonth;
            if (monthlyExpenseItem.TotalIncome != 0)
                monthlyExpenseItem.ActualUnit = monthlyExpenseItem.TotalCommonExpense / monthlyExpenseItem.TotalIncome; // Total Comm Exp/Total Income
            foreach (var owner in accountOwners)
            {
                var expItem = new AccountingItemModel();
                expItem.Owner = owner;
                expItem.Amount = commonExpnsForMonth.Where(x => x.Owner.Equals(owner)).Sum(x => x.Amount);
                monthlyExpenseItem.CommonExpenses.Add(expItem);

                // Calculate actuals
                var expItem2 = new AccountingItemModel();
                expItem2.Owner = owner;
                var ownersIncome = monthlyExpenseItem.Incomes.First(x => x.Owner.Equals(owner));
                expItem2.Amount = monthlyExpenseItem.ActualUnit * ownersIncome.Amount; // Unit Share * Owner's Income
                monthlyExpenseItem.Actuals.Add(expItem2);
            }

            // Calculate settlement
            var ajithsActual = monthlyExpenseItem.Actuals.First(x => x.Owner.Equals(OwnerNames.Ajith)).Amount;
            var ajithsSpent = monthlyExpenseItem.CommonExpenses.First(x => x.Owner.Equals(OwnerNames.Ajith)).Amount;
            monthlyExpenseItem.SettledAmount = ajithsSpent - ajithsActual;

            // Adjust monthly expense with actuals
            var ajithsExp = monthlyExpenseItem.Expenses.First(x => x.Owner.Equals(OwnerNames.Ajith));
            var vandhanasExp = monthlyExpenseItem.Expenses.First(x => x.Owner.Equals(OwnerNames.Vandhana));
            ajithsExp.Amount = ajithsExp.Amount - (monthlyExpenseItem.SettledAmount);
            vandhanasExp.Amount = vandhanasExp.Amount + (monthlyExpenseItem.SettledAmount);
        }