diff --git a/Basic/Types/Financial/BasicVatReport.cs b/Basic/Types/Financial/BasicVatReport.cs index 391fc421..82c94d6a 100644 --- a/Basic/Types/Financial/BasicVatReport.cs +++ b/Basic/Types/Financial/BasicVatReport.cs @@ -11,22 +11,44 @@ namespace Swarmops.Basic.Types.Financial { // Basic ctor - + public BasicVatReport(int vatReportId, int organizationId, string guid, DateTime createdDateTime, + int yearMonthStart, int monthCount, bool open, Int64 turnoverCents, Int64 vatInboundCents, + Int64 vatOutboundCents, bool underConstruction) + { + this.VatReportId = vatReportId; + this.OrganizationId = organizationId; + this.Guid = guid; + this.CreatedDateTime = createdDateTime; + this.YearMonthStart = yearMonthStart; + this.MonthCount = monthCount; + this.Open = open; + this.TurnoverCents = turnoverCents; + this.VatInboundCents = vatInboundCents; + this.VatOutboundCents = vatOutboundCents; + this.UnderConstruction = underConstruction; + } // Copy ctor - + public BasicVatReport(BasicVatReport original) + : this(original.Identity, original.OrganizationId, original.Guid, original.CreatedDateTime, + original.YearMonthStart, original.MonthCount, original.Open, original.TurnoverCents, + original.VatInboundCents, original.VatOutboundCents, original.UnderConstruction) + { + // empty copy ctor + } // Properties public int VatReportId { get; private set; } public int OrganizationId { get; private set; } public string Guid { get; private set; } - public int YearMonth { get; private set; } + public DateTime CreatedDateTime { get; private set; } + public int YearMonthStart { get; private set; } public int MonthCount { get; private set; } public Int64 TurnoverCents { get; protected set; } - public Int64 VatOutboundCents { get; protected set; } public Int64 VatInboundCents { get; protected set; } + public Int64 VatOutboundCents { get; protected set; } public bool Open { get; protected set; } public bool UnderConstruction { get; protected set; } diff --git a/Database/Configuration.cs b/Database/Configuration.cs index 0cb8b4ab..e0e2bfd2 100644 --- a/Database/Configuration.cs +++ b/Database/Configuration.cs @@ -14,7 +14,7 @@ namespace Swarmops.Database /// public static int DbVersionExpected { - get { return 44; } + get { return 45; } } diff --git a/Database/Database-VatReports.cs b/Database/Database-VatReports.cs index c7174d29..f977153e 100644 --- a/Database/Database-VatReports.cs +++ b/Database/Database-VatReports.cs @@ -12,45 +12,40 @@ namespace Swarmops.Database #region Field reading code private const string vatReportFieldSequence = - " SalaryId,PayrollItemId,PayoutDate,Salaries.BaseSalaryCents,NetSalaryCents," + // 0-4 - "SubtractiveTaxCents,AdditiveTaxCents,Attested,NetPaid,TaxPaid," + // 5-9 - "Salaries.Open " + // 10 - "FROM Salaries "; + " VatReportId,OrganizationId,Guid,CreatedDateTime,YearMonthStart," + // 0-4 + "MonthCount,VatReports.Open,TurnoverCents,VatInboundCents,VatOutboundCents," + // 5-9 + "UnderConstruction " + // 10 + "FROM VatReports "; private const string vatReportItemFieldSequence = - "" + - "FROM VatReportItems "; + " VatReportItemId,VatReportId,FinancialTransactionId,ForeignId,FinancialDependencyTypes.Name AS FinancialDependencyType," + // 0-4 + "TurnoverCents,VatInboundCents,VatOutboundCents " + + "FROM VatReportItems " + + "JOIN FinancialDependencyTypes ON (VatReportItemId.FinancialDependencyTypeId=FinancialDependencyTypes.FinancialDependencyTypeId) "; - private static BasicSalary ReadVatReportFromDataReader (IDataRecord reader) + private static BasicVatReport ReadVatReportFromDataReader (IDataRecord reader) { - int salaryId = reader.GetInt32 (0); - int payrollItemId = reader.GetInt32 (1); - DateTime payoutDate = reader.GetDateTime (2); - Int64 baseSalaryCents = reader.GetInt64 (3); - Int64 netSalaryCents = reader.GetInt64 (4); - Int64 subtractiveTaxCents = reader.GetInt64 (5); - Int64 additiveTaxCents = reader.GetInt64 (6); - bool attested = reader.GetBoolean (7); - bool netPaid = reader.GetBoolean (8); - bool taxPaid = reader.GetBoolean (9); - bool open = reader.GetBoolean (10); + int vatReportId = reader.GetInt32 (0); + int organizationId = reader.GetInt32(1); + string guid = reader.GetString(2); + DateTime createdDateTime = reader.GetDateTime (3); + int yearMonthStart = reader.GetInt32 (4); + int monthCount = reader.GetInt32 (5); + bool open = reader.GetBoolean(6); + Int64 turnoverCents = reader.GetInt64(7); + Int64 vatInboundCents = reader.GetInt64(8); + Int64 vatOutboundCents = reader.GetInt64(9); + bool underConstruction = reader.GetBoolean (10); - return new BasicSalary (salaryId, payrollItemId, payoutDate, baseSalaryCents, netSalaryCents, - subtractiveTaxCents, additiveTaxCents, - attested, netPaid, taxPaid, open); + return new BasicVatReport(vatReportId, organizationId, guid, createdDateTime, yearMonthStart, monthCount, + open, turnoverCents, vatInboundCents, vatOutboundCents, underConstruction); } #endregion #region Record reading - SELECT statements - /// - /// Gets a salary from the database. - /// - /// The salary database identity. - /// The requested salary. - /// Thrown if there is no such identity. - public BasicSalary GetVatReport (int salaryId) + public BasicVatReport GetVatReport (int vatReportId) { using (DbConnection connection = GetMySqlDbConnection()) { @@ -58,28 +53,21 @@ namespace Swarmops.Database DbCommand command = GetDbCommand ( - "SELECT" + salaryFieldSequence + "WHERE SalaryId=" + salaryId + ";", connection); + "SELECT" + vatReportFieldSequence + "WHERE VatReportId=" + vatReportId + ";", connection); using (DbDataReader reader = command.ExecuteReader()) { if (reader.Read()) { - return ReadSalaryFromDataReader (reader); + return ReadVatReportFromDataReader (reader); } - throw new ArgumentException ("Unknown Salary Id: " + salaryId); + throw new ArgumentException ("Unknown VAT Report Id: " + vatReportId); } } } - /// - /// Gets salaries from the database. - /// - /// - /// An optional combination of a Person and/or Organization object and/or DatabaseCondition - /// specifiers. - /// - /// A list of matching salaries. + public BasicSalary[] GetVatReports (params object[] conditions) { List result = new List(); @@ -131,17 +119,22 @@ namespace Swarmops.Database } - public void CreateVatReportItem (int salaryId, bool netPaid) + public void CreateVatReportItem (int vatReportId, int financialTransactionId, int foreignObjectId, string financialDependencyType, Int64 turnoverCents, Int64 vatInboundCents, Int64 vatOutboundCents) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); - DbCommand command = GetDbCommand ("SetSalaryNetPaid", connection); + DbCommand command = GetDbCommand ("CreateVatReportItem", connection); command.CommandType = CommandType.StoredProcedure; - AddParameterWithName (command, "salaryId", salaryId); - AddParameterWithName (command, "netPaid", netPaid); + AddParameterWithName(command, "vatReportId", vatReportId); + AddParameterWithName(command, "financialTransactionId", financialTransactionId); + AddParameterWithName(command, "foreignObjectId", foreignObjectId); + AddParameterWithName(command, "financialDependencyType", financialDependencyType); + AddParameterWithName(command, "turnoverCents", turnoverCents); + AddParameterWithName(command, "vatInboundCents", vatInboundCents); + AddParameterWithName(command, "vatOutboundCents", vatOutboundCents); command.ExecuteNonQuery(); @@ -151,34 +144,33 @@ namespace Swarmops.Database } - public void SetVatReportReleased (int salaryId, Int64 netSalaryCents) + public void SetVatReportReleased (int vatReportId) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); - DbCommand command = GetDbCommand ("SetSalaryNetSalaryPrecise", connection); + DbCommand command = GetDbCommand ("SetVatReportReleased", connection); command.CommandType = CommandType.StoredProcedure; - AddParameterWithName (command, "salaryId", salaryId); - AddParameterWithName (command, "netSalaryCents", netSalaryCents); + AddParameterWithName (command, "vatReportId", vatReportId); command.ExecuteNonQuery(); } } - public void SetVatReportOpen (int salaryId, bool taxPaid) + public void SetVatReportOpen (int vatReportId, bool open) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); - DbCommand command = GetDbCommand ("SetSalaryTaxPaid", connection); + DbCommand command = GetDbCommand ("SetVatReportOpen", connection); command.CommandType = CommandType.StoredProcedure; - AddParameterWithName (command, "salaryId", salaryId); - AddParameterWithName (command, "taxPaid", taxPaid); + AddParameterWithName (command, "vatReportId", vatReportId); + AddParameterWithName (command, "open", open); command.ExecuteNonQuery(); @@ -190,4 +182,5 @@ namespace Swarmops.Database #endregion } -} \ No newline at end of file +} + \ No newline at end of file diff --git a/Database/Schemata/revisions.txt b/Database/Schemata/revisions.txt index 8ab70123..e3c4f542 100644 --- a/Database/Schemata/revisions.txt +++ b/Database/Schemata/revisions.txt @@ -41,4 +41,5 @@ 0041 - InboundInvoice housekeeping; added OrgSeqId [RF] 0042 - Bugfix to SetInboundInvoiceOrganizationSequenceId [RF] 0043 - FinancialTransactionRows housekeeping [RF] -0044 - Mark bitcoin as cryptocurrency +0044 - Mark bitcoin as cryptocurrency [RF] +0045 - VAT Report work [RF] \ No newline at end of file diff --git a/Database/Schemata/upgrade-0045.sql b/Database/Schemata/upgrade-0045.sql new file mode 100644 index 00000000..32eb92f6 --- /dev/null +++ b/Database/Schemata/upgrade-0045.sql @@ -0,0 +1,73 @@ +ALTER TABLE `VatReports` +CHANGE COLUMN `VatInboundCents` `VatInboundCents` BIGINT(20) NOT NULL AFTER `Turnover` + + +# + + +DROP PROCEDURE IF EXISTS `AddVatReportItem` + + +# + + +CREATE PROCEDURE `CreateVatReportItem` ( + vatReportId INT, + financialTransactionId INT, + foreignObjectId 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; + + INSERT INTO VatReportItems (VatReportId, FinancialTransactionId, ForeignObjectId, FinancialDependencyTypeId, TurnoverCents, VatInboundCents, VatOutboundCents) + VALUES (vatReportId, financialTransactionId, foreignObjectId, financialDependencyTypeId, turnoverCents, vatInboundCents, vatOutboundCents); +END + + + +# + + +DROP PROCEDURE IF EXISTS `SetVatReportReleased` + + +# + + +CREATE PROCEDURE `SetVatReportReleased` ( + vatReportId INT +) + +BEGIN + UPDATE VatReports + SET + VatReports.TurnoverCents = (SELECT SUM(TurnoverCents) FROM VatReportItems WHERE VatReportItems.VatReportId=vatReportId), + VatReports.VatInboundCents = (SELECT SUM(VatInboundCents) FROM VatReportItems WHERE VatReportItems.VatReportId=vatReportId), + VatReports.VatOutboundCents = (SELECT SUM(VatOutboundCents) FROM VatReportItems WHERE VatReportItems.VatReportId=vatReportId), + VatReports.UnderConstruction = 0 + + WHERE VatReports.VatReportId=vatReportId; + +END