Parsers working; all required left is to actually resync

This commit is contained in:
Rick Falkvinge
2014-05-03 21:03:23 +00:00
parent b699d5a726
commit 5d020ce25d
11 changed files with 235 additions and 50 deletions
@@ -108,6 +108,10 @@ namespace Swarmops.Basic.Enums
/// </summary>
CostsCurrencyFluctuations,
/// <summary>
/// Various bookkeeping errors
/// </summary>
CostsMiscalculations,
/// <summary>
/// When closing books, this is the yearly result (balanced against DebtsEquity)
/// </summary>
CostsYearlyResult, // multi-account type
+64 -3
View File
@@ -161,10 +161,8 @@ namespace Swarmops.Logic.Financial
}
string amountNetString = lineFields[fieldNameLookup[ExternalBankDataFieldName.TransactionNet]];
double amountNetDouble = Double.Parse(amountNetString, NumberStyles.Currency,
new CultureInfo(Profile.Culture));
newRecord.TransactionNetCents = (long) (amountNetDouble*100.0);
newRecord.TransactionNetCents = ParseAmountString(amountNetString);
// TODO: Net, fee
@@ -198,6 +196,69 @@ namespace Swarmops.Logic.Financial
this.Records = recordList.ToArray();
}
private Int64 ParseAmountString (string input)
{
// This parser deals with a number of different cases and cultures.
// It returns cents.
// "1" => 100
// "1.00" => 100
// "1,00" => 100
// "100" => 10,000
// "1,000" => 100,000
// "1,000.00" => 100,000
// "1 000,00" => 100,000
// Normally, this could be parsed as a double, but your typical Pentium
// errors in double handling creates rare mismatches of single cents.
// Therefore, must be parsed as an int.
char testDecimalSeparator = '1'; // dummy non-separator
if (input.Length > 3)
{
testDecimalSeparator = input[input.Length - 3];
}
if (testDecimalSeparator != '.' && testDecimalSeparator != ',')
{
// No known decimal separator where one is expected for monetary amounts,
// so multiply amount by 100 by adding two zeroes to string, as we're supposed
// to be returning cents
bool weirdCaseTriggered = false;
if (input.Length > 2)
{
testDecimalSeparator = input[input.Length - 2];
if (testDecimalSeparator == '.' || testDecimalSeparator == ',')
{
// Fucking SEB doesn't even have the manners to only have whole units or cents,
// but can present a half-crown as ",5". Damn them...
input += "0";
weirdCaseTriggered = true;
}
}
if (!weirdCaseTriggered)
{
input += "00";
}
}
// remove all known noise from the digit sequence: spaces, thousands accents, commas, periods
input = input.Replace(" ", "").Replace("'", "").Replace(",", "").Replace(".", "");
// parse what's remaining as cents in Int64
return Int64.Parse(input);
}
public ExternalBankDataProfile Profile { get; set; }
public ExternalBankDataRecord[] Records { get; private set; }
public long LatestAccountBalanceCents;
+20
View File
@@ -88,6 +88,26 @@ namespace Swarmops.Logic.Financial
}
}
public Int64 this[FinancialAccount account]
{
get
{
Int64 result = 0;
FinancialTransactionRows rows = this.Rows;
foreach (FinancialTransactionRow row in rows)
{
if (row.FinancialAccountId == account.Identity)
{
result += row.AmountCents;
}
}
return result;
}
}
public new string Description
{
get { return base.Description; }
+14
View File
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Swarmops.Basic.Enums;
namespace Swarmops.Logic.Financial
{
public interface ISummable
{
long SumCents { get; }
OrganizationFinancialAccountType CounterAccountType { get; }
}
}
@@ -75,7 +75,15 @@ namespace Swarmops.Logic.Financial
{
get
{
return FinancialAccount.FromIdentity(SwarmDb.GetDatabaseForReading().GetOrganizationFinancialAccountId(_organizationId, accountType));
int accountId = SwarmDb.GetDatabaseForReading().GetOrganizationFinancialAccountId(_organizationId,
accountType);
if (accountId == 0)
{
return null; // not set
}
return FinancialAccount.FromIdentity(accountId);
}
set
{
@@ -182,6 +190,11 @@ namespace Swarmops.Logic.Financial
get { return this[OrganizationFinancialAccountType.IncomeSales]; }
}
public FinancialAccount ConstsMiscalculations
{
get { return this[OrganizationFinancialAccountType.CostsMiscalculations]; }
}
public FinancialAccounts CostsConferences
{
get
+17 -1
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using Swarmops.Basic.Enums;
using Swarmops.Basic.Types;
using Swarmops.Database;
using Swarmops.Logic.Swarm;
@@ -8,7 +9,7 @@ using Swarmops.Logic.Structure;
namespace Swarmops.Logic.Financial
{
public class PaymentGroup: BasicPaymentGroup
public class PaymentGroup: BasicPaymentGroup, ISummable
{
private PaymentGroup(BasicPaymentGroup basic): base (basic)
{
@@ -179,5 +180,20 @@ namespace Swarmops.Logic.Financial
return String.Join(", ", payerList.ToArray());
}
}
#region Implementation of ISummable
public long SumCents
{
get { return this.AmountCents; }
}
public OrganizationFinancialAccountType CounterAccountType
{
get { return OrganizationFinancialAccountType.AssetsOutboundInvoices; }
}
#endregion
}
}
+7 -1
View File
@@ -13,7 +13,7 @@ using Swarmops.Logic.Support;
namespace Swarmops.Logic.Financial
{
public class Payout: BasicPayout
public class Payout: BasicPayout, ISummable
{
#region Creation and Construction
@@ -663,5 +663,11 @@ namespace Swarmops.Logic.Financial
this.Open = false;
}
#region Implementation of ISummable
public long SumCents { get { return this.AmountCents; } }
public OrganizationFinancialAccountType CounterAccountType { get { return OrganizationFinancialAccountType.Unknown; } }
#endregion
}
}
+1
View File
@@ -107,6 +107,7 @@
<Compile Include="Financial\FinancialTransactionTagType.cs" />
<Compile Include="Financial\FinancialTransactionTagTypes.cs" />
<Compile Include="Financial\ImportedBankData.cs" />
<Compile Include="Financial\ISummable.cs" />
<Compile Include="Financial\YearlyReport.cs" />
<Compile Include="Security\Accesses.cs" />
<Compile Include="AssemblyInfo.cs" />
@@ -6,6 +6,7 @@ using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swarmops.Basic.Enums;
using Swarmops.Basic.Interfaces;
using Swarmops.Logic.Financial;
using Swarmops.Logic.Security;
@@ -50,11 +51,25 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
{
for (int masterIndex = 0; masterIndex < mismatchingRecord.MasterCents.Count(); masterIndex++)
{
string dependencyString = string.Empty;
object dependency = mismatchingRecord.TransactionDependencies[masterIndex];
if (dependency != null)
{
dependencyString = dependency.GetType().ToString();
int lastPeriod = dependencyString.LastIndexOf('.');
dependencyString = dependencyString.Substring(lastPeriod + 1);
dependencyString += " #" + (dependency as IHasIdentity).Identity.ToString("N0");
}
childItems.Add("{\"id\":\"" + rowId + childItems.Count.ToString("##0") + "\",\"rowName\":\"" +
JsonSanitize(mismatchingRecord.Description) + "\",\"swarmopsData\":\"" +
PrintNullableCents(mismatchingRecord.SwarmopsCents[masterIndex]) + "\",\"masterData\":\"" +
PrintNullableCents(mismatchingRecord.MasterCents[masterIndex]) + "\",\"notes\":\"" +
JsonSanitize(mismatchingRecord.ResyncActions[masterIndex].ToString()) + "\"}");
PrintNullableCents(currentOrganizationCurrency, mismatchingRecord.SwarmopsCents[masterIndex]) + "\",\"masterData\":\"" +
PrintNullableCents(currentOrganizationCurrency, mismatchingRecord.MasterCents[masterIndex]) + "\",\"resyncAction\":\"" +
JsonSanitize(mismatchingRecord.ResyncActions[masterIndex].ToString()) + "\",\"notes\":\"" +
JsonSanitize(dependencyString) + "\"}");
}
}
@@ -62,11 +77,11 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
string rowName = mismatch.DateTime.ToString(profile.DateTimeFormatString);
string swarmopsData = ((double) mismatch.SwarmopsDeltaCents/100.0).ToString("N2") + " " +
currentOrganizationCurrency;
string swarmopsData = currentOrganizationCurrency + " " +
((double) mismatch.SwarmopsDeltaCents/100.0).ToString("N2");
string masterData = ((double)mismatch.MasterDeltaCents / 100.0).ToString("N2") + " " +
currentOrganizationCurrency;
string masterData = currentOrganizationCurrency + " " +
((double) mismatch.MasterDeltaCents/100.0).ToString("N2");
string notes = "Diff: " + ((double)(mismatch.MasterDeltaCents - mismatch.SwarmopsDeltaCents) / 100.0).ToString("N2");
@@ -83,14 +98,14 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
private Dictionary<int, FinancialAccounts> _hashedAccounts;
private string PrintNullableCents (long cents)
private string PrintNullableCents (string currency, long cents)
{
if (cents == 0)
{
return Resources.Global.Global_Missing;
}
return (((double)cents) / 100.0).ToString("N2"); // TODO: Verify that culture is applied
return currency + " " + (((double)cents) / 100.0).ToString("N2"); // TODO: Verify that culture is applied
}
private string PrintCents (long cents)
@@ -203,8 +203,8 @@
<thead>
<tr>
<th field="rowName" width="180">Date / Transaction</th>
<th field="swarmopsData" width="140" align="left">Our Database</th>
<th field="masterData" width="140" align="left">Master File</span></th>
<th field="swarmopsData" width="140" align="right">Our Database</th>
<th field="masterData" width="140" align="right">Master File</span></th>
<th field="resyncAction" width="120" align="left">Resync Action</span></th>
<th field="notes" width="100" align="left">Notes</th>
</tr>
@@ -245,9 +245,9 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
long swarmopsDeltaCents = account.GetDeltaCents(lastTimestamp.AddSeconds(1), timeWalker.AddSeconds(1));
// "AddSeconds" because DeltaCents operates on ">= lowbound, < highbound"
int timestampStartIndex = currentRecordIndex;
long masterDeltaCents = externalData.Records[currentRecordIndex++].TransactionNetCents;
int masterTransactionCount = 1;
int timestampStartIndex = currentRecordIndex;
while (currentRecordIndex < externalData.Records.Length &&
externalData.Records[currentRecordIndex].DateTime == timeWalker)
@@ -275,14 +275,16 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
for (int innerRecordIndex = timestampStartIndex; innerRecordIndex < currentRecordIndex; innerRecordIndex++)
{
if (!mismatchConstruction.Master.ContainsKey(externalData.Records[innerRecordIndex].Description))
string description = externalData.Records[innerRecordIndex].Description.Replace(" ", " ");
if (!mismatchConstruction.Master.ContainsKey(description))
{
mismatchConstruction.Master[externalData.Records[innerRecordIndex].Description] =
mismatchConstruction.Master[description] =
new ExternalBankMismatchingRecordConstruction();
}
mismatchConstruction.Master[externalData.Records[innerRecordIndex].Description].Cents.Add(externalData.Records[innerRecordIndex].TransactionNetCents);
mismatchConstruction.Master[externalData.Records[innerRecordIndex].Description].Dependencies.Add(null); // no dependencies on the master side, only on swarmops side
mismatchConstruction.Master[description].Cents.Add(externalData.Records[innerRecordIndex].TransactionNetCents);
mismatchConstruction.Master[description].Dependencies.Add(null); // no dependencies on the master side, only on swarmops side
}
// Load from Swarmops
@@ -290,18 +292,29 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
FinancialAccountRows swarmopsTransactionRows =
account.GetRowsFar(lastTimestamp, timeWalker); // the "select far" is a boundary < x <= boundary selector. Default is boundary <= x < boundary.
Dictionary<int, FinancialTransaction> lookupTransactions = new Dictionary<int, FinancialTransaction>();
// note all transaction IDs, then sum up per transaction
foreach (FinancialAccountRow swarmopsTransactionRow in swarmopsTransactionRows)
{
FinancialTransaction swarmopsTransaction = swarmopsTransactionRow.Transaction;
lookupTransactions[swarmopsTransactionRow.FinancialTransactionId] =
swarmopsTransactionRow.Transaction;
}
if (!mismatchConstruction.Swarmops.ContainsKey(swarmopsTransaction.Description))
foreach (FinancialTransaction transaction in lookupTransactions.Values)
{
string description = transaction.Description.Replace(" ", " "); // for legacy compatibility with new importer
if (!mismatchConstruction.Swarmops.ContainsKey(description))
{
mismatchConstruction.Swarmops[swarmopsTransaction.Description] =
mismatchConstruction.Swarmops[description] =
new ExternalBankMismatchingRecordConstruction();
}
mismatchConstruction.Swarmops[swarmopsTransaction.Description].Cents.Add(swarmopsTransactionRow.AmountCents);
mismatchConstruction.Swarmops[swarmopsTransaction.Description].Dependencies.Add(swarmopsTransaction.Dependency);
mismatchConstruction.Swarmops[description].Cents.Add(transaction [account]);
mismatchConstruction.Swarmops[description].Dependencies.Add(transaction.Dependency);
}
// Then, parse the intermediate construction object to the presentation-and-action object.
@@ -332,27 +345,42 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
// STEP 1 - locate all identical matches
for (int masterIndex = 0; masterIndex < mismatchConstruction.Master[masterKey].Cents.Count; masterIndex++)
if (mismatchConstruction.Swarmops.ContainsKey(masterKey))
{
// no "continue" necessary on first run-through; nothing has been checked off yet
long findMasterCents = mismatchConstruction.Master[masterKey].Cents[masterIndex];
for (int swarmopsIndex = 0; swarmopsIndex < mismatchConstruction.Swarmops[masterKey].Cents.Count; swarmopsIndex++)
for (int masterIndex = 0;
masterIndex < mismatchConstruction.Master[masterKey].Cents.Count;
masterIndex++)
{
if (findMasterCents == mismatchConstruction.Swarmops[masterKey].Cents [swarmopsIndex])
// no "continue" necessary on first run-through; nothing has been checked off yet
long findMasterCents = mismatchConstruction.Master[masterKey].Cents[masterIndex];
for (int swarmopsIndex = 0;
swarmopsIndex < mismatchConstruction.Swarmops[masterKey].Cents.Count;
swarmopsIndex++)
{
// There is a match as per case 1. Record both, mark both as used, continue.
if (checkSwarmopsIndex.ContainsKey(swarmopsIndex))
{
continue; // may have been checked off already in the rare case of twin identical amounts
}
masterCentsList.Add(findMasterCents);
swarmopsCentsList.Add(mismatchConstruction.Swarmops[masterKey].Cents[swarmopsIndex]); // should be equal, we're defensive here
dependenciesList.Add(mismatchConstruction.Swarmops[masterKey].Dependencies[swarmopsIndex]);
actionsList.Add(ExternalBankMismatchResyncAction.NoAction);
checkMasterIndex[masterIndex] = true;
checkSwarmopsIndex[swarmopsIndex] = true;
if (findMasterCents == mismatchConstruction.Swarmops[masterKey].Cents[swarmopsIndex])
{
// There is a match as per case 1. Record both, mark both as used, continue.
break;
masterCentsList.Add(findMasterCents);
swarmopsCentsList.Add(
mismatchConstruction.Swarmops[masterKey].Cents[swarmopsIndex]);
// should be equal, we're defensive here
dependenciesList.Add(
mismatchConstruction.Swarmops[masterKey].Dependencies[swarmopsIndex]);
actionsList.Add(ExternalBankMismatchResyncAction.NoAction);
checkMasterIndex[masterIndex] = true;
checkSwarmopsIndex[swarmopsIndex] = true;
break;
}
}
}
}
@@ -361,7 +389,8 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
// TODO: improve logic to handle same number of records left on both sides
if (mismatchConstruction.Master[masterKey].Cents.Count - checkMasterIndex.Keys.Count == 1 &&
if (mismatchConstruction.Swarmops.ContainsKey(masterKey) &&
mismatchConstruction.Master[masterKey].Cents.Count - checkMasterIndex.Keys.Count == 1 &&
mismatchConstruction.Swarmops [masterKey].Cents.Count - checkSwarmopsIndex.Keys.Count == 1)
{
for (int masterIndex = 0; masterIndex < mismatchConstruction.Master[masterKey].Cents.Count; masterIndex++)
@@ -415,7 +444,7 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
// STEP 3b - log remaining on Swarmops side
if (mismatchConstruction.Swarmops[masterKey].Cents.Count > checkSwarmopsIndex.Keys.Count)
if (mismatchConstruction.Swarmops.ContainsKey(masterKey) && mismatchConstruction.Swarmops[masterKey].Cents.Count > checkSwarmopsIndex.Keys.Count)
{
for (int swarmopsIndex = 0; swarmopsIndex < mismatchConstruction.Swarmops[masterKey].Cents.Count; swarmopsIndex++)
{
@@ -451,18 +480,24 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
mismatchingRecordList[masterKey] = newRecord;
}
// Finally, add the transactions that were (described) in Swarmops but not in Master
foreach (string swarmopsKey in mismatchConstruction.Swarmops.Keys)
{
if (!mismatchingRecordList.ContainsKey(swarmopsKey))
{
mismatchingRecordList[swarmopsKey] = new ExternalBankMismatchingRecordDescription();
mismatchingRecordList[swarmopsKey].Description = swarmopsKey;
}
mismatchingRecordList[swarmopsKey].SwarmopsCents =
mismatchConstruction.Swarmops[swarmopsKey].Cents.ToArray();
mismatchingRecordList[swarmopsKey].TransactionDependencies =
mismatchConstruction.Swarmops[swarmopsKey].Dependencies.ToArray();
mismatchingRecordList[swarmopsKey].SwarmopsCents =
mismatchConstruction.Swarmops[swarmopsKey].Cents.ToArray();
mismatchingRecordList[swarmopsKey].TransactionDependencies =
mismatchConstruction.Swarmops[swarmopsKey].Dependencies.ToArray();
mismatchingRecordList[swarmopsKey].MasterCents =
new long[mismatchConstruction.Swarmops[swarmopsKey].Cents.Count]; // inits to zero
mismatchingRecordList[swarmopsKey].ResyncActions = new ExternalBankMismatchResyncAction[mismatchConstruction.Swarmops[swarmopsKey].Cents.Count]; // inits to "Unknown", TODO
}
}
newMismatch.MismatchingRecords = mismatchingRecordList.Values.ToArray();