diff --git a/Basic/Enums/OrganizationFinancialAccountType.cs b/Basic/Enums/OrganizationFinancialAccountType.cs index 69bf97de..76e824e7 100644 --- a/Basic/Enums/OrganizationFinancialAccountType.cs +++ b/Basic/Enums/OrganizationFinancialAccountType.cs @@ -108,6 +108,10 @@ namespace Swarmops.Basic.Enums /// CostsCurrencyFluctuations, /// + /// Various bookkeeping errors + /// + CostsMiscalculations, + /// /// When closing books, this is the yearly result (balanced against DebtsEquity) /// CostsYearlyResult, // multi-account type diff --git a/Logic/Financial/ExternalBankData.cs b/Logic/Financial/ExternalBankData.cs index 2f89aadd..454fff52 100644 --- a/Logic/Financial/ExternalBankData.cs +++ b/Logic/Financial/ExternalBankData.cs @@ -161,10 +161,8 @@ namespace Swarmops.Logic.Financial } string amountNetString = lineFields[fieldNameLookup[ExternalBankDataFieldName.TransactionNet]]; - double amountNetDouble = Double.Parse(amountNetString, NumberStyles.Currency, - new CultureInfo(Profile.Culture)); - newRecord.TransactionNetCents = (long) (amountNetDouble*100.0); + newRecord.TransactionNetCents = ParseAmountString(amountNetString); // TODO: Net, fee @@ -198,6 +196,69 @@ namespace Swarmops.Logic.Financial this.Records = recordList.ToArray(); } + + private Int64 ParseAmountString (string input) + { + // This parser deals with a number of different cases and cultures. + // It returns cents. + + // "1" => 100 + // "1.00" => 100 + // "1,00" => 100 + // "100" => 10,000 + // "1,000" => 100,000 + // "1,000.00" => 100,000 + // "1 000,00" => 100,000 + + // Normally, this could be parsed as a double, but your typical Pentium + // errors in double handling creates rare mismatches of single cents. + // Therefore, must be parsed as an int. + + char testDecimalSeparator = '1'; // dummy non-separator + + if (input.Length > 3) + { + testDecimalSeparator = input[input.Length - 3]; + } + + if (testDecimalSeparator != '.' && testDecimalSeparator != ',') + { + // No known decimal separator where one is expected for monetary amounts, + // so multiply amount by 100 by adding two zeroes to string, as we're supposed + // to be returning cents + + bool weirdCaseTriggered = false; + + if (input.Length > 2) + { + testDecimalSeparator = input[input.Length - 2]; + + if (testDecimalSeparator == '.' || testDecimalSeparator == ',') + { + // Fucking SEB doesn't even have the manners to only have whole units or cents, + // but can present a half-crown as ",5". Damn them... + + input += "0"; + weirdCaseTriggered = true; + } + } + + if (!weirdCaseTriggered) + { + input += "00"; + } + } + + + // remove all known noise from the digit sequence: spaces, thousands accents, commas, periods + + input = input.Replace(" ", "").Replace("'", "").Replace(",", "").Replace(".", ""); + + // parse what's remaining as cents in Int64 + + return Int64.Parse(input); + } + public ExternalBankDataProfile Profile { get; set; } public ExternalBankDataRecord[] Records { get; private set; } public long LatestAccountBalanceCents; diff --git a/Logic/Financial/FinancialTransaction.cs b/Logic/Financial/FinancialTransaction.cs index 037cdc0f..50c2a449 100644 --- a/Logic/Financial/FinancialTransaction.cs +++ b/Logic/Financial/FinancialTransaction.cs @@ -88,6 +88,26 @@ namespace Swarmops.Logic.Financial } } + public Int64 this[FinancialAccount account] + { + get + { + Int64 result = 0; + + FinancialTransactionRows rows = this.Rows; + + foreach (FinancialTransactionRow row in rows) + { + if (row.FinancialAccountId == account.Identity) + { + result += row.AmountCents; + } + } + + return result; + } + } + public new string Description { get { return base.Description; } diff --git a/Logic/Financial/ISummable.cs b/Logic/Financial/ISummable.cs new file mode 100644 index 00000000..fa817327 --- /dev/null +++ b/Logic/Financial/ISummable.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Swarmops.Basic.Enums; + +namespace Swarmops.Logic.Financial +{ + public interface ISummable + { + long SumCents { get; } + OrganizationFinancialAccountType CounterAccountType { get; } + } +} diff --git a/Logic/Financial/OrganizationFinancialAccounts.cs b/Logic/Financial/OrganizationFinancialAccounts.cs index 93abb79e..f2c331dd 100644 --- a/Logic/Financial/OrganizationFinancialAccounts.cs +++ b/Logic/Financial/OrganizationFinancialAccounts.cs @@ -75,7 +75,15 @@ namespace Swarmops.Logic.Financial { get { - return FinancialAccount.FromIdentity(SwarmDb.GetDatabaseForReading().GetOrganizationFinancialAccountId(_organizationId, accountType)); + int accountId = SwarmDb.GetDatabaseForReading().GetOrganizationFinancialAccountId(_organizationId, + accountType); + + if (accountId == 0) + { + return null; // not set + } + + return FinancialAccount.FromIdentity(accountId); } set { @@ -182,6 +190,11 @@ namespace Swarmops.Logic.Financial get { return this[OrganizationFinancialAccountType.IncomeSales]; } } + public FinancialAccount ConstsMiscalculations + { + get { return this[OrganizationFinancialAccountType.CostsMiscalculations]; } + } + public FinancialAccounts CostsConferences { get diff --git a/Logic/Financial/PaymentGroup.cs b/Logic/Financial/PaymentGroup.cs index 983cb1e7..318c31d0 100644 --- a/Logic/Financial/PaymentGroup.cs +++ b/Logic/Financial/PaymentGroup.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using Swarmops.Basic.Enums; using Swarmops.Basic.Types; using Swarmops.Database; using Swarmops.Logic.Swarm; @@ -8,7 +9,7 @@ using Swarmops.Logic.Structure; namespace Swarmops.Logic.Financial { - public class PaymentGroup: BasicPaymentGroup + public class PaymentGroup: BasicPaymentGroup, ISummable { private PaymentGroup(BasicPaymentGroup basic): base (basic) { @@ -179,5 +180,20 @@ namespace Swarmops.Logic.Financial return String.Join(", ", payerList.ToArray()); } } + + + #region Implementation of ISummable + + public long SumCents + { + get { return this.AmountCents; } + } + + public OrganizationFinancialAccountType CounterAccountType + { + get { return OrganizationFinancialAccountType.AssetsOutboundInvoices; } + } + + #endregion } } diff --git a/Logic/Financial/Payout.cs b/Logic/Financial/Payout.cs index dfdcbc61..7bd01f1f 100644 --- a/Logic/Financial/Payout.cs +++ b/Logic/Financial/Payout.cs @@ -13,7 +13,7 @@ using Swarmops.Logic.Support; namespace Swarmops.Logic.Financial { - public class Payout: BasicPayout + public class Payout: BasicPayout, ISummable { #region Creation and Construction @@ -663,5 +663,11 @@ namespace Swarmops.Logic.Financial this.Open = false; } + #region Implementation of ISummable + + public long SumCents { get { return this.AmountCents; } } + public OrganizationFinancialAccountType CounterAccountType { get { return OrganizationFinancialAccountType.Unknown; } } + + #endregion } } diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 72acdf8b..ee8b162d 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -107,6 +107,7 @@ + diff --git a/Site5/Pages/v5/Ledgers/Json-ResyncPreview.aspx.cs b/Site5/Pages/v5/Ledgers/Json-ResyncPreview.aspx.cs index 69dd200b..b9e544f0 100644 --- a/Site5/Pages/v5/Ledgers/Json-ResyncPreview.aspx.cs +++ b/Site5/Pages/v5/Ledgers/Json-ResyncPreview.aspx.cs @@ -6,6 +6,7 @@ using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Swarmops.Basic.Enums; +using Swarmops.Basic.Interfaces; using Swarmops.Logic.Financial; using Swarmops.Logic.Security; @@ -50,11 +51,25 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers { for (int masterIndex = 0; masterIndex < mismatchingRecord.MasterCents.Count(); masterIndex++) { + string dependencyString = string.Empty; + object dependency = mismatchingRecord.TransactionDependencies[masterIndex]; + + if (dependency != null) + { + dependencyString = dependency.GetType().ToString(); + int lastPeriod = dependencyString.LastIndexOf('.'); + dependencyString = dependencyString.Substring(lastPeriod + 1); + + dependencyString += " #" + (dependency as IHasIdentity).Identity.ToString("N0"); + + } + childItems.Add("{\"id\":\"" + rowId + childItems.Count.ToString("##0") + "\",\"rowName\":\"" + JsonSanitize(mismatchingRecord.Description) + "\",\"swarmopsData\":\"" + - PrintNullableCents(mismatchingRecord.SwarmopsCents[masterIndex]) + "\",\"masterData\":\"" + - PrintNullableCents(mismatchingRecord.MasterCents[masterIndex]) + "\",\"notes\":\"" + - JsonSanitize(mismatchingRecord.ResyncActions[masterIndex].ToString()) + "\"}"); + PrintNullableCents(currentOrganizationCurrency, mismatchingRecord.SwarmopsCents[masterIndex]) + "\",\"masterData\":\"" + + PrintNullableCents(currentOrganizationCurrency, mismatchingRecord.MasterCents[masterIndex]) + "\",\"resyncAction\":\"" + + JsonSanitize(mismatchingRecord.ResyncActions[masterIndex].ToString()) + "\",\"notes\":\"" + + JsonSanitize(dependencyString) + "\"}"); } } @@ -62,11 +77,11 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers string rowName = mismatch.DateTime.ToString(profile.DateTimeFormatString); - string swarmopsData = ((double) mismatch.SwarmopsDeltaCents/100.0).ToString("N2") + " " + - currentOrganizationCurrency; + string swarmopsData = currentOrganizationCurrency + " " + + ((double) mismatch.SwarmopsDeltaCents/100.0).ToString("N2"); - string masterData = ((double)mismatch.MasterDeltaCents / 100.0).ToString("N2") + " " + - currentOrganizationCurrency; + string masterData = currentOrganizationCurrency + " " + + ((double) mismatch.MasterDeltaCents/100.0).ToString("N2"); string notes = "Diff: " + ((double)(mismatch.MasterDeltaCents - mismatch.SwarmopsDeltaCents) / 100.0).ToString("N2"); @@ -83,14 +98,14 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers private Dictionary _hashedAccounts; - private string PrintNullableCents (long cents) + private string PrintNullableCents (string currency, long cents) { if (cents == 0) { return Resources.Global.Global_Missing; } - return (((double)cents) / 100.0).ToString("N2"); // TODO: Verify that culture is applied + return currency + " " + (((double)cents) / 100.0).ToString("N2"); // TODO: Verify that culture is applied } private string PrintCents (long cents) diff --git a/Site5/Pages/v5/Ledgers/ResyncExternalAccount.aspx b/Site5/Pages/v5/Ledgers/ResyncExternalAccount.aspx index 6a0d2a2a..21fbfc81 100644 --- a/Site5/Pages/v5/Ledgers/ResyncExternalAccount.aspx +++ b/Site5/Pages/v5/Ledgers/ResyncExternalAccount.aspx @@ -203,8 +203,8 @@ Date / Transaction - Our Database - Master File + Our Database + Master File Resync Action Notes diff --git a/Site5/Pages/v5/Ledgers/ResyncExternalAccount.aspx.cs b/Site5/Pages/v5/Ledgers/ResyncExternalAccount.aspx.cs index 88233362..34332645 100644 --- a/Site5/Pages/v5/Ledgers/ResyncExternalAccount.aspx.cs +++ b/Site5/Pages/v5/Ledgers/ResyncExternalAccount.aspx.cs @@ -245,9 +245,9 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers long swarmopsDeltaCents = account.GetDeltaCents(lastTimestamp.AddSeconds(1), timeWalker.AddSeconds(1)); // "AddSeconds" because DeltaCents operates on ">= lowbound, < highbound" + int timestampStartIndex = currentRecordIndex; long masterDeltaCents = externalData.Records[currentRecordIndex++].TransactionNetCents; int masterTransactionCount = 1; - int timestampStartIndex = currentRecordIndex; while (currentRecordIndex < externalData.Records.Length && externalData.Records[currentRecordIndex].DateTime == timeWalker) @@ -275,14 +275,16 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers for (int innerRecordIndex = timestampStartIndex; innerRecordIndex < currentRecordIndex; innerRecordIndex++) { - if (!mismatchConstruction.Master.ContainsKey(externalData.Records[innerRecordIndex].Description)) + string description = externalData.Records[innerRecordIndex].Description.Replace(" ", " "); + + if (!mismatchConstruction.Master.ContainsKey(description)) { - mismatchConstruction.Master[externalData.Records[innerRecordIndex].Description] = + mismatchConstruction.Master[description] = new ExternalBankMismatchingRecordConstruction(); } - mismatchConstruction.Master[externalData.Records[innerRecordIndex].Description].Cents.Add(externalData.Records[innerRecordIndex].TransactionNetCents); - mismatchConstruction.Master[externalData.Records[innerRecordIndex].Description].Dependencies.Add(null); // no dependencies on the master side, only on swarmops side + mismatchConstruction.Master[description].Cents.Add(externalData.Records[innerRecordIndex].TransactionNetCents); + mismatchConstruction.Master[description].Dependencies.Add(null); // no dependencies on the master side, only on swarmops side } // Load from Swarmops @@ -290,18 +292,29 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers FinancialAccountRows swarmopsTransactionRows = account.GetRowsFar(lastTimestamp, timeWalker); // the "select far" is a boundary < x <= boundary selector. Default is boundary <= x < boundary. + Dictionary lookupTransactions = new Dictionary(); + + // note all transaction IDs, then sum up per transaction + foreach (FinancialAccountRow swarmopsTransactionRow in swarmopsTransactionRows) { - FinancialTransaction swarmopsTransaction = swarmopsTransactionRow.Transaction; + lookupTransactions[swarmopsTransactionRow.FinancialTransactionId] = + swarmopsTransactionRow.Transaction; + } - if (!mismatchConstruction.Swarmops.ContainsKey(swarmopsTransaction.Description)) + + foreach (FinancialTransaction transaction in lookupTransactions.Values) + { + string description = transaction.Description.Replace(" ", " "); // for legacy compatibility with new importer + + if (!mismatchConstruction.Swarmops.ContainsKey(description)) { - mismatchConstruction.Swarmops[swarmopsTransaction.Description] = + mismatchConstruction.Swarmops[description] = new ExternalBankMismatchingRecordConstruction(); } - mismatchConstruction.Swarmops[swarmopsTransaction.Description].Cents.Add(swarmopsTransactionRow.AmountCents); - mismatchConstruction.Swarmops[swarmopsTransaction.Description].Dependencies.Add(swarmopsTransaction.Dependency); + mismatchConstruction.Swarmops[description].Cents.Add(transaction [account]); + mismatchConstruction.Swarmops[description].Dependencies.Add(transaction.Dependency); } // Then, parse the intermediate construction object to the presentation-and-action object. @@ -332,27 +345,42 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers // STEP 1 - locate all identical matches - for (int masterIndex = 0; masterIndex < mismatchConstruction.Master[masterKey].Cents.Count; masterIndex++) + if (mismatchConstruction.Swarmops.ContainsKey(masterKey)) { - // no "continue" necessary on first run-through; nothing has been checked off yet - - long findMasterCents = mismatchConstruction.Master[masterKey].Cents[masterIndex]; - - for (int swarmopsIndex = 0; swarmopsIndex < mismatchConstruction.Swarmops[masterKey].Cents.Count; swarmopsIndex++) + for (int masterIndex = 0; + masterIndex < mismatchConstruction.Master[masterKey].Cents.Count; + masterIndex++) { - if (findMasterCents == mismatchConstruction.Swarmops[masterKey].Cents [swarmopsIndex]) + // no "continue" necessary on first run-through; nothing has been checked off yet + + long findMasterCents = mismatchConstruction.Master[masterKey].Cents[masterIndex]; + + for (int swarmopsIndex = 0; + swarmopsIndex < mismatchConstruction.Swarmops[masterKey].Cents.Count; + swarmopsIndex++) { - // There is a match as per case 1. Record both, mark both as used, continue. + if (checkSwarmopsIndex.ContainsKey(swarmopsIndex)) + { + continue; // may have been checked off already in the rare case of twin identical amounts + } - masterCentsList.Add(findMasterCents); - swarmopsCentsList.Add(mismatchConstruction.Swarmops[masterKey].Cents[swarmopsIndex]); // should be equal, we're defensive here - dependenciesList.Add(mismatchConstruction.Swarmops[masterKey].Dependencies[swarmopsIndex]); - actionsList.Add(ExternalBankMismatchResyncAction.NoAction); - - checkMasterIndex[masterIndex] = true; - checkSwarmopsIndex[swarmopsIndex] = true; + if (findMasterCents == mismatchConstruction.Swarmops[masterKey].Cents[swarmopsIndex]) + { + // There is a match as per case 1. Record both, mark both as used, continue. - break; + masterCentsList.Add(findMasterCents); + swarmopsCentsList.Add( + mismatchConstruction.Swarmops[masterKey].Cents[swarmopsIndex]); + // should be equal, we're defensive here + dependenciesList.Add( + mismatchConstruction.Swarmops[masterKey].Dependencies[swarmopsIndex]); + actionsList.Add(ExternalBankMismatchResyncAction.NoAction); + + checkMasterIndex[masterIndex] = true; + checkSwarmopsIndex[swarmopsIndex] = true; + + break; + } } } } @@ -361,7 +389,8 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers // TODO: improve logic to handle same number of records left on both sides - if (mismatchConstruction.Master[masterKey].Cents.Count - checkMasterIndex.Keys.Count == 1 && + if (mismatchConstruction.Swarmops.ContainsKey(masterKey) && + mismatchConstruction.Master[masterKey].Cents.Count - checkMasterIndex.Keys.Count == 1 && mismatchConstruction.Swarmops [masterKey].Cents.Count - checkSwarmopsIndex.Keys.Count == 1) { for (int masterIndex = 0; masterIndex < mismatchConstruction.Master[masterKey].Cents.Count; masterIndex++) @@ -415,7 +444,7 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers // STEP 3b - log remaining on Swarmops side - if (mismatchConstruction.Swarmops[masterKey].Cents.Count > checkSwarmopsIndex.Keys.Count) + if (mismatchConstruction.Swarmops.ContainsKey(masterKey) && mismatchConstruction.Swarmops[masterKey].Cents.Count > checkSwarmopsIndex.Keys.Count) { for (int swarmopsIndex = 0; swarmopsIndex < mismatchConstruction.Swarmops[masterKey].Cents.Count; swarmopsIndex++) { @@ -451,18 +480,24 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers mismatchingRecordList[masterKey] = newRecord; } + // Finally, add the transactions that were (described) in Swarmops but not in Master + foreach (string swarmopsKey in mismatchConstruction.Swarmops.Keys) { if (!mismatchingRecordList.ContainsKey(swarmopsKey)) { mismatchingRecordList[swarmopsKey] = new ExternalBankMismatchingRecordDescription(); mismatchingRecordList[swarmopsKey].Description = swarmopsKey; - } - mismatchingRecordList[swarmopsKey].SwarmopsCents = - mismatchConstruction.Swarmops[swarmopsKey].Cents.ToArray(); - mismatchingRecordList[swarmopsKey].TransactionDependencies = - mismatchConstruction.Swarmops[swarmopsKey].Dependencies.ToArray(); + mismatchingRecordList[swarmopsKey].SwarmopsCents = + mismatchConstruction.Swarmops[swarmopsKey].Cents.ToArray(); + mismatchingRecordList[swarmopsKey].TransactionDependencies = + mismatchConstruction.Swarmops[swarmopsKey].Dependencies.ToArray(); + mismatchingRecordList[swarmopsKey].MasterCents = + new long[mismatchConstruction.Swarmops[swarmopsKey].Cents.Count]; // inits to zero + + mismatchingRecordList[swarmopsKey].ResyncActions = new ExternalBankMismatchResyncAction[mismatchConstruction.Swarmops[swarmopsKey].Cents.Count]; // inits to "Unknown", TODO + } } newMismatch.MismatchingRecords = mismatchingRecordList.Values.ToArray();