diff --git a/Database/Configuration.cs b/Database/Configuration.cs index 381f7808..764481c6 100644 --- a/Database/Configuration.cs +++ b/Database/Configuration.cs @@ -14,7 +14,7 @@ namespace Swarmops.Database /// public static int DbVersionExpected { - get { return 24; } + get { return 25; } } diff --git a/Database/Database-Payroll.cs b/Database/Database-Payroll.cs index f54d43a9..008399c9 100644 --- a/Database/Database-Payroll.cs +++ b/Database/Database-Payroll.cs @@ -102,13 +102,13 @@ namespace Swarmops.Database public int CreatePayrollItem (int personId, int organizationId, DateTime employedDate, int reportsToPersonId, int countryId, - Int64 baseSalaryCents, int subtractiveTaxLevelId, double additiveTaxLevel, int budgetId) + Int64 baseSalaryCents, int subtractiveTaxLevelId, double additiveTaxLevel, int budgetId, bool isContractor) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); - DbCommand command = GetDbCommand ("CreatePayrollItemPrecise", connection); + DbCommand command = GetDbCommand ("CreatePayrollItem", connection); command.CommandType = CommandType.StoredProcedure; AddParameterWithName (command, "personId", personId); @@ -116,10 +116,12 @@ namespace Swarmops.Database AddParameterWithName (command, "employedDate", DateTime.Now); AddParameterWithName (command, "reportsToPersonId", reportsToPersonId); AddParameterWithName (command, "countryId", countryId); - AddParameterWithName (command, "baseSalaryCents", baseSalaryCents); - AddParameterWithName (command, "subtractiveTaxLevelId", subtractiveTaxLevelId); - AddParameterWithName (command, "additiveTaxLevel", additiveTaxLevel); - AddParameterWithName (command, "budgetId", budgetId); + AddParameterWithName(command, "baseSalaryCents", baseSalaryCents); + AddParameterWithName(command, "budgetId", budgetId); + AddParameterWithName (command, "open", true); + AddParameterWithName(command, "additiveTaxLevel", additiveTaxLevel); + AddParameterWithName(command, "subtractiveTaxLevelId", subtractiveTaxLevelId); + AddParameterWithName(command, "isContractor", isContractor); return Convert.ToInt32 (command.ExecuteScalar()); } diff --git a/Database/Schemata/upgrade-0025.sql b/Database/Schemata/upgrade-0025.sql new file mode 100644 index 00000000..85cffe61 --- /dev/null +++ b/Database/Schemata/upgrade-0025.sql @@ -0,0 +1,32 @@ +DROP procedure IF EXISTS `CreatePayrollItem` + + +# + + +CREATE PROCEDURE `CreatePayrollItem` ( + IN personId INT, + IN organizationId INT, + IN employedDate DATETIME, + IN reportsToPersonId INT, + IN countryId INT, + IN baseSalaryCents INT, + IN budgetId INT, + IN open TINYINT, + IN additiveTaxLevel DOUBLE, + IN subtractiveTaxLevelId INT, + IN isContractor TINYINT +) +BEGIN + INSERT INTO Payroll + (PersonId, OrganizationId, EmployedDate, ReportsToPersonId, + CountryId, BaseSalaryCents, BudgetId, Open, AdditiveTaxLevel, + SubtractiveTaxLevelId, IsContractor, TerminatedDate) + VALUES + (personId, organizationId, employedDate, reportsToPersonId, + countryId, baseSalaryCents, budgetId, open, additiveTaxLevel, + subtractiveTaxLevelId, isContractor, '2099-12-31'); + + SELECT LAST_INSERT_ID() AS Identity; + +END diff --git a/Logic/Financial/PayrollItem.cs b/Logic/Financial/PayrollItem.cs index 31e8c84a..07d52cb2 100644 --- a/Logic/Financial/PayrollItem.cs +++ b/Logic/Financial/PayrollItem.cs @@ -1,3 +1,5 @@ +using System; +using System.IO; using Swarmops.Basic.Types; using Swarmops.Basic.Types.Swarm; using Swarmops.Database; @@ -13,6 +15,33 @@ namespace Swarmops.Logic.Financial // private constructor } + public PayrollItem FromPersonAndOrganization (Person person, Organization organization) + { + Payroll payrollItems = Payroll.FromArray(SwarmDb.GetDatabaseForReading().GetPayroll (person, organization)); + + if (payrollItems.Count > 1) + { + throw new InvalidDataException("More than one payroll item for a person/organization combination is not allowed"); + } + if (payrollItems.Count == 0) + { + return null; // no combo. Throw instead? This has not been consistently coded + } + + return payrollItems[0]; + } + + public PayrollItem Create (Person person, Organization organization, DateTime employedDate, + Person reportsToPerson, Country country, Int64 baseSalaryCents, FinancialAccount budget, + double additiveTaxLevel, int subtractiveTaxLevelId, bool isContractor) + { + int payrollItemId = SwarmDb.GetDatabaseForWriting() + .CreatePayrollItem (person.Identity, organization.Identity, employedDate, reportsToPerson.Identity, + country.Identity, baseSalaryCents, budget.Identity, additiveTaxLevel, subtractiveTaxLevelId, + isContractor); + + return FromIdentityAggressive (payrollItemId); + } private new int PersonId { @@ -55,9 +84,14 @@ namespace Swarmops.Logic.Financial return new PayrollItem (basic); // invoke private ctor } - public static PayrollItem FromIdentity (int payrollItemId) + public static PayrollItem FromIdentity(int payrollItemId) { - return FromBasic (SwarmDb.GetDatabaseForReading().GetPayrollItem (payrollItemId)); + return FromBasic(SwarmDb.GetDatabaseForReading().GetPayrollItem(payrollItemId)); + } + + private static PayrollItem FromIdentityAggressive(int payrollItemId) + { + return FromBasic(SwarmDb.GetDatabaseForWriting().GetPayrollItem(payrollItemId)); } } } \ No newline at end of file diff --git a/Logic/Financial/Salary.cs b/Logic/Financial/Salary.cs index 5401e0a5..7f87b5c8 100644 --- a/Logic/Financial/Salary.cs +++ b/Logic/Financial/Salary.cs @@ -186,27 +186,34 @@ namespace Swarmops.Logic.Financial } } - // calculate tax + Int64 subtractiveTaxCents = 0; + Int64 additiveTaxCents = 0; - Money grossInOrgCurrency = new Money + if (!payrollItem.IsContractor) { - Cents = payCents, - Currency = payrollItem.Organization.Currency, - ValuationDateTime = DateTime.UtcNow - }; - Money grossInTaxCurrency = grossInOrgCurrency.ToCurrency (payrollItem.Country.Currency); + // calculate tax - Money subtractiveTax = TaxLevels.GetTax (payrollItem.Country, payrollItem.SubtractiveTaxLevelId, - grossInTaxCurrency); + Money grossInOrgCurrency = new Money + { + Cents = payCents, + Currency = payrollItem.Organization.Currency, + ValuationDateTime = DateTime.UtcNow + }; - Money subtractiveTaxInOrgCurrency = subtractiveTax.ToCurrency (payrollItem.Organization.Currency); + Money grossInTaxCurrency = grossInOrgCurrency.ToCurrency (payrollItem.Country.Currency); - Int64 subtractiveTaxCents = (Int64) (subtractiveTaxInOrgCurrency.Cents); + Money subtractiveTax = TaxLevels.GetTax (payrollItem.Country, payrollItem.SubtractiveTaxLevelId, + grossInTaxCurrency); - Int64 additiveTaxCents = (Int64) (payCents*payrollItem.AdditiveTaxLevel); + Money subtractiveTaxInOrgCurrency = subtractiveTax.ToCurrency (payrollItem.Organization.Currency); - payCents -= subtractiveTaxCents; + subtractiveTaxCents = (Int64) (subtractiveTaxInOrgCurrency.Cents); + + additiveTaxCents = (Int64) (payCents*payrollItem.AdditiveTaxLevel); + + payCents -= subtractiveTaxCents; + } // Apply all after-tax adjustments