Lots of work on VAT reports

This commit is contained in:
Rick Falkvinge
2017-10-07 17:19:02 +02:00
parent 395c0debcb
commit 06a5732a09
8 changed files with 273 additions and 30 deletions
+25 -2
View File
@@ -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
+77 -9
View File
@@ -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<BasicSalary> result = new List<BasicSalary>();
List<BasicVatReport> result = new List<BasicVatReport>();
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<BasicVatReportItem> result = new List<BasicVatReportItem>();
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.
+10 -1
View File
@@ -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());
+1 -18
View File
@@ -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);
}
+69
View File
@@ -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); }
}
}
}
+65
View File
@@ -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
}
}
+23
View File
@@ -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<VatReportItems, VatReportItem, BasicVatReportItem>
{
#region Creation and Construction
public static VatReportItems ForReport(VatReport report)
{
return FromArray(SwarmDb.GetDatabaseForReading().GetVatReportItems(report));
}
#endregion
}
}
+3
View File
@@ -126,6 +126,9 @@
<Compile Include="Financial\HotBitcoinAddressUnspent.cs" />
<Compile Include="Financial\HotBitcoinAddressUnspents.cs" />
<Compile Include="Financial\IPayable.cs" />
<Compile Include="Financial\VatReport.cs" />
<Compile Include="Financial\VatReportItem.cs" />
<Compile Include="Financial\VatReportItems.cs" />
<Compile Include="Resources\Logic.Financial.FinancialAccount.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>