More work on Account Plan

This commit is contained in:
Rick Falkvinge
2014-09-13 12:20:04 +02:00
parent 40f5768b68
commit 60fac749f0
4 changed files with 112 additions and 4 deletions
+45
View File
@@ -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<int> transactionIds = new List<int>();
+37
View File
@@ -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
+1
View File
@@ -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);
}
+29 -4
View File
@@ -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;
}