Committing a large amount of work
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Swarmops.Logic.Structure;
|
||||
|
||||
namespace Swarmops.Logic.Financial
|
||||
{
|
||||
public class ExternalBankData
|
||||
{
|
||||
public void LoadData(Stream dataStream, Organization organization)
|
||||
{
|
||||
using (TextReader reader = new StreamReader(dataStream)) // TODO: Is encoding necessary?
|
||||
{
|
||||
LoadData(reader, organization);
|
||||
}
|
||||
}
|
||||
|
||||
public void LoadData (TextReader reader, Organization organization)
|
||||
{
|
||||
string data = reader.ReadToEnd();
|
||||
LoadData(data, organization);
|
||||
}
|
||||
|
||||
public void LoadData(string data, Organization organization)
|
||||
{
|
||||
List<ExternalBankDataRecord> recordList = new List<ExternalBankDataRecord>();
|
||||
|
||||
if (Profile == null)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot call LoadData before a profile has been set");
|
||||
}
|
||||
|
||||
string organizationCurrencyCode = organization.Currency.Code;
|
||||
|
||||
int crlfIndex = data.IndexOfAny(new char[] {'\n', '\r'});
|
||||
|
||||
string fieldKeyLine = data.Substring(0, crlfIndex);
|
||||
|
||||
string[] dataKeyFields = fieldKeyLine.Split('\t');
|
||||
|
||||
Dictionary<ExternalBankDataFieldName, int> fieldNameLookup =
|
||||
new Dictionary<ExternalBankDataFieldName, int>();
|
||||
|
||||
// below is an N^2 loop but doesn't matter in such a small context
|
||||
|
||||
foreach (ExternalBankDataFieldName fieldName in Profile.FieldNames.Keys)
|
||||
{
|
||||
for (int index = 0; index < dataKeyFields.Length; index++)
|
||||
{
|
||||
if (dataKeyFields[index].Trim() == Profile.FieldNames[fieldName])
|
||||
{
|
||||
fieldNameLookup[fieldName] = index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!fieldNameLookup.ContainsKey(fieldName)) // wasn't found
|
||||
{
|
||||
throw new ArgumentException("Field key \"" + fieldName + "\" was not supplied or found in data file");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
data = data.Substring(crlfIndex).Trim();
|
||||
|
||||
string[] lines = data.Split(new char[] {'\r', '\n'});
|
||||
|
||||
foreach (string lineData in lines)
|
||||
{
|
||||
string line = lineData.Trim();
|
||||
|
||||
if (line.Length < 1)
|
||||
{
|
||||
continue; // empty lines may exist due to split on either CR or LF
|
||||
}
|
||||
|
||||
string[] lineFields = line.Split('\t');
|
||||
|
||||
// If wrong currency, ignore
|
||||
|
||||
if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.Currency))
|
||||
{
|
||||
string currency = lineFields[fieldNameLookup[ExternalBankDataFieldName.Currency]];
|
||||
|
||||
if (currency != organizationCurrencyCode)
|
||||
{
|
||||
continue; // ignore this record
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ExternalBankDataRecord newRecord = new ExternalBankDataRecord();
|
||||
|
||||
if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.Description))
|
||||
{
|
||||
newRecord.Description = lineFields[fieldNameLookup[ExternalBankDataFieldName.Description]];
|
||||
}
|
||||
|
||||
if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.AccountBalance))
|
||||
{
|
||||
// Dividing up to step-by-step statements instead of one long statement assists debugging
|
||||
// of culture and other error sources
|
||||
|
||||
string balanceString = lineFields[fieldNameLookup[ExternalBankDataFieldName.AccountBalance]];
|
||||
double parsedBalance = Double.Parse(balanceString, NumberStyles.Currency, new CultureInfo(Profile.Culture));
|
||||
newRecord.AccountBalanceCents = (long) Math.Floor(parsedBalance*100.0);
|
||||
}
|
||||
|
||||
if (!fieldNameLookup.ContainsKey(ExternalBankDataFieldName.Date) && !fieldNameLookup.ContainsKey(ExternalBankDataFieldName.DateTime))
|
||||
{
|
||||
throw new InvalidOperationException("Cannot parse transactions file without at least a date field");
|
||||
}
|
||||
|
||||
DateTime dateTime = DateTime.MinValue;
|
||||
|
||||
if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.Date))
|
||||
{
|
||||
string dateString = lineFields[fieldNameLookup[ExternalBankDataFieldName.Date]];
|
||||
dateTime = DateTime.Parse(dateString, new CultureInfo(Profile.Culture));
|
||||
|
||||
if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.Time))
|
||||
{
|
||||
string timeString = lineFields[fieldNameLookup[ExternalBankDataFieldName.Time]];
|
||||
TimeSpan timeOfDay = TimeSpan.Parse(timeString);
|
||||
|
||||
dateTime += timeOfDay;
|
||||
}
|
||||
else
|
||||
{
|
||||
// move transaction to like mid-day of the organization's time zone. For now, all orgs are in Europe, so add 12 hours
|
||||
// this is a HACK HACK HACK
|
||||
|
||||
dateTime = dateTime.AddHours(12);
|
||||
}
|
||||
}
|
||||
else // no Date field, so by earlier logic, must have a DateTime field
|
||||
{
|
||||
dateTime = DateTime.Parse(lineFields[fieldNameLookup[ExternalBankDataFieldName.DateTime]], new CultureInfo(Profile.Culture));
|
||||
}
|
||||
|
||||
if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.TimeZone))
|
||||
{
|
||||
// Valid time zone formats are "XXX+hh:mm". The XXX are ignored.
|
||||
|
||||
// Throws exception if this doesn't parse, which is what we want
|
||||
|
||||
TimeSpan timeZone =
|
||||
TimeSpan.Parse(lineFields[fieldNameLookup[ExternalBankDataFieldName.TimeZone]].Substring(4));
|
||||
|
||||
dateTime -= timeZone; // minus, to bring the time to UTC. If time 13:00 is in tz +01:00, the UTC time is 12:00
|
||||
}
|
||||
|
||||
newRecord.DateTime = dateTime;
|
||||
|
||||
if (!fieldNameLookup.ContainsKey(ExternalBankDataFieldName.TransactionNet))
|
||||
{
|
||||
throw new ArgumentException("There must be a transaction amount field in the bank data profile");
|
||||
}
|
||||
|
||||
string amountNetString = lineFields[fieldNameLookup[ExternalBankDataFieldName.TransactionNet]];
|
||||
double amountNetDouble = Double.Parse(amountNetString, NumberStyles.Currency,
|
||||
new CultureInfo(Profile.Culture));
|
||||
|
||||
newRecord.TransactionNetCents = (long) (amountNetDouble*100.0);
|
||||
|
||||
// TODO: Net, fee
|
||||
|
||||
if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.UniqueId))
|
||||
{
|
||||
newRecord.UniqueId = lineFields[fieldNameLookup[ExternalBankDataFieldName.UniqueId]];
|
||||
}
|
||||
else if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.NotUniqueId))
|
||||
{
|
||||
newRecord.NotUniqueId = lineFields[fieldNameLookup[ExternalBankDataFieldName.NotUniqueId]];
|
||||
}
|
||||
|
||||
recordList.Add(newRecord);
|
||||
}
|
||||
|
||||
recordList.Sort(new ExternalBankDataRecord());
|
||||
|
||||
this.Records = recordList.ToArray();
|
||||
}
|
||||
|
||||
public ExternalBankDataProfile Profile { get; set; }
|
||||
public ExternalBankDataRecord[] Records { get; private set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Swarmops.Logic.Structure;
|
||||
|
||||
namespace Swarmops.Logic.Financial
|
||||
{
|
||||
public class ExternalBankDataProfile
|
||||
{
|
||||
private ExternalBankDataProfile()
|
||||
{
|
||||
this.FieldNames = new Dictionary<ExternalBankDataFieldName, string>();
|
||||
}
|
||||
|
||||
static public ExternalBankDataProfile FromIdentity (int externalBankDataProfileId)
|
||||
{
|
||||
ExternalBankDataProfile result = new ExternalBankDataProfile();
|
||||
|
||||
// Ugly hack for now
|
||||
|
||||
if (externalBankDataProfileId == SESebId)
|
||||
{
|
||||
result.Name = "SEB";
|
||||
result.Country = Country.FromCode("SE");
|
||||
result.Culture = "sv-SE";
|
||||
|
||||
result.FieldNames[ExternalBankDataFieldName.Date] = "Bokföringsdatum";
|
||||
result.FieldNames[ExternalBankDataFieldName.Description] = "Text/mottagare";
|
||||
result.FieldNames[ExternalBankDataFieldName.TransactionNet] = "Belopp";
|
||||
result.FieldNames[ExternalBankDataFieldName.AccountBalance] = "Saldo";
|
||||
result.FieldNames[ExternalBankDataFieldName.NotUniqueId] = "Verifikationsnummer";
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
if (externalBankDataProfileId == PaypalId)
|
||||
{
|
||||
result.Name = "Paypal";
|
||||
result.Country = null;
|
||||
result.Culture = "sv-SE"; // what the actual fuck? Paypal's giving me sv-SE culture
|
||||
|
||||
result.FieldNames[ExternalBankDataFieldName.Date] = "Date";
|
||||
result.FieldNames[ExternalBankDataFieldName.Time] = "Time";
|
||||
result.FieldNames[ExternalBankDataFieldName.TimeZone] = "Time Zone";
|
||||
result.FieldNames[ExternalBankDataFieldName.Description] = "Name";
|
||||
result.FieldNames[ExternalBankDataFieldName.Currency] = "Currency";
|
||||
result.FieldNames[ExternalBankDataFieldName.TransactionGross] = "Gross";
|
||||
result.FieldNames[ExternalBankDataFieldName.TransactionFee] = "Fee";
|
||||
result.FieldNames[ExternalBankDataFieldName.TransactionNet] = "Net";
|
||||
result.FieldNames[ExternalBankDataFieldName.AccountBalance] = "Balance";
|
||||
result.FieldNames[ExternalBankDataFieldName.UniqueId] = "Transaction ID";
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
throw new ArgumentException("Unrecognized profile Id");
|
||||
}
|
||||
|
||||
static public int SESebId { get { return 1; } } // Ugly pre-db hacks
|
||||
static public int PaypalId { get { return 2; } }
|
||||
|
||||
public string Name { get; set; }
|
||||
public Country Country { get; set; }
|
||||
public string Culture { get; set; }
|
||||
public string Currency { get; set; }
|
||||
|
||||
public Dictionary<ExternalBankDataFieldName, string> FieldNames { get; private set; }
|
||||
}
|
||||
|
||||
public enum ExternalBankDataFieldName
|
||||
{
|
||||
Unknown = 0,
|
||||
AccountBalance,
|
||||
TransactionGross,
|
||||
TransactionFee,
|
||||
TransactionNet,
|
||||
Description,
|
||||
UniqueId,
|
||||
NotUniqueId,
|
||||
Date,
|
||||
Time,
|
||||
DateTime,
|
||||
TimeZone,
|
||||
Currency
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Swarmops.Logic.Financial
|
||||
{
|
||||
public class ExternalBankDataRecord: IComparer<ExternalBankDataRecord>
|
||||
{
|
||||
public long AccountBalanceCents;
|
||||
public long TransactionGrossCents;
|
||||
public long TransactionNetCents;
|
||||
public long FeeCents;
|
||||
public string Description;
|
||||
public DateTime DateTime; // UTC!
|
||||
public string UniqueId;
|
||||
public string NotUniqueId; // As it says, but something that still assists in creating a hash together w/ other fields
|
||||
|
||||
#region Implementation of IComparer<ExternalBankDataRecord>
|
||||
|
||||
public int Compare(ExternalBankDataRecord x, ExternalBankDataRecord y)
|
||||
{
|
||||
// Use only DateTime to compare
|
||||
|
||||
return x.DateTime.CompareTo(y.DateTime);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -97,6 +97,9 @@
|
||||
<Compile Include="Communications\Transmission\PayloadBase.cs" />
|
||||
<Compile Include="Financial\CashAdvance.cs" />
|
||||
<Compile Include="Financial\CashAdvances.cs" />
|
||||
<Compile Include="Financial\ExternalBankData.cs" />
|
||||
<Compile Include="Financial\ExternalBankDataProfile.cs" />
|
||||
<Compile Include="Financial\ExternalBankDataRecord.cs" />
|
||||
<Compile Include="Financial\FinancialTransactionTagSet.cs" />
|
||||
<Compile Include="Financial\FinancialTransactionTagSets.cs" />
|
||||
<Compile Include="Financial\FinancialTransactionTagSetType.cs" />
|
||||
|
||||
Reference in New Issue
Block a user