Added PayrollItem.Create (and Db0025)

This commit is contained in:
Rick Falkvinge
2015-08-27 14:09:33 +02:00
parent 29ba10f739
commit a1b1550b71
5 changed files with 97 additions and 22 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ namespace Swarmops.Database
/// </summary>
public static int DbVersionExpected
{
get { return 24; }
get { return 25; }
}
+8 -6
View File
@@ -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());
}
+32
View File
@@ -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
+36 -2
View File
@@ -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));
}
}
}
+20 -13
View File
@@ -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