using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Globalization; using System.Text.RegularExpressions; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Activizr.Logic.Financial; using Activizr.Logic.Pirates; using Activizr.Logic.Security; using Activizr.Logic.Structure; using Activizr.Basic.Enums; public partial class Pages_v4_ImportBankTransactions : PageV4Base { private static readonly int PPOrgId = Organization.PPSEid; protected void Page_Load (object sender, EventArgs e) { // Identify person // HACK as to who has access to what data -- improve this later if (!IsPostBack) { if (_currentUser.Identity == 1) { this.DropOrganizations.Items.Add(new ListItem("Piratpartiet SE", Organization.PPSEid.ToString())); this.DropOrganizations.Items.Add(new ListItem("Sandbox", "3")); this.DropOrganizations.Items.Add(new ListItem("Rick's Sandbox", "55")); // Debug & Test } else if (_authority.HasPermission(Permission.CanDoEconomyTransactions, PPOrgId, -1, Authorization.Flag.ExactOrganization)) { this.DropOrganizations.Items.Add(new ListItem("Piratpartiet SE", Organization.PPSEid.ToString() )); } else { // Show some dialog saying that the user has no access to the tools on this page } } // Styles this.TextData.Style[HtmlTextWriterStyle.Width] = "100%"; } protected void DropOrganizations_SelectedIndexChanged (object sender, EventArgs e) { int organizationId = Int32.Parse(this.DropOrganizations.SelectedValue); Organization organization = Organization.FromIdentity(organizationId); // TODO: Security parsing this.DropAssetAccount.Items.Clear(); this.DropAutoDeposits.Items.Clear(); this.DropAutoWithdrawals.Items.Clear(); if (organizationId == PPOrgId && _authority.HasPermission(Permission.CanDoEconomyTransactions, PPOrgId, -1, Authorization.Flag.ExactOrganization)) { // TODO: Populate from a database table instead FinancialAccount assetAccount = organization.FinancialAccounts.AssetsBankAccountMain; FinancialAccount incomeAccount = organization.FinancialAccounts.IncomeDonations; FinancialAccount costAccount = organization.FinancialAccounts.CostsBankFees; this.DropAssetAccount.Items.Add(new ListItem(assetAccount.Name, assetAccount.Identity.ToString())); this.DropAssetAccount.Items.Add(new ListItem("Paypal", organization.FinancialAccounts.AssetsPaypal.Identity.ToString())); this.DropAutoDeposits.Items.Add(new ListItem(incomeAccount.Name, incomeAccount.Identity.ToString())); this.DropAutoWithdrawals.Items.Add(new ListItem(costAccount.Name, costAccount.Identity.ToString())); this.TextDepositLimit.Text = "1000"; this.TextWithdrawalLimit.Text = "0"; } else if (organizationId == 55) { // Rick's sandbox. Please leave in code for the time being. FinancialAccount assetAccount = FinancialAccount.FromIdentity(60); FinancialAccount incomeAccount = FinancialAccount.FromIdentity(63); FinancialAccount costAccount = FinancialAccount.FromIdentity(65); this.DropAssetAccount.Items.Add(new ListItem(assetAccount.Name, assetAccount.Identity.ToString())); this.DropAutoDeposits.Items.Add(new ListItem(incomeAccount.Name, incomeAccount.Identity.ToString())); this.DropAutoWithdrawals.Items.Add(new ListItem(costAccount.Name, costAccount.Identity.ToString())); this.TextDepositLimit.Text = "7500"; this.TextWithdrawalLimit.Text = "0"; } } protected void DropFilters_SelectedIndexChanged (object sender, EventArgs e) { switch (this.DropFilters.SelectedValue) { case "SEB.se": this.LabelFilterInstructions.Text = "Go to the account overview and select a specific account. You will see 100 transactions per page. Select this entire web page and paste it into the area below, then click Process. You will probably need to repeat this procedure for several pages."; break; case "Paypal": this.LabelFilterInstructions.Text = "Download the account history, all history, tab separated, for the specific time period, plus some overlap with previous history. Paste the entire contents of the downloaded file into the field below."; break; default: this.LabelFilterInstructions.Text = "Select an import filter."; break; } } protected void ButtonProcessText_Click (object sender, EventArgs e) { _currentUser = Person.FromIdentity(Int32.Parse(HttpContext.Current.User.Identity.Name)); // Process the contents of the paste area. ImportResult import = null; switch (this.DropFilters.SelectedValue) { case "SEB.se": import = ImportSebText(this.TextData.Text); break; case "Paypal": import = ImportPaypal(this.TextData.Text); break; default: throw new InvalidOperationException("Bad translation filter for this import"); // TODO: Print error message and bail out } ProcessImportedData(import); this.TextData.Text = string.Empty; } protected void ProcessImportedData (ImportResult import) { FinancialAccount assetAccount = FinancialAccount.FromIdentity(Int32.Parse(this.DropAssetAccount.SelectedValue)); FinancialAccount autoDepositAccount = FinancialAccount.FromIdentity(Int32.Parse(this.DropAutoDeposits.SelectedValue)); FinancialAccount autoWithdrawalAccount = FinancialAccount.FromIdentity(Int32.Parse(this.DropAutoWithdrawals.SelectedValue)); int autoDepositLimit = Int32.Parse(this.TextDepositLimit.Text); int autoWithdrawalLimit = Int32.Parse(this.TextWithdrawalLimit.Text); int organizationId = Int32.Parse(this.DropOrganizations.SelectedValue); Organization organization = Organization.FromIdentity(organizationId); int importedTransactionCount = 0; foreach (ImportedRow row in import.Rows) { // Each row is at least a stub, probably more. // If too old, ignore. if (row.DateTime < new DateTime(2008, 12, 4)) { continue; } string importKey = row.SuppliedTransactionId; // If importKey is empty, construct a hash from the data fields. if (string.IsNullOrEmpty(importKey)) { string hashKey = row.HashBase + row.Comment + (row.AmountCentsNet/100.0).ToString(CultureInfo.InvariantCulture) + row.CurrentBalance.ToString(CultureInfo.InvariantCulture) + row.DateTime.ToString("yyyy-MM-dd-hh-mm-ss"); importKey = SHA1.Hash(hashKey).Replace(" ", ""); } if (importKey.Length > 30) { importKey = importKey.Substring(0, 30); } Int64 amountCents = row.AmountCentsNet; if (amountCents == 0) { amountCents = row.AmountCentsGross; } FinancialTransaction transaction = FinancialTransaction.ImportWithStub(organizationId, row.DateTime, assetAccount.Identity, amountCents, row.Comment, importKey, _currentUser.Identity); if (transaction != null) { // The transaction was created. Examine if the autobook criteria are true. importedTransactionCount++; FinancialAccounts accounts = FinancialAccounts.FromBankTransactionTag(row.Comment); if (accounts.Count == 1) { // This is a labelled local donation. Geography geography = accounts[0].AssignedGeography; FinancialAccount localAccount = accounts[0]; transaction.AddRow(organization.FinancialAccounts.IncomeDonations, -amountCents, _currentUser); transaction.AddRow(organization.FinancialAccounts.CostsLocalDonationTransfers, amountCents, _currentUser); transaction.AddRow(localAccount, -amountCents, _currentUser); Activizr.Logic.Support.PWEvents.CreateEvent(EventSource.PirateWeb, EventType.LocalDonationReceived, _currentUser.Identity, organizationId, geography.Identity, 0, transaction.Identity, localAccount.Identity.ToString()); } else if (row.Comment.ToLowerInvariant().StartsWith("bg 451-0061 ")) // TODO: Organization.Parameters.FinancialTrackedTransactionPrefix { // Check for previously imported payment group PaymentGroup group = PaymentGroup.FromTag(organization, "SEBGM" + DateTime.Today.Year.ToString() + // TODO: Get tagging from org row.Comment.Substring(11)); if (group != null) { // 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 transaction.Dependency = group; group.Open = false; transaction.AddRow(organization.FinancialAccounts.AssetsOutboundInvoices, -amountCents, _currentUser); } } else if (amountCents < 0) { if ((-amountCents) < autoWithdrawalLimit * 100) { // Book against autoWithdrawal account. transaction.AddRow(autoWithdrawalAccount, -amountCents, _currentUser); } } else if (amountCents > 0) { if (row.Fee < 0) { // This is always an autodeposit, if there is a fee (which is never > 0.0) transaction.AddRow(organization.FinancialAccounts.CostsBankFees, -row.Fee, _currentUser); transaction.AddRow(autoDepositAccount, -row.AmountCentsGross, _currentUser); } else if (amountCents < autoDepositLimit * 100) { // Book against autoDeposit account. transaction.AddRow(autoDepositAccount, -amountCents, _currentUser); } } } } // Import complete. Examine if we expect more transactions -- if the imported balance differs from // the database balance: double databaseAccountBalance = assetAccount.BalanceTotal; bool mismatch = false; if (databaseAccountBalance != import.CurrentBalance) { mismatch = true; } string message = importedTransactionCount.ToString() + " transactions were imported."; if (importedTransactionCount == 0) { message = "No transactions were imported. "; } else if (importedTransactionCount == 1) { message = "One transaction was imported. "; } if (import.CurrentBalance > 0) { if (mismatch) { message += " Transactions are missing from the database. Import more transactions."; } else { message += " The account balance is up to date. No further import is necessary."; ScriptManager.RegisterStartupScript(this, Page.GetType(), "alldone", "alert ('The account balance is up to date. No further import is necessary.');", true); // Auto-match open payouts against new transactions Payouts.AutomatchAgainstUnbalancedTransactions(organization); } } this.LabelImportResultText.Text = message; } protected ImportResult ImportSeb (string contents) { string patternRows = @"