public ActionResult _CashflowDetail(Expenses Expense, int?ExpenseId)
        {
            if (Expense.Id == 0 && (ExpenseId == 0 || !ExpenseId.HasValue))
            {
                Entities entities  = new Entities();
                Expenses dbExpense = entities.Expenses.Where(x => x.Name.Equals(Expense.Name) && !x.DisabledDate.HasValue).OrderByDescending(x => x.StartDate).FirstOrDefault();
                if (dbExpense != null && !dbExpense.EndDate.HasValue)
                {
                    return(Content("-1"));
                }
                else if (dbExpense != null && Expense.StartDate <= dbExpense.EndDate)
                {
                    return(Content("-2"));
                }
            }
            CashFlowDetailListViewModel model = ExpenseQueries.GetCashFlowDetail(Expense, ExpenseId, UserQueries.GetCurrentUsername());

            return(PartialView(model));
        }
Esempio n. 2
0
        public static CashFlowDetailListViewModel GetCashFlowDetail(Expenses expense, int?id, string username)
        {
            CashFlowDetailListViewModel result = new CashFlowDetailListViewModel();
            DateTime current         = DateTime.Now;
            DateTime userCreatedDate = UserQueries.GetUserByUsername(username).CreatedDate;
            Expenses dbExpense       = null;

            result.BeforeAvailableMoney = AssetQueries.CheckAvailableMoney(username, current);
            result.AfterAvailableMoney  = result.BeforeAvailableMoney;
            DateTime startDate = expense.StartDate < userCreatedDate ? userCreatedDate : expense.StartDate;
            DateTime endDate   = current;

            if (id > 0)
            {
                expense = ExpenseQueries.GetExpenseById(id.Value);

                while (startDate <= endDate)
                {
                    CashFlowDetailViewModel model = new CashFlowDetailViewModel
                    {
                        Month        = startDate.ToString("MM/yyyy"),
                        IncomeBefore = 0 - expense.Value,
                        IncomeAfter  = 0
                    };
                    result.CashflowDetails.Add(model);
                    result.AfterAvailableMoney += expense.Value;
                    startDate = startDate.AddMonths(1);
                }
                result.Action = "Delete";
            }
            else if (expense.Id == 0)
            {
                while (startDate <= endDate)
                {
                    CashFlowDetailViewModel model = new CashFlowDetailViewModel
                    {
                        Month        = startDate.ToString("MM/yyyy"),
                        IncomeBefore = 0,
                        IncomeAfter  = 0 - expense.Value
                    };
                    result.CashflowDetails.Add(model);
                    result.AfterAvailableMoney -= expense.Value;
                    startDate = startDate.AddMonths(1);
                }
                result.Action = "Create";
            }
            else
            {
                dbExpense = ExpenseQueries.GetExpenseById(expense.Id);

                while (startDate <= endDate)
                {
                    CashFlowDetailViewModel model = new CashFlowDetailViewModel();
                    model.Month = startDate.ToString("MM/yyyy");
                    if (startDate >= dbExpense.StartDate && startDate <= (dbExpense.EndDate.HasValue ? dbExpense.EndDate : current))
                    {
                        model.IncomeBefore = 0 - dbExpense.Value;
                    }
                    else
                    {
                        model.IncomeBefore = 0;
                    }
                    if (startDate >= expense.StartDate && startDate <= (expense.EndDate.HasValue ? expense.EndDate : current))
                    {
                        model.IncomeAfter = 0 - expense.Value;
                    }
                    else
                    {
                        model.IncomeAfter = 0;
                    }
                    result.CashflowDetails.Add(model);
                    result.AfterAvailableMoney -= model.IncomeAfter - model.IncomeBefore;
                    startDate = startDate.AddMonths(1);
                }
                result.Action = "Update";
            }
            return(result);
        }