Skip to content
Permalink
Browse files

Payroll: Ceil last payroll date calculation

  • Loading branch information...
facuspagnuolo committed Apr 7, 2019
1 parent 6b52556 commit 51093091c2d16f394a3df83125208af6cae9d7da
@@ -602,6 +602,13 @@ contract Payroll is EtherTokenConstant, IForwarder, IsContract, AragonApp {

uint256 timeDiff = _payedAmount.div(employee.denominationTokenSalary);

// We check if the division was perfect, and if not, taking its ceiling to avoid giving away tiny amounts of salary.
// As an employee, you may lose up to a second's worth of payroll if you use the "request partial amount" feature.

if (timeDiff.mul(employee.denominationTokenSalary) != _payedAmount) {
timeDiff = timeDiff.add(1);
}

// This function is only called from _payday, where we make sure that _payedAmount is lower than or equal to the
// total owed amount, that is obtained from _getCurrentOwedSalary, which does exactly the opposite calculation:
// multiplying the employee's salary by an uint64 number of seconds. Therefore, timeDiff will always fit in 64.
@@ -90,7 +90,7 @@ contract('Payroll payday', ([owner, employee, anotherEmployee, anyone]) => {
const anotherTokenEvent = events.find(e => e.args.token === anotherToken.address).args
assert.equal(anotherTokenEvent.employee, employee, 'employee address does not match')
assert.equal(anotherTokenEvent.token, anotherToken.address, 'token address does not match')
assert.equal(anotherTokenEvent.amount.div(anotherTokenRate).trunc().toString(), Math.round(requestedAnotherTokenAmount), 'payment amount does not match')
assert.equal(anotherTokenEvent.amount.div(anotherTokenRate).trunc().toString(), requestedAnotherTokenAmount, 'payment amount does not match')
assert.equal(anotherTokenEvent.paymentReference, 'Payroll', 'payment reference does not match')
})

@@ -130,7 +130,7 @@ contract('Payroll payday', ([owner, employee, anotherEmployee, anyone]) => {
if (expectedRequestedAmount >= previousAccruedSalary) {
expectedAccruedSalary = 0
const remainder = expectedRequestedAmount - previousAccruedSalary
expectedLastPayrollDate = previousPayrollDate.plus(Math.floor(remainder / salary))
expectedLastPayrollDate = previousPayrollDate.plus(Math.ceil(remainder / salary))
} else {
expectedAccruedSalary = previousAccruedSalary.minus(expectedRequestedAmount).toString()
expectedLastPayrollDate = previousPayrollDate
@@ -282,7 +282,7 @@ contract('Payroll payday', ([owner, employee, anotherEmployee, anyone]) => {
})

context('when the requested amount is lower than the total owed salary', () => {
const requestedAmount = currentOwedSalary - 10
const requestedAmount = currentOwedSalary / 2

itHandlesPayrollProperlyNeverthelessExtrasOwedAmounts(requestedAmount, currentOwedSalary)
})
@@ -633,7 +633,7 @@ contract('Payroll payday', ([owner, employee, anotherEmployee, anyone]) => {
const anotherTokenEvent = events.find(e => e.args.token === anotherToken.address).args
assert.equal(anotherTokenEvent.employee, employee, 'employee address does not match')
assert.equal(anotherTokenEvent.token, anotherToken.address, 'token address does not match')
assert.equal(anotherTokenEvent.amount.div(anotherTokenRate).trunc().toString(), Math.round(requestedAnotherTokenAmount), 'payment amount does not match')
assert.equal(anotherTokenEvent.amount.div(anotherTokenRate).trunc().toString(), requestedAnotherTokenAmount, 'payment amount does not match')
assert.equal(anotherTokenEvent.paymentReference, 'Payroll', 'payment reference does not match')
})

@@ -663,8 +663,9 @@ contract('Payroll payday', ([owner, employee, anotherEmployee, anyone]) => {

const assertEmployeeIsUpdated = requestedAmount => {
it('updates the last payroll date', async () => {
const timeDiff = 1 // should be bn(requestedAmount).div(salary).ceil() but BN cannot represent such a small number, hardcoding it to 1
const previousPayrollDate = (await payroll.getEmployee(employeeId))[5]
const expectedLastPayrollDate = previousPayrollDate.plus(Math.floor(bn(requestedAmount).div(salary)))
const expectedLastPayrollDate = previousPayrollDate.plus(timeDiff)

await payroll.payday(PAYROLL_PAYMENT_TYPE, requestedAmount, { from })

0 comments on commit 5109309

Please sign in to comment.
You can’t perform that action at this time.