From 06a5732a09d51f33d463f5dc019bc0c52d9d6485 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 7 Oct 2017 17:19:02 +0200 Subject: [PATCH] Lots of work on VAT reports --- Basic/Types/Financial/BasicVatReportItem.cs | 27 ++++++- Database/Database-VatReports.cs | 86 ++++++++++++++++++--- Logic/Financial/FinancialTransaction.cs | 11 ++- Logic/Financial/FinancialValidations.cs | 19 +---- Logic/Financial/VatReport.cs | 69 +++++++++++++++++ Logic/Financial/VatReportItem.cs | 65 ++++++++++++++++ Logic/Financial/VatReportItems.cs | 23 ++++++ Logic/Logic.csproj | 3 + 8 files changed, 273 insertions(+), 30 deletions(-) create mode 100644 Logic/Financial/VatReport.cs create mode 100644 Logic/Financial/VatReportItem.cs create mode 100644 Logic/Financial/VatReportItems.cs diff --git a/Basic/Types/Financial/BasicVatReportItem.cs b/Basic/Types/Financial/BasicVatReportItem.cs index 2bda38e2..0da3077e 100644 --- a/Basic/Types/Financial/BasicVatReportItem.cs +++ b/Basic/Types/Financial/BasicVatReportItem.cs @@ -12,15 +12,38 @@ namespace Swarmops.Basic.Types.Financial { // Normal ctor + public BasicVatReportItem(int vatReportItemId, int vatReportId, int financialTransactionId, int foreignId, + FinancialDependencyType dependencyType, Int64 turnoverCents, Int64 vatInboundCents, Int64 vatOutboundCents) + { + this.VatReportItemId = vatReportItemId; + this.VatReportId = VatReportId; + this.FinancialTransactionId = financialTransactionId; + this.ForeignId = foreignId; + this.DependencyType = dependencyType; + this.TurnoverCents = turnoverCents; + this.VatInboundCents = vatInboundCents; + this.VatOutboundCents = vatOutboundCents; + } + // Copy ctor + public BasicVatReportItem(BasicVatReportItem original) : + this(original.VatReportItemId, original.VatReportId, original.FinancialTransactionId, original.ForeignId, + original.DependencyType, original.TurnoverCents, original.VatInboundCents, original.VatOutboundCents) + { + // empty copy ctor + } + // Properties public int VatReportItemId { get; private set; } - - public int ForeignId { get; private set; } + public int VatReportId { get; private set; } public int FinancialTransactionId { get; private set; } + public int ForeignId { get; private set; } public FinancialDependencyType DependencyType { get; private set; } + public Int64 TurnoverCents { get; private set; } + public Int64 VatInboundCents { get; private set; } + public Int64 VatOutboundCents { get; private set; } // Interfaces diff --git a/Database/Database-VatReports.cs b/Database/Database-VatReports.cs index f977153e..28604bf9 100644 --- a/Database/Database-VatReports.cs +++ b/Database/Database-VatReports.cs @@ -1,9 +1,11 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Data; using System.Data.Common; using Swarmops.Basic.Types; using Swarmops.Basic.Types.Financial; +using Swarmops.Common.Enums; namespace Swarmops.Database { @@ -41,6 +43,24 @@ namespace Swarmops.Database open, turnoverCents, vatInboundCents, vatOutboundCents, underConstruction); } + + private static BasicVatReportItem ReadVatReportItemFromDataReader(IDataRecord reader) + { + int vatReportItemId = reader.GetInt32(0); + int vatReportId = reader.GetInt32(1); + int financialTransactionId = reader.GetInt32(2); + int foreignId = reader.GetInt32(3); + FinancialDependencyType dependencyType = + (FinancialDependencyType) Enum.Parse(typeof (FinancialDependencyType), reader.GetString(4)); + + Int64 turnoverCents = reader.GetInt64(5); + Int64 vatInboundCents = reader.GetInt64(6); + Int64 vatOutboundCents = reader.GetInt64(7); + + return new BasicVatReportItem(vatReportItemId, vatReportId, financialTransactionId, foreignId, + dependencyType, turnoverCents, vatInboundCents, vatOutboundCents); + } + #endregion #region Record reading - SELECT statements @@ -68,24 +88,72 @@ namespace Swarmops.Database } - public BasicSalary[] GetVatReports (params object[] conditions) + public BasicVatReport[] GetVatReports(params object[] conditions) { - List result = new List(); + List result = new List(); using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = - GetDbCommand ( - "SELECT" + salaryFieldSequence + "JOIN Payroll USING (PayrollItemId)" + - ConstructWhereClause ("Salaries", conditions), connection); + GetDbCommand( + "SELECT" + vatReportFieldSequence + ConstructWhereClause("VatReports", conditions), connection); using (DbDataReader reader = command.ExecuteReader()) { while (reader.Read()) { - result.Add (ReadSalaryFromDataReader (reader)); + result.Add(ReadVatReportFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + public BasicVatReportItem GetVatReportItem(int vatReportItemId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "SELECT" + vatReportItemFieldSequence + "WHERE VatReportItemId=" + vatReportItemId + ";", connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + return ReadVatReportItemFromDataReader(reader); + } + + throw new ArgumentException("Unknown VAT Report Item Id: " + vatReportItemId); + } + } + } + + + + + public BasicVatReportItem[] GetVatReportItems(params object[] conditions) + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "SELECT" + vatReportItemFieldSequence + ConstructWhereClause("VatReportItems", conditions), connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadVatReportItemFromDataReader(reader)); } return result.ToArray(); @@ -119,7 +187,7 @@ namespace Swarmops.Database } - public void CreateVatReportItem (int vatReportId, int financialTransactionId, int foreignObjectId, string financialDependencyType, Int64 turnoverCents, Int64 vatInboundCents, Int64 vatOutboundCents) + public int CreateVatReportItem (int vatReportId, int financialTransactionId, int foreignObjectId, FinancialDependencyType financialDependencyType, Int64 turnoverCents, Int64 vatInboundCents, Int64 vatOutboundCents) { using (DbConnection connection = GetMySqlDbConnection()) { @@ -131,12 +199,12 @@ namespace Swarmops.Database AddParameterWithName(command, "vatReportId", vatReportId); AddParameterWithName(command, "financialTransactionId", financialTransactionId); AddParameterWithName(command, "foreignObjectId", foreignObjectId); - AddParameterWithName(command, "financialDependencyType", financialDependencyType); + AddParameterWithName(command, "financialDependencyType", financialDependencyType.ToString()); AddParameterWithName(command, "turnoverCents", turnoverCents); AddParameterWithName(command, "vatInboundCents", vatInboundCents); AddParameterWithName(command, "vatOutboundCents", vatOutboundCents); - command.ExecuteNonQuery(); + return Convert.ToInt32(command.ExecuteScalar()); // "Open" is set in the stored procedure to "NOT (TaxPaid AND NetPaid)". // So if both are paid, Open is set to false. diff --git a/Logic/Financial/FinancialTransaction.cs b/Logic/Financial/FinancialTransaction.cs index 71ccdf2c..c566e011 100644 --- a/Logic/Financial/FinancialTransaction.cs +++ b/Logic/Financial/FinancialTransaction.cs @@ -247,6 +247,11 @@ namespace Swarmops.Logic.Financial return FromIdentity (foreignId); } + if (dependencyType == FinancialDependencyType.VatReport) + { + return VatReport.FromIdentity(foreignId); + } + throw new NotImplementedException ("Unimplemented dependency type: " + dependencyType); } } @@ -337,7 +342,7 @@ namespace Swarmops.Logic.Financial } - private static FinancialDependencyType GetFinancialDependencyType (IHasIdentity foreignObject) + public static FinancialDependencyType GetFinancialDependencyType (IHasIdentity foreignObject) { if (foreignObject is ExpenseClaim) { @@ -367,6 +372,10 @@ namespace Swarmops.Logic.Financial { return FinancialDependencyType.FinancialTransaction; } + if (foreignObject is VatReport) + { + return FinancialDependencyType.VatReport; + } throw new NotImplementedException ("Unidentified dependency encountered in GetFinancialDependencyType:" + foreignObject.GetType()); diff --git a/Logic/Financial/FinancialValidations.cs b/Logic/Financial/FinancialValidations.cs index b95afba6..28b3cd7d 100644 --- a/Logic/Financial/FinancialValidations.cs +++ b/Logic/Financial/FinancialValidations.cs @@ -26,24 +26,7 @@ namespace Swarmops.Logic.Financial private static FinancialDependencyType GetDependencyType (IHasIdentity foreignObject) { - if (foreignObject is ExpenseClaim) - { - return FinancialDependencyType.ExpenseClaim; - } - if (foreignObject is InboundInvoice) - { - return FinancialDependencyType.InboundInvoice; - } - if (foreignObject is Salary) - { - return FinancialDependencyType.Salary; - } - if (foreignObject is CashAdvance) - { - return FinancialDependencyType.CashAdvance; - } - - throw new NotImplementedException ("Unknown dependency: " + foreignObject.GetType()); + return FinancialTransaction.GetFinancialDependencyType(foreignObject); } diff --git a/Logic/Financial/VatReport.cs b/Logic/Financial/VatReport.cs new file mode 100644 index 00000000..39896573 --- /dev/null +++ b/Logic/Financial/VatReport.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Swarmops.Basic.Types.Financial; +using Swarmops.Common.Interfaces; +using Swarmops.Database; + +namespace Swarmops.Logic.Financial +{ + public class VatReport: BasicVatReport + { + #region Creation and Construction + + private VatReport(BasicVatReport basic): base (basic) + { + // private ctor prevents random instantiation + } + + public static VatReport FromBasic(BasicVatReport basic) + { + // Interface to private ctor + return new VatReport(basic); + } + + public static VatReport FromIdentity(int vatReportId) + { + return FromBasic(SwarmDb.GetDatabaseForReading().GetVatReport(vatReportId)); + } + + protected static VatReport FromIdentityAggressive(int vatReportId) + { + // "Open For Writing" is intentional - it bypasses the lag of replication-to-readonly + // instances of large-deployment databases and reads at the write source + + return FromBasic(SwarmDb.GetDatabaseForWriting().GetVatReport(vatReportId)); + } + + #endregion + + public void AddItem(FinancialTransaction transaction, Int64 turnoverCents, + Int64 vatInboundCents, Int64 vatOutboundCents) + { + VatReportItem newItem = VatReportItem.Create(this, transaction, turnoverCents, vatInboundCents, + vatOutboundCents); + } + + public void Release() + { + // if we're not under construction, throw exception + + if (!this.UnderConstruction) + { + throw new InvalidOperationException("VAT report is already released"); + } + + // Releasing calculates sums of turnover, VAT inbound, outbound across items into report record + + SwarmDb.GetDatabaseForWriting().SetVatReportReleased(this.Identity); + base.UnderConstruction = false; + } + + public VatReportItems Items + { + get { return VatReportItems.ForReport(this); } + } + } +} diff --git a/Logic/Financial/VatReportItem.cs b/Logic/Financial/VatReportItem.cs new file mode 100644 index 00000000..3e9cfd09 --- /dev/null +++ b/Logic/Financial/VatReportItem.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Swarmops.Basic.Types.Financial; +using Swarmops.Common.Enums; +using Swarmops.Common.Interfaces; +using Swarmops.Database; + +namespace Swarmops.Logic.Financial +{ + public class VatReportItem: BasicVatReportItem + { + #region Creation and Construction + + private VatReportItem(BasicVatReportItem basic) : + base(basic) + { + // private ctor prevents random uncontrolled instantiation + } + + public static VatReportItem FromBasic(BasicVatReportItem basic) + { + // interface to private ctor + return new VatReportItem(basic); + } + + public static VatReportItem FromIdentity(int vatReportItemId) + { + return FromBasic(SwarmDb.GetDatabaseForReading().GetVatReportItem(vatReportItemId)); + } + + public static VatReportItem FromIdentityAggressive(int vatReportItemId) + { + // "Open For Writing" is intentional - it bypasses the lag of replication-to-readonly + // instances of large-deployment databases and reads at the write source + + return FromBasic(SwarmDb.GetDatabaseForWriting().GetVatReportItem(vatReportItemId)); + } + + public static VatReportItem Create(VatReport report, FinancialTransaction transaction, Int64 turnoverCents, + Int64 vatInboundCents, Int64 vatOutboundCents) + { + // Assumes there's a dependency of some sort + + IHasIdentity foreignObject = transaction.Dependency; + FinancialDependencyType dependencyType = + (foreignObject != null + ? FinancialTransaction.GetFinancialDependencyType(transaction.Dependency) + : FinancialDependencyType.Unknown); + + // The transaction dependency is stored for quick lookup; it duplicates information in the database + // to save an expensive query as a mere optimization. + + int newVatReportItemId = SwarmDb.GetDatabaseForWriting() + .CreateVatReportItem(report.Identity, transaction.Identity, foreignObject?.Identity ?? 0, + dependencyType, turnoverCents, vatInboundCents, vatOutboundCents); + + return FromIdentityAggressive(newVatReportItemId); + } + + #endregion + } +} diff --git a/Logic/Financial/VatReportItems.cs b/Logic/Financial/VatReportItems.cs new file mode 100644 index 00000000..6191c100 --- /dev/null +++ b/Logic/Financial/VatReportItems.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Swarmops.Basic.Types.Financial; +using Swarmops.Database; +using Swarmops.Logic.Support; + +namespace Swarmops.Logic.Financial +{ + public class VatReportItems: PluralBase + { + #region Creation and Construction + + public static VatReportItems ForReport(VatReport report) + { + return FromArray(SwarmDb.GetDatabaseForReading().GetVatReportItems(report)); + } + + #endregion + } +} diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 250e9a0c..1f2acb4a 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -126,6 +126,9 @@ + + + True True