Committing a large amount of work

This commit is contained in:
Rick Falkvinge
2014-04-14 17:16:04 +00:00
parent de1e396524
commit 397b44ba5b
15 changed files with 830 additions and 23 deletions
+2 -1
View File
@@ -74,7 +74,8 @@ namespace Swarmops.Database
string sqlQuery =
"SELECT Documents.DocumentId,Documents.ServerFileName,Documents.ClientFileName,Documents.Description,DocumentTypes.Name AS DocumentType,Documents.ForeignId,Documents.FileSize,Documents.UploadedByPersonId,Documents.UploadedDateTime From Documents,DocumentTypes " +
"WHERE Documents.Description = '" + description.Replace ("'", "''") + "' AND " +
"WHERE Documents.DocumentTypeId=DocumentTypes.DocumentTypeId AND " +
"Documents.Description = '" + description.Replace("'", "''") + "' AND " +
"Documents.UploadedDateTime > '" + DateTime.UtcNow.AddDays (-1).ToString ("yyyy-MM-dd HH:mm") + "'";
DbCommand command = GetDbCommand(sqlQuery, connection);
+191
View File
@@ -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
}
}
+30
View File
@@ -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
}
}
+3
View File
@@ -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" />
+4
View File
@@ -391,4 +391,8 @@
<data name="Financial_Income" xml:space="preserve">
<value>Revenue</value>
</data>
<data name="Global_SelectOne" xml:space="preserve">
<value>- - Select one - -</value>
<comment>Shown in dropdowns when selection required</comment>
</data>
</root>
+3
View File
@@ -270,6 +270,9 @@
<data name="Menu5_Ledgers_ProfitLossStatement" xml:space="preserve">
<value>Profit &amp; Loss Statement</value>
</data>
<data name="Menu5_Ledgers_ResyncExternalAccount" xml:space="preserve">
<value>Resync Bank Account</value>
</data>
<data name="Menu5_Ledgers_SetBudgets" xml:space="preserve">
<value>Set Budget Amounts</value>
</data>
@@ -218,6 +218,12 @@
<value>YTD</value>
<comment>Year To Date</comment>
</data>
<data name="ResyncExternalAccount_Info" xml:space="preserve">
<value>If your bookkeeping in Swarmops has gotten out of sync with your bank records for whatever technical or human reason, you can resynchronize them here by uploading a master file that covers the time frame you need to resync.&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;Warning:&lt;/strong&gt; this is a time-consuming process that may do a &lt;em&gt;lot&lt;/em&gt; of changes to your bookkeeping. Do not use unless you have discovered a mismatch in the records vs. your bank.&lt;br/&gt;&lt;br/&gt;(You will get a chance to review proposed changes before they are committed to your financial records.)</value>
</data>
<data name="ResyncExternalAccount_PageTitle" xml:space="preserve">
<value>Resynchronize Bank Account</value>
</data>
<data name="SetRootBudgets_BudgetOwnerHeader" xml:space="preserve">
<value>Budget, Owner</value>
<comment>Displayed above editables</comment>
+23 -17
View File
@@ -182,28 +182,34 @@ namespace Swarmops.Frontend.Automation
ms.Write(fileData, 0, file.ContentLength);
ms.Position = 0;*/
// Try to load as image. If fails, not an acceptable file
string filterType = context.Request.QueryString["Filter"];
try
if (filterType != "NoFilter")
{
Image image = Image.FromStream (file.InputStream);
image.Dispose();
}
catch (Exception)
{
// TODO: If general files accepted, then ok, otherwise fuck off
// TODO: Accept general files
// Try to load as image. If fails, not an acceptable file
var errorStatus = new FilesStatus (fullName, file.ContentLength);
errorStatus.error = "ERR_NOT_IMAGE";
errorStatus.url = string.Empty;
errorStatus.delete_url = string.Empty;
errorStatus.thumbnail_url = string.Empty;
try
{
Image image = Image.FromStream(file.InputStream);
image.Dispose();
}
catch (Exception)
{
// TODO: If general files accepted, then ok, otherwise fuck off
statuses.Add (errorStatus);
// -1 for length means the file was NOT saved, and that it could not be parsed as image.
return;
// TODO: Accept general files
var errorStatus = new FilesStatus(fullName, file.ContentLength);
errorStatus.error = "ERR_NOT_IMAGE";
errorStatus.url = string.Empty;
errorStatus.delete_url = string.Empty;
errorStatus.thumbnail_url = string.Empty;
statuses.Add(errorStatus);
// -1 for length means the file was NOT saved, and that it could not be parsed as image.
return;
}
}
}
+11 -3
View File
@@ -9,16 +9,24 @@
var imageFileEnd = "' /\x3E"; // needs to be separate var rather than inline because of mono-server optimization bug
var imageUploadSuccess = imageFileStart + imageFileSuccess + imageFileEnd;
var imageUploadFail = imageFileStart + imageFileFail + imageFileEnd;
$(document).ready(function() {
$('#<%=this.ClientID %>_ButtonUploadHidden').fileupload({
url: '/Automation/UploadFileHandler.ashx?Guid=<%= GuidString %>',
url: '/Automation/UploadFileHandler.ashx?Guid=<%= GuidString %>&Filter=<%=Filter %>',
dataType: 'json',
done: function(e, data) {
$.each(data.result, function(index, file) {
if (file.error == null || file.error.length < 1) {
$('#<%=this.ClientID %>_DivUploadCount').append(imageUploadSuccess);
<% if (this.ClientUploadCompleteCallback.Length > 0)
{ %>
<%= this.ClientUploadCompleteCallback %>();
<% } %>
} else {
$('#<%=this.ClientID %>_DivUploadCount').append(imageUploadFail); // Assume ERR_NOT_IMAGE
alertify.error('File "' + file.name + '" could not be uploaded. Is it really an image?');
@@ -56,5 +64,5 @@
</script>
<asp:HiddenField runat="server" ID="HiddenGuid"/>
<div style="height:36px;float:left"><input id="<%=this.ClientID %>_ButtonUploadVisible" class="ButtonSwarmopsUpload NoInputFocus" type="button" /><input id="<%=this.ClientID %>_ButtonUploadHidden" type="file" name="files[]" multiple style="display:none" /></div><div style="height:36px;width:270px;margin-right:10px;float:right;border:none"><div id="<%=this.ClientID %>_DivUploadCount" style="display:none;overflow:hidden;height:64px"></div><div id="<%=this.ClientID %>_DivProgressUpload" style="width:100%;margin-top:8px;display:none"></div></div>&nbsp;<br/>
<div style="height:36px;float:left"><input id="<%=this.ClientID %>_ButtonUploadVisible" class="ButtonSwarmopsUpload NoInputFocus" type="button" <%= this.HideTrigger? "style='display:none'" : string.Empty %> /><input id="<%=this.ClientID %>_ButtonUploadHidden" type="file" name="files[]" multiple style="display:none" /></div><div style="height:36px;width:270px;margin-right:10px;float:right;border:none"><div id="<%=this.ClientID %>_DivUploadCount" style='display:none;overflow:hidden;height:'<%=this.DisplayCount < 9? 32: 64%>px'></div><div id="<%=this.ClientID %>_DivProgressUpload" style="width:100%;margin-top:8px;display:none"></div></div>&#8203;<br/>
+11
View File
@@ -21,6 +21,14 @@ namespace Swarmops.Controls.Base
{
Filter = UploadFilter.NoFilter;
}
if (this.DisplayCount == 0) // if not set
{
this.DisplayCount = 16;
}
if (this.ClientUploadCompleteCallback == null)
{
this.ClientUploadCompleteCallback = string.Empty;
}
}
else
{
@@ -30,6 +38,9 @@ namespace Swarmops.Controls.Base
public string GuidString;
public UploadFilter Filter { get; set; }
public int DisplayCount { get; set; }
public bool HideTrigger { get; set; }
public string ClientUploadCompleteCallback { get; set; }
public enum UploadFilter
+1
View File
@@ -190,6 +190,7 @@
<telerik:RadMenuItem runat="server" Text="XYZ Set Budget" NavigateUrl="~/Pages/v5/Ledgers/SetRootBudgets.aspx" ImageUrl="~/Images/PageIcons/iconshock-moneybag-16px.png" Permission="Financial:Write" UserLevel="3" GlobalResourceKey="Menu5_Ledgers_SetBudgets" />
<telerik:RadMenuItem runat="server" IsSeparator="true" UserLevel="3" />
<telerik:RadMenuItem runat="server" Text="XYZ Upload Bank Files" NavigateUrl="~/Pages/v5/Ledgers/UploadBankFiles.aspx" ImageUrl="~/Images/PageIcons/iconshock-bank-16px.png" Permission="Economy:Write" UserLevel="3" GlobalResourceKey="Menu5_Ledgers_UploadBankFiles" />
<telerik:RadMenuItem runat="server" Text="XYZ Resync external account" NavigateUrl="~/Pages/v5/Ledgers/ResyncExternalAccount.aspx" ImageUrl="~/Images/PageIcons/iconshock-bank-16px.png" Permission="Economy:Write" UserLevel="3" GlobalResourceKey="Menu5_Ledgers_ResyncExternalAccount" />
<telerik:RadMenuItem runat="server" IsSeparator="true" UserLevel="3" />
<telerik:RadMenuItem runat="server" Text="XYZ View Outstanding Accounts" NavigateUrl="~/Pages/v5/Ledgers/ViewOutstandingAccounts.aspx" ImageUrl="~/Images/PageIcons/iconshock-cabinet-16px.png" Permission="EconomyDetail:Read" UserLevel="3" GlobalResourceKey="Menu5_Ledgers_ViewOutstandingAccounts" />
<telerik:RadMenuItem runat="server" Text="XYZ Debug Ledgers" NavigateUrl="~/Pages/v5/Ledgers/DebugLedgers.aspx" ImageUrl="~/Images/PageIcons/iconshock-tester-16px.png" Permission="EconomyDetail:Read" UserLevel="3" GlobalResourceKey="Menu5_Ledgers_DebugLedgers" />
@@ -0,0 +1,222 @@
<%@ Page Title="" Language="C#" MasterPageFile="~/Master-v5.master" AutoEventWireup="true" CodeFile="ResyncExternalAccount.aspx.cs" Inherits="Swarmops.Frontend.Pages.v5.Ledgers.ResyncExternalAccount" %>
<%@ Register src="~/Controls/v5/UI/ExternalScripts.ascx" tagname="ExternalScripts" tagprefix="Swarmops5" %>
<%@ Register src="~/Controls/v5/Base/FileUpload.ascx" tagname="FileUpload" tagprefix="Swarmops5" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderHead" Runat="Server">
<Swarmops5:ExternalScripts ID="ExternalScripts1" Package="easyui" Control="tree" runat="server" />
<!-- The Iframe Transport is required for browsers without support for XHR file uploads -->
<script src="/Scripts/jquery.fileupload/jquery.iframe-transport.js" type="text/javascript" language="javascript"></script>
<!-- The basic File Upload plugin -->
<script src="/Scripts/jquery.fileupload/jquery.fileupload.js" type="text/javascript" language="javascript"></script>
<link rel="stylesheet" type="text/css" href="/Style/v5-easyui-elements.css">
<script type="text/javascript">
$(document).ready(function () {
$('#tableProfitLoss').treegrid(
{
onBeforeExpand: function (foo) {
$('span.profitlossdata-collapsed-' + foo.id).fadeOut('fast', function () {
$('span.profitlossdata-expanded-' + foo.id).fadeIn('slow');
});
},
onBeforeCollapse: function (foo) {
$('span.profitlossdata-expanded-' + foo.id).fadeOut('fast', function () {
$('span.profitlossdata-collapsed-' + foo.id).fadeIn('slow');
});
},
onLoadSuccess: function () {
$('div.datagrid').css('opacity', 1);
$('#imageLoadIndicator').hide();
$('span.loadingHeader').hide();
var selectedYear = $('#<%=DropAccounts.ClientID %>').val();
$('div#linkDownloadReport').attr("onclick", "document.location='Csv-BalanceData.aspx?Year=" + selectedYear + "';");
$('#spanDownloadText').text('<%=Resources.Pages.Ledgers.BalanceSheet_DownloadFileName %>' + selectedYear + "-<%=DateTime.Today.ToString("yyyyMMdd") %>.csv");
$('#headerStartYear').text('<%=Resources.Pages.Ledgers.BalanceSheet_StartYear %>'.replace('XXXX',selectedYear));
if (selectedYear == currentYear)
{
$('span.previousYearsHeader').hide();
$('span.currentYearHeader').show();
}
else
{
$('#previousYtd').text('<%=Resources.Pages.Ledgers.BalanceSheet_EndYear %>'.replace('XXXX', selectedYear));
$('span.currentYearHeader').hide();
$('span.previousYearsHeader').show();
}
$('span.commonHeader').show();
}
});
$('#<%=DropAccounts.ClientID %>').change(function () {
var selectedAccountId = $('#<%=DropAccounts.ClientID %>').val();
if (selectedAccountId != 0)
{
$('#<%=this.UploadFile.ClientID %>_ButtonUploadVisible').fadeIn();
} else {
$('#<%=this.UploadFile.ClientID %>_ButtonUploadVisible').fadeOut();
}
$('#tableProfitLoss').treegrid({ url: 'Json-BalanceSheetData.aspx?Year=' + selectedYear });
$('#imageLoadIndicator').show();
$('div.datagrid').css('opacity', 0.4);
$('#tableProfitLoss').treegid('reload');
});
$('div.datagrid').css('opacity', 0.4);
});
function uploadCompletedCallback() {
$('#DivStepUpload').slideUp().fadeOut();
$('#DivStepProcessing').fadeIn();
$('#DivProgressProcessing').progressbar({ value: 0, max: 100 });
$.ajax({
type: "POST",
url: "ResyncExternalAccount.aspx/InitializeProcessing",
data: "{'guid': '<%= this.UploadFile.GuidString %>'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
setTimeout('updateProgressProcessing();', 1000);
}
});
// Set timeout to update progress bar
}
function updateProgressProcessing() {
$.ajax({
type: "POST",
url: "ResyncExternalAccount.aspx/GetProcessingProgress",
data: "{'guid': '<%= this.UploadFile.GuidString %>'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg.d > 99) {
$("#DivProgressProcessing .progressbar-value").animate(
{
width: "100%"
}, { queue: false });
alert("all done!");
$(".buttonNext").click();
} else {
// We're not done yet. Keep the progress bar on-screen and keep re-checking.
if (msg.d == 1) {
$('#DivProgressProcessing').progressbar( { value: 1 });
} else {
$("#DivProgressProcessing .progressbar-value").animate(
{
width: msg.d + "%"
}, { queue: false });
}
if (msg.d > 0 && !progressReceived) {
progressReceived = true;
$.ajax({
type: "POST",
url: "ResyncExternalAccount.aspx/GetProcessingStatistics",
data: "{'guid': '<%= this.UploadFile.GuidString %>'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg2) {
$('#SpanFirstTx').text(msg2.d.FirstTransaction);
$('#SpanLastTx').text(msg2.d.LastTransaction);
$('#SpanTxCount').text(msg2.d.TransactionCount);
}
});
}
setTimeout('updateProgressProcessing();', 1000);
}
}
});
}
var progressReceived = false;
var currentYear = <%=DateTime.Today.Year %>;
</script>
<style>
.content h2 select {
font-size: 16px;
font-weight: bold;
color: #1C397E;
letter-spacing: 1px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderMain" Runat="Server">
<div id="DivStepUpload">
<h2>Step 1/4: Upload Master File</h2>
<div id="DivPrepData">
<div class="entryFields">
<asp:DropDownList runat="server" ID="DropAccounts"/>&nbsp;
<Swarmops5:FileUpload runat="server" ID="UploadFile" Filter="NoFilter" DisplayCount="8" HideTrigger="true" ClientUploadCompleteCallback="uploadCompletedCallback" /></div>
<div class="entryLabels">
Account to resync<br/>
Upload master file
</div>
</div>
<br clear="all"/>
</div>
<div id="DivStepProcessing" style="display:none">
<h2>Step 2/4: Comparing records to master...</h2>
<div id="DivProgressProcessing" style="width:100%"></div>
<ul><li>The first transaction in the master file was on <span id="SpanFirstTx">Foo</span>.</li>
<li>The last transaction in master file was on <span id="SpanLastTx">Foo</span>.</li>
<li>There are <span id="SpanTxCount">Bar</span> transactions in the master file.</li>
</ul>
</div>
<h2>Step 3/4: Comparison results - Resync?</h2>
<h2>Step 4/4: Resynchronizing with master...</h2>
<h2>Resynchronization complete</h2>
<table id="tableProfitLoss" title="" class="easyui-treegrid" style="width:680px;height:600px"
url=""
rownumbers="false"
animate="true"
fitColumns="true"
idField="id" treeField="name">
<thead>
<tr>
<th field="name" width="178"><asp:Literal ID="LiteralHeaderAccountName" runat="server"/></th>
<th field="lastYear" width="80" align="right"><span class="commonHeader" id="headerStartYear" style="display:none"></span><span class="loadingHeader">&mdash;</span></th>
<th field="q1" width="80" align="right"><span class="commonHeader" id="headerQ1" style="display:none"><asp:Literal ID="LiteralHeaderQ1" runat="server" /></span><span class="loadingHeader">&mdash;</span></th>
<th field="q2" width="80" align="right"><span class="commonHeader" id="headerQ2" style="display:none"><asp:Literal ID="LiteralHeaderQ2" runat="server" /></span><span class="loadingHeader">&mdash;</span></th>
<th field="q3" width="80" align="right"><span class="commonHeader" id="headerQ3" style="display:none"><asp:Literal ID="LiteralHeaderQ3" runat="server" /></span><span class="loadingHeader">&mdash;</span></th>
<th field="q4" width="80" align="right"><span class="commonHeader" id="headerQ4" style="display:none"><asp:Literal ID="LiteralHeaderQ4" runat="server" /></span><span class="loadingHeader">&mdash;</span></th>
<th field="ytd" width="80" align="right"><span class="previousYearsHeader" id="previousYtd" style="display:none"></span><span class="currentYearHeader" style="display:none"><asp:Literal ID="LiteralHeaderYtd" runat="server" /></span><span class="loadingHeader">&mdash;</span></th>
</tr>
</thead>
</table>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderSide" Runat="Server">
</asp:Content>
@@ -0,0 +1,229 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using Swarmops.Basic.Enums;
using Swarmops.Logic.Financial;
using Swarmops.Logic.Structure;
using Swarmops.Logic.Support;
namespace Swarmops.Frontend.Pages.v5.Ledgers
{
public partial class ResyncExternalAccount : PageV5Base
{
static ResyncExternalAccount()
{
_staticDataLookup = new Dictionary<string, object>(); // This does not work at all for webfarms, which may be a problem down the road
}
public static string StorageRoot
{
get
{
if (Debugger.IsAttached)
{
return @"C:\Windows\Temp\\Swarmops-Debug\\"; // Windows debugging environment
}
else
{
return "/opt/swarmops/upload/"; // production location on Debian installation TODO: config file
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.CurrentOrganization.IsEconomyEnabled)
{
Response.Redirect("/Pages/v5/Financial/EconomyNotEnabled.aspx", true);
return;
}
this.PageIcon = "iconshock-treasure";
this.PageTitle = Resources.Pages.Ledgers.ResyncExternalAccount_PageTitle;
this.InfoBoxLiteral = Resources.Pages.Ledgers.ResyncExternalAccount_Info;
if (!Page.IsPostBack)
{
this.DropAccounts.Items.Add(new ListItem(Resources.Global.Global_SelectOne, "0"));
FinancialAccounts accounts = FinancialAccounts.ForOrganization(this.CurrentOrganization,
FinancialAccountType.Asset);
foreach (FinancialAccount account in accounts)
{
// TODO: COnstruct import settings for accounts
if (this.CurrentOrganization.Name == "Piratpartiet SE" && account.Identity == 1)
{
this.DropAccounts.Items.Add(new ListItem(account.Name + " (SE/SEB)", account.Identity.ToString(CultureInfo.InvariantCulture)));
}
}
Localize();
}
}
private void Localize()
{
}
[WebMethod(true)]
public static int GetProcessingProgress(string guid)
{
// Get data from static variables
string key = guid + "PercentRead";
if (!_staticDataLookup.ContainsKey(key))
{
return 0;
}
return (int)_staticDataLookup[key];
}
[WebMethod(true)]
public static ExternalBankDataStatistics GetProcessingStatistics(string guid)
{
// Get data from static variables
string key = guid + "PercentRead";
if (!_staticDataLookup.ContainsKey(key))
{
return null;
}
ExternalBankDataStatistics result = new ExternalBankDataStatistics();
result.FirstTransaction = (string)_staticDataLookup[guid + "FirstTx"];
result.LastTransaction = (string)_staticDataLookup[guid + "LastTx"];
result.TransactionCount = (string)_staticDataLookup[guid + "TxCount"];
return result;
}
[WebMethod(true)]
public static void InitializeProcessing(string guid)
{
// Start an async thread that does all the work, then return
AuthenticationData authData = GetAuthenticationDataAndCulture();
Thread initThread = new Thread(ProcessUploadThread);
ProcessThreadArguments args = new ProcessThreadArguments
{Guid = guid, Organization = authData.CurrentOrganization};
initThread.Start(args);
}
public class ExternalBankDataStatistics
{
public string FirstTransaction { get; set; }
public string LastTransaction { get; set; }
public string TransactionCount { get; set; }
}
private class ProcessThreadArguments
{
public string Guid { get; set; }
public Organization Organization { get; set; }
}
private static void ProcessUploadThread(object args)
{
string guid = ((ProcessThreadArguments) args).Guid;
Documents documents = Documents.RecentFromDescription(guid);
if (documents.Count != 1)
{
return; // abort
}
Document uploadedDoc = documents[0];
ExternalBankData externalData = new ExternalBankData();
externalData.Profile = ExternalBankDataProfile.FromIdentity(ExternalBankDataProfile.SESebId); // HACK HACK HACK
using (StreamReader reader = new StreamReader(StorageRoot + uploadedDoc.ServerFileName, Encoding.GetEncoding(1252)))
{
externalData.LoadData(reader, ((ProcessThreadArguments) args).Organization);
}
_staticDataLookup[guid + "FirstTx"] = externalData.Records[0].DateTime.ToLongDateString();
_staticDataLookup[guid + "LastTx"] =
externalData.Records[externalData.Records.Length - 1].DateTime.ToLongDateString();
_staticDataLookup[guid + "TxCount"] = externalData.Records.Length.ToString("N0");
_staticDataLookup[guid + "PercentRead"] = 1;
int accountId = 1; // TODO: HACK HACK HACK HACK
FinancialAccount account = FinancialAccount.FromIdentity(accountId);
DateTime timeWalker = externalData.Records[0].DateTime;
int currentRecordIndex = 0;
// Walk past the first identical time records, and verify that we have at least one balance record that matches
// our own for this timestamp. There may be several transactions in the master file, but at least one should have
// a post-transaction balance that matches our records, or something is much more broken.
long swarmopsCentsStart = account.GetDeltaCents(DateTime.MinValue, timeWalker.AddSeconds(1));
// At least one of the transactions for this timestamp should match.
bool foundMatch = false;
while (externalData.Records [currentRecordIndex].DateTime == timeWalker)
{
if (externalData.Records[currentRecordIndex].AccountBalanceCents == swarmopsCentsStart)
{
foundMatch = true;
// continue loop until on first record past initial conditions
}
currentRecordIndex++; // no test for array overrun in while
}
if (!foundMatch)
{
throw new InvalidOperationException("Unable to locate stable initial conditions for resynchronization");
}
// From here on out, every new timestamp should have the exact same delta in Swarmops as it has in the master file.
DateTime lastTimestamp = timeWalker;
timeWalker = externalData.Records[currentRecordIndex].DateTime;
long swarmopsDeltaCents = account.GetDeltaCents(lastTimestamp.AddSeconds(1), timeWalker.AddSeconds(1)); // "AddSeconds" because DeltaCents operates on ">= lowbound, < highbound"
long masterDeltaCents = externalData.Records[currentRecordIndex].TransactionNetCents;
}
private static Dictionary<string, object> _staticDataLookup;
}
}
@@ -282,7 +282,7 @@ namespace Swarmops.Site.Pages.Ledgers
this.LabelImportResultsHeader.Text = Resources.Pages.Ledgers.UploadBankFiles_FileImportedHeader_ErrorBalance;
}
}
catch (Exception exception)
catch (InvalidOperationException exception)
{
this.LiteralDownloadInstructionsModal.Text = exception.ToString(); // For debug purposes -- will not be shown to user
this.PanelErrorImage.Visible = true;
@@ -883,8 +883,13 @@ namespace Swarmops.Site.Pages.Ledgers
row.Comment = StripQuotes(parts[4]);
row.DateTime = DateTime.Parse(StripQuotes(parts[0]) + " " + StripQuotes(parts[1]), CultureInfo.InvariantCulture);
row.AmountCentsGross = Int64.Parse(StripQuotes(parts[7]).Replace(".", "").Replace(",", "").Replace(" ", ""));
row.FeeCents = Int64.Parse(StripQuotes(parts[8]).Replace(".", "").Replace(",", "").Replace(" ", ""), CultureInfo.InvariantCulture);
row.AmountCentsNet = Int64.Parse(StripQuotes(parts[9]).Replace(".", "").Replace(",", "").Replace(" ", ""));
row.FeeCents = 0;
if (parts[8] != "\"...\"") // used as fee field for payments held/cleared
{
row.FeeCents = Int64.Parse(StripQuotes(parts[8]).Replace(".", "").Replace(",", "").Replace(" ", ""), CultureInfo.InvariantCulture);
}
// Adjust for timezone -- necessary for Paypal and other int'l services