diff --git a/Site5/App_GlobalResources/Pages.Ledgers.resx b/Site5/App_GlobalResources/Pages.Ledgers.resx index 9e1def59..6ea91c46 100644 --- a/Site5/App_GlobalResources/Pages.Ledgers.resx +++ b/Site5/App_GlobalResources/Pages.Ledgers.resx @@ -145,7 +145,7 @@ The XXXX is replaced with year number. - The Balance Sheet is an overview of assets and debts at a snapshot in time, and is used to see how results affect the overall financial health of the organization. You may also want to look at the <a href="ProfitLossStatement.aspx">profit-and-loss statement</a> which is the overview of income and expenditure. + The Balance Sheet is an overview of assets and debts at a snapshot in time, and is used to see how results affect the overall financial health of the organization. You may also want to look at the <a href="ProfitLossStatement">profit-and-loss statement</a> which is the overview of income and expenditure. Balance Sheet @@ -178,7 +178,7 @@ Must ONLY contain A-Z, a-z characters - nothing accented! - The P&L is an overview of income and expenditures, which are grouped by budgets or cost/income type. The sum of all income and expenditure is the yearly result. You may also want to look at the <a href="BalanceSheet.aspx">balance sheet</a> which is the overview of assets and debts. + The P&L is an overview of income and expenditures, which are grouped by budgets or cost/income type. The sum of all income and expenditure is the yearly result. You may also want to look at the <a href="BalanceSheet">balance sheet</a> which is the overview of assets and debts. Last Year diff --git a/Site5/Images/Icons/iconshock-balloon-yes-16px-disabled-hot.png b/Site5/Images/Icons/iconshock-balloon-yes-16px-disabled-hot.png new file mode 100644 index 00000000..f7c275c8 Binary files /dev/null and b/Site5/Images/Icons/iconshock-balloon-yes-16px-disabled-hot.png differ diff --git a/Site5/Pages/v5/Financial/AttestCosts.aspx b/Site5/Pages/v5/Financial/AttestCosts.aspx index 01a826bf..f4bc6ea4 100644 --- a/Site5/Pages/v5/Financial/AttestCosts.aspx +++ b/Site5/Pages/v5/Financial/AttestCosts.aspx @@ -1,5 +1,5 @@ <%@ Page Title="" Language="C#" MasterPageFile="~/Master-v5.master" AutoEventWireup="true" CodeFile="AttestCosts.aspx.cs" Inherits="Swarmops.Frontend.Pages.v5.Financial.AttestCosts" %> -<%@ Register src="~/Controls/v5/UI/ExternalScripts.ascx" tagname="ExternalScripts" tagprefix="Swarmops5" %> +<%@ Register src="~/Controls/v5/Base/ModalDialog.ascx" tagname="ModalDialog" tagprefix="Swarmops5" %> @@ -18,6 +18,7 @@ '/Images/Abstract/ajaxloader-medium.gif', '/Images/Icons/iconshock-balloon-yes-16px-hot.png', '/Images/Icons/iconshock-balloon-yes-16px-disabled.png', + '/Images/Icons/iconshock-balloon-yes-16px-disabled-hot.png', '/Images/Icons/iconshock-balloon-no-16px-hot.png', '/Images/Icons/iconshock-greentick-16px.png', '/Images/Icons/iconshock-redcross-16px.png', @@ -27,80 +28,135 @@ $(document).ready(function () { $('#TableAttestableCosts').datagrid( { + rowStyler: function (index, rowData) { + if (rowData.itemId != null) { + return { class: "row" + rowData.itemId.replace(/\|/g, '') }; + } + + return ''; + }, + onLoadSuccess: function () { - $(".LocalIconApproval").attr("src", "/Images/Icons/iconshock-balloon-yes-16px.png"); + $(".LocalIconApproval").attr("src", "/Images/Icons/iconshock-balloon-yes-16px-disabled.png"); // initialize as disabled until budgets known $(".LocalIconApproved").attr("src", "/Images/Icons/iconshock-greentick-16px.png"); - $(".LocalIconApproved, .LocalIconDenied").css("display", "none"); + $(".LocalIconUndo").attr("src", "/Images/Icons/iconshock-balloon-undo-16px.png"); + $(".LocalIconApproved, .LocalIconDenied, .LocalIconUndo").css("display", "none"); $(".LocalIconDenial").attr("src", "/Images/Icons/iconshock-balloon-no-16px.png"); - $(".LocalIconApproval, .LocalIconApproved, .LocalIconDenial").css("cursor", "pointer"); + $(".LocalIconApproval, .LocalIconUndo, .LocalIconDenial").css("cursor", "pointer"); $(".LocalIconApproval").mouseover(function () { if ($(this).attr("rel") != "loading") { - $(this).attr("src", "/Images/Icons/iconshock-balloon-yes-16px-hot.png"); + if ($(this).hasClass("LocalFundsInsufficient")) { + $(this).attr("src", "/Images/Icons/iconshock-balloon-yes-16px-disabled-hot.png"); + } else { + $(this).attr("src", "/Images/Icons/iconshock-balloon-yes-16px-hot.png"); + } } }); $(".LocalIconApproval").mouseout(function () { if ($(this).attr("rel") != "loading") { - $(this).attr("src", "/Images/Icons/iconshock-balloon-yes-16px.png"); + if ($(this).hasClass("LocalFundsInsufficient")) { + $(this).attr("src", "/Images/Icons/iconshock-balloon-yes-16px-disabled.png"); + } else { + $(this).attr("src", "/Images/Icons/iconshock-balloon-yes-16px.png"); + } } }); - $(".LocalIconApproved").mouseover(function () { + $(".LocalIconUndo").mouseover(function () { if ($(this).attr("rel") != "loading") { - $(this).attr("src", "/Images/Icons/undo-16px.png"); + $(this).attr("src", "/Images/Icons/iconshock-balloon-undo-16px-hot.png"); } }); - $(".LocalIconApproved").mouseout(function () { + $(".LocalIconUndo").mouseout(function () { if ($(this).attr("rel") != "loading") { - $(this).attr("src", "/Images/Icons/iconshock-greentick-16px.png"); + $(this).attr("src", "/Images/Icons/iconshock-balloon-undo-16px.png"); } }); $(".LocalIconApproval").click(function () { + if ($(this).hasClass("LocalFundsInsufficient")) { + alertify.error (decodeURIComponent('')); + return; + } + if ($(this).attr("rel") != "loading") { $(this).attr("rel", "loading"); $(this).attr("src", "/Images/Abstract/ajaxloader-medium.gif"); $("#IconDenial" + $(this).attr("baseid")).fadeTo(1000, 0.01).css("cursor", "default"); - var thisIcon = this; $.ajax({ type: "POST", url: "/Pages/v5/Financial/AttestCosts.aspx/Attest", data: "{'identifier': '" + escape($(this).attr("baseid")) + "'}", contentType: "application/json; charset=utf-8", dataType: "json", - success: function (msg) { - $(thisIcon).css("display", "none"); - $(thisIcon).attr("src", "/Images/Icons/iconshock-balloon-yes-16px.png"); - $(thisIcon).attr("rel", "active"); - $("#IconApproved" + $(thisIcon).attr("baseid")).fadeIn(100); - alertify.success(unescape(msg.d)); - } + success: $.proxy(function (msg) { + var baseid = $(this).attr("baseid"); + if (msg.d.Success) { + $(this).attr("src", "/Images/Icons/iconshock-balloon-yes-16px.png"); + $(this).attr("rel", "active"); + $(this).hide(); + $("#IconApproved" + baseid).fadeIn(100); + $("#IconDenial" + baseid).css('opacity', 1.0).hide(); + $("#IconUndo" + baseid).fadeIn(100); + $('.row' + baseid).animate({ color: "#888" }, 400); + alertify.success(msg.d.DisplayMessage); + + var accountId = $(this).attr("accountid"); + var funds = parseFloat($(this).attr("amount")); + budgetRemainingLookup[accountId] -= funds; + setAttestability(); + recheckBudgets(); // will double-check budgets against server + } else { + // failure, likely from attesting too quickly and overrunning budget + $(this).attr("rel", ""); + $(this).attr("src", "/Images/Icons/iconshock-balloon-yes-16px-disabled.png"); + $("#IconDenial" + baseid).css('opacity', 1.0).css("cursor", "pointer"); + alertify.error(msg.d.DisplayMessage); + + recheckBudgets(); + } + }, this) }); } }); - $(".LocalIconApproved").click(function () { + $(".LocalIconUndo").click(function () { if ($(this).attr("rel") != "loading") { $(this).attr("rel", "loading"); $(this).attr("src", "/Images/Abstract/ajaxloader-medium.gif"); - var thisIcon = this; + $("#IconApproved" + $(this).attr("baseid")).fadeTo(1000, 0.01); $.ajax({ type: "POST", url: "/Pages/v5/Financial/AttestCosts.aspx/Deattest", data: "{'identifier': '" + escape($(this).attr("baseid")) + "'}", contentType: "application/json; charset=utf-8", dataType: "json", - success: function (msg) { - $(thisIcon).css("display", "none"); - $(thisIcon).attr("src", "/Images/Icons/iconshock-greentick-16px.png"); - $(thisIcon).attr("rel", ""); - $("#IconApproval" + $(thisIcon).attr("baseid")).fadeIn(100); - $("#" + $(thisIcon).attr("rel"), ""); - alertify.log(unescape(msg.d).replace('+', ' ')); - $("#IconDenial" + $(thisIcon).attr("baseid")).fadeTo(100, 1).css("cursor", "pointer"); - } + success: $.proxy(function (msg) { + if (msg.d.Success) { + var baseid = $(this).attr("baseid"); + $(this).attr("src", "/Images/Icons/iconshock-balloon-undo-16px.png"); + $(this).attr("rel", ""); + $(this).hide(); + $("#IconApproved" + baseid).css('opacity', 1.0).hide(); + $("#IconApproval" + baseid).fadeIn(100); + $("#IconDenial" + baseid).fadeIn(100).css("cursor", "pointer"); + $('.row' + baseid).animate({ color: "#000" }, 100); + alertify.log(msg.d.DisplayMessage); + + var accountId = $("#IconApproval" + baseid).attr("accountid"); + var funds = parseFloat($("#IconApproval" + baseid).attr("amount")); + budgetRemainingLookup[accountId] += funds; + setAttestability(); + recheckBudgets(); // will double-check budgets against server + } else { + $(this).attr("src", "/Images/Icons/iconshock-greentick-16px.png"); + alertify.error(msg.d.DisplayMessage); + // TODO: Add alert box? + } + }, this) }); } @@ -137,6 +193,15 @@ 'type': 'image', 'opacity': true }); + + + // Check if budgets have been fetched, and if so, initialize attestability + + if (budgetRemainingLookup.budgetsLoaded == true) { + setAttestability(); + } + + budgetRemainingLookup.rowsLoaded = true; } } ); @@ -147,9 +212,52 @@ data.forEach(function(accountData, dummy1, dummy2) { budgetRemainingLookup[accountData.AccountId] = accountData.Remaining; }); + + if (budgetRemainingLookup.rowsLoaded == true) { + setAttestability(); + } + + budgetRemainingLookup.budgetsLoaded = true; }); }); + + function recheckBudgets() { + SwarmopsJS.ajaxCall("/Pages/v5/Financial/AttestCosts.aspx/GetRemainingBudgets", {}, function(data) { + data.forEach(function(accountData, dummy1, dummy2) { + budgetRemainingLookup[accountData.AccountId] = accountData.Remaining; + }); + + setAttestability(); + }); + } + + + function setAttestability() { + + $('.LocalIconApproval').each(function() { + var accountId = $(this).attr('accountid'); + var amountRequested = $(this).attr('amount'); + + var fundsInBudget = budgetRemainingLookup[accountId]; + if (fundsInBudget >= amountRequested) { + $(this).removeClass("LocalFundsInsufficient"); + if ($(this).attr("rel") != "loading") { + $(this).attr("src", "/Images/Icons/iconshock-balloon-yes-16px.png"); + } + } + else if (!$(this).hasClass("LocalFundsInsufficient")) { + $(this).addClass("LocalFundsInsufficient"); + if ($(this).attr("rel") != "loading") { + $(this).attr("src", "/Images/Icons/iconshock-balloon-yes-16px-disabled.png"); + } + } + + }); + + budgetRemainingLookup.attestabilityInitialized = true; + } + var budgetRemainingLookup = {}; @@ -178,6 +286,12 @@ + + + + + +
diff --git a/Site5/Pages/v5/Financial/AttestCosts.aspx.cs b/Site5/Pages/v5/Financial/AttestCosts.aspx.cs index d919a6f6..c8f1be3c 100644 --- a/Site5/Pages/v5/Financial/AttestCosts.aspx.cs +++ b/Site5/Pages/v5/Financial/AttestCosts.aspx.cs @@ -1,12 +1,14 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Linq; using System.ServiceModel.Security; using System.Web; using System.Web.Services; using Resources; using Swarmops.Logic.Financial; using Swarmops.Logic.Security; +using Swarmops.Logic.Structure; using Swarmops.Logic.Support; using Swarmops.Logic.Swarm; @@ -61,7 +63,8 @@ namespace Swarmops.Frontend.Pages.v5.Financial foreach (FinancialAccount account in accounts) { - if (account.OwnerPersonId == authData.CurrentUser.Identity) + if (account.OwnerPersonId == authData.CurrentUser.Identity || + (account.OwnerPersonId == 0 && authData.CurrentUser.HasAccess (new Access (authData.CurrentOrganization, AccessAspect.Administration)))) { result[account.Identity] = true; } @@ -82,23 +85,124 @@ namespace Swarmops.Frontend.Pages.v5.Financial this.LabelGridHeaderDocs.Text = Resources.Pages.Financial.AttestCosts_GridHeader_Docs; this.LabelGridHeaderItem.Text = Resources.Pages.Financial.AttestCosts_GridHeader_Item; this.LabelGridHeaderRequested.Text = Resources.Pages.Financial.AttestCosts_GridHeader_Requested; + + this.LiteralErrorInsufficientBudget.Text = + System.Uri.EscapeDataString (Resources.Pages.Financial.AttestCosts_OutOfBudget); } [WebMethod] public static BudgetRemainder[] GetRemainingBudgets() { Dictionary accountLookup = GetAttestationRights(); + + if (accountLookup.Count == 0) + { + return new BudgetRemainder[0]; + } + + Organization organization = FinancialAccount.FromIdentity (accountLookup.Keys.First()).Organization; + List result = new List(); int currentYear = DateTime.UtcNow.Year; + Dictionary budgetAdjustments = GetAccountingAdjustments (organization); foreach (int accountId in accountLookup.Keys) { - result.Add (new BudgetRemainder { AccountId = accountId, Remaining = FinancialAccount.FromIdentity (accountId).GetBudgetCents (currentYear)/100.0 }); + FinancialAccount account = FinancialAccount.FromIdentity (accountId); + Int64 remaining = GetBudgetRemaining (account, currentYear); + + if (budgetAdjustments.ContainsKey (accountId)) + { + remaining += budgetAdjustments[accountId]; + } + + result.Add (new BudgetRemainder { AccountId = accountId, Remaining = remaining/100.0 }); } return result.ToArray(); } + private static Dictionary GetAccountingAdjustments (Organization organization) + { + // This function returns a dictionary for the cents that are either accounted for but not attested, + // or attested but accounted for, to be used to understand how much is really left in budget + + // Positive adjustment means more [cost] budget available, negative less [cost] budget available + + Dictionary result = new Dictionary(); + + // Cash advances are accounted for when paid out. Make sure they count toward the budget when attested. + + CashAdvances advances = CashAdvances.ForOrganization (organization); + foreach (CashAdvance advance in advances) + { + if (!result.ContainsKey (advance.BudgetId)) + { + result[advance.BudgetId] = 0; + } + + if (advance.Attested) + { + result[advance.BudgetId] -= advance.AmountCents; + } + } + + // Expense claims, Inbound invoices, and Salaries are accounted for when filed. Make sure they DON'T + // count toward the budget while they are NOT attested. + + ExpenseClaims claims = ExpenseClaims.ForOrganization (organization); // gets all open claims + foreach (ExpenseClaim claim in claims) + { + if (!result.ContainsKey(claim.BudgetId)) + { + result[claim.BudgetId] = 0; + } + + if (!claim.Attested) + { + result[claim.BudgetId] += claim.AmountCents; + } + } + + InboundInvoices invoices = InboundInvoices.ForOrganization (organization); + foreach (InboundInvoice invoice in invoices) + { + if (!result.ContainsKey(invoice.BudgetId)) + { + result[invoice.BudgetId] = 0; + } + + if (!invoice.Attested) + { + result[invoice.BudgetId] += invoice.AmountCents; + } + } + + Salaries salaries = Salaries.ForOrganization (organization); + foreach (Salary salary in salaries) + { + if (!result.ContainsKey(salary.PayrollItem.BudgetId)) + { + result[salary.PayrollItem.BudgetId] = 0; + } + + if (!salary.Attested) + { + result[salary.PayrollItem.BudgetId] += (salary.GrossSalaryCents + salary.AdditiveTaxCents); + } + } + + return result; + } + + private static Int64 GetBudgetRemaining (FinancialAccount account, int year) + { + Int64 deltaCentsYear = account.GetDeltaCents(new DateTime(year, 1, 1), + new DateTime(year + 1, 1, 1)); + Int64 budgetYear = account.GetBudgetCents(year); + return -(budgetYear - deltaCentsYear); + } + public class BudgetRemainder { public int AccountId { get; set; } @@ -108,27 +212,23 @@ namespace Swarmops.Frontend.Pages.v5.Financial [WebMethod] - public static string Attest (string identifier) + public static AttestOperationResult Attest (string identifier) { identifier = HttpUtility.UrlDecode (identifier); - string result = HandleAttestationDeattestation (identifier, AttestationMode.Attestation); - - return HttpUtility.UrlEncode (result).Replace ("+", "%20"); + return HandleAttestationDeattestation (identifier, AttestationMode.Attestation); } [WebMethod] - public static string Deattest (string identifier) + public static AttestOperationResult Deattest (string identifier) { identifier = HttpUtility.UrlDecode (identifier); - string result = HandleAttestationDeattestation (identifier, AttestationMode.Deattestation); - - return HttpUtility.UrlEncode (result).Replace ("+", "%20"); + return HandleAttestationDeattestation (identifier, AttestationMode.Deattestation); } - private static string HandleAttestationDeattestation (string identifier, AttestationMode mode) + private static AttestOperationResult HandleAttestationDeattestation (string identifier, AttestationMode mode) { AuthenticationData authData = GetAuthenticationDataAndCulture(); @@ -250,6 +350,23 @@ namespace Swarmops.Frontend.Pages.v5.Financial if (mode == AttestationMode.Attestation) { + Int64 budgetRemaining = GetBudgetRemaining (attestableItem.Budget, DateTime.UtcNow.Year); + Dictionary budgetAdjustments = GetAccountingAdjustments (authData.CurrentOrganization); + + if (budgetAdjustments.ContainsKey (attestableItem.Budget.Identity)) + { + budgetRemaining += budgetAdjustments[attestableItem.Budget.Identity]; + } + + if (amountCents > budgetRemaining) + { + return new AttestOperationResult + { + DisplayMessage = Resources.Pages.Financial.AttestCosts_OutOfBudget, + Success = false + }; + } + attestableItem.Attest (authData.CurrentUser); result = string.Format (attestedTemplate, itemId, beneficiary, authData.CurrentOrganization.Currency.Code, @@ -267,7 +384,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial throw new InvalidOperationException ("Unknown Attestation Mode: " + mode); } - return result; + return new AttestOperationResult {DisplayMessage = result, Success = true}; } @@ -341,5 +458,12 @@ namespace Swarmops.Frontend.Pages.v5.Financial public string BaseId { get; set; } public string Title { get; set; } } + + public struct AttestOperationResult + { + public string DisplayMessage; + public bool Success; + } + } } \ No newline at end of file diff --git a/Site5/Pages/v5/Financial/Json-AttestableCosts.aspx.cs b/Site5/Pages/v5/Financial/Json-AttestableCosts.aspx.cs index 9c1f39d1..bf347682 100644 --- a/Site5/Pages/v5/Financial/Json-AttestableCosts.aspx.cs +++ b/Site5/Pages/v5/Financial/Json-AttestableCosts.aspx.cs @@ -49,14 +49,15 @@ public partial class Pages_v5_Finance_Json_AttestableCosts : DataV5Base "\"item\":\"{0}\",\"beneficiary\":\"{1}\",\"description\":\"{2}\",\"budgetName\":\"{3}\",\"amountRequested\":\"{4:N2}\",\"itemId\":\"{5}\"," + "\"dox\":\"" + (item.HasDox ? hasDoxString : " ") + "\"," + "\"actions\":\"" + - "" + + "" + "  " + "" + - "\"", - JsonSanitize (GetGlobalResourceObject ("Global", item.IdentityDisplay).ToString()), + "" + + "\"", + JsonSanitize(GetGlobalResourceObject("Global", item.IdentityDisplay).ToString()), JsonSanitize (item.Beneficiary), JsonSanitize (TryLocalize (item.Description)), JsonSanitize (item.BudgetName), - item.AmountRequestedCents/100.0, item.Identity); + item.AmountRequestedCents/100.0, item.Identity, item.Budget.Identity); result.Append ("},"); }