Added Payson import, too
This commit is contained in:
@@ -5,16 +5,8 @@ namespace Activizr.Logic.Financial
|
||||
{
|
||||
public class ImportedBankData
|
||||
{
|
||||
public ImportedBankData()
|
||||
{
|
||||
this.EarliestTransaction = DateTime.MaxValue;
|
||||
this.LatestTransaction = DateTime.MinValue;
|
||||
}
|
||||
|
||||
public double CurrentBalance;
|
||||
public Int64 CurrentBalanceCents;
|
||||
public DateTime EarliestTransaction;
|
||||
public DateTime LatestTransaction;
|
||||
public List<ImportedBankRow> Rows;
|
||||
}
|
||||
|
||||
|
||||
@@ -136,6 +136,9 @@
|
||||
<data name="UploadBankFiles_DownloadInstructions_PaypalFile" xml:space="preserve">
|
||||
<value>Log on to PayPal. Go to <strong>Your Account</strong>, and under the <strong>History</strong> menu, select <strong>Download History</strong>. Select <strong>Custom Date Range</strong>, enter the range since last download (as indicated) plus some overlap, and most importantly, select <strong>Tab Delimited - All Activity</strong>. Click <strong>Download</strong>.</p><p>Some time will pass before you get a mail from PayPal saying that the file is ready for download. It will be named <em>download.txt</em>. Upload this file.</value>
|
||||
</data>
|
||||
<data name="UploadBankFiles_DownloadInstructions_PaysonFile" xml:space="preserve">
|
||||
<value>Log on to Payson. Go to the <strong>History</strong> tab, and click <strong>Export history</strong>. The downloaded file will be named <em>Payson_Export_[date].xls</em>. Upload this file.</value>
|
||||
</data>
|
||||
<data name="UploadBankFiles_DownloadInstructions_SebAccountFile" xml:space="preserve">
|
||||
<value>Open SEB's internet bank. Go to <strong>your account</strong>, and enter the date span you wish to download in the search box. Press <strong>filter</strong>. After that, click the link to <strong>save the account history</strong>. A popup will appear. Choose <strong>csv</strong> and <strong>tab separated</strong>, then <strong>save</strong>. The downloaded file will be named <em>Kontohändelser.csv</em>. Upload this file.</value>
|
||||
<comment>Only translate to languages where SEB is active.</comment>
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 36 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 5.3 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
@@ -120,6 +120,7 @@ div.BankUploadInstructionsImage
|
||||
<h3><asp:Label ID="LabelSelectFileType" Text="Bank LOC" runat="server" /></h3>
|
||||
<asp:ImageButton OnClick="ButtonSebAccountFile_Click" CssClass="FileTypeImage" ID="ButtonSebAccountFile" runat="server" ImageUrl="~/Images/Ledgers/uploadbankfiles-type-seb-kontoutdrag.png"/>
|
||||
<asp:ImageButton OnClick="ButtonPaypalFile_Click" CssClass="FileTypeImage" ID="ButtonPaypalFile" runat="server" ImageUrl="~/Images/Ledgers/uploadbankfiles-type-paypal.png"/>
|
||||
<asp:ImageButton OnClick="ButtonPaysonFile_Click" CssClass="FileTypeImage" ID="ButtonPaysonFile" runat="server" ImageUrl="~/Images/Ledgers/uploadbankfiles-type-payson.png"/>
|
||||
<asp:HiddenField ID="HiddenFileType" runat="server"/>
|
||||
<div style="clear:both;margin-bottom:10px"></div>
|
||||
<div id="DivSelectAccount" <asp:Literal ID="LiteralSelectAccountDivStyle" runat="server" Text="style='opacity:0;display:none'" />>
|
||||
@@ -130,6 +131,7 @@ div.BankUploadInstructionsImage
|
||||
<Triggers>
|
||||
<asp:AsyncPostBackTrigger ControlID="ButtonSebAccountFile" EventName="Click" />
|
||||
<asp:AsyncPostBackTrigger ControlID="ButtonPaypalFile" EventName="Click" />
|
||||
<asp:AsyncPostBackTrigger ControlID="ButtonPaysonFile" EventName="Click" />
|
||||
</Triggers>
|
||||
</asp:UpdatePanel>
|
||||
|
||||
@@ -160,6 +162,7 @@ div.BankUploadInstructionsImage
|
||||
<asp:AsyncPostBackTrigger ControlID="ButtonSebAccountFile" EventName="Click" />
|
||||
<asp:AsyncPostbackTrigger ControlID="DropAccounts" EventName="SelectedIndexChanged" />
|
||||
<asp:AsyncPostBackTrigger ControlID="ButtonPaypalFile" EventName="Click" />
|
||||
<asp:AsyncPostBackTrigger ControlID="ButtonPaysonFile" EventName="Click" />
|
||||
</Triggers>
|
||||
</asp:UpdatePanel>
|
||||
|
||||
@@ -185,6 +188,7 @@ div.BankUploadInstructionsImage
|
||||
<Triggers>
|
||||
<asp:AsyncPostbackTrigger ControlID="ButtonSebAccountFile" EventName="Click" />
|
||||
<asp:AsyncPostBackTrigger ControlID="ButtonPaypalFile" EventName="Click" />
|
||||
<asp:AsyncPostBackTrigger ControlID="ButtonPaysonFile" EventName="Click" />
|
||||
</Triggers>
|
||||
</asp:UpdatePanel>
|
||||
</div>
|
||||
|
||||
@@ -110,10 +110,32 @@ namespace Activizr.Site.Pages.Ledgers
|
||||
}
|
||||
|
||||
|
||||
protected void ButtonPaysonFile_Click(object sender, ImageClickEventArgs e)
|
||||
{
|
||||
OnSelectedFileType();
|
||||
|
||||
this.HiddenFileType.Value = "Payson";
|
||||
|
||||
this.ButtonPaysonFile.CssClass = "FileTypeImage FileTypeImageSelected";
|
||||
|
||||
this.ImageDownloadInstructions.ImageUrl = "~/Images/Ledgers/uploadbankfiles-payson-small.png";
|
||||
|
||||
this.ImageDownloadInstructionsFull.ImageUrl =
|
||||
"~/Images/Ledgers/uploadbankfiles-payson-full.png";
|
||||
|
||||
this.LiteralDownloadInstructions.Text =
|
||||
this.LiteralDownloadInstructionsModal.Text =
|
||||
Resources.Pages.Ledgers.UploadBankFiles_DownloadInstructions_PaysonFile;
|
||||
|
||||
this.LiteralLastAccountRecord.Visible = true;
|
||||
}
|
||||
|
||||
|
||||
private void OnSelectedFileType()
|
||||
{
|
||||
this.ButtonPaypalFile.CssClass =
|
||||
this.ButtonSebAccountFile.CssClass = "FileTypeImage UnselectedType";
|
||||
this.ButtonSebAccountFile.CssClass =
|
||||
this.ButtonPaysonFile.CssClass = "FileTypeImage UnselectedType";
|
||||
|
||||
ScriptManager.RegisterClientScriptBlock(this.PanelFileTypeAccount, this.PanelFileTypeAccount.GetType(), "FadeType", "$(\".UnselectedType\").fadeTo('fast',0.2);", true);
|
||||
ScriptManager.RegisterClientScriptBlock(this.PanelFileTypeAccount, this.PanelFileTypeAccount.GetType(), "FadeAccount1",
|
||||
@@ -203,6 +225,9 @@ namespace Activizr.Site.Pages.Ledgers
|
||||
case "PayPal":
|
||||
bankData = ImportPaypal(file.InputStream);
|
||||
break;
|
||||
case "Payson":
|
||||
bankData = ImportPayson(file.InputStream);
|
||||
break;
|
||||
default:
|
||||
throw new InvalidOperationException("File type value not set to a valid filter name");
|
||||
}
|
||||
@@ -265,6 +290,14 @@ namespace Activizr.Site.Pages.Ledgers
|
||||
|
||||
protected class ImportResults
|
||||
{
|
||||
public ImportResults()
|
||||
{
|
||||
this.EarliestTransaction = DateTime.MaxValue;
|
||||
this.LatestTransaction = DateTime.MinValue;
|
||||
}
|
||||
|
||||
public DateTime EarliestTransaction;
|
||||
public DateTime LatestTransaction;
|
||||
public int TransactionsImported;
|
||||
public int DuplicateTransactions;
|
||||
public bool AccountBalanceMatchesBank;
|
||||
@@ -297,6 +330,19 @@ namespace Activizr.Site.Pages.Ledgers
|
||||
UpdateImportProgressBar(percent);
|
||||
}
|
||||
|
||||
// Update high- and low-water marks.
|
||||
|
||||
if (row.DateTime < result.EarliestTransaction)
|
||||
{
|
||||
result.EarliestTransaction = row.DateTime;
|
||||
}
|
||||
|
||||
if (row.DateTime > result.LatestTransaction)
|
||||
{
|
||||
result.LatestTransaction = row.DateTime;
|
||||
}
|
||||
|
||||
|
||||
// Each row is at least a stub, probably more.
|
||||
|
||||
string importKey = row.SuppliedTransactionId;
|
||||
@@ -415,7 +461,7 @@ namespace Activizr.Site.Pages.Ledgers
|
||||
// bookkeeping account with new transactions; it will already have fed transactions
|
||||
// beyond the end-of-file.
|
||||
|
||||
Int64 beyondEofCents = assetAccount.GetDeltaCents(import.LatestTransaction.AddSeconds(1), DateTime.Now.AddDays(2)); // Caution: the "AddSeconds(1)" is not foolproof, there may be other new txs on the same second.
|
||||
Int64 beyondEofCents = assetAccount.GetDeltaCents(result.LatestTransaction.AddSeconds(1), DateTime.Now.AddDays(2)); // Caution: the "AddSeconds(1)" is not foolproof, there may be other new txs on the same second.
|
||||
|
||||
if (databaseAccountBalanceCents - beyondEofCents == import.CurrentBalanceCents)
|
||||
{
|
||||
@@ -558,16 +604,70 @@ namespace Activizr.Site.Pages.Ledgers
|
||||
row.DateTime = row.DateTime.AddHours(8).ToLocalTime();
|
||||
|
||||
rows.Add(row);
|
||||
}
|
||||
|
||||
if (row.DateTime < result.EarliestTransaction)
|
||||
result.Rows = rows;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
protected ImportedBankData ImportPayson (Stream fileStream)
|
||||
{
|
||||
string contents = string.Empty;
|
||||
using (TextReader reader = new StreamReader(fileStream, Encoding.GetEncoding(1252)))
|
||||
{
|
||||
contents = reader.ReadToEnd();
|
||||
}
|
||||
|
||||
string regexPattern = @"<tr>\s+<td>\s*(?<datetime>[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2})\s*</td><td>(?<comment1>[^<]*)</td><td>[^>]*</td><td>(?<txid>[0-9]+)</td>\s*<td>(?<from>[^<]+)</td>\s*<td>(?<to>[^<]+)</td><td class=\""tal\"">(?<gross>[\-0-9,]+)</td><td class=\""tal\"">(?<fee>[\-0-9,]+)</td><td class=\""tal\"">(?<vat>[\-0-9,]+)</td><td class=\""tal\"">(?<net>[\-0-9,]+)</td><td class=\""tal\"">(?<balance>[\-0-9,]+)</td><td>(?<currency>[^<]+)</td><td>(?<reference>[^<]+)</td><td[^>]+?>(?<comment2>[^<]+)</td>";
|
||||
|
||||
Regex regex = new Regex(regexPattern, RegexOptions.Singleline);
|
||||
Match match = regex.Match(contents);
|
||||
|
||||
ImportedBankData result = new ImportedBankData();
|
||||
List<ImportedBankRow> rows = new List<ImportedBankRow>();
|
||||
|
||||
while (match.Success)
|
||||
{
|
||||
if (match.Groups["currency"].Value != "SEK")
|
||||
{
|
||||
result.EarliestTransaction = row.DateTime;
|
||||
continue; // HACK: Need to fix currency support at some time
|
||||
}
|
||||
|
||||
if (row.DateTime > result.LatestTransaction)
|
||||
// Get current balance from the first line in the file
|
||||
|
||||
if (result.CurrentBalanceCents == 0)
|
||||
{
|
||||
result.LatestTransaction = row.DateTime;
|
||||
result.CurrentBalanceCents = Int64.Parse(match.Groups["balance"].Value.Replace(",", "")) / 100;
|
||||
}
|
||||
|
||||
ImportedBankRow row = new ImportedBankRow();
|
||||
|
||||
string comment = HttpUtility.HtmlDecode(match.Groups["comment2"].Value.Trim());
|
||||
if (String.IsNullOrEmpty(comment))
|
||||
{
|
||||
comment = match.Groups["comment1"].Value.Trim();
|
||||
}
|
||||
|
||||
row.SuppliedTransactionId = "Payson-" + match.Groups["txid"].Value;
|
||||
row.Comment = comment;
|
||||
row.DateTime = DateTime.Parse(match.Groups["datetime"].Value, CultureInfo.InvariantCulture);
|
||||
row.AmountCentsGross = Int64.Parse(match.Groups["gross"].Value.Replace(".", "").Replace(",", "")) / 100;
|
||||
row.FeeCents = Int64.Parse(match.Groups["fee"].Value.Replace(".", "").Replace(",", "")) / 100;
|
||||
row.AmountCentsNet = Int64.Parse(match.Groups["net"].Value.Replace(".", "").Replace(",", "")) / 100;
|
||||
|
||||
if (row.DateTime < new DateTime(2010,4,1))
|
||||
{
|
||||
// This is for historical reasons of how Payson transactions were originally handled.
|
||||
// TODO: Re-handle by not touching closed books instead.
|
||||
|
||||
match = match.NextMatch();
|
||||
continue;
|
||||
}
|
||||
|
||||
rows.Add(row);
|
||||
|
||||
match = match.NextMatch();
|
||||
}
|
||||
|
||||
result.Rows = rows;
|
||||
|
||||
Reference in New Issue
Block a user