Example #1
0
        public static CurrentMonth GetCurrentMonth(string userId)
        {
            try
            {
                DbMonth       dbThisMonth   = LogService.Data.GetDbMonthByDate(userId, DateTime.Now);
                DbPreferences dbPreferences = LogService.Data.GetPreferencesByUserId(userId);

                string _monthName = MonthOrdinalToName(dbThisMonth.StartDate.Month);



                List <double> _ajustments = new List <double>();
                //int _yearId = 0;
                DateTime _yearStartDate       = new DateTime();
                int      _year                = _yearStartDate.Year;
                double   _yearStartAmount     = 0;
                int      _yearGoal            = 0;
                bool     _isCurrentYear       = true;
                double   _monthSaved          = dbThisMonth.GrossCredits - dbThisMonth.GrossDebits;
                double   _balance             = CalculateBalance(dbThisMonth.StartAmount, dbThisMonth.GrossCredits, dbThisMonth.GrossDebits);
                int      _monthCalculatedGoal = CalculateMonthGoal(
                    currentBalance: _balance,
                    yearGoal: dbPreferences.YearGoal,
                    startDate: dbThisMonth.StartDate,
                    monthFixedGoal: dbPreferences.MonthFixedGoal);
                int _goal = dbPreferences.MonthGoalType == MonthGoalType.Fixed
                    ? dbPreferences.MonthFixedGoal : _monthCalculatedGoal;
                double        _yearSaved        = _balance - _yearStartAmount;
                double        _amountLeftToSave = _goal - _balance;
                List <string> _categories       = new List <string>();

                return(new CurrentMonth(
                           monthId: dbThisMonth.MonthId,
                           previousMonthId: dbThisMonth.PreviousMonthId,
                           startDate: dbThisMonth.StartDate,
                           monthName: _monthName,
                           goal: _goal,
                           monthFixedGoal: dbPreferences.MonthFixedGoal,
                           monthGoalType: dbPreferences.MonthGoalType,
                           monthCalculatedGoal: _monthCalculatedGoal,
                           grossCredits: dbThisMonth.GrossCredits,
                           grossDebits: dbThisMonth.GrossDebits,
                           startAmount: dbThisMonth.StartAmount,
                           ajustments: _ajustments,
                           year: _year,
                           yearStartDate: _yearStartDate,
                           yearStartAmount: _yearStartAmount,
                           yearGoal: _yearGoal,
                           isCurrentYear: _isCurrentYear,
                           yearSaved: _yearSaved,
                           monthSaved: _monthSaved,
                           balance: _balance,
                           amountLeftToSave: _amountLeftToSave,
                           categories: _categories));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Example #2
0
            // --| Months |--------------------------------------------------------------------------->


            // Updated sept-04-17
            public static int InsertDbMonth(DbMonth model)
            {
                int monthId = 0;

                try
                {
                    DataProvider.ExecuteNonQuery(GetConnection, "dbo.sp_Month_Insert",
                                                 inputParamMapper : delegate(SqlParameterCollection paramCollection)
                    {
                        paramCollection.AddWithValue("@userId", model.UserId);
                        paramCollection.AddWithValue("@startDate", model.StartDate);
                        paramCollection.AddWithValue("@grossCredits", model.GrossCredits);
                        paramCollection.AddWithValue("@grossDebits", model.GrossDebits);
                        paramCollection.AddWithValue("@startAmount", model.StartAmount);
                        paramCollection.AddWithValue("@previousMonthId", model.PreviousMonthId);
                        paramCollection.AddWithValue("@isStartAmountConfirmed", model.IsStartAmountConfirmed);

                        var p       = new SqlParameter("@monthId", System.Data.SqlDbType.Int);
                        p.Direction = System.Data.ParameterDirection.Output;

                        paramCollection.Add(p);
                    },
                                                 returnParameters : delegate(SqlParameterCollection param)
                    {
                        int.TryParse(param["@monthId"].Value.ToString(), out monthId);
                    });
                }
                catch (Exception e)
                {
                    throw e;
                }

                return(monthId);
            }
Example #3
0
            // updated sept-04-17
            public static DbMonth GetDbMonthZeroForUser(string userId)
            {
                var dbMonth = new DbMonth();

                try
                {
                    DataProvider.ExecuteCmd(GetConnection, "dbo.sp_Months_Select_FirstDateCreatedForUser",
                                            inputParamMapper : delegate(SqlParameterCollection paramCollection)
                    {
                        paramCollection.AddWithValue("@userId", userId);
                    },
                                            map : delegate(IDataReader reader, short set)
                    {
                        int startingIndex = 0;

                        dbMonth = new DbMonth(
                            monthId: reader.GetSafeInt32(startingIndex++),
                            userId: reader.GetSafeString(startingIndex++),
                            startDate: reader.GetSafeUtcDateTime(startingIndex++),
                            grossCredits: reader.GetSafeDouble(startingIndex++),
                            grossDebits: reader.GetSafeDouble(startingIndex++),
                            startAmount: reader.GetSafeDouble(startingIndex++),
                            previousMonthId: reader.GetSafeInt32(startingIndex++),
                            isStartAmountConfirmed: reader.GetSafeBool(startingIndex++));
                    });
                }
                catch (Exception e)
                {
                    throw e;
                }

                return(dbMonth);
            }
Example #4
0
 public static Month GetMonthByUserIdAndDate(string userId, DateTime date)
 {
     try
     {
         DateTime monthStart = GetMonthStartOfDate(date);
         DbMonth  dbMonth    = Data.GetDbMonthByDate(userId, monthStart);
         return(Log.RenderMonth(dbMonth));
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Example #5
0
        private static void UpdateLogBeforeAndUpToTransactionDate(
            string userId,
            DateTime date,
            double netImpactOnBalance)
        {
            try
            {
                if (Data.DoesConfirmedMonthStartAmountExistOnOrBeforeDate(userId, date))
                {
                    return;
                }

                List <DbMonth> unconfirmedMonths = Data.GetUnconfirmedMonthsOnOrBeforeDate(userId, date);

                var updateList = new List <DbMonth>();

                double updatedNewYearStartAmount = 0;


                foreach (var dbMonth in unconfirmedMonths)
                {
                    var monthToUpdate = new DbMonth(
                        monthId: dbMonth.MonthId,
                        userId: dbMonth.UserId,
                        startDate: dbMonth.StartDate,
                        grossCredits: dbMonth.GrossCredits,
                        grossDebits: dbMonth.GrossDebits,
                        startAmount: dbMonth.StartAmount + netImpactOnBalance,
                        previousMonthId: dbMonth.PreviousMonthId,
                        isStartAmountConfirmed: dbMonth.IsStartAmountConfirmed);

                    updateList.Add(monthToUpdate);

                    if (DateFallsInJanuaryOfThisYear(monthToUpdate.StartDate))
                    {
                        updatedNewYearStartAmount = monthToUpdate.StartAmount;
                    }
                }

                Data.UpdateDbMonthCollection(updateList);

                if (updatedNewYearStartAmount != 0)
                {
                    Data.UpdateYearStartAmount(userId, updatedNewYearStartAmount);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Example #6
0
        public static bool CreateNewLog(string userId, NewUserLogInsertRequest model)
        {
            bool isSuccess = true;
            // verify log does not already exist
            LogStatusType logStatus = LogService.Data.GetLogStatus(userId);

            if (logStatus != LogStatusType.NewUser)
            {
                throw new ApplicationException("A log already exists for the current user.");
            }

            DateTime today = DateTime.Today;

            DateTime startOfThisYear       = LogService.GetYearStartOfDate(today);
            DateTime startOfThisMonth      = LogService.GetMonthStartOfDate(today);
            string   defaultMonthGoalType  = "Fixed";
            int      defaultMonthFixedGoal = 100;
            var      firstMonth            = new DbMonth(
                monthId: 0,
                userId: userId,
                startDate: startOfThisMonth,
                grossCredits: 0,
                grossDebits: 0,
                startAmount: model.StartAmount,
                previousMonthId: null,
                isStartAmountConfirmed: true);

            try
            {
                // Create Preferences
                LogService.Data.InsertUserPreferences(userId, defaultMonthGoalType, defaultMonthFixedGoal);

                // Create Year
                LogService.Data.InsertYear(userId, startOfThisYear, model.StartAmount, model.YearGoal);


                // Create Month
                LogService.Data.InsertDbMonth(firstMonth);

                // Create Categories
                LogService.Data.SetDefaultCategories(userId);
            }
            catch (Exception e)
            {
                isSuccess = false;
                throw e;
            }

            return(isSuccess);
        }
Example #7
0
        public static DbMonth CreatePredecessorDbMonth(string userId, DbMonth sourceMonth)
        {
            // build a check in the insert Month/ Year Proc to make sure no duplicate Log Entites are being made
            DateTime predecessorStartDate = LogService.GetMonthStartOfDate(sourceMonth.StartDate.AddMonths(-1));

            double sourceBalance = CalculateBalance(sourceMonth.StartAmount, sourceMonth.GrossCredits, sourceMonth.GrossDebits);

            var predecessorMonth = new DbMonth(
                monthId: 0,
                userId: userId,
                startDate: predecessorStartDate,
                grossCredits: 0,
                grossDebits: 0,
                startAmount: sourceMonth.StartAmount,
                previousMonthId: 0,
                isStartAmountConfirmed: false);

            return(predecessorMonth);
        }
Example #8
0
        public static Month RenderMonth(DbMonth dbMonth)
        {
            DbPreferences dbPrederences = LogService.Data.GetPreferencesByUserId(dbMonth.UserId);
            List <double> adjustments   = LogService.Data.GetMonthAdjustments(dbMonth.MonthId);


            bool isCurrentMonth = (GetMonthStartOfDate(DateTime.Today) == GetMonthStartOfDate(dbMonth.StartDate));

            return(new MonthSnapshot(
                       monthId: dbMonth.MonthId,
                       previousMonthId: dbMonth.PreviousMonthId,
                       startDate: dbMonth.StartDate,
                       monthName: MonthOrdinalToName(dbMonth.StartDate.Month),
                       goal: 0,
                       grossCredits: dbMonth.GrossCredits,
                       grossDebits: dbMonth.GrossDebits,
                       startAmount: dbMonth.StartAmount,
                       adustments: adjustments,
                       year: dbMonth.StartDate.Year,
                       yearStartDate: LogService.GetYearStartOfDate(dbMonth.StartDate),
                       isCurrentMonth: isCurrentMonth));
        }
Example #9
0
        public static Month GetMonthByUserAndMonthId(string userId, int monthId)
        {
            DbMonth dbMonth = Data.GetDbMonthById(userId, monthId);

            return(Log.RenderMonth(dbMonth));
        }
Example #10
0
        public static Month GetNewestMonthOnFile(string userId)
        {
            DbMonth dbMonth = Data.GetNewestDbMonthOnFile(userId);

            return(Log.RenderMonth(dbMonth));
        }
Example #11
0
        public static Month GetMonthZeroForUser(string userId)
        {
            DbMonth dbMonth = Data.GetDbMonthZeroForUser(userId);

            return(Log.RenderMonth(dbMonth));
        }
Example #12
0
        private static void UpdateLogAfterTransactionDate(
            string userId,
            DateTime date,
            double netImpactOnBalance)
        {
            try
            {
                List <DbMonth> unconfirmedMonths = Data.GetUnconfirmedMonthsAfterDate(userId, date);

                var updateList = new List <DbMonth>();

                double updatedNewYearStartAmount = 0;

                foreach (var dbMonth in unconfirmedMonths)
                {
                    if (dbMonth.IsStartAmountConfirmed)
                    {
                        throw new ApplicationException("Unexpected value: DbMonth.StartAmount is confirmed!");
                    }

                    var monthToUpdate = new DbMonth(
                        monthId: dbMonth.MonthId,
                        userId: dbMonth.UserId,
                        startDate: dbMonth.StartDate,
                        grossCredits: dbMonth.GrossCredits,
                        grossDebits: dbMonth.GrossDebits,
                        startAmount: dbMonth.StartAmount + netImpactOnBalance,
                        previousMonthId: dbMonth.PreviousMonthId,
                        isStartAmountConfirmed: dbMonth.IsStartAmountConfirmed);

                    updateList.Add(monthToUpdate);

                    if (DateFallsInJanuaryOfThisYear(monthToUpdate.StartDate))
                    {
                        updatedNewYearStartAmount = monthToUpdate.StartAmount;
                    }
                }

                Data.UpdateDbMonthCollection(updateList);


                if (updatedNewYearStartAmount != 0)
                {
                    Data.UpdateYearStartAmount(userId, updatedNewYearStartAmount);
                }


                if (updateList.Count > 0)
                {
                    DbMonth lastMonth = updateList.MaxBy(m => m.StartDate);

                    string AdjustmentNote = String.Format("Transaction on {0} for $({1})", date, netImpactOnBalance);

                    Data.AddAdjustment(userId, lastMonth.MonthId, netImpactOnBalance, AdjustmentNote);
                }
                else
                {
                    DbMonth transactionMonth = Data.GetDbMonthByDate(userId, date);

                    string AdjustmentNote = String.Format("Transaction on {0} for $({1})", date, netImpactOnBalance);

                    Data.AddAdjustment(userId, transactionMonth.MonthId, netImpactOnBalance, AdjustmentNote);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Example #13
0
        private static void FillGapBetweenMonthZeroAndDate(string userId, Month monthZero, DateTime date)
        {
            try
            {
                if (!UserOwnsThisMonth(userId, monthZero.MonthId))
                {
                    throw new ApplicationException("User is not authorized to edit this log");
                }


                DateTime targetDate = GetMonthStartOfDate(date);

                int indexIncrement;
                int dateDiff = DateDiff.Days(monthZero.StartDate, targetDate);

                if (dateDiff == 0)
                {
                    return;
                }
                else if (dateDiff > 0)
                {
                    indexIncrement = 1;
                }
                else
                {
                    indexIncrement = -1;
                }

                var newDbMonthList = new List <DbMonth>();

                if (indexIncrement > 0)
                {
                    DbMonth newestDbMonthToDate = Data.GetNewestDbMonthOnFile(userId);

                    // Verify newestMonthToDate is not the current Month
                    if (DateFallsInCurrentMonth(newestDbMonthToDate.StartDate))
                    {
                        return;
                    }

                    if (DateDiff.DoDatesSpanFromLastYearToThisYear(newestDbMonthToDate.StartDate, targetDate))
                    {
                        throw new ApplicationException("The bridge from last year to this year should not be auto-populated.");
                    }

                    DbMonth newDbMonthToAdd = Log.CreateSuccessorDbMonth(userId, newestDbMonthToDate);

                    while (DateDiff.Days(newDbMonthToAdd.StartDate, targetDate) <= 0)
                    {
                        newDbMonthList.Add(newDbMonthToAdd);
                        newDbMonthToAdd = Log.CreateSuccessorDbMonth(userId, newDbMonthToAdd);
                    }

                    Data.InsertListOfDbMonths(newDbMonthList);
                }
                else
                {
                    DbMonth earliestDbMonthToDate = Data.GetEarliestMonthToDate(userId);

                    DbMonth newDbMonthToAdd = Log.CreatePredecessorDbMonth(userId, earliestDbMonthToDate);

                    while (DateDiff.Days(targetDate, newDbMonthToAdd.StartDate) <= 0)
                    {
                        newDbMonthList.Add(newDbMonthToAdd);
                        newDbMonthToAdd = Log.CreatePredecessorDbMonth(userId, newDbMonthToAdd);
                    }

                    Data.InsertListOfDbMonths(newDbMonthList);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }