Пример #1
0
        public async Task <bool> AddExtraAmount(AdditionalExpenseRequest expenseAddRequest)
        {
            UriBuilder uriBuilder = new UriBuilder(ApiConstants.BaseUri)
            {
                Path = ApiConstants.AddAdditionalAmount
            };
            bool isAmountAdded = await _genericRepository.PostAsync <AdditionalExpenseRequest, bool>(uriBuilder.ToString(), expenseAddRequest);

            return(isAmountAdded);
        }
Пример #2
0
        public IHttpActionResult AddAdditionAmount(AdditionalExpenseRequest additionalExpenseRequest)
        {
            if (ModelState.IsValid)
            {
                int currentWeek  = HelperFunctions.HelperFunctions.CurrentWeekNumber;
                int currentMonth = DateTime.UtcNow.Month;
                int currentYear  = DateTime.UtcNow.Year;
                int userDataid   = additionalExpenseRequest.UserDataID;
                int toCategoryID = db.Categories.Where(e => e.CategoryName.Equals(additionalExpenseRequest.ToCategory)).FirstOrDefault().CategoryID;
                if (additionalExpenseRequest.IsFromCategory)
                {
                    int fromCategoryID = db.Categories.Where(e => e.CategoryName.Equals(additionalExpenseRequest.FromCategory)).FirstOrDefault().CategoryID;

                    #region subtract_amount_from selected category

                    // Create a new entry for DailyExpense
                    DailyExpense dailyExpense = new DailyExpense();
                    dailyExpense.UserDataID   = userDataid;
                    dailyExpense.CategoryID   = fromCategoryID;
                    dailyExpense.Amount       = additionalExpenseRequest.AmountToAdd;
                    dailyExpense.CurrentWeek  = currentWeek;
                    dailyExpense.CurrentMonth = currentMonth;
                    dailyExpense.CurrentYear  = currentYear;
                    dailyExpense.DateCreated  = DateTime.UtcNow;
                    dailyExpense.DateModified  = DateTime.UtcNow;

                    // Update WeeklySaving - Subtract the daily expense amount from the total amount for that category

                    WeeklySaving weeklySavingfromCategory = db.WeeklySavings.Where(e =>
                                                                                   e.UserDataID == userDataid &&
                                                                                   e.CategoryID == fromCategoryID &&
                                                                                   e.CurrentMonth == currentMonth &&
                                                                                   e.CurrentYear == currentYear &&
                                                                                   e.CurrentWeek == currentWeek).FirstOrDefault();

                    if (weeklySavingfromCategory != null)
                    {
                        weeklySavingfromCategory.Amount -= additionalExpenseRequest.AmountToAdd;
                        if (this.WeeklyExpenseForUserExists(userDataid, toCategoryID))
                        {
                            WeeklyExpenseDetail weeklyExpenseDetail = db.WeeklyExpenseDetails.Where(e =>
                                                                                                    e.UserDataID == userDataid &&
                                                                                                    e.CategoryID == fromCategoryID).FirstOrDefault();

                            if (weeklyExpenseDetail != null)
                            {
                                // Update WeeklyExpenseDetails with new reset values
                                weeklyExpenseDetail.Amount         -= additionalExpenseRequest.AmountToAdd;
                                weeklyExpenseDetail.DateModified     = DateTime.UtcNow;
                                db.Entry(weeklyExpenseDetail).State = EntityState.Modified;
                            }
                            else
                            {
                                return(BadRequest());
                            }
                        }
                    }
                    else
                    {
                        return(BadRequest());
                    }

                    #endregion
                }

                #region add amount to requested category

                if (this.WeeklyExpenseForUserExists(userDataid, toCategoryID))
                {
                    WeeklyExpenseDetail weeklyExpenseDetail = db.WeeklyExpenseDetails.Where(e =>
                                                                                            e.UserDataID == userDataid &&
                                                                                            e.CategoryID == toCategoryID).FirstOrDefault();

                    if (weeklyExpenseDetail != null)
                    {
                        // Update WeeklyExpenseDetails with new reset values
                        weeklyExpenseDetail.Amount         += additionalExpenseRequest.AmountToAdd;
                        weeklyExpenseDetail.DateModified     = DateTime.UtcNow;
                        db.Entry(weeklyExpenseDetail).State = EntityState.Modified;

                        // Check WeeklySavngs exits
                        WeeklySaving weeklySavingsToUpdate = db.WeeklySavings.Where(e =>
                                                                                    e.UserDataID == userDataid &&
                                                                                    e.CategoryID == toCategoryID).FirstOrDefault();
                        if (weeklySavingsToUpdate != null)
                        {
                            weeklySavingsToUpdate.Amount         += additionalExpenseRequest.AmountToAdd;
                            weeklyExpenseDetail.DateModified       = DateTime.UtcNow;
                            db.Entry(weeklySavingsToUpdate).State = EntityState.Modified;
                            try
                            {
                                db.SaveChanges();
                                return(Ok(true));
                            }
                            catch (DbUpdateConcurrencyException)
                            {
                                throw;
                            }
                        }
                        else
                        {
                            return(BadRequest());
                        }
                    }
                    else
                    {
                        return(BadRequest());
                    }
                }
                else
                {
                    return(BadRequest());
                }

                #endregion
            }
            else
            {
                return(BadRequest());
            }
        }