diff --git a/loopback/locale/es.json b/loopback/locale/es.json index a54d2169f..2611ee0dd 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -214,5 +214,7 @@ "You can't change the credit set to zero from a manager": "No puedes cambiar el cŕedito establecido a cero por un gerente", "The PDF document does not exists": "El documento PDF no existe. Prueba a regenerarlo desde la opción 'Regenerar PDF factura'", "The type of business must be filled in basic data": "El tipo de negocio debe estar rellenado en datos básicos", - "You can't create a claim from a ticket delivered more than seven days ago": "No puedes crear una reclamación de un ticket entregado hace más de siete días" + "You can't create a claim from a ticket delivered more than seven days ago": "No puedes crear una reclamación de un ticket entregado hace más de siete días", + "The worker has hours recorded that day": "El trabajador tiene horas fichadas ese día", + "The worker has a marked absence that day": "El trabajador tiene marcada una ausencia ese día" } \ No newline at end of file diff --git a/modules/worker/back/methods/worker-time-control/addTimeEntry.js b/modules/worker/back/methods/worker-time-control/addTimeEntry.js index 80786b723..62269afa3 100644 --- a/modules/worker/back/methods/worker-time-control/addTimeEntry.js +++ b/modules/worker/back/methods/worker-time-control/addTimeEntry.js @@ -47,11 +47,24 @@ module.exports = Self => { throw new UserError(`You don't have enough privileges`); const timed = new Date(args.timed); + timed.setHours(0, 0, 0, 0); + + query = `SELECT * FROM vn.workerLabour WHERE workerFk = ? AND (ended >= ? OR ended IS NULL);`; + const [workerLabour] = await Self.rawSql(query, [workerId, timed]); + const hasAbsence = await models.Calendar.findOne({ + where: { + businessFk: workerLabour.businessFk, + dated: timed + } + }); + + if (hasAbsence) + throw new UserError(`The worker has a marked absence that day`); return models.WorkerTimeControl.create({ userFk: workerId, direction: args.direction, - timed: timed, + timed: args.timed, manual: true }, myOptions); }; diff --git a/modules/worker/back/methods/worker-time-control/specs/timeEntry.spec.js b/modules/worker/back/methods/worker-time-control/specs/timeEntry.spec.js index 5829653cc..0c2914934 100644 --- a/modules/worker/back/methods/worker-time-control/specs/timeEntry.spec.js +++ b/modules/worker/back/methods/worker-time-control/specs/timeEntry.spec.js @@ -77,6 +77,23 @@ describe('workerTimeControl add/delete timeEntry()', () => { } }); + it('should fail to add a time entry if the target user has absent that day', async() => { + activeCtx.accessToken.userId = salesBossId; + const workerId = salesPersonId; + let error; + + let calendar = await app.models.Calendar.findById(3); + + try { + ctx.args = {timed: new Date(calendar.dated), direction: 'in'}; + await models.WorkerTimeControl.addTimeEntry(ctx, workerId); + } catch (e) { + error = e; + } + + expect(error.message).toBe(`The worker has a marked absence that day`); + }); + it('should try but fail to delete his own time entry', async() => { activeCtx.accessToken.userId = salesBossId; const workerId = salesBossId; diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js index 89830197d..a3b26a730 100644 --- a/modules/worker/back/methods/worker/createAbsence.js +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -65,6 +65,15 @@ module.exports = Self => { if (args.dated < labour.started || (labour.ended != null && args.dated > labour.ended)) throw new UserError(`The contract was not active during the selected date`); + query = `SELECT * + FROM vn.workerTimeControl + WHERE userFk = ? AND timed BETWEEN DATE(?) AND CONCAT(DATE(?), ' 23:59:59') + LIMIT 1;`; + const [hasHoursRecorded] = await Self.rawSql(query, [args.id, args.dated, args.dated]); + + if (hasHoursRecorded) + throw new UserError(`The worker has hours recorded that day`); + const result = await Self.rawSql( `SELECT COUNT(*) halfHolidayCounter FROM vn.calendar c diff --git a/modules/worker/back/methods/worker/specs/createAbsence.spec.js b/modules/worker/back/methods/worker/specs/createAbsence.spec.js index 0d6ebfc80..e9f6b9d9c 100644 --- a/modules/worker/back/methods/worker/specs/createAbsence.spec.js +++ b/modules/worker/back/methods/worker/specs/createAbsence.spec.js @@ -102,4 +102,32 @@ describe('Worker createAbsence()', () => { expect(error.message).toEqual(`Cannot add more than one '1/2 day vacation'`); }); + + it(`should throw an error when adding a absence if the worker has hours recorded that day`, async() => { + const ctx = { + req: {accessToken: {userId: 19}}, + args: { + id: 1106, + businessFk: 1106, + absenceTypeId: 6, + dated: new Date() + } + }; + + const tx = await app.models.Calendar.beginTransaction({}); + + let error; + try { + const options = {transaction: tx}; + + await app.models.Worker.createAbsence(ctx, workerId, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + error = e; + } + + expect(error.message).toEqual(`The worker has hours recorded that day`); + }); }); diff --git a/modules/worker/back/models/worker-labour.json b/modules/worker/back/models/worker-labour.json index 8ad7bf41e..b80090e57 100644 --- a/modules/worker/back/models/worker-labour.json +++ b/modules/worker/back/models/worker-labour.json @@ -11,6 +11,9 @@ "id": true, "type": "Number" }, + "workerFk": { + "type": "Number" + }, "started": { "type": "date" },