Ejemplo n.º 1
0
        public Dictionary <string, decimal> GetRequiredSavingsDict()
        {
            try
            {
                var            savingsAccountBalances = new Dictionary <string, decimal>();
                ExpenseManager expenseManager         = new ExpenseManager();
                AccountManager accountManager         = new AccountManager();
                BillManager    billManager            = new BillManager();
                List <Expense> unpaidExpenses         = expenseManager.GetAllUnpaidExpenses();

                //foreach (var bill in _db.Bills.ToList())
                foreach (var expense in unpaidExpenses)
                {
                    try
                    {
                        var bill = billManager.GetBill(expense.BillId);
                        if (bill == null)
                        {
                            Logger.Instance.Debug($"No Bill found WHERE expense.BillId = {expense.BillId}");
                            Logger.Instance.DataFlow($"No Bill found WHERE expense.BillId = {expense.BillId}");
                            continue;
                        }

                        bill.Account = accountManager.GetAccount(bill.AccountId);
                        if (bill.Account == null)
                        {
                            continue;
                        }
                        var billTotal       = expense.Amount; // Use info from Expense and not Bill to account for when the current Bill.Amount differs from past amounts
                        var dueDate         = expense.Due;
                        var payPeriodsLeft  = PayPeriodsTilDue(dueDate);
                        var savePerPaycheck = 0.0m;
                        var save            = 0.0m;

                        // Calculate how much to save each pay period
                        if (dueDate > DateTime.Today)
                        {
                            switch (bill.PaymentFrequency)
                            {
                            case FrequencyEnum.Annually:
                                savePerPaycheck = billTotal / 24;
                                Logger.Instance.Calculation($"{expense.Name} save/paycheck = {Math.Round(savePerPaycheck, 2)} to {bill.Account.Name} account");
                                break;

                            case FrequencyEnum.SemiAnnually:
                                savePerPaycheck = billTotal / 12;
                                Logger.Instance.Calculation($"{expense.Name} save/paycheck = {Math.Round(savePerPaycheck, 2)} to {bill.Account.Name} account");
                                break;

                            case FrequencyEnum.Quarterly:
                                savePerPaycheck = billTotal / 6;
                                Logger.Instance.Calculation($"{expense.Name} save/paycheck = {Math.Round(savePerPaycheck, 2)} to {bill.Account.Name} account");
                                break;

                            case FrequencyEnum.SemiMonthly:
                                savePerPaycheck = billTotal / 4;
                                Logger.Instance.Calculation($"{expense.Name} save/paycheck = {Math.Round(savePerPaycheck, 2)} to {bill.Account.Name} account");
                                break;

                            case FrequencyEnum.Monthly:
                                savePerPaycheck = billTotal / 2;
                                Logger.Instance.Calculation($"{expense.Name} save/paycheck = {Math.Round(savePerPaycheck, 2)} to {bill.Account.Name} account");
                                break;

                            case FrequencyEnum.BiWeekly:
                                savePerPaycheck = billTotal;
                                Logger.Instance.Calculation($"{expense.Name} save/paycheck = {Math.Round(savePerPaycheck, 2)} to {bill.Account.Name} account");
                                break;

                            case FrequencyEnum.Weekly:
                                savePerPaycheck = billTotal * 2;
                                Logger.Instance.Calculation($"{expense.Name} save/paycheck = {Math.Round(savePerPaycheck, 2)} to {bill.Account.Name} account");
                                break;

                            default:
                                savePerPaycheck = billTotal / 2;
                                Logger.Instance.Calculation($"{expense.Name} save/paycheck = {Math.Round(savePerPaycheck, 2)} to {bill.Account.Name} account");
                                break;
                            }

                            save = payPeriodsLeft > 1 ? Math.Round(billTotal - payPeriodsLeft * savePerPaycheck, 2) : savePerPaycheck;
                        }
                        else
                        {
                            save = expense.Amount;
                        }

                        // required savings = bill amount due - (how many pay periods before due date * how much to save per pay period)

                        Logger.Instance.Calculation($"{bill.Account.Name} - [{Math.Round(billTotal, 2)}] [{bill.DueDate:d}] [{payPeriodsLeft}(ppl)] [{Math.Round(savePerPaycheck, 2)}(spp)] [{Math.Round(save, 2)}(req save)]");

                        if (savingsAccountBalances.ContainsKey(bill.Account.Name))
                        {
                            savingsAccountBalances[bill.Account.Name] += save;
                        }
                        else
                        {
                            savingsAccountBalances.Add(bill.Account.Name, save);
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Instance.Error(e);
                        throw;
                    }
                }


                return(savingsAccountBalances);
            }
            catch (Exception e)
            {
                Logger.Instance.Error(e);
                throw;
            }
        }