Committing payment file upload capability

This commit is contained in:
Rick Falkvinge
2012-01-08 15:01:59 +00:00
parent c5bcdcd7e5
commit ebfed16393
13 changed files with 413 additions and 19 deletions
+1 -1
View File
@@ -48,7 +48,7 @@ namespace Activizr.Database
DbCommand command =
GetDbCommand("SELECT" + paymentGroupFieldSequence +
"WHERE OrganizationId=" + organizationId.ToString() + " AND Tag='" + tag.Replace("'", "''") + "' AND Open=1",
"WHERE OrganizationId=" + organizationId.ToString() + " AND Tag='" + tag.Replace("'", "''") + "'",
connection);
using (DbDataReader reader = command.ExecuteReader())
+22
View File
@@ -197,6 +197,28 @@ namespace Activizr.Database
}
}
[Obsolete ("This should never be called, ever: it ruins trackability.")]
public void DeletePayment(int paymentId)
{
// This function is necessary since the Swedish bank system only allows for a dupe check AFTER all payments
// have been communicated, so they are created as data is received. If the data was a dupe, it is therefore
// deleted after the dupecheck triggers.
// That code was later re-written to do in-memory dupechecking. This delete function should never be called.
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand("DeletePayment", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "paymentId", paymentId);
command.ExecuteNonQuery();
}
}
#endregion
+15
View File
@@ -259,6 +259,21 @@ namespace Activizr.Logic.Structure
#region Properties and Get Methods
/// <summary>
/// Gets all economy-enabled organizations.
/// </summary>
public static Organizations EconomyEnabled
{
get
{
// TODO
Organizations result = new Organizations();
result.Add(Organization.PPSE);
return result;
}
}
public int[] Identities
{
get { return LogicServices.ObjectsToIdentifiers(ToArray()); }
+31
View File
@@ -124,6 +124,37 @@ public class PageV5Base : System.Web.UI.Page
base.OnPreRender(e);
}
protected string LocalizeCount (string resourceString, int count)
{
return LocalizeCount(resourceString, count, false);
}
protected string LocalizeCount (string resourceString, int count, bool capitalize)
{
string result;
string[] parts = resourceString.Split('|');
switch (count)
{
case 0:
result = parts[0];
break;
case 1:
result = parts[1];
break;
default:
result = String.Format(parts[2], count);
break;
}
if (capitalize)
{
result = Char.ToUpperInvariant(result[0]) + result.Substring(1);
}
return result;
}
}
@@ -133,6 +133,9 @@
<value>Bank Download Instructions</value>
<comment>Header above instructions for generation of the bank file type selected.</comment>
</data>
<data name="UploadBankFiles_DownloadInstructions_BankgiroSEFile" xml:space="preserve">
<value>Different banks have different means of obtaining the BGMAX format file. On SEB, you go to &lt;strong&gt;Payments&lt;/strong&gt; and &lt;strong&gt;File communications&lt;/strong&gt; to see a list of files to retrieve. Other banks will vary. In any case, upload them here.</value>
</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>
@@ -174,6 +177,30 @@
<value>Upload Bank Files</value>
<comment>Displayed above main content.</comment>
</data>
<data name="UploadBankFiles_PaymentAllDuplicates" xml:space="preserve">
<value>However, they were all duplicates of payment information that was previously uploaded. No new payment information has been imported to Activizr.</value>
</data>
<data name="UploadBankFiles_PaymentCount" xml:space="preserve">
<value>no payments|one payment|{0} payments</value>
<comment>none|one|many</comment>
</data>
<data name="UploadBankFiles_PaymentFileUploadedHeader" xml:space="preserve">
<value>Payment File Uploaded</value>
</data>
<data name="UploadBankFiles_PaymentGroupCount" xml:space="preserve">
<value>no payment groups|one payment group|{0} payment groups</value>
<comment>none|one|many</comment>
</data>
<data name="UploadBankFiles_PaymentSummary1" xml:space="preserve">
<value>The payment file contained {0}.</value>
</data>
<data name="UploadBankFiles_PaymentSummary2" xml:space="preserve">
<value>&lt;strong&gt;{0} {1:N2}&lt;/strong&gt; in payments were imported, distributed over {2} across {3}.</value>
<comment>like EUR 2,102.00 in 3 payments across 2 groups </comment>
</data>
<data name="UploadBankFiles_PaymentSummaryDuplicates" xml:space="preserve">
<value>There was also some duplicate information ({0} across {1}).</value>
</data>
<data name="UploadBankFiles_SelectAccount" xml:space="preserve">
<value>Select Bookkeeping Account</value>
<comment>Select prompt for the financial account in the organization's bookkeeping.</comment>
Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.7 KiB

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

+10 -6
View File
@@ -118,18 +118,20 @@ div.BankUploadInstructionsImage
<ContentTemplate>
<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 OnClick="ButtonBankgiroSEFile_Click" CssClass="FileTypeImage" ID="ButtonBankgiroSEFile" runat="server" ImageUrl="~/Images/Ledgers/uploadbankfiles-type-bankgirose.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:ImageButton OnClick="ButtonSebAccountFile_Click" CssClass="FileTypeImage" ID="ButtonSebFile" runat="server" ImageUrl="~/Images/Ledgers/uploadbankfiles-type-seb.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'" />>
<h3 style="padding-top:8px"><asp:Label ID="LabelSelectAccount" runat="server" Text="Bookkeeping Account LOC" /></h3>
<asp:DropDownList runat="server" ID="DropAccounts" OnSelectedIndexChanged="DropAccounts_SelectedIndexChanged" AutoPostBack="true"/>
<h3 style="padding-top:8px"><asp:Label ID="LabelSelectAccount" runat="server" Text="Bookkeeping Account LOC" /></h3>
<asp:DropDownList runat="server" ID="DropAccounts" OnSelectedIndexChanged="DropAccounts_SelectedIndexChanged" AutoPostBack="true"/>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonSebAccountFile" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonSebFile" EventName="Click" />
<asp:AsyncPostbackTrigger ControlID="ButtonBankgiroSEFile" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonPaypalFile" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonPaysonFile" EventName="Click" />
</Triggers>
@@ -159,7 +161,8 @@ div.BankUploadInstructionsImage
<h3><asp:Label ID="LabelUploadH3Header" Text="Upload File To Activizr LOC" runat="server" /></h3>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonSebAccountFile" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonSebFile" EventName="Click" />
<asp:AsyncPostbackTrigger ControlID="ButtonBankgiroSEFile" EventName="Click" />
<asp:AsyncPostbackTrigger ControlID="DropAccounts" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="ButtonPaypalFile" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonPaysonFile" EventName="Click" />
@@ -186,7 +189,8 @@ div.BankUploadInstructionsImage
<asp:Literal ID="LiteralDownloadInstructionsModal" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostbackTrigger ControlID="ButtonSebAccountFile" EventName="Click" />
<asp:AsyncPostbackTrigger ControlID="ButtonSebFile" EventName="Click" />
<asp:AsyncPostbackTrigger ControlID="ButtonBankgiroSEFile" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonPaypalFile" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonPaysonFile" EventName="Click" />
</Triggers>
+306 -10
View File
@@ -8,6 +8,7 @@ using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Activizr.Basic.Enums;
using Activizr.Basic.Types;
using Activizr.Logic.Financial;
using Activizr.Logic.Pirates;
using Activizr.Logic.Security;
@@ -73,9 +74,9 @@ namespace Activizr.Site.Pages.Ledgers
{
OnSelectedFileType();
this.HiddenFileType.Value = "SebAccount";
this.HiddenFileType.Value = "Seb";
this.ButtonSebAccountFile.CssClass = "FileTypeImage FileTypeImageSelected";
this.ButtonSebFile.CssClass = "FileTypeImage FileTypeImageSelected";
this.ImageDownloadInstructions.ImageUrl = "~/Images/Ledgers/uploadbankfiles-seb-kontoutdrag-small.png";
@@ -89,6 +90,27 @@ namespace Activizr.Site.Pages.Ledgers
}
protected void ButtonBankgiroSEFile_Click(object sender, ImageClickEventArgs e)
{
OnSelectedFileType();
this.HiddenFileType.Value = "BankgiroSE";
this.ButtonBankgiroSEFile.CssClass = "FileTypeImage FileTypeImageSelected";
this.ImageDownloadInstructions.ImageUrl = "~/Images/Ledgers/uploadbankfiles-bankgirose-small.png";
this.ImageDownloadInstructionsFull.ImageUrl =
"~/Images/Ledgers/uploadbankfiles-bankgirose-full.png";
this.LiteralDownloadInstructions.Text =
this.LiteralDownloadInstructionsModal.Text =
Resources.Pages.Ledgers.UploadBankFiles_DownloadInstructions_BankgiroSEFile;
this.LiteralLastAccountRecord.Visible = false;
}
protected void ButtonPaypalFile_Click(object sender, ImageClickEventArgs e)
{
OnSelectedFileType();
@@ -134,7 +156,8 @@ namespace Activizr.Site.Pages.Ledgers
private void OnSelectedFileType()
{
this.ButtonPaypalFile.CssClass =
this.ButtonSebAccountFile.CssClass =
this.ButtonSebFile.CssClass =
this.ButtonBankgiroSEFile.CssClass =
this.ButtonPaysonFile.CssClass = "FileTypeImage UnselectedType";
ScriptManager.RegisterClientScriptBlock(this.PanelFileTypeAccount, this.PanelFileTypeAccount.GetType(), "FadeType", "$(\".UnselectedType\").fadeTo('fast',0.2);", true);
@@ -219,7 +242,7 @@ namespace Activizr.Site.Pages.Ledgers
switch(this.HiddenFileType.Value)
{
case "SebAccount":
case "Seb":
bankData = ImportSebText(file.InputStream);
break;
case "PayPal":
@@ -228,6 +251,10 @@ namespace Activizr.Site.Pages.Ledgers
case "Payson":
bankData = ImportPayson(file.InputStream);
break;
case "BankgiroSE": // Payment file, not transaction file
ImportedPaymentData paymentData = ImportBankgiroSE(file.InputStream);
PresentPaymentFileResults(paymentData);
return; // Exit function here -- shortcut for when there's payment data and no transaction data
default:
throw new InvalidOperationException("File type value not set to a valid filter name");
}
@@ -265,10 +292,6 @@ namespace Activizr.Site.Pages.Ledgers
this.LabelImportResultsHeader.Text = Resources.Pages.Ledgers.UploadBankFiles_ImportError;
this.LiteralImportResults.Text = Resources.Pages.Ledgers.UploadBankFiles_ErrorInterpretation;
}
// Import the data to database
}
}
@@ -288,6 +311,42 @@ namespace Activizr.Site.Pages.Ledgers
}
private void PresentPaymentFileResults (ImportedPaymentData data)
{
string literalResult = String.Format(Resources.Pages.Ledgers.UploadBankFiles_PaymentSummary1,
LocalizeCount(Resources.Pages.Ledgers.UploadBankFiles_PaymentCount, data.DuplicatePaymentCount + data.PaymentCount)) + " ";
if (data.PaymentCount == 0 && data.DuplicatePaymentCount > 0)
{
literalResult += Resources.Pages.Ledgers.UploadBankFiles_PaymentAllDuplicates;
}
else if (data.PaymentCount > 0)
{
literalResult += String.Format(Resources.Pages.Ledgers.UploadBankFiles_PaymentSummary2,
data.Currency.Code,
data.PaymentCentsTotal/100.0,
LocalizeCount(Resources.Pages.Ledgers.UploadBankFiles_PaymentCount,
data.PaymentCount),
LocalizeCount(Resources.Pages.Ledgers.UploadBankFiles_PaymentGroupCount,
data.PaymentGroupCount));
if (data.DuplicatePaymentCount > 0)
{
literalResult += " " +
String.Format(Resources.Pages.Ledgers.UploadBankFiles_PaymentSummaryDuplicates,
LocalizeCount(Resources.Pages.Ledgers.UploadBankFiles_PaymentCount,
data.DuplicatePaymentCount),
LocalizeCount(
Resources.Pages.Ledgers.UploadBankFiles_PaymentGroupCount,
data.DuplicatePaymentGroupCount));
}
}
this.LiteralImportResults.Text = @"<p>" + literalResult + @"</p>";
this.LabelImportResultsHeader.Text = Resources.Pages.Ledgers.UploadBankFiles_PaymentFileUploadedHeader;
}
protected class ImportResults
{
public ImportResults()
@@ -410,10 +469,10 @@ namespace Activizr.Site.Pages.Ledgers
// Check for previously imported payment group
PaymentGroup group = PaymentGroup.FromTag(_currentOrganization,
"SEBGM" + DateTime.Today.Year.ToString() + // TODO: Get tagging from org
"SEBGM" + DateTime.Today.Year.ToString() + // TODO: Get tags from org
row.Comment.Substring(11));
if (group != null)
if (group != null && group.Open)
{
// There was a previously imported and not yet closed payment group matching this transaction
// Close the payment group and match the transaction against accounts receivable
@@ -487,10 +546,247 @@ namespace Activizr.Site.Pages.Ledgers
protected class ImportedPaymentData
{
public int PaymentGroupCount;
public int PaymentCount;
public int DuplicatePaymentCount;
public int DuplicatePaymentGroupCount;
public Int64 PaymentCentsTotal;
public Currency Currency;
}
// These Functions Copied From PWv4 -- may need adaptation and/or modernization
protected class InMemoryPayment
{
public InMemoryPayment()
{
this.Information = new List<InMemoryPaymentInformation>();
}
public Int64 AmountCents;
public string Reference;
public bool HasImage;
public string FromAccount;
public string Key;
public List<InMemoryPaymentInformation> Information;
}
protected class InMemoryPaymentInformation
{
public InMemoryPaymentInformation(PaymentInformationType type, string data)
{
this.Type = type;
this.Data = data;
}
public PaymentInformationType Type;
public string Data;
}
protected ImportedPaymentData ImportBankgiroSE(Stream fileStream)
{
string contents = string.Empty;
using (TextReader reader = new StreamReader(fileStream, Encoding.GetEncoding(1252)))
{
contents = reader.ReadToEnd();
}
string[] lines = contents.Split('\n');
DateTime timestamp = DateTime.MinValue;
int bgMaxVersion = 0;
ImportedPaymentData result = new ImportedPaymentData();
Currency currency = null;
List<InMemoryPayment> curPayments = null;
InMemoryPayment curPayment = null;
Int64 curPaymentGroupAmountCents = 0;
foreach (string line in lines)
{
if (line.Length < 2)
{
continue; // CR/LF split causes every other line to be empty
}
switch (line.Substring(0, 2))
{
case "01": // BGMAX intro
string bgmaxmarker = line.Substring(2, 20).Trim();
if (bgmaxmarker != "BGMAX")
{
throw new Exception("bad format -- not bgmax");
}
bgMaxVersion = Int32.Parse(line.Substring(22, 2));
timestamp = DateTime.ParseExact(line.Substring(24, 20), "yyyyMMddHHmmssffffff",
CultureInfo.InvariantCulture);
break;
case "05": // Begin payment group
if (bgMaxVersion < 1)
{
throw new InvalidOperationException("BGMax record must precede first payment group");
}
curPayments = new List<InMemoryPayment>();
currency = Currency.FromCode(line.Substring(22, 3));
result.Currency = currency;
curPaymentGroupAmountCents = 0;
break;
case "20": // Begin payment
if (curPayments == null)
{
throw new InvalidOperationException("Payment group start must precede first payment");
}
// If we have a previous payment in this group, add it to list
if (curPayment != null)
{
curPayments.Add(curPayment);
}
curPayment = new InMemoryPayment();
curPayment.FromAccount = line.Substring(2, 10);
curPayment.Reference = line.Substring(12, 25).Trim(); // left space padded in BgMax format
curPayment.AmountCents = Int64.Parse(line.Substring(37, 18), CultureInfo.InvariantCulture);
curPayment.Key = "SEBGM" + DateTime.Today.Year.ToString() + line.Substring(57, 12);
curPayment.HasImage = (line[69] == '1' ? true : false);
// TODO: Check if existed already -- must do -- IMPORTANT (same todo as below)
curPaymentGroupAmountCents += curPayment.AmountCents;
break;
case "25": // Payment info: Freeform
if (curPayment == null)
{
throw new InvalidOperationException("Payment start must precede payment information");
}
curPayment.Information.Add(new InMemoryPaymentInformation(PaymentInformationType.Freeform, line.Substring(2, 50).Trim()));
break;
case "26": // Payment info: Name
if (curPayment == null)
{
throw new InvalidOperationException("Payment start must precede payment information");
}
curPayment.Information.Add(new InMemoryPaymentInformation(PaymentInformationType.Name, line.Substring(2, 35).Trim()));
break;
case "27": // Payment info: Street, postal code
if (curPayment == null)
{
throw new InvalidOperationException("Payment start must precede payment information");
}
curPayment.Information.Add(new InMemoryPaymentInformation(PaymentInformationType.Street, line.Substring(2, 35).Trim()));
curPayment.Information.Add(new InMemoryPaymentInformation(PaymentInformationType.PostalCode, line.Substring(37, 9).Replace(" ", ""))); // also removes inspace
break;
case "28": // Payment info: City, Country
if (curPayment == null)
{
throw new InvalidOperationException("Payment start must precede payment information");
}
curPayment.Information.Add(new InMemoryPaymentInformation(PaymentInformationType.City, line.Substring(2, 35).Trim()));
curPayment.Information.Add(new InMemoryPaymentInformation(PaymentInformationType.Country, line.Substring(37, 35).Trim()));
curPayment.Information.Add(new InMemoryPaymentInformation(PaymentInformationType.CountryCode, line.Substring(72, 2).Trim()));
break;
case "29": // Payment info: Organization or personal ID number
if (curPayment == null)
{
throw new InvalidOperationException("Payment start must precede payment information");
}
curPayment.Information.Add(new InMemoryPaymentInformation(PaymentInformationType.OrgNumber, line.Substring(2, 12).Trim()));
break;
case "15": // End payment group
if (curPayments == null)
{
throw new InvalidOperationException("Payment group start must precede payment group end");
}
// Add currently building payment to group before committing
curPayments.Add(curPayment);
// This is where we finally get a unique identifier that allows us to dupecheck.
string tag = timestamp.Year.ToString() + line.Substring(45, 5);
if (timestamp.Year >= 2012)
{
tag = "SEBGM" + tag; // a flag date where we add a tag for SE Bankgiro Max format, enabling other formats in other namespaces
}
// Dupe check
PaymentGroup dupe = PaymentGroup.FromTag(_currentOrganization, tag);
if (dupe == null)
{
// Commit all recorded payments
PaymentGroup newGroup = PaymentGroup.Create(_currentOrganization, timestamp, currency, _currentUser);
result.PaymentGroupCount++;
Int64 reportedAmountCents = Int64.Parse(line.Substring(50, 18), CultureInfo.InvariantCulture); // may differ because of duplicates
newGroup.AmountCents = curPaymentGroupAmountCents;
result.PaymentCentsTotal += curPaymentGroupAmountCents;
foreach (InMemoryPayment payment in curPayments)
{
// TODO: DUPECHECK PAYMENT KEY AS WELL (same todo as above)
Payment newPayment = newGroup.CreatePayment(payment.AmountCents/100.0, payment.Reference,
payment.FromAccount, payment.Key,
payment.HasImage);
foreach (InMemoryPaymentInformation paymentInfo in payment.Information)
{
newPayment.AddInformation(paymentInfo.Type, paymentInfo.Data);
}
result.PaymentCount++;
}
newGroup.Tag = tag;
newGroup.Open = true; // flags payment group as ready
newGroup.MapTransaction();
}
else
{
// This was a duplicate
result.DuplicatePaymentGroupCount++;
result.DuplicatePaymentCount += curPayments.Count;
}
curPayment = null;
curPayments = null;
break;
case "70": // BGMAX termination
break; // don't care
default:
break; // don't care about other fields
}
}
if (timestamp.Year < 1900)
{
// The file contained no instructions at all
throw new ArgumentException("This was not a BGMAX-SE file");
}
return result;
}
protected ImportedBankData ImportSebText(Stream fileStream)
{
string contents = string.Empty;
+1 -2
View File
@@ -10,8 +10,7 @@ namespace Activizr.Utility.Financial
{
public static void Run()
{
Organizations economyEnabledOrgs = new Organizations();
economyEnabledOrgs.Add(Organization.PPSE);
Organizations economyEnabledOrgs = Organizations.EconomyEnabled;
foreach (Organization organization in economyEnabledOrgs)
{