PayPal file import seems to work now, building + deploying

This commit is contained in:
Rick Falkvinge
2012-01-06 15:36:58 +00:00
parent 1278ca803f
commit 4c17e1a16a
9 changed files with 140 additions and 38 deletions
+9 -2
View File
@@ -5,8 +5,16 @@ 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;
}
@@ -16,10 +24,9 @@ namespace Activizr.Logic.Financial
public string HashBase;
public string SuppliedTransactionId;
public Int64 CurrentBalanceCents;
public double CurrentBalance;
public Int64 AmountCentsNet;
public Int64 AmountCentsGross;
public double Fee;
public Int64 FeeCents;
public string Comment;
};
}
+1 -1
View File
@@ -14,7 +14,7 @@
<SourceWebPhysicalPath>..\Site5</SourceWebPhysicalPath>
<SourceWebProject>{367F36EA-AE1C-4E15-BAF9-BA75B18B3807}|C:\Lab\Activizr\Site5</SourceWebProject>
<SourceWebVirtualPath>/Site5</SourceWebVirtualPath>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
+7 -1
View File
@@ -133,12 +133,16 @@
<value>Bank Download Instructions</value>
<comment>Header above instructions for generation of the bank file type selected.</comment>
</data>
<data name="UploadBankFiles_DownloadInstructions_PaypalFile" xml:space="preserve">
<value>Log on to PayPal. Go to &lt;strong&gt;Your Account&lt;/strong&gt;, and under the &lt;strong&gt;History&lt;/strong&gt; menu, select &lt;strong&gt;Download History&lt;/strong&gt;. Select &lt;strong&gt;Custom Date Range&lt;/strong&gt;, enter the range since last download (as indicated) plus some overlap, and most importantly, select &lt;strong&gt;Tab Delimited - All Activity&lt;/strong&gt;. Click &lt;strong&gt;Download&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;Some time will pass before you get a mail from PayPal saying that the file is ready for download. It will be named &lt;em&gt;download.txt&lt;/em&gt;. Upload this file.</value>
</data>
<data name="UploadBankFiles_DownloadInstructions_SebAccountFile" xml:space="preserve">
<value>Open SEB's internet bank. Go to &lt;strong&gt;your account&lt;/strong&gt;, and enter the date span you wish to download in the search box. Press &lt;strong&gt;filter&lt;/strong&gt;. After that, click the link to &lt;strong&gt;save the account history&lt;/strong&gt;. A popup will appear. Choose &lt;strong&gt;csv&lt;/strong&gt; and &lt;strong&gt;tab separated&lt;/strong&gt;, then &lt;strong&gt;save&lt;/strong&gt;. The downloaded file will be named &lt;em&gt;Kontohändelser.csv&lt;/em&gt;.</value>
<value>Open SEB's internet bank. Go to &lt;strong&gt;your account&lt;/strong&gt;, and enter the date span you wish to download in the search box. Press &lt;strong&gt;filter&lt;/strong&gt;. After that, click the link to &lt;strong&gt;save the account history&lt;/strong&gt;. A popup will appear. Choose &lt;strong&gt;csv&lt;/strong&gt; and &lt;strong&gt;tab separated&lt;/strong&gt;, then &lt;strong&gt;save&lt;/strong&gt;. The downloaded file will be named &lt;em&gt;Kontohändelser.csv&lt;/em&gt;. Upload this file.</value>
<comment>Only translate to languages where SEB is active.</comment>
</data>
<data name="UploadBankFiles_ErrorBalance" xml:space="preserve">
<value>&lt;p&gt;The file contained {0:N0} transactions. &lt;strong&gt;{1:N0} transactions&lt;/strong&gt; have been successfully imported into the &lt;em&gt;{2}&lt;/em&gt; bookkeeping account. ({3:N0} transactions were duplicates.)&lt;/p&gt;&lt;p&gt;&lt;strong&gt;However, the bank balance does not match the records in Activizr.&lt;/strong&gt; This is usually due to missing transactions. Please upload a transaction list with a larger time span to make sure that Activizr sees all transactions.&lt;/p&gt;&lt;p&gt;Activizr's bookkeeping account is currently &lt;strong&gt;not in sync&lt;/strong&gt; with that of the bank.&lt;/p&gt;</value>
<comment>Displayed when import successful, but end balance out of sync. Assumes plural along all pasted numbers.</comment>
</data>
<data name="UploadBankFiles_ErrorInterpretation" xml:space="preserve">
<value>&lt;h3&gt;The bank file could not be imported.&lt;/h3&gt;&lt;p&gt;This has one of three reasons: you missed some detail in the download instructions, the bank has changed its format, or you accidentally uploaded the wrong file entirely (like a photo from your last party instead of the transactions list). No transactions were imported at all. &lt;strong&gt;Please retry.&lt;/strong&gt;</value>
@@ -149,6 +153,7 @@
</data>
<data name="UploadBankFiles_FileImportedHeader_ErrorBalance" xml:space="preserve">
<value>File Imported, But Data Out Of Sync</value>
<comment>Displayed as header after a good processing that ended in sync failure</comment>
</data>
<data name="UploadBankFiles_ImportError" xml:space="preserve">
<value>Error on importing file</value>
@@ -156,6 +161,7 @@
</data>
<data name="UploadBankFiles_ImportResults" xml:space="preserve">
<value>&lt;p&gt;The file contained {0:N0} transactions. &lt;strong&gt;{1:N0} transactions&lt;/strong&gt; have been successfully imported into the &lt;em&gt;{2}&lt;/em&gt; bookkeeping account. ({3:N0} transactions were duplicates.)&lt;/p&gt;&lt;p&gt;After import, the account balance in Activizr matches that of the bank.&lt;/p&gt;</value>
<comment>Displayed on successful import. Assumes plural among all pasted numbers.</comment>
</data>
<data name="UploadBankFiles_Info" xml:space="preserve">
<value>Here, you can upload files from your bank or payment provider into your organization's bookkeeping. Examples include account history and payments received.</value>
Binary file not shown.

After

Width:  |  Height:  |  Size: 158 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.1 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

+10 -7
View File
@@ -5,10 +5,10 @@
<style type="text/css">
input.FileTypeImage
{
width:160px;
height:70px;
width:90px;
height:45px;
border: 1px solid #1C397E !important;
margin-right:11px;
margin-right:20px;
margin-top:7px;
margin-bottom:6px;
float:left;
@@ -16,9 +16,9 @@ input.FileTypeImage
input.FileTypeImageSelected
{
box-shadow: 0px 0px 4px 4px #FFBC37;
-moz-box-shadow: 0px 0px 4px 4px #FFBC37;
-webkit-box-shadow: 0px 0px 4px 4px #FFBC37;
box-shadow: 0px 0px 2px 2px #FFBC37;
-moz-box-shadow: 0px 0px 2px 2px #FFBC37;
-webkit-box-shadow: 0px 0px 2px 2px #FFBC37;
border: 1px solid #C78B15 !important;
}
@@ -119,7 +119,7 @@ div.BankUploadInstructionsImage
<h2><asp:Label ID="LabelSelectBankAndAccount" Text="Select Bank And Bookkeeping LOC" runat="server"/></h2>
<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 CssClass="FileTypeImage" ID="ButtonSebPaymentFile" runat="server" ImageUrl="~/Images/Ledgers/uploadbankfiles-type-seb-bankgirofil.png"/>
<asp:ImageButton OnClick="ButtonPaypalFile_Click" CssClass="FileTypeImage" ID="ButtonPaypalFile" runat="server" ImageUrl="~/Images/Ledgers/uploadbankfiles-type-paypal.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'" />>
@@ -129,6 +129,7 @@ div.BankUploadInstructionsImage
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonSebAccountFile" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonPaypalFile" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
@@ -158,6 +159,7 @@ div.BankUploadInstructionsImage
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonSebAccountFile" EventName="Click" />
<asp:AsyncPostbackTrigger ControlID="DropAccounts" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="ButtonPaypalFile" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
@@ -182,6 +184,7 @@ div.BankUploadInstructionsImage
</ContentTemplate>
<Triggers>
<asp:AsyncPostbackTrigger ControlID="ButtonSebAccountFile" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonPaypalFile" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
+113 -27
View File
@@ -50,6 +50,11 @@ namespace Activizr.Site.Pages.Ledgers
this.LabelProcessing.Text = Resources.Pages.Global.Global_ProcessingFile;
this.LinkUploadAnother.Text = Resources.Pages.Ledgers.UploadBankFiles_UploadAnother;
this.LabelModalInstructionHeader.Text = Resources.Pages.Ledgers.UploadBankFiles_BankScreenshot;
// Populate the asset account dropdown, if needed for file type
PopulateAccountDropDown();
}
if (!IsPostBack)
@@ -66,19 +71,11 @@ namespace Activizr.Site.Pages.Ledgers
protected void ButtonSebAccountFile_Click(object sender, ImageClickEventArgs e)
{
OnSelectedFileType();
this.HiddenFileType.Value = "SebAccount";
this.ButtonSebAccountFile.CssClass = "FileTypeImage FileTypeImageSelected";
this.ButtonSebPaymentFile.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",
"$(\"#DivSelectAccount\").fadeTo('slow', 1.0);", true);
ScriptManager.RegisterClientScriptBlock(this.PanelFileTypeAccount, this.PanelFileTypeAccount.GetType(), "FadeAccount2",
"$(\"#DivSelectAccount\").css('display','inline');", true);
PopulateAccountDropDown();
this.ButtonSebAccountFile.Enabled = false;
this.ButtonSebPaymentFile.Enabled = false;
this.ImageDownloadInstructions.ImageUrl = "~/Images/Ledgers/uploadbankfiles-seb-kontoutdrag-small.png";
@@ -91,6 +88,43 @@ namespace Activizr.Site.Pages.Ledgers
}
protected void ButtonPaypalFile_Click(object sender, ImageClickEventArgs e)
{
OnSelectedFileType();
this.HiddenFileType.Value = "PayPal";
this.ButtonPaypalFile.CssClass = "FileTypeImage FileTypeImageSelected";
this.ImageDownloadInstructions.ImageUrl = "~/Images/Ledgers/uploadbankfiles-paypal-small.png";
this.ImageDownloadInstructionsFull.ImageUrl =
"~/Images/Ledgers/uploadbankfiles-paypal-full.png";
this.LiteralDownloadInstructions.Text =
this.LiteralDownloadInstructionsModal.Text =
Resources.Pages.Ledgers.UploadBankFiles_DownloadInstructions_PaypalFile;
this.LiteralLastAccountRecord.Visible = false;
}
private void OnSelectedFileType()
{
this.ButtonPaypalFile.CssClass =
this.ButtonSebAccountFile.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",
"$(\"#DivSelectAccount\").fadeTo('slow', 1.0);", true);
ScriptManager.RegisterClientScriptBlock(this.PanelFileTypeAccount, this.PanelFileTypeAccount.GetType(), "FadeAccount2",
"$(\"#DivSelectAccount\").css('display','inline');", true);
this.LiteralLastAccountRecord.Visible = true;
}
private void PopulateAccountDropDown()
{
FinancialAccounts accounts = FinancialAccounts.ForOrganization(_currentOrganization,
@@ -161,9 +195,18 @@ namespace Activizr.Site.Pages.Ledgers
UpdateImportProgressBar(1);
// Assume SEB. Later, parameterize to filters.
switch(this.HiddenFileType.Value)
{
case "SebAccount":
bankData = ImportSebText(file.InputStream);
break;
case "PayPal":
bankData = ImportPaypal(file.InputStream);
break;
default:
throw new InvalidOperationException("File type value not set to a valid filter name");
}
bankData = ImportSebText(file.InputStream);
this.LabelProcessing.Text = bankData.Rows.Count.ToString();
ImportResults results = ProcessImportedData(bankData);
@@ -268,7 +311,7 @@ namespace Activizr.Site.Pages.Ledgers
commentKey = commentKey.ToLowerInvariant();
}
string hashKey = row.HashBase + commentKey + (row.AmountCentsNet / 100.0).ToString(CultureInfo.InvariantCulture) + row.CurrentBalance.ToString(CultureInfo.InvariantCulture) +
string hashKey = row.HashBase + commentKey + (row.AmountCentsNet / 100.0).ToString(CultureInfo.InvariantCulture) + (row.CurrentBalanceCents / 100.0).ToString(CultureInfo.InvariantCulture) +
row.DateTime.ToString("yyyy-MM-dd-hh-mm-ss");
importKey = SHA1.Hash(hashKey).Replace(" ", "");
@@ -340,11 +383,11 @@ namespace Activizr.Site.Pages.Ledgers
}
else if (amountCents > 0)
{
if (row.Fee < 0)
if (row.FeeCents < 0)
{
// This is always an autodeposit, if there is a fee (which is never > 0.0)
transaction.AddRow(_currentOrganization.FinancialAccounts.CostsBankFees, -row.Fee, _currentUser);
transaction.AddRow(_currentOrganization.FinancialAccounts.CostsBankFees, -row.FeeCents, _currentUser);
transaction.AddRow(autoDepositAccount, -row.AmountCentsGross, _currentUser);
}
else if (amountCents < autoDepositLimit * 100)
@@ -367,7 +410,14 @@ namespace Activizr.Site.Pages.Ledgers
Int64 databaseAccountBalanceCents = assetAccount.BalanceTotalCents;
if (databaseAccountBalanceCents == import.CurrentBalanceCents)
// Subtract any transactions made after the most recent imported transaction.
// This is necessary in case of Paypal and others which continuously feed the
// 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.
if (databaseAccountBalanceCents - beyondEofCents == import.CurrentBalanceCents)
{
Payouts.AutomatchAgainstUnbalancedTransactions(_currentOrganization);
result.AccountBalanceMatchesBank = true;
@@ -397,8 +447,11 @@ namespace Activizr.Site.Pages.Ledgers
protected ImportedBankData ImportSebText(Stream fileStream)
{
TextReader reader = new StreamReader(fileStream, Encoding.GetEncoding(1252));
string contents = reader.ReadToEnd();
string contents = string.Empty;
using (TextReader reader = new StreamReader(fileStream, Encoding.GetEncoding(1252)))
{
contents = reader.ReadToEnd();
}
string[] lines = contents.Split('\n');
@@ -434,7 +487,6 @@ namespace Activizr.Site.Pages.Ledgers
row.DateTime = DateTime.Parse(lineParts[0]);
row.Comment = lineParts[3].Replace(" ", " ").Trim();
row.CurrentBalanceCents = Int64.Parse(lineParts[5].Trim().Replace(",", ""), CultureInfo.InvariantCulture);
row.CurrentBalance = row.CurrentBalanceCents/100.0;
row.AmountCentsNet = Int64.Parse(amountString);
row.HashBase = lineParts[2];
@@ -446,9 +498,21 @@ namespace Activizr.Site.Pages.Ledgers
}
protected ImportedBankData ImportPaypal(string contents)
protected ImportedBankData ImportPaypal(Stream fileStream)
{
string contents = string.Empty;
using (TextReader reader = new StreamReader(fileStream, Encoding.GetEncoding(1252)))
{
contents = reader.ReadToEnd();
}
string[] lines = contents.Split('\n');
if (lines[0].Trim() != "Date\t Time\t Time Zone\t Name\t Type\t Status\t Currency\t Gross\t Fee\t Net\t From Email Address\t To Email Address\t Transaction ID\t Counterparty Status\t Address Status\t Item Title\t Item ID\t Shipping and Handling Amount\t Insurance Amount\t Sales Tax\t Option 1 Name\t Option 1 Value\t Option 2 Name\t Option 2 Value\t Auction Site\t Buyer ID\t Item URL\t Closing Date\t Escrow Id\t Invoice Id\t Reference Txn ID\t Invoice Number\t Custom Number\t Receipt ID\t Balance\t Address Line 1\t Address Line 2/District/Neighborhood\t Town/City\t State/Province/Region/County/Territory/Prefecture/Republic\t Zip/Postal Code\t Country\t Contact Phone Number")
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
{
throw new ArgumentException("Unable to parse");
}
ImportedBankData result = new ImportedBankData();
List<ImportedBankRow> rows = new List<ImportedBankRow>();
@@ -468,9 +532,9 @@ namespace Activizr.Site.Pages.Ledgers
// Get current balance from the first line in the file
if (result.CurrentBalance == 0.0)
if (result.CurrentBalanceCents == 0)
{
result.CurrentBalance = Double.Parse(StripQuotes(parts[34]), CultureInfo.InvariantCulture);
result.CurrentBalanceCents = Int64.Parse(StripQuotes(parts[34].Replace(".","").Replace(",","")), CultureInfo.InvariantCulture);
}
ImportedBankRow row = new ImportedBankRow();
@@ -478,11 +542,32 @@ namespace Activizr.Site.Pages.Ledgers
row.SuppliedTransactionId = StripQuotes(parts[12]);
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(".", ""));
row.Fee = Double.Parse(StripQuotes(parts[8]), CultureInfo.InvariantCulture);
row.AmountCentsNet = Int64.Parse(StripQuotes(parts[9]).Replace(".", ""));
row.AmountCentsGross = Int64.Parse(StripQuotes(parts[7]).Replace(".", "").Replace(",", ""));
row.FeeCents = Int64.Parse(StripQuotes(parts[8]).Replace(".", "").Replace(",", ""), CultureInfo.InvariantCulture);
row.AmountCentsNet = Int64.Parse(StripQuotes(parts[9]).Replace(".", "").Replace(",", ""));
// Adjust for timezone -- necessary for Paypal and other int'l services
string timeZoneString = StripQuotes(parts[2]);
if (timeZoneString != "PST" && timeZoneString != "PDT") // this is cheating slightly as DSTs do not coincide
{
throw new ArgumentException("Paypal import files should have time zone PST");
}
row.DateTime = row.DateTime.AddHours(8).ToLocalTime();
rows.Add(row);
if (row.DateTime < result.EarliestTransaction)
{
result.EarliestTransaction = row.DateTime;
}
if (row.DateTime > result.LatestTransaction)
{
result.LatestTransaction = row.DateTime;
}
}
result.Rows = rows;
@@ -520,5 +605,6 @@ namespace Activizr.Site.Pages.Ledgers
return input;
}
}
}