diff --git a/Database/Database-Financials.cs b/Database/Database-Financials.cs index 5c8e85c5..fcf8ab67 100644 --- a/Database/Database-Financials.cs +++ b/Database/Database-Financials.cs @@ -9,6 +9,8 @@ using Swarmops.Basic.Types.Financial; // This is the first part of Database to fully use MySql. +// TODO: This was written with several classes in one file. Break out to one class per file. + namespace Swarmops.Database { public partial class SwarmDb @@ -319,16 +321,27 @@ namespace Swarmops.Database } public BasicFinancialAccountRow[] GetFinancialAccountRows (int financialAccountId, DateTime startDateTime, - DateTime endDateTime) + DateTime endDateTime, bool selectFar) { - return GetFinancialAccountRows(new int[] {financialAccountId}, startDateTime, endDateTime); + return GetFinancialAccountRows(new int[] {financialAccountId}, startDateTime, endDateTime, selectFar); } public BasicFinancialAccountRow[] GetFinancialAccountRows(int[] financialAccountIds, DateTime startDateTime, - DateTime endDateTime) + DateTime endDateTime, bool selectFar) { var result = new List(); + // initialize to near selector (equal sign by the lower bound + + string selectorLower = ">="; + string selectorUpper = "<"; + + if (selectFar) // equal sign by the upper bound + { + selectorLower = ">"; + selectorUpper = "<="; + } + using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); @@ -336,9 +349,9 @@ namespace Swarmops.Database DbCommand command = GetDbCommand( "select FinancialTransactionRows.FinancialAccountId,FinancialTransactionRows.FinancialTransactionId,FinancialTransactions.DateTime,FinancialTransactions.Comment,FinancialTransactionRows.AmountCents,FinancialTransactionRows.CreatedDateTime,FinancialTransactionRows.CreatedByPersonId FROM FinancialTransactions,FinancialTransactionRows WHERE FinancialTransactionRows.Deleted=0 AND FinancialTransactions.FinancialTransactionId=FinancialTransactionRows.FinancialTransactionId AND FinancialTransactionRows.FinancialAccountId IN (" + - JoinIds(financialAccountIds) + ") AND DateTime >= '" + + JoinIds(financialAccountIds) + ") AND DateTime " + selectorLower + " '" + startDateTime.ToString("yyyy-MM-dd HH:mm:ss") + - "' AND DateTime < '" + endDateTime.ToString("yyyy-MM-dd HH:mm:ss") + "' ORDER BY DateTime,FinancialTransactions.FinancialTransactionId,FinancialTransactionRows.CreatedDateTime;", + "' AND DateTime " + selectorUpper + " '" + endDateTime.ToString("yyyy-MM-dd HH:mm:ss") + "' ORDER BY DateTime,FinancialTransactions.FinancialTransactionId,FinancialTransactionRows.CreatedDateTime;", connection); using (DbDataReader reader = command.ExecuteReader()) @@ -444,8 +457,6 @@ namespace Swarmops.Database { connection.Open(); - // TODO: Must limit this to dimension 1 - string commandString = "select FinancialTransactions.FinancialTransactionId, " + "FinancialTransactions.OrganizationId, FinancialTransactions.DateTime, " + "FinancialTransactions.Comment, FinancialTransactions.ImportHash, " + diff --git a/Logic/Financial/ExternalBankData.cs b/Logic/Financial/ExternalBankData.cs index 597392b0..15f47766 100644 --- a/Logic/Financial/ExternalBankData.cs +++ b/Logic/Financial/ExternalBankData.cs @@ -219,6 +219,15 @@ namespace Swarmops.Logic.Financial public class ExternalBankMismatchingRecordDescription { + public ExternalBankMismatchingRecordDescription() + { + this.Description = string.Empty; + this.MasterCents = new long[0]; + this.SwarmopsCents = new long[0]; + this.TransactionDependencies = new object[0]; + this.ResyncAction = ExternalBankMismatchResyncAction.Unknown; + } + public string Description; public long[] MasterCents; // may have multiple txs with this description public long[] SwarmopsCents; // may have multiple txs with this description @@ -226,21 +235,21 @@ namespace Swarmops.Logic.Financial public object[] TransactionDependencies; // matching the Swarmops array } - public class ExternalBankMismatchHelper + public class ExternalBankMismatchConstruction { - public ExternalBankMismatchHelper() + public ExternalBankMismatchConstruction() { - Master = new List(); - Swarmops = new List(); + Master = new Dictionary(); + Swarmops = new Dictionary(); } - public List Master; - public List Swarmops; + public Dictionary Master; + public Dictionary Swarmops; } - public class ExternalBankMismatchingRecordHelper + public class ExternalBankMismatchingRecordConstruction { - public ExternalBankMismatchingRecordHelper() + public ExternalBankMismatchingRecordConstruction() { Cents = new List(); Dependencies = new List(); @@ -262,6 +271,10 @@ namespace Swarmops.Logic.Financial /// DeleteSwarmops, /// + /// Create a Swarmops transaction (master exists, not in Swarmops) + /// + CreateSwarmops, + /// /// No resync possible - manual action required /// ManualAction diff --git a/Logic/Financial/FinancialAccount.cs b/Logic/Financial/FinancialAccount.cs index 46ad2abb..ab3be6ea 100644 --- a/Logic/Financial/FinancialAccount.cs +++ b/Logic/Financial/FinancialAccount.cs @@ -62,7 +62,13 @@ namespace Swarmops.Logic.Financial public FinancialAccountRows GetRows(DateTime start, DateTime end) { - BasicFinancialAccountRow[] basicRows = SwarmDb.GetDatabaseForReading().GetFinancialAccountRows(Identity, start, end); + BasicFinancialAccountRow[] basicRows = SwarmDb.GetDatabaseForReading().GetFinancialAccountRows(Identity, start, end, false); + return FinancialAccountRows.FromArray(basicRows); + } + + public FinancialAccountRows GetRowsFar(DateTime start, DateTime end) // selects lowerbound < x <= upperbound + { + BasicFinancialAccountRow[] basicRows = SwarmDb.GetDatabaseForReading().GetFinancialAccountRows(Identity, start, end, true); return FinancialAccountRows.FromArray(basicRows); } diff --git a/Logic/Financial/FinancialAccountRow.cs b/Logic/Financial/FinancialAccountRow.cs index ff6b88eb..1b58c19b 100644 --- a/Logic/Financial/FinancialAccountRow.cs +++ b/Logic/Financial/FinancialAccountRow.cs @@ -23,5 +23,10 @@ namespace Swarmops.Logic.Financial } #endregion + + public FinancialTransaction Transaction + { + get { return FinancialTransaction.FromIdentity(this.FinancialTransactionId); } + } } } \ No newline at end of file diff --git a/Logic/Financial/FinancialAccounts.cs b/Logic/Financial/FinancialAccounts.cs index a7078587..4f7d8445 100644 --- a/Logic/Financial/FinancialAccounts.cs +++ b/Logic/Financial/FinancialAccounts.cs @@ -75,9 +75,15 @@ namespace Swarmops.Logic.Financial return FromArray(SwarmDb.GetDatabaseForReading().GetFinancialAccounts(financialAccountIds)); } - public FinancialAccountRows GetRows (DateTime start, DateTime end) + public FinancialAccountRows GetRows(DateTime start, DateTime end) { - BasicFinancialAccountRow[] basicRows = SwarmDb.GetDatabaseForReading().GetFinancialAccountRows(Identities, start, end); + BasicFinancialAccountRow[] basicRows = SwarmDb.GetDatabaseForReading().GetFinancialAccountRows(Identities, start, end, false); + return FinancialAccountRows.FromArray(basicRows); + } + + public FinancialAccountRows GetRowsFar(DateTime start, DateTime end) + { + BasicFinancialAccountRow[] basicRows = SwarmDb.GetDatabaseForReading().GetFinancialAccountRows(Identities, start, end, true); return FinancialAccountRows.FromArray(basicRows); } diff --git a/Site5/App_GlobalResources/Global.resx b/Site5/App_GlobalResources/Global.resx index 6954ca52..4742520a 100644 --- a/Site5/App_GlobalResources/Global.resx +++ b/Site5/App_GlobalResources/Global.resx @@ -395,4 +395,12 @@ - - Select one - - Shown in dropdowns when selection required + + - missing - + When a record or similar is missing. Used in grids. + + + - multiple - + When there are multiple candidates for something. Synonym: "several". Used in grids. + \ No newline at end of file diff --git a/Site5/Pages/v5/Ledgers/Json-ResyncPreview.aspx.cs b/Site5/Pages/v5/Ledgers/Json-ResyncPreview.aspx.cs index 7fd9ff74..8c0435a3 100644 --- a/Site5/Pages/v5/Ledgers/Json-ResyncPreview.aspx.cs +++ b/Site5/Pages/v5/Ledgers/Json-ResyncPreview.aspx.cs @@ -42,9 +42,21 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers foreach (ExternalBankMismatchingDateTime mismatch in mismatchArray) { - string childrenString = string.Empty; // Room for next step here - string rowId = mismatch.DateTime.ToString("yyyyMMddHHmmss"); + + List childItems = new List(); + + foreach (ExternalBankMismatchingRecordDescription mismatchingRecord in mismatch.MismatchingRecords) + { + childItems.Add("{\"id\":\"" + rowId + childItems.Count.ToString("##0") + "\",\"rowName\":\"" + + JsonSanitize(mismatchingRecord.Description) + "\",\"swarmopsData\":\"" + + PrintCentsArray(mismatchingRecord.SwarmopsCents) + "\",\"masterData\":\"" + + PrintCentsArray(mismatchingRecord.MasterCents) + "\",\"notes\":\"" + + JsonSanitize("-todo-") + "\"}"); + } + + string childrenString = String.Join(",", childItems.ToArray()); + string rowName = mismatch.DateTime.ToString(profile.DateTimeFormatString); string swarmopsData = ((double) mismatch.SwarmopsDeltaCents/100.0).ToString("N2") + " " + @@ -68,6 +80,28 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers private Dictionary _hashedAccounts; + private string PrintCents (long cents) + { + return (((double) cents) / 100.0).ToString("N2"); // TODO: Verify that culture is applied + } + + private string PrintCentsArray (long[] cents) + { + if (cents.Length == 1) + { + return PrintCents(cents[0]); + } + else if (cents.Length == 0) + { + return Resources.Global.Global_Missing; + } + else + { + return Resources.Global.Global_Multiple; + } + } + + private string GetAccountGroup (FinancialAccountType type, string localizedGroupName) { string childrenString = GetAccountsRecurse(type, 0); diff --git a/Site5/Pages/v5/Ledgers/ResyncExternalAccount.aspx.cs b/Site5/Pages/v5/Ledgers/ResyncExternalAccount.aspx.cs index cc83cef3..88d307e0 100644 --- a/Site5/Pages/v5/Ledgers/ResyncExternalAccount.aspx.cs +++ b/Site5/Pages/v5/Ledgers/ResyncExternalAccount.aspx.cs @@ -247,6 +247,7 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers // "AddSeconds" because DeltaCents operates on ">= lowbound, < highbound" long masterDeltaCents = externalData.Records[currentRecordIndex++].TransactionNetCents; int masterTransactionCount = 1; + int timestampStartIndex = currentRecordIndex; while (currentRecordIndex < externalData.Records.Length && externalData.Records[currentRecordIndex].DateTime == timeWalker) @@ -266,9 +267,74 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers newMismatch.SwarmopsDeltaCents = swarmopsDeltaCents; newMismatch.SwarmopsTransactionCount = 0; // TODO - // Load transactions from both sources + // Load transactions from both sources. First, create the interim construction object. - // Analyze actions + ExternalBankMismatchConstruction mismatchConstruction = new ExternalBankMismatchConstruction(); + + // Load from Master + + for (int innerRecordIndex = timestampStartIndex; innerRecordIndex < currentRecordIndex; innerRecordIndex++) + { + if (!mismatchConstruction.Master.ContainsKey(externalData.Records[innerRecordIndex].Description)) + { + mismatchConstruction.Master[externalData.Records[innerRecordIndex].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 + } + + // Load from Swarmops + + FinancialAccountRows swarmopsTransactionRows = + account.GetRowsFar(lastTimestamp, timeWalker); // the "select far" is a boundary < x <= boundary selector. Default is boundary <= x < boundary. + + foreach (FinancialAccountRow swarmopsTransactionRow in swarmopsTransactionRows) + { + FinancialTransaction swarmopsTransaction = swarmopsTransactionRow.Transaction; + + if (!mismatchConstruction.Swarmops.ContainsKey(swarmopsTransaction.Description)) + { + mismatchConstruction.Swarmops[swarmopsTransaction.Description] = + new ExternalBankMismatchingRecordConstruction(); + } + + mismatchConstruction.Swarmops[swarmopsTransaction.Description].Cents.Add(swarmopsTransactionRow.AmountCents); + mismatchConstruction.Swarmops[swarmopsTransaction.Description].Dependencies.Add(swarmopsTransaction.Dependency); + } + + // Then, parse the construction object to the presentation object. + + Dictionary mismatchingRecordList = new Dictionary(); + + foreach (string masterKey in mismatchConstruction.Master.Keys) + { + ExternalBankMismatchingRecordDescription newRecord = + new ExternalBankMismatchingRecordDescription(); + newRecord.Description = masterKey; + newRecord.MasterCents = mismatchConstruction.Master[masterKey].Cents.ToArray(); + + mismatchingRecordList[masterKey] = newRecord; + } + + 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(); + } + + newMismatch.MismatchingRecords = mismatchingRecordList.Values.ToArray(); + + // TODO: Analyze actions mismatchList.Add(newMismatch);