From 60fac749f034ba4a7efa0463a619a5eff74fe495 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 13 Sep 2014 12:20:04 +0200 Subject: [PATCH] More work on Account Plan --- Database/Database-Financials.cs | 45 ++++++++++++++++++++++ Logic/Financial/FinancialAccount.cs | 37 ++++++++++++++++++ Site5/Pages/v5/Ledgers/AccountPlan.aspx | 1 + Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs | 33 ++++++++++++++-- 4 files changed, 112 insertions(+), 4 deletions(-) diff --git a/Database/Database-Financials.cs b/Database/Database-Financials.cs index c697aaa3..918c6f1c 100644 --- a/Database/Database-Financials.cs +++ b/Database/Database-Financials.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Data; using System.Data.Common; +using System.Globalization; using Swarmops.Basic.Enums; using Swarmops.Basic.Interfaces; using Swarmops.Basic.Types; @@ -662,6 +663,22 @@ namespace Swarmops.Database } + public void SetFinancialAccountOpenedYear(int financialAccountId, int openedYear) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetFinancialAccountOpenedYear", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "financialAccountId", financialAccountId); + AddParameterWithName(command, "openedYear", openedYear); + + command.ExecuteNonQuery(); + } + } + public void SetFinancialAccountOwner(int financialAccountId, int ownerPersonId) { using (DbConnection connection = GetMySqlDbConnection()) @@ -883,6 +900,34 @@ namespace Swarmops.Database } + + public DateTime GetFinancialAccountFirstTransactionDate(int financialAccountId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "SELECT FinancialTransactions.DateTime FROM FinancialTransactions,FinancialTransactionRows " + + "WHERE FinancialTransactions.FinancialTransactionId=FinancialTransactionRows.FinancialTransactionId " + + "AND FinancialTransactionRows.FinancialAccountId = " + financialAccountId.ToString(CultureInfo.InvariantCulture) + " " + + "ORDER BY FinancialTransactions.DateTime LIMIT 1;", connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + return reader.GetDateTime(0); + } + else + { + throw new Exception("No transactions for this account yet"); // need better type here than "Exception" + }; + } + } + } + public BasicFinancialTransaction[] GetDependentFinancialTransactions(FinancialDependencyType dependencyType, int foreignId) { List transactionIds = new List(); diff --git a/Logic/Financial/FinancialAccount.cs b/Logic/Financial/FinancialAccount.cs index 6aedc061..1cdb9426 100644 --- a/Logic/Financial/FinancialAccount.cs +++ b/Logic/Financial/FinancialAccount.cs @@ -201,6 +201,43 @@ namespace Swarmops.Logic.Financial } + public new int OpenedYear + { + get + { + int testYear = base.OpenedYear; + if (testYear < 1900) // not initialized + { + DateTime firstTransactionDate = DateTime.MinValue; + try + { + firstTransactionDate = + SwarmDb.GetDatabaseForReading().GetFinancialAccountFirstTransactionDate(this.Identity); + } + catch (Exception) + { + // ignore + } + + if (firstTransactionDate.Year > 1900) + { + SwarmDb.GetDatabaseForWriting().SetFinancialAccountOpenedYear(this.Identity, firstTransactionDate.Year); + base.OpenedYear = firstTransactionDate.Year; // TODO: Adapt for non-calendar fiscal years as future expansion? + } + } + + if (base.OpenedYear > 1900) + { + return base.OpenedYear; + } + else + { + throw new ArgumentException("FinancialAccount is not opened yet (no transactions)"); + } + } + } + + #region ITreeNode Members diff --git a/Site5/Pages/v5/Ledgers/AccountPlan.aspx b/Site5/Pages/v5/Ledgers/AccountPlan.aspx index d120d477..126e90c7 100644 --- a/Site5/Pages/v5/Ledgers/AccountPlan.aspx +++ b/Site5/Pages/v5/Ledgers/AccountPlan.aspx @@ -140,6 +140,7 @@ } else { $('#TextAccountName').css('background-color', '#FFA0A0'); $('#TextAccountName').val(modalAccountName); + alertify.error("Cannot change accounts that have transactions in closed books - yet."); } $('#TextAccountName').animate({ backgroundColor: "#FFFFFF" }, 250); } diff --git a/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs b/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs index 067031ff..e867f1d9 100644 --- a/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs +++ b/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs @@ -72,6 +72,33 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers return result; } + + private static bool PrepareAccountChange(FinancialAccount account, AuthenticationData authData) + { + // TODO: Check permissions, too (may be read-only) + + if (account.OrganizationId != authData.CurrentOrganization.Identity) + { + throw new UnauthorizedAccessException("A million nopes"); + } + + try + { + if (account.OpenedYear <= authData.CurrentOrganization.Parameters.FiscalBooksClosedUntilYear) + { + // This require breaking the account, which we can't do yet (in this sprint, will come next sprint). + return false; + } + } + catch (Exception) + { + // OpenedYear throws because there isn't a transaction. That's fine. + } + + return true; + } + + [WebMethod] public static bool SetAccountName(int accountId, string name) { @@ -79,13 +106,11 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers FinancialAccount account = FinancialAccount.FromIdentity(accountId); - if (account.OrganizationId != authData.CurrentOrganization.Identity) + if (!PrepareAccountChange(account, authData)) { - throw new UnauthorizedAccessException("A million nopes"); + return false; } - // TODO - CRITICAL: CHECK ACCOUNTOPENEDYEAR - account.Name = HttpContext.Current.Server.UrlDecode(name); return true; }