示例#1
0
        public BudgetViewCategoryMonth GetLastCategoryMonth(string categoryId, DateTime month, out bool exactMatch)
        {
            if (_lastResult == null)
            {
                RecalculateCache();
            }

            CategoryMonthKey        key           = new CategoryMonthKey(nameof(Category), categoryId, month);
            BudgetViewCategoryMonth categoryMonth = null;

            if (_lastResult.CategoryMonths.TryGetValue(key, out categoryMonth))
            {
                exactMatch = true;
                return(categoryMonth);
            }

            List <BudgetViewCategoryMonth> monthList = null;

            if (_lastResult.CategoryMonthsOrdered.TryGetValue(new CategoryKey(key), out monthList))
            {
                int index = monthList.BinarySearch(m => m.Month, month);
                index      = ~index;
                exactMatch = false;
                if (index == 0)
                {
                    return(null);
                }
                return(monthList[index - 1]);
            }
            else
            {
                exactMatch = false;
                return(null);
            }
        }
        private void PopulateCategoryMonthData(CategoryResultsDictionary results)
        {
            var categoryMonths = _snapshotStore.GetAllSnapshots <CategoryMonthSnapshot>();

            foreach (var categoryMonth in categoryMonths)
            {
                if (categoryMonth.AmountBudgeted == 0M && categoryMonth.NegativeBalanceHandling == null)
                {
                    continue;
                }

                CategoryMonthKey        monthKey    = new CategoryMonthKey(categoryMonth.Parent, categoryMonth.Month);
                BudgetViewCategoryMonth monthResult = null;

                BudgetViewCategoryMonth monthValues = null;
                if (results.TryGetValue(monthKey, out monthValues))
                {
                    if (categoryMonth.AmountBudgeted != 0M)
                    {
                        monthValues.AmountBudgeted = GetCategoryMonthAmount(categoryMonth);
                    }

                    if (categoryMonth.NegativeBalanceHandling != null)
                    {
                        monthValues.NegativeBalanceHandlingIsExplicit = true;
                        monthValues.NegativeBalanceHandling           = categoryMonth.NegativeBalanceHandling.Value;
                    }
                }
                else
                {
                    monthResult = new BudgetViewCategoryMonth(monthKey.EntityID, monthKey.FirstDayOfMonth);
                    if (categoryMonth.AmountBudgeted != 0M)
                    {
                        monthResult.AmountBudgeted = GetCategoryMonthAmount(categoryMonth);
                    }

                    if (categoryMonth.NegativeBalanceHandling != null)
                    {
                        monthResult.NegativeBalanceHandlingIsExplicit = true;
                        monthResult.NegativeBalanceHandling           = categoryMonth.NegativeBalanceHandling.Value;
                    }
                    results[monthKey] = monthResult;
                }
            }
        }