public static BudgetRemainder[] GetRemainingBudgets()
        {
            Dictionary <int, bool> accountLookup = GetAttestationRights();

            if (accountLookup.Count == 0)
            {
                return(new BudgetRemainder[0]);
            }

            Organization organization = FinancialAccount.FromIdentity(accountLookup.Keys.First()).Organization;

            List <BudgetRemainder> result = new List <BudgetRemainder>();
            int currentYear = DateTime.UtcNow.Year;

            foreach (int accountId in accountLookup.Keys)
            {
                FinancialAccount account   = FinancialAccount.FromIdentity(accountId);
                Int64            remaining = account.GetBudgetCentsRemaining(currentYear);

                result.Add(new BudgetRemainder {
                    AccountId = accountId, Remaining = remaining / 100.0
                });
            }

            return(result.ToArray());
        }
        public static AjaxCallResult AttestCorrectedItem(string recordId, string amountString)
        {
            AuthenticationData authData = GetAuthenticationDataAndCulture();
            Int64 amountCents           = 0;

            try
            {
                amountCents = Formatting.ParseDoubleStringAsCents(amountString);
            }
            catch (Exception)
            {
                return(new AjaxCallResult
                {
                    Success = false,
                    DisplayMessage = String.Format(Resources.Global.Error_CurrencyParsing, 1000.00)
                });
            }

            if (amountCents < 0)
            {
                return(new AjaxCallResult
                {
                    Success = false,
                    DisplayMessage = Resources.Pages.Financial.AttestCosts_CannotAttestNegative
                });
            }

            if (amountCents == 0)
            {
                return(new AjaxCallResult
                {
                    Success = false,
                    DisplayMessage = Resources.Pages.Financial.AttestCosts_CannotAttestZero
                });
            }

            IPayable         payable = PayableFromRecordId(recordId);
            FinancialAccount budget  = payable.Budget;

            if (budget.OrganizationId != authData.CurrentOrganization.Identity ||
                budget.OwnerPersonId != authData.CurrentUser.Identity)
            {
                throw new UnauthorizedAccessException();
            }

            Int64 centsRemaining = budget.GetBudgetCentsRemaining();

            if (centsRemaining < amountCents)
            {
                // TODO: Handle the special case where the IPayable is not on current year, so against another (last) year's budget

                string notEnoughFunds;

                if (centsRemaining > 0)
                {
                    notEnoughFunds = String.Format(Resources.Pages.Financial.AttestCosts_OutOfBudgetPrecise,
                                                   authData.CurrentOrganization.Currency.DisplayCode, centsRemaining / 100.0, DateTime.UtcNow.Year);
                }
                else
                {
                    notEnoughFunds = String.Format(Resources.Pages.Financial.AttestCosts_BudgetIsEmpty,
                                                   DateTime.UtcNow.Year);
                }

                return(new AjaxCallResult
                {
                    Success = false,
                    DisplayMessage = notEnoughFunds
                });
            }

            payable.SetAmountCents(amountCents, authData.CurrentUser);
            payable.Approve(authData.CurrentUser);

            return(new AjaxCallResult
            {
                Success = true
            });
        }