Ejemplo n.º 1
0
    protected void ButtonSetBudgets_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem repeaterItem in this.RepeaterAccountBudgets.Items)
        {
            HiddenField      hiddenAccountId = (HiddenField)repeaterItem.FindControl("HiddenAccountId");
            TextBox          textBudget      = (TextBox)repeaterItem.FindControl("TextBudget");
            FinancialAccount account         = FinancialAccount.FromIdentity(Int32.Parse(hiddenAccountId.Value));

            // TODO: Possible race condition here, fix with HiddenField

            Int64 newBudgetAmount = Int64.Parse(textBudget.Text.Replace(",", "").Replace(" ", ""), Thread.CurrentThread.CurrentCulture);  // TODO: May throw -- catch and send error message

            Int64 currentBudgetAmount = account.GetTree().GetBudgetSumCents(_year) / 100;

            // Set the new amount to the difference between the single budget of this account and the intended amount

            if (newBudgetAmount != currentBudgetAmount)
            {
                account.SetBudgetCents(_year,
                                       account.GetBudgetCents(_year) + (newBudgetAmount - currentBudgetAmount) * 100);
            }
        }

        // After updating budgets, rebind repeater

        FinancialAccounts accounts = GetRootLevelResultAccounts();

        UpdateYearlyResult(accounts);

        this.RepeaterAccountBudgets.DataSource = accounts;
        this.RepeaterAccountBudgets.DataBind();
    }
Ejemplo n.º 2
0
        public static ChangeAccountDataResult SetAccountBudget(int accountId, string budget)
        {
            AuthenticationData authData = GetAuthenticationDataAndCulture();
            FinancialAccount   account  = FinancialAccount.FromIdentity(accountId);

            if (!PrepareAccountChange(account, authData, false))
            {
                return(new ChangeAccountDataResult
                {
                    Result = ChangeAccountDataOperationsResult.NoPermission
                });
            }

            Int64 newTreeBudget;

            budget = budget.Replace("%A0", "%20"); // some very weird browser space-to-otherspace translation weirds out number parsing
            budget = HttpContext.Current.Server.UrlDecode(budget);

            if (budget.Trim().Length > 0 && Int64.TryParse(budget, NumberStyles.Currency, CultureInfo.CurrentCulture, out newTreeBudget))
            {
                newTreeBudget *= 100; // convert to cents

                int year = DateTime.Today.Year;
                FinancialAccounts accountTree         = account.GetTree();
                Int64             currentTreeBudget   = accountTree.GetBudgetSumCents(year);
                Int64             currentSingleBudget = account.GetBudgetCents(year);
                Int64             suballocatedBudget  = currentTreeBudget - currentSingleBudget;

                Int64 newSingleBudget = newTreeBudget - suballocatedBudget;

                account.SetBudgetCents(DateTime.Today.Year, newSingleBudget);
                return(new ChangeAccountDataResult
                {
                    Result = ChangeAccountDataOperationsResult.Changed,
                    NewData = (newTreeBudget / 100).ToString("N0", CultureInfo.CurrentCulture)
                });
            }
            else
            {
                return(new ChangeAccountDataResult
                {
                    Result = ChangeAccountDataOperationsResult.Invalid
                });
            }
        }
Ejemplo n.º 3
0
        public static ChangeAccountDataResult SetAccountBudget(int accountId, string budget)
        {
            try
            {
                AuthenticationData authData = GetAuthenticationDataAndCulture();
                FinancialAccount   account  = FinancialAccount.FromIdentity(accountId);

                if (!PrepareAccountChange(account, authData, false))
                {
                    return(new ChangeAccountDataResult
                    {
                        Result = ChangeAccountDataOperationsResult.NoPermission
                    });
                }

                Int64 newTreeBudget;
                budget = budget.Replace("%A0", "%20");
                // some very weird browser space-to-otherspace translation weirds out number parsing
                budget = HttpContext.Current.Server.UrlDecode(budget);

                if (budget.Trim().Length > 0 &&
                    Int64.TryParse(budget, NumberStyles.Currency, CultureInfo.CurrentCulture, out newTreeBudget))
                {
                    newTreeBudget *= 100; // convert to cents

                    int year = DateTime.Today.Year;
                    FinancialAccounts accountTree         = account.ThisAndBelow();
                    Int64             currentTreeBudget   = accountTree.GetBudgetSumCents(year);
                    Int64             currentSingleBudget = account.GetBudgetCents(year);
                    Int64             suballocatedBudget  = currentTreeBudget - currentSingleBudget;

                    Int64 newSingleBudget = newTreeBudget - suballocatedBudget;

                    account.SetBudgetCents(DateTime.Today.Year, newSingleBudget);

                    // Once we've set the budget, also update the "yearly result" budget.
                    // The "yearly result" budget isn't shown in the account plan, but is
                    // abstracted to "projected loss" or "projected gain" pseudobudgets.

                    int thisYear = DateTime.UtcNow.Year;
                    FinancialAccounts allProfitLossAccounts   = FinancialAccounts.ForOrganization(authData.CurrentOrganization);
                    Int64             newProfitLossProjection = allProfitLossAccounts.Where(queryAccount => queryAccount.Identity != authData.CurrentOrganization.FinancialAccounts.CostsYearlyResult.Identity).Sum(queryAccount => queryAccount.GetBudgetCents(thisYear));

                    authData.CurrentOrganization.FinancialAccounts.CostsYearlyResult.SetBudgetCents(thisYear, -newProfitLossProjection);

                    return(new ChangeAccountDataResult
                    {
                        Result = ChangeAccountDataOperationsResult.Changed,
                        NewData = (newTreeBudget / 100).ToString("N0", CultureInfo.CurrentCulture)
                    });
                }

                return(new ChangeAccountDataResult
                {
                    Result = ChangeAccountDataOperationsResult.Invalid
                });
            }
            catch (Exception weirdException)
            {
                // Exceptions are happening here in deployment ONLY. We're logging it to find which one and why.
                // TODO: This really needs to be in Logic. DO NOT DO NOT DO NOT call Database layer directly from Site layer.

                SwarmDb.GetDatabaseForWriting()
                .CreateExceptionLogEntry(DateTime.UtcNow, "AccountPlan-SetBudget", weirdException);

                throw;
            }
        }