diff --git a/Logic/Financial/FinancialAccount.cs b/Logic/Financial/FinancialAccount.cs index 04db0532..b4e445da 100644 --- a/Logic/Financial/FinancialAccount.cs +++ b/Logic/Financial/FinancialAccount.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Linq; using System.Runtime.CompilerServices; using Swarmops.Basic.Types.Financial; using Swarmops.Common; @@ -171,7 +172,13 @@ namespace Swarmops.Logic.Financial public FinancialAccountDocument GetMostRecentDocument(FinancialAccountDocumentType documentType) { - // TODO: Continue here + FinancialAccountDocuments documents = FinancialAccountDocuments.ForAccount(this, documentType); + + if (documents.Count() > 0) + { + return documents.Last(); + } + return null; } diff --git a/Logic/Financial/FinancialAccountDocuments.cs b/Logic/Financial/FinancialAccountDocuments.cs index a4d2bfeb..f0b8577d 100644 --- a/Logic/Financial/FinancialAccountDocuments.cs +++ b/Logic/Financial/FinancialAccountDocuments.cs @@ -4,11 +4,39 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Swarmops.Basic.Types.Financial; +using Swarmops.Common.Enums; +using Swarmops.Database; using Swarmops.Logic.Support; namespace Swarmops.Logic.Financial { public class FinancialAccountDocuments: PluralBase { + public static FinancialAccountDocuments ForAccount(FinancialAccount account, FinancialAccountDocumentType typeRequested = FinancialAccountDocumentType.Unknown) + { + FinancialAccountDocuments result = FromArray(SwarmDb.GetDatabaseForReading().GetFinancialAccountDocuments(account)); + result.Sort(new FinancialAccountDocumentsSorterByTypeThenDate()); + + if (typeRequested != FinancialAccountDocumentType.Unknown) + { + return (FinancialAccountDocuments) result.Where(doc => doc.Type == typeRequested); + } + + return result; + } + + } + + internal class FinancialAccountDocumentsSorterByTypeThenDate : IComparer + { + public int Compare(FinancialAccountDocument a, FinancialAccountDocument b) + { + if (a.Type != b.Type) + { + return a.Type.CompareTo(b.Type); + } + + return (a.ConcernsPeriodStart.CompareTo(b.ConcernsPeriodStart)); + } } }