diff --git a/Logic/Financial/ExternalBankData.cs b/Logic/Financial/ExternalBankData.cs index 79efd987..40ba6904 100644 --- a/Logic/Financial/ExternalBankData.cs +++ b/Logic/Financial/ExternalBankData.cs @@ -16,31 +16,24 @@ namespace Swarmops.Logic.Financial public ExternalBankDataProfile Profile { get; set; } public ExternalBankDataRecord[] Records { get; private set; } - public void LoadData (TextReader textReader, FinancialAccount automatedAccount) + public void LoadData(TextReader reader, Organization organization, Currency expectedCurrency) { - /* - - CsvHelper.Configuration.Configuration config = new CsvHelper.Configuration.Configuration(); - config.HasHeaderRecord = true; - - CsvReader csvReader = new CsvReader(textReader, config); - string data = reader.ReadToEnd(); - LoadData (data, organization, expectedCurrency);*/ + LoadData(data, organization, expectedCurrency); } - public void LoadData (string data, Organization organization, Currency accountCurrency) + public void LoadData(string data, Organization organization, Currency accountCurrency) { List recordList = new List(); if (Profile == null) { - throw new InvalidOperationException ("Cannot call LoadData before a profile has been set"); + throw new InvalidOperationException("Cannot call LoadData before a profile has been set"); } if (Profile.BankDataAccountReader != StockBankDataReaders.CommaSeparatedValuesAccountReader) { - throw new NotImplementedException ("Only tab separated values are supported at this point"); + throw new NotImplementedException("Only tab separated values are supported at this point"); } // TODO: Implement bank data reader factory here @@ -59,9 +52,9 @@ namespace Swarmops.Logic.Financial { string[] replacements = Profile.InitialReplacements.Split('|'); - for (int loop = 0; loop < replacements.Length/2; loop++) + for (int loop = 0; loop < replacements.Length / 2; loop++) { - data = data.Replace(replacements[loop*2], replacements[loop*2 + 1]); + data = data.Replace(replacements[loop * 2], replacements[loop * 2 + 1]); } } @@ -80,11 +73,11 @@ namespace Swarmops.Logic.Financial } } - crlfIndex = data.IndexOfAny (new[] {'\n', '\r'}); + crlfIndex = data.IndexOfAny(new[] { '\n', '\r' }); - string fieldKeyLine = data.Substring (0, crlfIndex); + string fieldKeyLine = data.Substring(0, crlfIndex); - string[] dataKeyFields = fieldKeyLine.Split ('\t'); + string[] dataKeyFields = fieldKeyLine.Split('\t'); Dictionary fieldNameLookup = new Dictionary(); @@ -102,17 +95,17 @@ namespace Swarmops.Logic.Financial } } - if (!fieldNameLookup.ContainsKey (fieldName)) // wasn't found + if (!fieldNameLookup.ContainsKey(fieldName)) // wasn't found { - throw new InvalidOperationException ("Field key \"" + fieldName + + throw new InvalidOperationException("Field key \"" + fieldName + "\" was not supplied or found in data file"); } } - data = data.Substring (crlfIndex).Trim(); + data = data.Substring(crlfIndex).Trim(); - string[] lines = data.Split (new[] {'\r', '\n'}); + string[] lines = data.Split(new[] { '\r', '\n' }); foreach (string lineData in lines) { @@ -123,13 +116,13 @@ namespace Swarmops.Logic.Financial continue; // empty lines may exist due to split on either CR or LF } - string[] lineFields = line.Split ('\t'); + string[] lineFields = line.Split('\t'); // If wrong currency, ignore - if (fieldNameLookup.ContainsKey (ExternalBankDataFieldName.Currency)) + if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.Currency)) { - string currency = StripQuotes (lineFields[fieldNameLookup[ExternalBankDataFieldName.Currency]]); + string currency = StripQuotes(lineFields[fieldNameLookup[ExternalBankDataFieldName.Currency]]); if (currency != expectedCurrencyCode) { @@ -139,10 +132,10 @@ namespace Swarmops.Logic.Financial ExternalBankDataRecord newRecord = new ExternalBankDataRecord(); - if (fieldNameLookup.ContainsKey (ExternalBankDataFieldName.DescriptionPrimary)) + if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.DescriptionPrimary)) { newRecord.Description = - StripQuotes (lineFields[fieldNameLookup[ExternalBankDataFieldName.DescriptionPrimary]]); + StripQuotes(lineFields[fieldNameLookup[ExternalBankDataFieldName.DescriptionPrimary]]); } // If primary description is empty, try to apply a secondary description @@ -198,35 +191,35 @@ namespace Swarmops.Logic.Financial - if (fieldNameLookup.ContainsKey (ExternalBankDataFieldName.AccountBalance)) + if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.AccountBalance)) { // Dividing up to step-by-step statements instead of one long statement assists debugging // of culture and other error sources string balanceString = - StripQuotes (lineFields[fieldNameLookup[ExternalBankDataFieldName.AccountBalance]]); + StripQuotes(lineFields[fieldNameLookup[ExternalBankDataFieldName.AccountBalance]]); try { - newRecord.AccountBalanceCents = ParseAmountString (balanceString); + newRecord.AccountBalanceCents = ParseAmountString(balanceString); } catch (Exception innerException) { - throw new FormatException ("Couldn't parse account balance string - \"" + balanceString + "\"", + throw new FormatException("Couldn't parse account balance string - \"" + balanceString + "\"", innerException); } } - if (!fieldNameLookup.ContainsKey (ExternalBankDataFieldName.Date) && - !fieldNameLookup.ContainsKey (ExternalBankDataFieldName.DateTime)) + if (!fieldNameLookup.ContainsKey(ExternalBankDataFieldName.Date) && + !fieldNameLookup.ContainsKey(ExternalBankDataFieldName.DateTime)) { - throw new InvalidOperationException ("Cannot parse transactions file without at least a date field"); + throw new InvalidOperationException("Cannot parse transactions file without at least a date field"); } DateTime dateTime = Constants.DateTimeLow; - if (fieldNameLookup.ContainsKey (ExternalBankDataFieldName.Date)) + if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.Date)) { - string dateString = StripQuotes (lineFields[fieldNameLookup[ExternalBankDataFieldName.Date]]); + string dateString = StripQuotes(lineFields[fieldNameLookup[ExternalBankDataFieldName.Date]]); if (!String.IsNullOrEmpty(Profile.DateTimeCustomFormatString)) { @@ -237,10 +230,10 @@ namespace Swarmops.Logic.Financial dateTime = DateTime.Parse(dateString, new CultureInfo(Profile.Culture)); } - if (fieldNameLookup.ContainsKey (ExternalBankDataFieldName.Time)) + if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.Time)) { - string timeString = StripQuotes (lineFields[fieldNameLookup[ExternalBankDataFieldName.Time]]); - TimeSpan timeOfDay = TimeSpan.Parse (timeString); + string timeString = StripQuotes(lineFields[fieldNameLookup[ExternalBankDataFieldName.Time]]); + TimeSpan timeOfDay = TimeSpan.Parse(timeString); dateTime += timeOfDay; } @@ -249,25 +242,25 @@ namespace Swarmops.Logic.Financial // move transaction to like mid-day of the organization's time zone. For now, all orgs are in Europe, so add 12 hours // this is a HACK HACK HACK - dateTime = dateTime.AddHours (12); + dateTime = dateTime.AddHours(12); } } else // no Date field, so by earlier logic, must have a DateTime field { dateTime = - DateTime.Parse (StripQuotes (lineFields[fieldNameLookup[ExternalBankDataFieldName.DateTime]]), - new CultureInfo (Profile.Culture)); + DateTime.Parse(StripQuotes(lineFields[fieldNameLookup[ExternalBankDataFieldName.DateTime]]), + new CultureInfo(Profile.Culture)); } - if (fieldNameLookup.ContainsKey (ExternalBankDataFieldName.TimeZone)) + if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.TimeZone)) { // Valid time zone formats are "XXX+hh:mm". The XXX are ignored. // Throws exception if this doesn't parse, which is what we want - string timeZoneString = StripQuotes (lineFields[fieldNameLookup[ExternalBankDataFieldName.TimeZone]]); - timeZoneString = timeZoneString.Substring (timeZoneString.Length - 6); - TimeSpan timeZone = TimeSpan.Parse (timeZoneString); + string timeZoneString = StripQuotes(lineFields[fieldNameLookup[ExternalBankDataFieldName.TimeZone]]); + timeZoneString = timeZoneString.Substring(timeZoneString.Length - 6); + TimeSpan timeZone = TimeSpan.Parse(timeZoneString); dateTime -= timeZone; // minus, to bring the time to UTC. If time 13:00 is in tz +01:00, the UTC time is 12:00 @@ -280,35 +273,35 @@ namespace Swarmops.Logic.Financial // PILOT SPECIAL CASE: if Paypal and PPSE Pilot program, ignore everything before 2014 if (dateTime.Year < 2014 && organization.Identity == 1 && - fieldNameLookup.ContainsKey (ExternalBankDataFieldName.UniqueId) && - PilotInstallationIds.IsPilot (PilotInstallationIds.PiratePartySE)) + fieldNameLookup.ContainsKey(ExternalBankDataFieldName.UniqueId) && + PilotInstallationIds.IsPilot(PilotInstallationIds.PiratePartySE)) { continue; // Do not import PayPal records from before 2013 } newRecord.DateTime = dateTime; - if (!fieldNameLookup.ContainsKey (ExternalBankDataFieldName.TransactionNet)) + if (!fieldNameLookup.ContainsKey(ExternalBankDataFieldName.TransactionNet)) { - throw new ArgumentException ("There must be a transaction amount field in the bank data profile"); + throw new ArgumentException("There must be a transaction amount field in the bank data profile"); } string amountNetString = - StripQuotes (lineFields[fieldNameLookup[ExternalBankDataFieldName.TransactionNet]]); + StripQuotes(lineFields[fieldNameLookup[ExternalBankDataFieldName.TransactionNet]]); - newRecord.TransactionNetCents = ParseAmountString (amountNetString); + newRecord.TransactionNetCents = ParseAmountString(amountNetString); - if (fieldNameLookup.ContainsKey (ExternalBankDataFieldName.TransactionGross)) + if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.TransactionGross)) { string amountGrossString = - StripQuotes (lineFields[fieldNameLookup[ExternalBankDataFieldName.TransactionGross]]); - newRecord.TransactionGrossCents = ParseAmountString (amountGrossString); + StripQuotes(lineFields[fieldNameLookup[ExternalBankDataFieldName.TransactionGross]]); + newRecord.TransactionGrossCents = ParseAmountString(amountGrossString); - if (fieldNameLookup.ContainsKey (ExternalBankDataFieldName.TransactionFee)) + if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.TransactionFee)) { string amountFeeString = - StripQuotes (lineFields[fieldNameLookup[ExternalBankDataFieldName.TransactionFee]]); - newRecord.FeeCents = ParseAmountString (amountFeeString); + StripQuotes(lineFields[fieldNameLookup[ExternalBankDataFieldName.TransactionFee]]); + newRecord.FeeCents = ParseAmountString(amountFeeString); if (Profile.FeeSignage == FeeSignage.Positive) { @@ -326,21 +319,21 @@ namespace Swarmops.Logic.Financial if (newRecord.TransactionNetCents != newRecord.TransactionGrossCents + newRecord.FeeCents) { - throw new InvalidDataException ( + throw new InvalidDataException( "For a record, the net transaction amount does not match the gross less the fee."); } - if (fieldNameLookup.ContainsKey (ExternalBankDataFieldName.UniqueId)) + if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.UniqueId)) { - newRecord.UniqueId = StripQuotes (lineFields[fieldNameLookup[ExternalBankDataFieldName.UniqueId]]); + newRecord.UniqueId = StripQuotes(lineFields[fieldNameLookup[ExternalBankDataFieldName.UniqueId]]); } - else if (fieldNameLookup.ContainsKey (ExternalBankDataFieldName.NotUniqueId)) + else if (fieldNameLookup.ContainsKey(ExternalBankDataFieldName.NotUniqueId)) { newRecord.NotUniqueId = - StripQuotes (lineFields[fieldNameLookup[ExternalBankDataFieldName.NotUniqueId]]); + StripQuotes(lineFields[fieldNameLookup[ExternalBankDataFieldName.NotUniqueId]]); } - recordList.Add (newRecord); + recordList.Add(newRecord); } if (Profile.LatestTransactionLocation == LatestTransactionLocation.Top) @@ -353,16 +346,16 @@ namespace Swarmops.Logic.Financial } else { - throw new ArgumentException ("LatestTransactionLocation is undefined"); + throw new ArgumentException("LatestTransactionLocation is undefined"); } - recordList.Sort (new ExternalBankDataRecord()); + recordList.Sort(new ExternalBankDataRecord()); Records = recordList.ToArray(); } - private Int64 ParseAmountString (string input) + private Int64 ParseAmountString(string input) { // This parser deals with a number of different cases and cultures. // It returns cents. @@ -418,14 +411,14 @@ namespace Swarmops.Logic.Financial // remove all known noise from the digit sequence: all whitespace, thousands accents, commas, periods input = input.RemoveAllWhitespace(); - input = input.Replace ("'", "").Replace (",", "").Replace (".", ""); + input = input.Replace("'", "").Replace(",", "").Replace(".", ""); // parse what's remaining as cents in Int64 - return Int64.Parse (input); + return Int64.Parse(input); } - private string StripQuotes (string input) + private string StripQuotes(string input) { input = input.Trim(); @@ -433,9 +426,9 @@ namespace Swarmops.Logic.Financial { if (input[0] == '\"') { - if (input.EndsWith ("\"")) + if (input.EndsWith("\"")) { - return input.Trim ('\"', ' '); + return input.Trim('\"', ' '); } } } diff --git a/Site/Pages/v5/Ledgers/UploadBankFiles.aspx b/Site/Pages/v5/Ledgers/UploadBankFiles.aspx index 4e1c9dbf..62f7e43e 100644 --- a/Site/Pages/v5/Ledgers/UploadBankFiles.aspx +++ b/Site/Pages/v5/Ledgers/UploadBankFiles.aspx @@ -156,7 +156,6 @@ var halfway = false; var currentYear = <%=DateTime.Today.Year %>; - var localizedFirstUploadConfirmation = SwarmopsJS.unescape("<%=this.Localized_ConfirmFirstUpload%>"); @@ -212,4 +211,3 @@ - diff --git a/Site/Pages/v5/Ledgers/UploadBankFiles.aspx.cs b/Site/Pages/v5/Ledgers/UploadBankFiles.aspx.cs index fb31cb02..f38212c2 100644 --- a/Site/Pages/v5/Ledgers/UploadBankFiles.aspx.cs +++ b/Site/Pages/v5/Ledgers/UploadBankFiles.aspx.cs @@ -41,11 +41,11 @@ namespace Swarmops.Site.Pages.Ledgers Payments } - protected void Page_Load (object sender, EventArgs e) + protected void Page_Load(object sender, EventArgs e) { PageTitle = Resources.Pages.Ledgers.UploadBankFiles_PageTitle; PageIcon = "iconshock-bank"; - PageAccessRequired = new Access (CurrentOrganization, AccessAspect.Bookkeeping, AccessType.Write); + PageAccessRequired = new Access(CurrentOrganization, AccessAspect.Bookkeeping, AccessType.Write); if (!IsPostBack) { @@ -183,7 +183,7 @@ namespace Swarmops.Site.Pages.Ledgers FinancialAccounts accounts = CurrentOrganization.FinancialAccountsExternal; this.DropAccounts.Items.Clear(); - this.DropAccounts.Items.Add (new ListItem (Global.Global_DropInits_SelectFinancialAccount, "0")); + this.DropAccounts.Items.Add(new ListItem(Global.Global_DropInits_SelectFinancialAccount, "0")); foreach (FinancialAccount account in accounts) { @@ -198,8 +198,8 @@ namespace Swarmops.Site.Pages.Ledgers } - [WebMethod (true)] - public static AjaxCallResult InitializeProcessing (string guid, int accountId) + [WebMethod(true)] + public static AjaxCallResult InitializeProcessing(string guid, int accountId) { // Start an async thread that does all the work, then return @@ -212,16 +212,16 @@ namespace Swarmops.Site.Pages.Ledgers fileType = BankFileType.PaymentDetails; } - FinancialAccount account = FinancialAccount.FromIdentity (accountId); + FinancialAccount account = FinancialAccount.FromIdentity(accountId); if (account.Organization.Identity != authData.CurrentOrganization.Identity || - !authData.Authority.HasAccess (new Access (authData.CurrentOrganization, AccessAspect.Bookkeeping, + !authData.Authority.HasAccess(new Access(authData.CurrentOrganization, AccessAspect.Bookkeeping, AccessType.Write))) { throw new UnauthorizedAccessException(); } - Thread initThread = new Thread (ProcessUploadThread); + Thread initThread = new Thread(ProcessUploadThread); ProcessThreadArguments args = new ProcessThreadArguments { @@ -232,14 +232,14 @@ namespace Swarmops.Site.Pages.Ledgers FileType = fileType }; - initThread.Start (args); + initThread.Start(args); - return new AjaxCallResult {Success = true}; + return new AjaxCallResult { Success = true }; } [WebMethod] - public static AjaxCallResult GetAutomationProfileName (string guid, int accountId) + public static AjaxCallResult GetAutomationProfileName(string guid, int accountId) { FinancialAccount account = FinancialAccount.FromIdentity(accountId); @@ -283,14 +283,14 @@ namespace Swarmops.Site.Pages.Ledgers [WebMethod] - public static ReportedImportResults GetReportedImportResults (string guid) + public static ReportedImportResults GetReportedImportResults(string guid) { AuthenticationData authData = GetAuthenticationDataAndCulture(); ReportedImportResults results = new ReportedImportResults(); - ImportResultsCategory category = (ImportResultsCategory) GuidCache.Get (guid + "-Result"); - ImportResults resultDetail = GuidCache.Get (guid + "-ResultDetails") as ImportResults; - ImportedPaymentData paymentsDetail = GuidCache.Get (guid + "-ResultDetails") as ImportedPaymentData; + ImportResultsCategory category = (ImportResultsCategory)GuidCache.Get(guid + "-Result"); + ImportResults resultDetail = GuidCache.Get(guid + "-ResultDetails") as ImportResults; + ImportedPaymentData paymentsDetail = GuidCache.Get(guid + "-ResultDetails") as ImportedPaymentData; string exceptionText = GuidCache.Get(guid + "-Exception") as string; string html = string.Empty; @@ -298,7 +298,7 @@ namespace Swarmops.Site.Pages.Ledgers switch (category) { case ImportResultsCategory.Good: - html = String.Format (Resources.Pages.Ledgers.UploadBankFiles_ResultsGood, + html = String.Format(Resources.Pages.Ledgers.UploadBankFiles_ResultsGood, resultDetail.TransactionsImported, resultDetail.DuplicateTransactions, resultDetail.EarliestTransaction, resultDetail.LatestTransaction); @@ -307,7 +307,7 @@ namespace Swarmops.Site.Pages.Ledgers html += "

" + String.Format(Resources.Pages.Ledgers.UploadBankFiles_ResultsInitialBalanceSet, Currency.FromCode(resultDetail.InitialBalanceCurrencyCode).DisplayCode, - resultDetail.InitialBalanceCents/100.0, + resultDetail.InitialBalanceCents / 100.0, authData.CurrentOrganization.FirstFiscalYear); if (resultDetail.CurrencyCode != resultDetail.InitialBalanceCurrencyCode) @@ -326,9 +326,9 @@ namespace Swarmops.Site.Pages.Ledgers break; case ImportResultsCategory.Questionable: - html = String.Format (Resources.Pages.Ledgers.UploadBankFiles_ResultsQuestionable, + html = String.Format(Resources.Pages.Ledgers.UploadBankFiles_ResultsQuestionable, resultDetail.TransactionsImported, resultDetail.DuplicateTransactions, - Math.Abs (resultDetail.BalanceMismatchCents/100.0), resultDetail.CurrencyCode); + Math.Abs(resultDetail.BalanceMismatchCents / 100.0), resultDetail.CurrencyCode); break; case ImportResultsCategory.Bad: html = Resources.Pages.Ledgers.UploadBankFiles_ResultsBad + "