diff --git a/Database/Configuration.cs b/Database/Configuration.cs index fb4c54b7..2d037e58 100644 --- a/Database/Configuration.cs +++ b/Database/Configuration.cs @@ -14,7 +14,7 @@ namespace Swarmops.Database /// public static int DbVersionExpected { - get { return 37; } + get { return 38; } } diff --git a/Database/Schemata/revisions.txt b/Database/Schemata/revisions.txt index 2a693997..6a34cdad 100644 --- a/Database/Schemata/revisions.txt +++ b/Database/Schemata/revisions.txt @@ -34,4 +34,5 @@ 0034 - Fix for SetFinancialTransactionRowNativeCurrency 0035 - Added OrganizationSequenceId on FinancialTransactions, VatCents setter on invoices [RF] 0036 - Added support for VatCents on ExpenseClaims [RF] -0037 - Added Suppliers, ExternalContacts tables; no procs for them yet [RF] \ No newline at end of file +0037 - Added Suppliers, ExternalContacts tables; no procs for them yet [RF] +0038 - VAT Report tables and functions [RF] \ No newline at end of file diff --git a/Database/Schemata/upgrade-0038.sql b/Database/Schemata/upgrade-0038.sql index 576de6fe..eeb40e50 100644 --- a/Database/Schemata/upgrade-0038.sql +++ b/Database/Schemata/upgrade-0038.sql @@ -2,7 +2,7 @@ CREATE TABLE `VatReports` ( `VatReportId` INT NOT NULL AUTO_INCREMENT, `OrganizationId` INT NOT NULL, `CreatedDateTime` DATETIME NOT NULL, - `DateTimeStart` DATETIME NOT NULL, + `YearMonthStart` BIGINT NOT NULL, `MonthCount` INT NOT NULL, `Open` TINYINT NOT NULL, `Turnover` BIGINT NOT NULL, @@ -11,7 +11,7 @@ CREATE TABLE `VatReports` ( `UnderConstruction` TINYINT NOT NULL COMMENT 'This is 1 when rows are being populated and the report is not yet ready to display.', PRIMARY KEY (`VatReportId`), INDEX `Ix_Organization` (`OrganizationId` ASC), - INDEX `Ix_DateTimeStart` (`DateTimeStart` ASC), + INDEX `Ix_YearMonthStart` (`YearMonthStart` ASC), INDEX `Ix_Open` (`Open` ASC), INDEX `Ix_Construction` (`UnderConstruction` ASC)) @@ -22,15 +22,15 @@ CREATE TABLE `VatReports` ( CREATE TABLE `VatReportItems` ( `VatReportItemId` INT NOT NULL AUTO_INCREMENT, `VatReportId` INT NOT NULL, - `FinancialTransactionRowId` INT NOT NULL, - `ForeignObjectId` INT NOT NULL, + `FinancialTransactionId` INT NOT NULL, + `ForeignId` INT NOT NULL, `FinancialDependencyTypeId` INT NOT NULL, `TurnoverCents` BIGINT NOT NULL, `VatInboundCents` BIGINT NOT NULL, `VatOutboundCents` BIGINT NOT NULL, PRIMARY KEY (`VatReportItemId`), INDEX `Ix_ReportId` (`VatReportId` ASC), - INDEX `Ix_TxRowId` (`FinancialTransactionRowId` ASC)) + INDEX `Ix_TxId` (`FinancialTransactionId` ASC)) # @@ -39,7 +39,7 @@ DROP PROCEDURE IF EXISTS `CreateVatReport` # -DROP PROCEDURE IF EXISTS `SetVatReportComplete` +DROP PROCEDURE IF EXISTS `SetVatReportReleased` # @@ -54,15 +54,15 @@ DROP PROCEDURE IF EXISTS `CreateVatReportItem` CREATE PROCEDURE `CreateVatReport` ( organizationId INT, createdDateTime DATETIME, - dateTimeStart DATETIME, + yearMonthStart BIGINT, monthCount INT ) BEGIN INSERT INTO VatReports - (OrganizationId,CreatedDateTime,DateTimeStart,MonthCount,Open,TurnoverCents,VatInboundCents,VatOutboundCents,UnderConstruction) + (OrganizationId,CreatedDateTime,YearMonthStart,MonthCount,Open,TurnoverCents,VatInboundCents,VatOutboundCents,UnderConstruction) VALUES - (organizationId,createdDateTime,dateTimeStart,monthCount,1,0,0,0,1); + (organizationId,createdDateTime,yearMonthStart,monthCount,1,0,0,0,1); SELECT LAST_INSERT_ID() AS Identity; END @@ -70,7 +70,7 @@ END # -CREATE PROCEDURE `SetVatReportCompleted` ( +CREATE PROCEDURE `SetVatReportReleased` ( vatReportId INT ) @@ -96,21 +96,40 @@ END CREATE PROCEDURE `AddVatReportItem` ( vatReportId INT, - financialTransactionRowId INT, + financialTransactionId INT, foreignObjectId INT, - financialDepedencyTypeId INT, + financialDependencyType VARCHAR(64), turnoverCents BIGINT, vatInboundCents BIGINT, vatOutboundCents BIGINT ) BEGIN + + DECLARE financialDependencyTypeId INTEGER; + + SELECT 0 INTO financialDependencyTypeId; + + IF ((SELECT COUNT(*) FROM FinancialDependencyTypes WHERE FinancialDependencyTypes.Name=financialDependencyType) = 0) + THEN + INSERT INTO FinancialDependencyTypes (Name) + VALUES (financialDependencyType); + + SELECT LAST_INSERT_ID() INTO financialDependencyTypeId; + + ELSE + + SELECT FinancialDependencyTypes.FinancialDependencyTypeId INTO FinancialDependencyTypeId FROM FinancialDependencyTypes + WHERE FinancialDependencyTypes.Name=financialDependencyType; + + END IF; + UPDATE VatReports Set VatReports.TurnoverCents = VatReports.TurnoverCents + turnoverCents, VatReports.VatInboundCents = VatReports.VatInboundCents + vatInboundCents, VatReports.VatOutboundCents = VatReports.VatOutboundCents + vatOutboundCents WHERE VatReportId = vatReportId; - INSERT INTO VatReportItems (VatReportId, FinancialTransactionRowId, ForeignObjectId, FinancialDependencyTypeId, TurnoverCents, VatInboundCents, VatOutboundCents) - VALUES (vatReportId, financialTransactionRowId, foreignObjectId, financialDependencyTypeId, turnoverCents, vatInboundCents, vatOutboundCents); + INSERT INTO VatReportItems (VatReportId, FinancialTransactionId, ForeignObjectId, FinancialDependencyTypeId, TurnoverCents, VatInboundCents, VatOutboundCents) + VALUES (vatReportId, financialTransactionId, foreignObjectId, financialDependencyTypeId, turnoverCents, vatInboundCents, vatOutboundCents); END