diff --git a/Database/Schemata/upgrade-0062.sql b/Database/Schemata/upgrade-0062.sql new file mode 100644 index 00000000..d9cfe0f6 --- /dev/null +++ b/Database/Schemata/upgrade-0062.sql @@ -0,0 +1,86 @@ +ALTER TABLE `OutboundInvoices` +ADD COLUMN `OrganizationSequenceId` INT(11) NOT NULL DEFAULT 0 AFTER `OrganizationId`, +ADD INDEX `Index_SequenceId` (`OrganizationSequenceId` ASC) + +# + +DROP PROCEDURE IF EXISTS `SetOutboundInvoiceOrganizationSequenceId` + +# + +DROP PROCEDURE IF EXISTS `CreateOutboundInvoice` + +# + +CREATE PROCEDURE `CreateOutboundInvoice`( + organizationId INTEGER, + createdDateTime DATETIME, + createdByPersonId INTEGER, + dueDate DATETIME, + budgetId INTEGER, + customerName VARCHAR(128), + invoiceAddressPaper VARCHAR(256), + invoiceAddressMail VARCHAR(128), + currencyId INTEGER, + reference VARCHAR(64), + domestic TINYINT(1), + securityCode VARCHAR(16), + theirReference VARCHAR(64) +) +BEGIN + + DECLARE newIdentity INTEGER; + DECLARE sequenceNumber INTEGER; + + INSERT INTO OutboundInvoices + (CustomerName,InvoiceAddressPaper,InvoiceAddressMail,CurrencyId, + BudgetId,OrganizationId,CreatedDateTime,CreatedByPersonId, + DueDate,ReminderCount,Reference,Open,Domestic,Sent,SecurityCode,TheirReference) + VALUES + (customerName,invoiceAddressPaper,invoiceAddressMail,currencyId, + budgetId,organizationId,createdDateTime,createdByPersonId, + dueDate,0,reference,1,domestic,0,securityCode,theirReference); + + SELECT LAST_INSERT_ID() INTO newIdentity; + + SELECT COUNT(*) FROM OutboundInvoices WHERE + OutboundInvoices.OrganizationId = organizationId AND + OutboundInvoices.InboundInvoiceId <= newIdentity INTO sequenceNumber; + + UPDATE OutboundInvoices + SET OutboundInvoices.OrganizationSequenceId = sequenceNumber + WHERE OutboundInvoices.OutboundInvoiceId = newIdentity; + + SELECT newIdentity AS Identity; + +END + + +# + + +CREATE PROCEDURE `SetOutboundInvoiceOrganizationSequenceId`( + IN outboundInvoiceId INTEGER +) +BEGIN + + DECLARE organizationId INTEGER; + DECLARE sequenceNumber INTEGER; + + SELECT OutboundInvoices.OrganizationId + FROM OutboundInvoices + WHERE OutboundInvoices.OutboundInvoiceId = outboundInvoiceId + INTO organizationId; + + SELECT COUNT(*) + FROM OutboundInvoices + WHERE OutboundInvoices.OrganizationId=organizationId + AND OutboundInvoices.OutboundInvoiceId <= outboundInvoiceId + INTO sequenceNumber; + + UPDATE OutboundInvoices SET OrganizationSequenceId = sequenceNumber + WHERE OutboundInvoices.OutboundInvoiceId = outboundInvoiceId; + + SELECT sequenceNumber AS OrganizationSequenceId; + +END diff --git a/Database/Schemata/upgrade-0063.sql b/Database/Schemata/upgrade-0063.sql new file mode 100644 index 00000000..54256a22 --- /dev/null +++ b/Database/Schemata/upgrade-0063.sql @@ -0,0 +1,98 @@ +ALTER TABLE `HotBitcoinAddresses` +ADD COLUMN `UniqueDerive` INT NOT NULL DEFAULT -1 AFTER `DerivationPath`, +ADD INDEX `Ix_Path` (`DerivationPath` ASC), +ADD INDEX `Ix_Unique` (`UniqueDerive` ASC) + + +# + + +DROP PROCEDURE IF EXISTS `SetHotBitcoinAddressAddress` + + +# + + +DROP PROCEDURE IF EXISTS `SetHotBitcoinAddress` + + +# + + +DROP PROCEDURE IF EXISTS `SetHotBitcoinAddressFallbackAddress` + + +# + + +DROP PROCEDURE IF EXISTS `CreateHotBitcoinAddress` + + +# + + +CREATE PROCEDURE `CreateHotBitcoinAddress`( + IN organizationId INT, + IN bitcoinChainId INT, + IN derivationPath VARCHAR(256) +) +BEGIN + + DECLARE newIdentity INTEGER; + DECLARE sequenceNumber INTEGER; + + INSERT INTO HotBitcoinAddresses + (OrganizationId,BitcoinChainId,DerivationPath,UniqueDerive,AddressString,AddressStringFallback,BalanceSatoshis,ThroughputSatoshis) + VALUES + (organizationId,bitcoinChainId,derivationPath,-1,'--TBD--','',0,0); + + SELECT LAST_INSERT_ID() INTO newIdentity; + + SELECT COUNT(*) FROM HotBitcoinAddresses WHERE + HotBitcoinAddresses.DerivationPath = derivationPath AND + HotBitcoinAddresses.OrganizationId = organizationId AND + HotBitcoinAddresses.HotBitcoinAddressId <= newIdentity INTO sequenceNumber; + + UPDATE HotBitcoinAddresses + SET HotBitcoinAddresses.UniqueDerive = sequenceNumber + WHERE HotBitcoinAddreses.HotBitcoinAddressId = newIdentity; + + SELECT newIdentity AS Identity; + +END + + +# + + +CREATE PROCEDURE `SetHotBitcoinAddressAddress`( + IN hotBitcoinAddressId INT, + IN addressString BIGINT +) +BEGIN + UPDATE HotBitcoinAddresses + SET HotBitcoinAddresses.AddressString=addressString + WHERE HotBitcoinAddresses.HotBitcoinAddressId=hotBitcoinAddressId; + + SELECT ROW_COUNT() AS RowsUpdated; + +END + + +# + + +CREATE PROCEDURE `SetHotBitcoinAddressFallbackAddress`( + IN hotBitcoinAddressId INT, + IN addressStringFallback BIGINT +) +BEGIN + UPDATE HotBitcoinAddresses + SET HotBitcoinAddresses.AddressStringFallback=addressStringFallback + WHERE HotBitcoinAddresses.HotBitcoinAddressId=hotBitcoinAddressId; + + SELECT ROW_COUNT() AS RowsUpdated; + +END + + diff --git a/Site/Pages/v5/Financial/Json-ListInvoicesOutbound.aspx b/Site/Pages/v5/Financial/Json-ListInvoicesOutbound.aspx new file mode 100644 index 00000000..954b8429 --- /dev/null +++ b/Site/Pages/v5/Financial/Json-ListInvoicesOutbound.aspx @@ -0,0 +1 @@ +<%@ Page Language="C#" AutoEventWireup="true" Inherits="Pages_v5_Finance_Json_ListInvoicesOutbound" Codebehind="Json-ListInvoicesOutbound.aspx.cs" %> diff --git a/Site/Pages/v5/Financial/Json-ListInvoicesOutbound.aspx.cs b/Site/Pages/v5/Financial/Json-ListInvoicesOutbound.aspx.cs new file mode 100644 index 00000000..4326591c --- /dev/null +++ b/Site/Pages/v5/Financial/Json-ListInvoicesOutbound.aspx.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Text; +using Swarmops.Common.Enums; +using Swarmops.Common.Interfaces; +using Swarmops.Frontend; +using Swarmops.Logic.Financial; +using Swarmops.Logic.Support; +using Swarmops.Logic.Swarm; + +public partial class Pages_v5_Finance_Json_ListInvoicesOutbound : DataV5Base +{ + private OutboundInvoices _invoices; + + protected void Page_Load (object sender, EventArgs e) + { + // Get invoices and attestation rights + + this._invoices = OutboundInvoices.ForOrganization(this.CurrentOrganization, true); + + _invoices.Sort(SortInvoicesByCreatedDateReverse); + + // Format as JSON and return + + Response.ContentType = "application/json"; + string json = FormatAsJson(); + Response.Output.WriteLine (json); + Response.End(); + } + + private static int SortInvoicesByCreatedDateReverse(OutboundInvoice a, OutboundInvoice b) + { + return DateTime.Compare(b.CreatedDateTime, a.CreatedDateTime); + } + + + private string FormatAsJson() + { + // Fields: item, dueDate, sender, budget, amount, progress, docs, action + + StringBuilder result = new StringBuilder (16384); + + string hasDoxString = + ""; + + result.Append ("{\"rows\":["); + + DateTime dueDateFormatBreakDate = DateTime.Today.AddDays(-240); + + foreach (OutboundInvoice invoice in _invoices) + { + result.Append("{"); + result.AppendFormat( + "\"item\":\"{0}\",\"dueDate\":\"{1}\",\"sender\":\"{2}\",\"budget\":\"{3}\",\"amount\":\"{4}\",\"progress\":\"{5}\"," + + "\"dox\":\"" + (invoice.Documents.Count > 0 ? hasDoxString : " ") + "\"," + + "\"actions\":\"" + + //"" + + //"" + + //"" + + //"" + + //"" + + "\"", + "#" + invoice.Identity.ToString("N0"), + + JsonSanitize(invoice.DueDate.ToString(invoice.DueDate < dueDateFormatBreakDate ? "yyyy-MMM" : "MMM-dd")), + "Supplier", + JsonSanitize(invoice.Budget.Name), + JsonSanitize((invoice.AmountCents/100.0).ToString("N2")), + GetProgressTicks(invoice), + invoice.Identity); // Item #6 is only present in hasDoxString above + result.Append("},"); + } + + if (_invoices.Count > 1) + { + result.Remove(result.Length - 1, 1); // remove last comma + } + + result.Append ("]}"); + + return result.ToString(); + } + + private string _emptyTick = ""; + private string _greenTick = ""; + private string _redCross = ""; + private string _filler = ""; + + + private string GetProgressTicks(OutboundInvoice invoice) + { + StringBuilder ticks = new StringBuilder(512); + + // The first tick is whether the invoice was even received yet, in anticipation of Purchase Orders + // For now, it is always filled + + ticks.Append(_greenTick); + + // For the time being, the other three are either all filled or all empty + + if (invoice.Open) + { + ticks.Append(_emptyTick + _emptyTick + _emptyTick); + } + else + { + ticks.Append(_greenTick + _greenTick + _greenTick); + } + + return ticks.ToString(); + } + + + + +} \ No newline at end of file diff --git a/Site/Pages/v5/Financial/Json-ListInvoicesOutbound.aspx.designer.cs b/Site/Pages/v5/Financial/Json-ListInvoicesOutbound.aspx.designer.cs new file mode 100644 index 00000000..65549842 --- /dev/null +++ b/Site/Pages/v5/Financial/Json-ListInvoicesOutbound.aspx.designer.cs @@ -0,0 +1,13 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + + +public partial class Pages_v5_Finance_Json_ListInvoicesOutbound { +} diff --git a/Site/Pages/v5/Financial/ListInvoicesOutbound.aspx b/Site/Pages/v5/Financial/ListInvoicesOutbound.aspx new file mode 100644 index 00000000..e544cd54 --- /dev/null +++ b/Site/Pages/v5/Financial/ListInvoicesOutbound.aspx @@ -0,0 +1,128 @@ +<%@ Page Title="" Language="C#" MasterPageFile="~/Master-v5.master" AutoEventWireup="true" Inherits="Swarmops.Frontend.Pages.v5.Financial.ListInvoicesOutbound" Codebehind="ListInvoicesOutbound.aspx.cs" %> +<%@ Register src="~/Controls/v5/Base/ModalDialog.ascx" tagname="ModalDialog" tagprefix="Swarmops5" %> +<%@ Register src="~/Controls/v5/Financial/ComboBudgets.ascx" tagname="ComboBudgets" tagprefix="Swarmops5" %> +<%@ Register src="~/Controls/v5/Financial/CurrencyTextBox.ascx" tagname="CurrencyTextBox" tagprefix="Swarmops5" %> + + + + + + + + + + + + +

+ + + + + + + + + + + + + +
+ +
+ + + + + &hq=1" title="<%# Eval("Title") %>" class="FancyBox_Gallery" rel="<%# Eval("BaseId") %>">  + + + +
+
+ + + diff --git a/Site/Pages/v5/Financial/ListInvoicesOutbound.aspx.cs b/Site/Pages/v5/Financial/ListInvoicesOutbound.aspx.cs new file mode 100644 index 00000000..ebc4c739 --- /dev/null +++ b/Site/Pages/v5/Financial/ListInvoicesOutbound.aspx.cs @@ -0,0 +1,228 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Security; +using System.Threading; +using System.Web; +using System.Web.Services; +using System.Web.UI; +using Resources; +using Swarmops.Common.Enums; +using Swarmops.Logic.Financial; +using Swarmops.Logic.Security; +using Swarmops.Logic.Structure; +using Swarmops.Logic.Support; +using Swarmops.Logic.Swarm; + +namespace Swarmops.Frontend.Pages.v5.Financial +{ + public partial class ListInvoicesOutbound : PageV5Base + { + private List _documentList; + + protected void Page_Load (object sender, EventArgs e) + { + if (!CurrentOrganization.IsEconomyEnabled) + { + Response.Redirect ("/Pages/v5/Financial/EconomyNotEnabled.aspx", true); + return; + } + + PageIcon = "iconshock-stamped-paper"; + + PageAccessRequired = new Access (CurrentOrganization, AccessAspect.Participant, AccessType.Read); // No specific access aspect for owning a budget (yet?) + + if (!Page.IsPostBack) + { + Localize(); + } + + // this.TextDenyReason.Style [HtmlTextWriterStyle.FontSize] = "60% !important"; + + this._documentList = new List(); + + PopulateOutboundInvoices(); + + RegisterControl(EasyUIControl.DataGrid); + + this.RepeaterLightboxItems.DataSource = this._documentList; + this.RepeaterLightboxItems.DataBind(); + } + + + static private Dictionary GetAttestationRights() + { + AuthenticationData authData = GetAuthenticationDataAndCulture(); + + // Right now, this function is quite primitive. At some point in the future, it needs to take into + // account that a budget may have several attesters. Right now, it just loops over all accounts and + // checks the owner. + + Dictionary result = new Dictionary(); + FinancialAccounts accounts = FinancialAccounts.ForOrganization(authData.CurrentOrganization); + + foreach (FinancialAccount account in accounts) + { + if (account.OwnerPersonId == authData.CurrentUser.Identity || + (account.OwnerPersonId == 0 && authData.Authority.HasAccess(new Access(authData.CurrentOrganization, AccessAspect.Administration)))) + { + if (account.AccountType == FinancialAccountType.Cost) + { + result[account.Identity] = true; + } + } + } + + return result; + } + + [WebMethod] + public static int[] GetUninitializedBudgets() + { + AuthenticationData authData = GetAuthenticationDataAndCulture(); + + // This function finds the exceptional accounts that aren't initialized, to let a sysadmin approve expenses to them. + + List result = new List(); + FinancialAccounts accounts = FinancialAccounts.ForOrganization(authData.CurrentOrganization); + + foreach (FinancialAccount account in accounts) + { + if (account.AccountType == FinancialAccountType.Cost) + { + if (account.OwnerPersonId == 0 && + authData.Authority.HasAccess (new Access (authData.CurrentOrganization, + AccessAspect.Administration))) + { + result.Add (account.Identity); + } + } + } + + return result.ToArray(); + } + + private void Localize() + { + PageTitle = Resources.Pages.Financial.ListInboundInvoices_Title; + InfoBoxLiteral = Resources.Pages.Financial.ListInboundInvoices_Info; + this.LabelListInboundInvoicesHeader.Text = Resources.Pages.Financial.ListInboundInvoices_Header; + this.LabelGridHeaderItem.Text = Resources.Pages.Financial.AttestCosts_GridHeader_Item; + this.LabelGridHeaderDueDate.Text = Resources.Global.Financial_DueDate; + this.LabelGridHeaderBeneficiary.Text = Resources.Pages.Financial.ListInboundInvoices_Header_Sender; + this.LabelGridHeaderBudget.Text = Resources.Global.Financial_Budget; + this.LabelGridHeaderRequested.Text = Resources.Pages.Financial.AttestCosts_GridHeader_Requested; + this.LabelGridHeaderProgress.Text = Resources.Global.Global_Progress; + this.LabelGridHeaderDocs.Text = Resources.Pages.Financial.AttestCosts_GridHeader_Docs; + this.LabelGridHeaderActions.Text = Resources.Global.Global_Action; + + /* this.LiteralErrorInsufficientBudget.Text = JavascriptEscape (Resources.Pages.Financial.AttestCosts_OutOfBudget); + this.LiteralWarnUnintializedBudget.Text = + JavascriptEscape (Resources.Pages.Financial.AttestCosts_Warn_UninitializedBudget);*/ + /* + this.LabelDescribeDeny.Text = Resources.Pages.Financial.AttestCosts_Modal_DescribeOptionDeny; + this.LabelDescribeCorrect.Text = String.Format (Resources.Pages.Financial.AttestCosts_Modal_DescribeOptionAmount, CurrentOrganization.Currency.DisplayCode); + this.LabelDescribeRebudget.Text = Resources.Pages.Financial.AttestCosts_Modal_DescribeOptionRebudget; + + this.LabelRadioCorrect.Text = Resources.Pages.Financial.AttestCosts_Modal_RadioOptionAmount; + this.LabelRadioDeny.Text = Resources.Pages.Financial.AttestCosts_Modal_RadioOptionDeny; + this.LabelRadioRebudget.Text = Resources.Pages.Financial.AttestCosts_Modal_RadioOptionRebudget;*/ + + /* this.LabelModalDenyHeader.Text = Resources.Pages.Financial.AttestCosts_Modal_Header; + this.LabelWhatProblem.Text = Resources.Pages.Financial.AttestCosts_Modal_WhatIsProblem; + + this.LiteralPleaseSelectBudget.Text = JavascriptEscape (Resources.Pages.Financial.AttestCosts_Error_PleaseSelectBudget); + this.LiteralCannotRebudgetSalary.Text = + JavascriptEscape (Resources.Pages.Financial.AttestCosts_Error_CantRebudgetSalary); + + this.LiteralConfirmOverdraftNo.Text = JavascriptEscape(Resources.Pages.Financial.AttestCosts_OverdraftConfirmNo); + this.LiteralConfirmOverdraftYes.Text = JavascriptEscape(Resources.Pages.Financial.AttestCosts_OverdraftConfirmYes); + this.LiteralConfirmOverdraft.Text = JavascriptEscape(Resources.Pages.Financial.AttestCosts_OverdraftConfirm);*/ + } + + static protected IPayable PayableFromRecordId (string recordId) + { + char recordType = recordId[0]; + int itemId = Int32.Parse(recordId.Substring(1)); + + switch (recordType) + { + case 'E': // Expense claim + return ExpenseClaim.FromIdentity (itemId); + case 'A': // Cash advance + return CashAdvance.FromIdentity(itemId); + case 'I': // Inbound invoice + return InboundInvoice.FromIdentity (itemId); + default: + throw new NotImplementedException("Unknown record type"); + } + } + + + [WebMethod] + public static void RebudgetItem (string recordId, int newAccountId) + { + AuthenticationData authData = GetAuthenticationDataAndCulture(); + + IPayable payable = PayableFromRecordId (recordId); + FinancialAccount newAccount = FinancialAccount.FromIdentity(newAccountId); + + if (payable.Budget.OrganizationId != authData.CurrentOrganization.Identity || + payable.Budget.OwnerPersonId != authData.CurrentUser.Identity || + newAccount.OrganizationId != authData.CurrentOrganization.Identity) + { + throw new UnauthorizedAccessException(); + } + + payable.SetBudget (newAccount, authData.CurrentUser); + } + + + + private void PopulateOutboundInvoices() + { + OutboundInvoices invoices = OutboundInvoices.ForOrganization(CurrentOrganization, true); + + foreach (OutboundInvoice invoice in invoices) + { + Documents dox = invoice.Documents; + bool hasDox = (dox.Count > 0 ? true : false); + + AddDocuments(invoice.Documents, "O" + invoice.Identity.ToString(CultureInfo.InvariantCulture), + String.Format(Global.Financial_OutboundInvoiceSpecificationWithCustomer + " - ", + invoice.OrganizationSequenceId, invoice.CustomerName) + Global.Global_ImageSpecification); + } + } + + + private void AddDocuments(Documents docs, string baseId, string titleBase) + { + int countTotal = docs.Count; + int count = 0; + + foreach (Document document in docs) + { + RepeatedDocument newDoc = new RepeatedDocument + { + DocId = document.Identity, + BaseId = baseId, + Title = String.Format(titleBase, ++count, countTotal) + }; + + this._documentList.Add(newDoc); + } + } + + + + public class RepeatedDocument + { + public int DocId { get; set; } + public string BaseId { get; set; } + public string Title { get; set; } + } + + + } +} \ No newline at end of file diff --git a/Site/Pages/v5/Financial/ListInvoicesOutbound.aspx.designer.cs b/Site/Pages/v5/Financial/ListInvoicesOutbound.aspx.designer.cs new file mode 100644 index 00000000..64851ac4 --- /dev/null +++ b/Site/Pages/v5/Financial/ListInvoicesOutbound.aspx.designer.cs @@ -0,0 +1,105 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Swarmops.Frontend.Pages.v5.Financial { + + + public partial class ListInvoicesOutbound { + + /// + /// LabelListInboundInvoicesHeader control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Label LabelListInboundInvoicesHeader; + + /// + /// LabelGridHeaderItem control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Label LabelGridHeaderItem; + + /// + /// LabelGridHeaderDueDate control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Label LabelGridHeaderDueDate; + + /// + /// LabelGridHeaderBeneficiary control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Label LabelGridHeaderBeneficiary; + + /// + /// LabelGridHeaderBudget control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Label LabelGridHeaderBudget; + + /// + /// LabelGridHeaderRequested control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Label LabelGridHeaderRequested; + + /// + /// LabelGridHeaderProgress control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Label LabelGridHeaderProgress; + + /// + /// LabelGridHeaderDocs control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Label LabelGridHeaderDocs; + + /// + /// LabelGridHeaderActions control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Label LabelGridHeaderActions; + + /// + /// RepeaterLightboxItems control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Repeater RepeaterLightboxItems; + } +}