diff --git a/Logic/Financial/ImportedBankData.cs b/Logic/Financial/ImportedBankData.cs index 8aaf6119..d960b01e 100644 --- a/Logic/Financial/ImportedBankData.cs +++ b/Logic/Financial/ImportedBankData.cs @@ -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 Rows; } diff --git a/Site5/App_GlobalResources/Pages.Ledgers.resx b/Site5/App_GlobalResources/Pages.Ledgers.resx index 56e46e5e..6ebd5cee 100644 --- a/Site5/App_GlobalResources/Pages.Ledgers.resx +++ b/Site5/App_GlobalResources/Pages.Ledgers.resx @@ -136,6 +136,9 @@ 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. + + 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. + 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. Only translate to languages where SEB is active. diff --git a/Site5/Images/Ledgers/uploadbankfiles-payson-full.png b/Site5/Images/Ledgers/uploadbankfiles-payson-full.png new file mode 100644 index 00000000..c36d454e Binary files /dev/null and b/Site5/Images/Ledgers/uploadbankfiles-payson-full.png differ diff --git a/Site5/Images/Ledgers/uploadbankfiles-payson-small.png b/Site5/Images/Ledgers/uploadbankfiles-payson-small.png new file mode 100644 index 00000000..b4b13dba Binary files /dev/null and b/Site5/Images/Ledgers/uploadbankfiles-payson-small.png differ diff --git a/Site5/Images/Ledgers/uploadbankfiles-type-payson.png b/Site5/Images/Ledgers/uploadbankfiles-type-payson.png new file mode 100644 index 00000000..bd6e9176 Binary files /dev/null and b/Site5/Images/Ledgers/uploadbankfiles-type-payson.png differ diff --git a/Site5/Pages/v5/Ledgers/UploadBankFiles.aspx b/Site5/Pages/v5/Ledgers/UploadBankFiles.aspx index d3e45585..fc940677 100644 --- a/Site5/Pages/v5/Ledgers/UploadBankFiles.aspx +++ b/Site5/Pages/v5/Ledgers/UploadBankFiles.aspx @@ -120,6 +120,7 @@ div.BankUploadInstructionsImage

+
> @@ -130,6 +131,7 @@ div.BankUploadInstructionsImage + @@ -160,6 +162,7 @@ div.BankUploadInstructionsImage + @@ -185,6 +188,7 @@ div.BankUploadInstructionsImage +
diff --git a/Site5/Pages/v5/Ledgers/UploadBankFiles.aspx.cs b/Site5/Pages/v5/Ledgers/UploadBankFiles.aspx.cs index 3950ee65..fc74e8c9 100644 --- a/Site5/Pages/v5/Ledgers/UploadBankFiles.aspx.cs +++ b/Site5/Pages/v5/Ledgers/UploadBankFiles.aspx.cs @@ -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 = @"\s+\s*(?[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2})\s*(?[^<]*)[^>]*(?[0-9]+)\s*(?[^<]+)\s*(?[^<]+)(?[\-0-9,]+)(?[\-0-9,]+)(?[\-0-9,]+)(?[\-0-9,]+)(?[\-0-9,]+)(?[^<]+)(?[^<]+)]+?>(?[^<]+)"; + + Regex regex = new Regex(regexPattern, RegexOptions.Singleline); + Match match = regex.Match(contents); + + ImportedBankData result = new ImportedBankData(); + List rows = new List(); + + 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;