using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Globalization; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Activizr.Logic.Financial; using Activizr.Logic.Pirates; using Activizr.Logic.Structure; using Telerik.Web.UI; public partial class Pages_v4_Financial_ManageBudget : PageV4Base { protected void Page_Init (object sender, EventArgs e) { // In case we're in a postback, we need to recreate the suballocation controls in order for the viewstate to be recaptured. // But we don't have the viewstate in Init, yet. Still, we must have it here to recapture ViewState. // // The solution is to use hidden fields with the information we need to recreate the controls and then read them directly // from the Request object. string accountIdString = Request["ctl00$BodyContent$HiddenInitBudgetId"]; string yearString = Request["ctl00$BodyContent$HiddenInitYear"]; if (!string.IsNullOrEmpty(accountIdString)) { _account = FinancialAccount.FromIdentity(Int32.Parse(accountIdString)); _year = Int32.Parse(yearString); InitSuballocation(false); } // If these are not present in the Request objects, the suballocation controls will be created in Page_Load without postback values. } private FinancialAccount _account; private int _year; private bool _initializingBudgets; protected void Page_Load(object sender, EventArgs e) { if (ViewState[this.ClientID + "_BudgetId"] != null) { int accountId = Int32.Parse((string)ViewState[this.ClientID + "_BudgetId"]); int year = Int32.Parse((string)ViewState[this.ClientID + "_Year"]); if (_year != 0 && year != _year) { throw new InvalidOperationException("Mismatch between years in hidden fields and viewstate"); } else { _year = year; } if (_account != null) { if (_account.Identity != accountId) { throw new InvalidOperationException("Mismatch between account identities in hidden fields and viewstate"); } } else { _account = FinancialAccount.FromIdentity(accountId); InitSuballocation(false); } } // Reset widths of textboxen for (int index = 1; index <= 12; index++) { TextBox predict = (TextBox) this.Master.FindControl("BodyContent").FindControl("TextPredictMonth" + index.ToString()); predict.Style[HtmlTextWriterStyle.Width] = "55px"; predict.Style[HtmlTextWriterStyle.TextAlign] = "right"; TextBox actual = (TextBox) this.Master.FindControl("BodyContent").FindControl("TextActualsMonth" + index.ToString()); actual.Style[HtmlTextWriterStyle.Width] = "55px"; actual.Style[HtmlTextWriterStyle.TextAlign] = "right"; } // Rebind tooltips' persons and authorities -- these are lost on postback RebindTooltips(); } protected void RebindTooltips() { foreach (RepeaterItem repeaterItem in this.RepeaterBudgetOwners.Items) { HiddenField hiddenAccountId = (HiddenField) repeaterItem.FindControl("HiddenAccountId"); FinancialAccount child = FinancialAccount.FromIdentity(Int32.Parse(hiddenAccountId.Value)); RadToolTip toolTip = (RadToolTip) repeaterItem.FindControl("ToolTip"); Controls_v4_PersonDetailPopup personDetail = (Controls_v4_PersonDetailPopup)toolTip.FindControl("PersonDetail"); FinancialAccount account = FinancialAccount.FromIdentity(Int32.Parse(hiddenAccountId.Value)); personDetail.Authority = _authority; personDetail.Person = account.Owner; personDetail.Account = account; } } protected void Page_PreRender(object sender, EventArgs e) { if (!Page.IsPostBack) { PopulateControls(); } } private void PopulateControls() { FinancialAccounts accounts = FinancialAccounts.ForOwner(_currentUser); this.DropBudgets.Items.Clear(); this.DropYears.Items.Clear(); foreach (FinancialAccount account in accounts) { this.DropBudgets.Items.Add(new ListItem(account.Name + " / " + account.Organization.Name, account.Identity.ToString())); } bool selectedBudget = false; string accountParameterString = Request.QueryString["AccountId"]; if (!String.IsNullOrEmpty(accountParameterString)) { int accountId = Int32.Parse(accountParameterString); if (FinancialAccount.FromIdentity(accountId).OwnerPersonId == _currentUser.Identity) { this.DropBudgets.SelectedValue = accountId.ToString(); } else if (_currentUser.Identity == 1 && System.Diagnostics.Debugger.IsAttached) // DEBUG { FinancialAccount account = FinancialAccount.FromIdentity(accountId); this.DropBudgets.Items.Add(new ListItem(account.Name + " / " + account.Organization.Name, account.Identity.ToString())); this.DropBudgets.SelectedValue = accountId.ToString(); } } ViewState[this.ClientID + "_BudgetId"] = this.DropBudgets.SelectedValue; _account = FinancialAccount.FromIdentity(Int32.Parse(this.DropBudgets.SelectedValue)); this.HiddenInitBudgetId.Value = _account.Identity.ToString(); int yearStart = DateTime.Today.Year - 1; if (DateTime.Today.Month <= 6) { yearStart--; } for (int yearIndex = 0; yearIndex < 3; yearIndex++) { this.DropYears.Items.Add((yearStart+yearIndex).ToString()); } this.DropYears.SelectedValue = DateTime.Today.Year.ToString(); _year = DateTime.Today.Year; ViewState[this.ClientID + "_Year"] = this.DropYears.SelectedValue; this.HiddenInitYear.Value = this.DropYears.SelectedValue; PopulateBudgetData(); } private void PopulateBudgetData() { Ledger.Accounts = FinancialAccounts.FromSingle(_account); int year = Int32.Parse(this.DropYears.SelectedValue); Ledger.DateStart = new DateTime(year, 1, 1); Ledger.DateEnd = new DateTime(year, 12, 31); Ledger.MaxAmount = 1.0e12m; double budget = _account.GetBudget(year); Ledger.Populate(); // Populate predictions int monthIterator = 1; Int64[] monthlyPredicts = _account.GetBudgetMonthly(year); while (monthIterator <= 12) { TextBox box = (TextBox) this.Master.FindControl("BodyContent").FindControl("TextPredictMonth" + monthIterator.ToString()); if (monthlyPredicts.Length == 12) { box.Text = (monthlyPredicts[monthIterator - 1] / 100).ToString("N0", new CultureInfo("sv-SE")); } else { box.Text = string.Empty; } monthIterator++; } // Populate actuals monthIterator = 1; while (monthIterator <= 12 && new DateTime (year,monthIterator,1).AddMonths(1) < DateTime.Today) { DateTime start = new DateTime(year, monthIterator, 1); Int64 deltaCents = _account.GetDeltaCents(start, start.AddMonths(1)); decimal deltaDecimal = -deltaCents/100.0m; TextBox actual = (TextBox)this.Master.FindControl("BodyContent").FindControl("TextActualsMonth" + monthIterator.ToString()); actual.Text = deltaDecimal.ToString("N0", new CultureInfo(_account.Organization.DefaultCountry.Culture)); monthIterator++; } // Clear the non-set actuals fields while (monthIterator <= 12) { TextBox actual = (TextBox)this.Master.FindControl("BodyContent").FindControl("TextActualsMonth" + monthIterator.ToString()); actual.Text = string.Empty; monthIterator++; } InitSuballocation(true); ScriptManager.RegisterStartupScript(this, Page.GetType(), "set_budget_iframe", "var currWidth = document.getElementById('DivIframeContainer').offsetWidth; document.getElementById('DivIframeContainer').innerHTML='