From 35fa251168d782abc64105c7e4582925437b89f2 Mon Sep 17 00:00:00 2001 From: Falkvinge Date: Sat, 29 Apr 2017 22:52:27 +0200 Subject: [PATCH] Added more Inbound VAT considerations --- Basic/Types/Financial/BasicInboundInvoice.cs | 12 +++++- Database/Database-Financials.cs | 9 +++++ Database/Database-InboundInvoices.cs | 31 +++++++++++--- Logic/Financial/InboundInvoice.cs | 40 ++++++++++++++----- .../v5/Financial/Json-AttestableCosts.aspx.cs | 2 +- 5 files changed, 76 insertions(+), 18 deletions(-) diff --git a/Basic/Types/Financial/BasicInboundInvoice.cs b/Basic/Types/Financial/BasicInboundInvoice.cs index e5d762f2..eacb0734 100644 --- a/Basic/Types/Financial/BasicInboundInvoice.cs +++ b/Basic/Types/Financial/BasicInboundInvoice.cs @@ -6,7 +6,7 @@ namespace Swarmops.Basic.Types.Financial public class BasicInboundInvoice : IHasIdentity { public BasicInboundInvoice (int inboundInvoiceId, int organizationId, DateTime createdDateTime, - DateTime dueDate, Int64 amountCents, int budgetId, string supplier, string + DateTime dueDate, Int64 amountCents, Int64 vatCents, int budgetId, string supplier, string payToAccount, string ocr, string invoiceReference, bool attested, bool open, DateTime closedDateTime, int closedByPersonId) { @@ -15,6 +15,7 @@ namespace Swarmops.Basic.Types.Financial this.CreatedDateTime = createdDateTime; this.DueDate = dueDate; this.AmountCents = amountCents; + this.VatCents = vatCents; this.BudgetId = budgetId; this.Supplier = supplier; this.PayToAccount = payToAccount; @@ -28,7 +29,7 @@ namespace Swarmops.Basic.Types.Financial public BasicInboundInvoice (BasicInboundInvoice original) : this (original.InboundInvoiceId, original.OrganizationId, original.CreatedDateTime, - original.DueDate, original.AmountCents, original.BudgetId, + original.DueDate, original.AmountCents, original.VatCents, original.BudgetId, original.Supplier, original.PayToAccount, original.Ocr, original.InvoiceReference, original.Attested, original.Open, original.ClosedDateTime, original.ClosedByPersonId) { @@ -48,7 +49,14 @@ namespace Swarmops.Basic.Types.Financial public int ClosedByPersonId { get; protected set; } public int OrganizationId { get; protected set; } public string Supplier { get; protected set; } + /// + /// Total amount, INCLUDING any possible VAT + /// public Int64 AmountCents { get; protected set; } + /// + /// The VAT part of the AmountCents, or zero if no VAT applied + /// + public Int64 VatCents { get; protected set; } #region IHasIdentity Members diff --git a/Database/Database-Financials.cs b/Database/Database-Financials.cs index 2085b59c..91f14819 100644 --- a/Database/Database-Financials.cs +++ b/Database/Database-Financials.cs @@ -1326,6 +1326,15 @@ namespace Swarmops.Database // TODO: Return BasicFinancialValidation object + public void CreateFinancialValidation(FinancialValidationType validationType, + FinancialDependencyType dependencyType, int foreignId, + DateTime validatedDateTime, int personId, Int64 amountCents) + { + CreateFinancialValidation(validationType, dependencyType, + foreignId, validatedDateTime, personId, amountCents/100.0); + } + + public void CreateFinancialValidation (FinancialValidationType validationType, FinancialDependencyType dependencyType, int foreignId, diff --git a/Database/Database-InboundInvoices.cs b/Database/Database-InboundInvoices.cs index 513c6448..7a396eb8 100644 --- a/Database/Database-InboundInvoices.cs +++ b/Database/Database-InboundInvoices.cs @@ -16,7 +16,8 @@ namespace Swarmops.Database private const string inboundInvoiceFieldSequence = " InboundInvoiceId,OrganizationId,CreatedDateTime,CreatedByPersonId,DueDate," + "AmountCents,BudgetId,Attested,Open,Supplier," + - "PayToAccount,Ocr,InvoiceReference,ClosedDateTime,ClosedByPersonId " + + "PayToAccount,Ocr,InvoiceReference,ClosedDateTime,ClosedByPersonId, " + + "VatCents " + "FROM InboundInvoices "; private BasicInboundInvoice ReadInboundInvoiceFromDataReader (DbDataReader reader) @@ -36,9 +37,10 @@ namespace Swarmops.Database string invoiceReference = reader.GetString (12); DateTime closedDateTime = reader.GetDateTime (13); int closedByPersonId = reader.GetInt32 (14); + Int64 vatCents = reader.GetInt64(15); return new BasicInboundInvoice (inboundInvoiceId, organizationId, createdDateTime, dueDate, - amountCents, budgetId, supplier, payToAccount, ocr, invoiceReference, + amountCents, vatCents, budgetId, supplier, payToAccount, ocr, invoiceReference, attested, open, closedDateTime, closedByPersonId); } @@ -208,17 +210,34 @@ namespace Swarmops.Database } - public void SetInboundInvoiceAmount (int inboundInvoiceId, Int64 amountCents) + public void SetInboundInvoiceAmount(int inboundInvoiceId, Int64 amountCents) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); - DbCommand command = GetDbCommand ("SetInboundInvoiceAmountPrecise", connection); + DbCommand command = GetDbCommand("SetInboundInvoiceAmountPrecise", connection); command.CommandType = CommandType.StoredProcedure; - AddParameterWithName (command, "inboundInvoiceId", inboundInvoiceId); - AddParameterWithName (command, "amountCents", amountCents); + AddParameterWithName(command, "inboundInvoiceId", inboundInvoiceId); + AddParameterWithName(command, "amountCents", amountCents); + + command.ExecuteNonQuery(); + } + } + + + public void SetInboundInvoiceVatCents(int inboundInvoiceId, Int64 amountCents) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetInboundInvoiceVatCents", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "inboundInvoiceId", inboundInvoiceId); + AddParameterWithName(command, "vatCents", amountCents); command.ExecuteNonQuery(); } diff --git a/Logic/Financial/InboundInvoice.cs b/Logic/Financial/InboundInvoice.cs index b6acc943..50103d8a 100644 --- a/Logic/Financial/InboundInvoice.cs +++ b/Logic/Financial/InboundInvoice.cs @@ -131,7 +131,7 @@ namespace Swarmops.Logic.Financial SwarmDb.GetDatabaseForWriting().SetInboundInvoiceAttested (Identity, true); SwarmDb.GetDatabaseForWriting().CreateFinancialValidation (FinancialValidationType.Attestation, FinancialDependencyType.InboundInvoice, Identity, - DateTime.UtcNow, attester.Identity, (double) Amount); + DateTime.UtcNow, attester.Identity, BudgetAmountCents); base.Attested = true; } @@ -140,7 +140,7 @@ namespace Swarmops.Logic.Financial SwarmDb.GetDatabaseForWriting().SetInboundInvoiceAttested (Identity, false); SwarmDb.GetDatabaseForWriting().CreateFinancialValidation (FinancialValidationType.Deattestation, FinancialDependencyType.InboundInvoice, Identity, - DateTime.UtcNow, deattester.Identity, (double) Amount); + DateTime.UtcNow, deattester.Identity, BudgetAmountCents); base.Attested = false; } @@ -169,7 +169,7 @@ namespace Swarmops.Logic.Financial } public static InboundInvoice Create (Organization organization, DateTime dueDate, Int64 amountCents, - Int64 amountVatCents, FinancialAccount budget, string supplier, string description, string payToAccount, string ocr, + Int64 vatCents, FinancialAccount budget, string supplier, string description, string payToAccount, string ocr, string invoiceReference, Person creatingPerson) { InboundInvoice newInvoice = FromIdentity (SwarmDb.GetDatabaseForWriting(). @@ -179,6 +179,11 @@ namespace Swarmops.Logic.Financial newInvoice.Description = description; // Not in original schema; not cause for schema update + if (vatCents > 0) + { + newInvoice.VatCents = vatCents; + } + // Create a corresponding financial transaction with rows FinancialTransaction transaction = @@ -186,11 +191,11 @@ namespace Swarmops.Logic.Financial "Invoice #" + newInvoice.Identity + " from " + supplier); transaction.AddRow (organization.FinancialAccounts.DebtsInboundInvoices, -amountCents, creatingPerson); - if (amountVatCents > 0) + if (vatCents > 0) { - transaction.AddRow(organization.FinancialAccounts.AssetsVatInboundUnreported, amountVatCents, + transaction.AddRow(organization.FinancialAccounts.AssetsVatInboundUnreported, vatCents, creatingPerson); - transaction.AddRow(budget, amountCents - amountVatCents, creatingPerson); + transaction.AddRow(budget, amountCents - vatCents, creatingPerson); } else { @@ -204,7 +209,7 @@ namespace Swarmops.Logic.Financial // Create notification (slightly misplaced logic, but this is failsafest place) - OutboundComm.CreateNotificationAttestationNeeded (budget, creatingPerson, supplier, amountCents/100.0, + OutboundComm.CreateNotificationAttestationNeeded (budget, creatingPerson, supplier, (amountCents-vatCents)/100.0, description, NotificationResource.InboundInvoice_Created); // Slightly misplaced logic, but failsafer here SwarmopsLogEntry.Create (creatingPerson, @@ -214,11 +219,18 @@ namespace Swarmops.Logic.Financial // Clear a cache FinancialAccount.ClearAttestationAdjustmentsCache(organization); - - return newInvoice; } + /// + /// The amount that charges the budget (invoice total minus inbound VAT) + /// + public Int64 BudgetAmountCents + { + get { return this.AmountCents - this.VatCents; } + } + + public static InboundInvoice FromBasic (BasicInboundInvoice basic) { return new InboundInvoice (basic); @@ -246,6 +258,16 @@ namespace Swarmops.Logic.Financial } + public new Int64 VatCents + { + get { return base.VatCents; } + internal set + { + base.VatCents = value; + SwarmDb.GetDatabaseForWriting().SetInboundInvoiceVatCents(this.Identity, value); + } + } + private void UpdateTransaction (Person updatingPerson) { Dictionary nominalTransaction = new Dictionary(); diff --git a/Site/Pages/v5/Financial/Json-AttestableCosts.aspx.cs b/Site/Pages/v5/Financial/Json-AttestableCosts.aspx.cs index 9f658af3..75a4d1c0 100644 --- a/Site/Pages/v5/Financial/Json-AttestableCosts.aspx.cs +++ b/Site/Pages/v5/Financial/Json-AttestableCosts.aspx.cs @@ -190,7 +190,7 @@ public partial class Pages_v5_Finance_Json_AttestableCosts : DataV5Base invoice.Budget.OwnerPersonId == Person.NobodyId) { AttestableItem item = new AttestableItem ("I" + invoice.Identity.ToString (CultureInfo.InvariantCulture), - invoice.Supplier, invoice.AmountCents, invoice.Budget, invoice.InvoiceReference, + invoice.Supplier, invoice.BudgetAmountCents, invoice.Budget, invoice.InvoiceReference, "Financial_InvoiceInbound", hasDox, invoice); if (invoice.Attested)