コード例 #1
0
        public IActionResult Get()
        {
            var userFromAuth = UserService.GetUserFromClaims(this.User, UserRepo, RequestLogger);

            RequestLogger.UserId = userFromAuth.Id.ToString();

            var accountTemplates = AccountTemplateRepo.FindAllTemplatesByOwner(userFromAuth.Id);

            return(Ok(accountTemplates));
        }
コード例 #2
0
        private void CompleteMissingAccountActuals(Budget budget, Guid ownerId, List <Account> ownerAccounts)
        {
            List <AccountTemplate> accountTemplates      = null;
            AccountCategory        budgetAccountCategory = null;
            AccountActualTemplate  budgetLinkedAccountDepositActualTemplate   = null;
            AccountActualTemplate  budgetLinkedAccountWithdrawlActualTemplate = null;

            if (budget.AccountLinkId.HasValue)
            {
                if (accountTemplates == null)
                {
                    accountTemplates = AccountTemplateRepo.FindAllTemplatesByOwner(ownerId);
                }

                if (GetAccountAndCategoryFromLinkIDs(ownerAccounts, budget.AccountLinkId.Value, budget.AccountCategoryLinkId, out Account budgetAccount, out budgetAccountCategory))
                {
                    budgetLinkedAccountDepositActualTemplate   = GetDefaultActualTemplateForAccountOfIncomeType(accountTemplates, budgetAccount.AccountTemplateId, true);
                    budgetLinkedAccountWithdrawlActualTemplate = GetDefaultActualTemplateForAccountOfIncomeType(accountTemplates, budgetAccount.AccountTemplateId, false);
                }
            }

            foreach (var budgetCategory in budget.BudgetCategories)
            {
                foreach (var budgetItem in budgetCategory.BudgetItems)
                {
                    foreach (var budgetActual in budgetItem.BudgetActuals)
                    {
                        if (budgetActual.AccountActuals == null)
                        {
                            budgetActual.AccountActuals = new List <AccountActual>();
                        }

                        // Build out or delete transfer links and the like.
                        // ------------------------------------------------
                        if (budgetActual.IsDeleted)
                        {
                            foreach (var actual in budgetActual.AccountActuals)
                            {
                                actual.IsDeleted = true;
                            }
                        }
                        // New user selected account links are only partially created by the application,
                        // app expects the API to properly manage them from there.
                        else if (budgetActual.IsNew || budgetActual.IsDirty)
                        {
                            if (budgetActual.IsLinked && budgetActual.AccountLinkId.HasValue)
                            {
                                if (accountTemplates == null)
                                {
                                    accountTemplates = AccountTemplateRepo.FindAllTemplatesByOwner(ownerId);
                                }

                                // If we found all the linking pieces then link this actual, if we didn't then delete this actual as otherwise it could cause a failure of the entire budget
                                // to save.
                                if (GetAccountObjectsNeededForLinking(ownerAccounts, accountTemplates, budgetActual.IsCreditWithdrawl,
                                                                      budgetCategory.CategoryTemplate.IsIncomeCategory, budgetActual.AccountLinkId.Value,
                                                                      budgetActual.AccountCategoryLinkId.Value, out AccountCategory ownerAccountCategory,
                                                                      out AccountActualTemplate accountActualTemplate) && budgetActual.AccountActuals.Any(aa => !aa.IsBudgetDefaultLink))
                                {
                                    var linkedAccountActual = budgetActual.AccountActuals.First(aa => !aa.IsBudgetDefaultLink);
                                    linkedAccountActual.CategoryId       = ownerAccountCategory.Id;
                                    linkedAccountActual.Description      = $"{budgetActual.Description} on budget from {budget.Month}/{budget.Year}".VerifySize(256);
                                    linkedAccountActual.Amount           = budgetActual.Amount;
                                    linkedAccountActual.ActualTemplateId = accountActualTemplate.Id;
                                    linkedAccountActual.RelevantOn       = budgetActual.RelevantOn;
                                    linkedAccountActual.IsDirty          = true;
                                }
                                else
                                {
                                    var accountActualTemplateId =
                                        accountActualTemplate == null ? "null" : accountActualTemplate.Id.ToString();
                                    RequestLogger.LogWarn(
                                        "Unable to create linked Account Actual from Linked Budget Actual. Deleting stub Link.",
                                        $"BudgetActual={budgetActual.Id}, Account={budgetActual.AccountLinkId}, AccountCategory={budgetActual.AccountCategoryLinkId}, AccountActualTemplate={accountActualTemplateId}");
                                    budgetActual.AccountActuals.Clear();
                                    budgetActual.AccountLinkId         = null;
                                    budgetActual.AccountCategoryLinkId = null;
                                    budgetActual.IsLinked = false;
                                }
                            }
                            else if (budgetActual.IsLinked && budgetActual.IsNew)
                            {
                                var accountLinkId = budgetActual.AccountLinkId.HasValue
                  ? budgetActual.AccountLinkId.Value.ToString()
                  : "null";
                                var accountCategoryLinkId = budgetActual.AccountCategoryLinkId.HasValue
                  ? budgetActual.AccountCategoryLinkId.Value.ToString()
                  : "null";
                                RequestLogger.LogWarn(
                                    "New Linked Budget Actual not properly linked to Account Actual. Deleting stub Link.",
                                    $"BudgetActual={budgetActual.Id}, Account={accountLinkId}, AccountCategory={accountCategoryLinkId}");
                                budgetActual.AccountActuals.Clear();
                                budgetActual.AccountLinkId         = null;
                                budgetActual.AccountCategoryLinkId = null;
                                budgetActual.IsLinked = false;
                            }
                            // Delete no longer relevant links.
                            else if (budgetActual.AccountActuals.Any(aa => !aa.IsBudgetDefaultLink))
                            {
                                foreach (var accountActual in budgetActual.AccountActuals)
                                {
                                    if (!accountActual.IsBudgetDefaultLink)
                                    {
                                        RequestLogger.LogWarn(
                                            "Cleaning up linked Account Actual. Deleting Link.",
                                            $"BudgetActual={budgetActual.Id}, ActualId={accountActual.Id}, AccountCategory={accountActual.CategoryId}");

                                        accountActual.IsDeleted = true;
                                    }
                                }
                            }
                        }


                        // Build out main account links if not already built and if the budget is linked to an account.
                        // Don't add if this is a credit withdrawl as that is not linked to the main account and is
                        // linked only to the credit account the user setup. The payoff(s) from the virtual category
                        // item(s) is what will be linked to this account.
                        // --------------------------------------------------------------------------------------------
                        if (budgetLinkedAccountDepositActualTemplate != null && budgetLinkedAccountWithdrawlActualTemplate != null)
                        {
                            var defaultAABudgetLink = budgetActual.AccountActuals.FirstOrDefault(aa => aa.IsBudgetDefaultLink);

                            // Since budget may be version 1.X style we may need to build out the default "main account link" actual at any time.
                            if ((budgetActual.IsNew || defaultAABudgetLink == null))
                            {
                                // Only add the default account link when this isn't a credit withdrawl that isn't linked to the main account.
                                if (!budgetActual.IsCreditWithdrawl)
                                {
                                    var linkedAccountActual = new AccountActual()
                                    {
                                        Id                 = Guid.NewGuid(),
                                        CategoryId         = budgetAccountCategory.Id,
                                        BudgetActualLinkId = budgetActual.Id,
                                        IsLinked           = true,
                                        IsNew              = true,
                                        IsDirty            = true,
                                        Description        =
                                            $"{budgetActual.Description} on budget from {budget.Month}/{budget.Year}".VerifySize(256),
                                        Amount           = budgetActual.Amount,
                                        ActualTemplateId = budgetCategory.CategoryTemplate.IsIncomeCategory
                      ? budgetLinkedAccountDepositActualTemplate.Id
                      : budgetLinkedAccountWithdrawlActualTemplate.Id,
                                        RelevantOn          = budgetActual.RelevantOn,
                                        IsBudgetDefaultLink = true,
                                    };
                                    budgetActual.AccountActuals.Add(linkedAccountActual);
                                }
                            }
                            else if (budgetActual.IsDirty)
                            {
                                // Change anything about link that can change when actual is modified.
                                defaultAABudgetLink.CategoryId  = budgetAccountCategory.Id;
                                defaultAABudgetLink.IsDirty     = true;
                                defaultAABudgetLink.Description =
                                    $"{budgetActual.Description} on budget from {budget.Month}/{budget.Year}".VerifySize(256);
                                defaultAABudgetLink.Amount           = budgetActual.Amount;
                                defaultAABudgetLink.ActualTemplateId = budgetCategory.CategoryTemplate.IsIncomeCategory
                  ? budgetLinkedAccountDepositActualTemplate.Id
                  : budgetLinkedAccountWithdrawlActualTemplate.Id;
                                defaultAABudgetLink.RelevantOn = budgetActual.RelevantOn;
                            }
                        }
                    }
                }
            }