From 5aa059cc634f5914302a9323280c90718cdc4b9e Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 7 Oct 2017 10:03:18 +0200 Subject: [PATCH] Housekeeping on InboundInvoices; added OrgSeqId --- Basic/Types/Financial/BasicInboundInvoice.cs | 8 +- Database/Configuration.cs | 2 +- Database/Database-InboundInvoices.cs | 57 ++++++------ Database/Schemata/revisions.txt | 3 +- Database/Schemata/upgrade-0041.sql | 86 +++++++++++++++++++ Logic/Financial/InboundInvoice.cs | 16 ++++ .../Json-ListInvoicesInbound.aspx.cs | 2 +- 7 files changed, 135 insertions(+), 39 deletions(-) create mode 100644 Database/Schemata/upgrade-0041.sql diff --git a/Basic/Types/Financial/BasicInboundInvoice.cs b/Basic/Types/Financial/BasicInboundInvoice.cs index eacb0734..9fb72f6e 100644 --- a/Basic/Types/Financial/BasicInboundInvoice.cs +++ b/Basic/Types/Financial/BasicInboundInvoice.cs @@ -5,8 +5,8 @@ namespace Swarmops.Basic.Types.Financial { public class BasicInboundInvoice : IHasIdentity { - public BasicInboundInvoice (int inboundInvoiceId, int organizationId, DateTime createdDateTime, - DateTime dueDate, Int64 amountCents, Int64 vatCents, int budgetId, string supplier, string + public BasicInboundInvoice (int inboundInvoiceId, int organizationId, int organizationSequenceId, + DateTime createdDateTime, DateTime dueDate, Int64 amountCents, Int64 vatCents, int budgetId, string supplier, string payToAccount, string ocr, string invoiceReference, bool attested, bool open, DateTime closedDateTime, int closedByPersonId) { @@ -28,7 +28,8 @@ namespace Swarmops.Basic.Types.Financial } public BasicInboundInvoice (BasicInboundInvoice original) - : this (original.InboundInvoiceId, original.OrganizationId, original.CreatedDateTime, + : this (original.InboundInvoiceId, original.OrganizationId, original.OrganizationSequenceId, + original.CreatedDateTime, original.DueDate, original.AmountCents, original.VatCents, original.BudgetId, original.Supplier, original.PayToAccount, original.Ocr, original.InvoiceReference, original.Attested, original.Open, original.ClosedDateTime, original.ClosedByPersonId) @@ -38,6 +39,7 @@ namespace Swarmops.Basic.Types.Financial public int InboundInvoiceId { get; private set; } public DateTime CreatedDateTime { get; private set; } + public int OrganizationSequenceId { get; protected set; } public DateTime DueDate { get; protected set; } public int BudgetId { get; protected set; } public bool Attested { get; protected set; } diff --git a/Database/Configuration.cs b/Database/Configuration.cs index 674a1090..3bb31401 100644 --- a/Database/Configuration.cs +++ b/Database/Configuration.cs @@ -14,7 +14,7 @@ namespace Swarmops.Database /// public static int DbVersionExpected { - get { return 39; } + get { return 41; } } diff --git a/Database/Database-InboundInvoices.cs b/Database/Database-InboundInvoices.cs index 7a396eb8..c523c442 100644 --- a/Database/Database-InboundInvoices.cs +++ b/Database/Database-InboundInvoices.cs @@ -17,7 +17,7 @@ namespace Swarmops.Database " InboundInvoiceId,OrganizationId,CreatedDateTime,CreatedByPersonId,DueDate," + "AmountCents,BudgetId,Attested,Open,Supplier," + "PayToAccount,Ocr,InvoiceReference,ClosedDateTime,ClosedByPersonId, " + - "VatCents " + + "VatCents, OrganizationSequenceId " + "FROM InboundInvoices "; private BasicInboundInvoice ReadInboundInvoiceFromDataReader (DbDataReader reader) @@ -38,8 +38,9 @@ namespace Swarmops.Database DateTime closedDateTime = reader.GetDateTime (13); int closedByPersonId = reader.GetInt32 (14); Int64 vatCents = reader.GetInt64(15); + int organizationSequenceId = reader.GetInt32(16); - return new BasicInboundInvoice (inboundInvoiceId, organizationId, createdDateTime, dueDate, + return new BasicInboundInvoice (inboundInvoiceId, organizationId, organizationSequenceId, createdDateTime, dueDate, amountCents, vatCents, budgetId, supplier, payToAccount, ocr, invoiceReference, attested, open, closedDateTime, closedByPersonId); } @@ -106,32 +107,6 @@ namespace Swarmops.Database #region Creation and manipulation - stored procedures - /* -- UNUSED - public int CreateInboundInvoice(int organizationId, DateTime dueDate, int budgetId, - string supplier, string payToAccount, string ocr, string invoiceReference, double amount, int createdByPersonId) - { - using (DbConnection connection = GetMySqlDbConnection()) - { - connection.Open(); - - DbCommand command = GetDbCommand("CreateInboundInvoice", connection); - command.CommandType = CommandType.StoredProcedure; - - AddParameterWithName(command, "organizationId", organizationId); - AddParameterWithName(command, "createdDateTime", DateTime.Now); - AddParameterWithName(command, "dueDate", dueDate); - AddParameterWithName(command, "budgetId", budgetId); - AddParameterWithName(command, "supplier", supplier); - AddParameterWithName(command, "payToAccount", payToAccount); - AddParameterWithName(command, "ocr", ocr); - AddParameterWithName(command, "invoiceReference", invoiceReference); - AddParameterWithName(command, "amount", amount); - AddParameterWithName(command, "createdByPersonId", createdByPersonId); - - return Convert.ToInt32(command.ExecuteScalar()); - } - }*/ - public int CreateInboundInvoice (int organizationId, DateTime dueDate, int budgetId, string supplier, string payToAccount, string ocr, string invoiceReference, Int64 amountCents, int createdByPersonId) @@ -140,7 +115,7 @@ namespace Swarmops.Database { connection.Open(); - DbCommand command = GetDbCommand ("CreateInboundInvoicePrecise", connection); + DbCommand command = GetDbCommand ("CreateInboundInvoice", connection); command.CommandType = CommandType.StoredProcedure; AddParameterWithName (command, "organizationId", organizationId); @@ -159,17 +134,33 @@ namespace Swarmops.Database } - public void SetInboundInvoiceOpen (int inboundInvoiceId, bool open) + public int SetInboundInvoiceSequence(int inboundInvoiceId) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); - DbCommand command = GetDbCommand ("SetInboundInvoiceOpen", connection); + DbCommand command = GetDbCommand("SetInboundInvoiceOrganizationSequenceId", connection); command.CommandType = CommandType.StoredProcedure; - AddParameterWithName (command, "inboundInvoiceId", inboundInvoiceId); - AddParameterWithName (command, "open", open); + AddParameterWithName(command, "inboundInvoiceId", inboundInvoiceId); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + + public void SetInboundInvoiceOpen(int inboundInvoiceId, bool open) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetInboundInvoiceOpen", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "inboundInvoiceId", inboundInvoiceId); + AddParameterWithName(command, "open", open); command.ExecuteNonQuery(); } diff --git a/Database/Schemata/revisions.txt b/Database/Schemata/revisions.txt index a0e47d33..676848bb 100644 --- a/Database/Schemata/revisions.txt +++ b/Database/Schemata/revisions.txt @@ -37,4 +37,5 @@ 0037 - Added Suppliers, ExternalContacts tables; no procs for them yet [RF] 0038 - VAT Report tables and functions [RF] 0039 - VatReport.Guid added [RF] -0040 - Document.HiresStatus added [RF] \ No newline at end of file +0040 - Document.HiresStatus added [RF] +0041 - InboundInvoice housekeeping; added OrgSeqId [RF] diff --git a/Database/Schemata/upgrade-0041.sql b/Database/Schemata/upgrade-0041.sql new file mode 100644 index 00000000..602afb69 --- /dev/null +++ b/Database/Schemata/upgrade-0041.sql @@ -0,0 +1,86 @@ +ALTER TABLE `InboundInvoices` +DROP COLUMN `Amount`, +ADD COLUMN `OrganizationSequenceId` INT NOT NULL DEFAULT 0 AFTER `OrganizationId`, +ADD INDEX `Index_SequenceId` (`OrganizationSequenceId` ASC) + + +# + +DROP PROCEDURE IF EXISTS `CreateInboundInvoice` + + +# + + +DROP PROCEDURE IF EXISTS `CreateInboundInvoicePrecise` + + +# + +CREATE PROCEDURE `CreateInboundInvoice`( + organizationId INTEGER, + createdDateTime DATETIME, + dueDate DATETIME, + budgetId INTEGER, + supplier VARCHAR(64), + payToAccount VARCHAR(64), + ocr VARCHAR(64), + invoiceReference VARCHAR(64), + amountCents BIGINT, + createdByPersonId INTEGER +) +BEGIN + + DECLARE newIdentity INTEGER; + DECLARE sequenceNumber INTEGER; + + INSERT INTO InboundInvoices + (OrganizationId, CreatedDateTime, DueDate, BudgetId, Supplier, Attested, Open, PayToAccount, Ocr, + InvoiceReference, ClosedDateTime, AmountCents, CreatedByPersonId, ClosedByPersonId) + VALUES + (organizationId, createdDateTime, dueDate, budgetId, supplier, 0, 1, payToAccount, ocr, + invoiceReference, createdDateTime, amountCents, createdByPersonId, 0); + + SELECT LAST_INSERT_ID() INTO newIdentity; + + SELECT COUNT(*) FROM InboundInvoices WHERE + InboundInvoices.OrganizationId = organizationId AND + InboundInvoices.InboundInvoiceId <= newIdentity INTO sequenceNumber; + + UPDATE InboundInvoices + SET InboundInvoices.OrganizationSequenceId = sequenceNumber + WHERE InboundInvoices.InboundInvoiceId = newIdentity; + + SELECT newIdentity AS Identity; + +END + + +# + + +CREATE PROCEDURE `SetInboundInvoiceOrganizationSequenceId`( + IN inboundInvoiceId INTEGER +) +BEGIN + + DECLARE organizationId INTEGER; + DECLARE sequenceNumber INTEGER; + + SELECT InboundInvoices.OrganizationId + FROM InboundInvoices + WHERE InboundInvoices.InboundInvoiceId = inboundInvoiceId + INTO organizationId; + + SELECT COUNT(*) + FROM InboundInvoices + WHERE InboundInvoices.OrganizationId=organizationId + AND InboundInvoices.InboundInvoicesId <= inboundInvoiceId + INTO sequenceNumber; + + UPDATE InboundInvoices SET OrganizationSequenceId = sequenceNumber + WHERE InboundInvoices.InboundInvoiceId = inboundInvoiceId; + + SELECT sequenceNumber AS OrganizationSequenceId; + +END diff --git a/Logic/Financial/InboundInvoice.cs b/Logic/Financial/InboundInvoice.cs index f72854a0..62372313 100644 --- a/Logic/Financial/InboundInvoice.cs +++ b/Logic/Financial/InboundInvoice.cs @@ -46,6 +46,22 @@ namespace Swarmops.Logic.Financial } + public int OrganizationSequenceId + { + get + { + if (this.OrganizationSequenceId == 0) + { + SwarmDb db = SwarmDb.GetDatabaseForWriting(); + base.OrganizationSequenceId = db.SetInboundInvoiceSequence(this.Identity); + return base.OrganizationSequenceId; + } + + return base.OrganizationSequenceId; + } + } + + public decimal Amount { get { return base.AmountCents/100.0m; } diff --git a/Site/Pages/v5/Financial/Json-ListInvoicesInbound.aspx.cs b/Site/Pages/v5/Financial/Json-ListInvoicesInbound.aspx.cs index 5eaf61cf..32dbeb14 100644 --- a/Site/Pages/v5/Financial/Json-ListInvoicesInbound.aspx.cs +++ b/Site/Pages/v5/Financial/Json-ListInvoicesInbound.aspx.cs @@ -63,7 +63,7 @@ public partial class Pages_v5_Finance_Json_ListInvoicesInbound : DataV5Base //"" + //"" + "\"", - "#" + invoice.Identity.ToString("N0"), + "#" + invoice.OrganizationSequenceId.ToString("N0"), JsonSanitize(invoice.DueDate.ToString(invoice.DueDate < dueDateFormatBreakDate ? "yyyy-MMM" : "MMM-dd")), JsonSanitize(invoice.Supplier),