Beispiel #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 TransactionDictionary GroupTransactions()
        {
            var groupedTransactions   = new Dictionary <CategoryMonthKey, decimal>();
            var transacations         = _snapshotStore.GetAllSnapshots <TransactionSnapshot>().Where(t => !t.IsDeleted).ToList();
            var transactionReferences = transacations.Select(t => new EntityReference(nameof(Transaction), t.EntityID)).ToList();
            var subTransactionsLookup = _snapshotStore.GetChildSnapshots <SubTransactionSnapshot>(transactionReferences)?.ToDictionary(kvp => kvp.Key.EntityID, kvp => kvp.Value);

            foreach (var transaction in transacations)
            {
                if (transaction.TransactionType == TransactionTypes.Normal)
                {
                    CategoryMonthKey category = GetCategoryMonthKey(transaction);
                    if (category == null)
                    {
                        continue;
                    }

                    if (groupedTransactions.ContainsKey(category))
                    {
                        groupedTransactions[category] += GetTransactionAmount(transaction);
                    }
                    else
                    {
                        groupedTransactions[category] = GetTransactionAmount(transaction);
                    }
                }
                else if (transaction.TransactionType == TransactionTypes.SplitTransaction)
                {
                    if (subTransactionsLookup != null &&
                        subTransactionsLookup.TryGetValue(transaction.EntityID, out List <SubTransactionSnapshot> subTransactions))
                    {
                        foreach (var subTransaction in subTransactions)
                        {
                            CategoryMonthKey category = GetCategoryMonthKey(transaction, subTransaction);
                            if (category == null)
                            {
                                continue;
                            }

                            if (groupedTransactions.ContainsKey(category))
                            {
                                groupedTransactions[category] += GetTransactionAmount(subTransaction);
                            }
                            else
                            {
                                groupedTransactions[category] = GetTransactionAmount(subTransaction);
                            }
                        }
                    }
                }
            }

            return(groupedTransactions);
        }
        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;
                }
            }
        }