Merge branch 'dev' into 2931-entry_summary_pagination
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
commit
187aa8d318
|
@ -888,7 +888,7 @@ export default {
|
||||||
},
|
},
|
||||||
workerCalendar: {
|
workerCalendar: {
|
||||||
year: 'vn-worker-calendar vn-autocomplete[ng-model="$ctrl.year"]',
|
year: 'vn-worker-calendar vn-autocomplete[ng-model="$ctrl.year"]',
|
||||||
totalHolidaysUsed: 'vn-worker-calendar div.totalBox > div',
|
totalHolidaysUsed: 'vn-worker-calendar div.totalBox:first-child > div',
|
||||||
penultimateMondayOfJanuary: 'vn-worker-calendar vn-calendar:nth-child(2) section:nth-child(22) > div',
|
penultimateMondayOfJanuary: 'vn-worker-calendar vn-calendar:nth-child(2) section:nth-child(22) > div',
|
||||||
lastMondayOfMarch: 'vn-worker-calendar vn-calendar:nth-child(4) section:nth-child(29) > div',
|
lastMondayOfMarch: 'vn-worker-calendar vn-calendar:nth-child(4) section:nth-child(29) > div',
|
||||||
fistMondayOfMay: 'vn-worker-calendar vn-calendar:nth-child(6) section:nth-child(8) > div',
|
fistMondayOfMay: 'vn-worker-calendar vn-calendar:nth-child(6) section:nth-child(8) > div',
|
||||||
|
@ -896,11 +896,11 @@ export default {
|
||||||
secondTuesdayOfMay: 'vn-worker-calendar vn-calendar:nth-child(6) section:nth-child(16) > div',
|
secondTuesdayOfMay: 'vn-worker-calendar vn-calendar:nth-child(6) section:nth-child(16) > div',
|
||||||
secondWednesdayOfMay: 'vn-worker-calendar vn-calendar:nth-child(6) section:nth-child(17) > div',
|
secondWednesdayOfMay: 'vn-worker-calendar vn-calendar:nth-child(6) section:nth-child(17) > div',
|
||||||
secondThursdayOfMay: 'vn-worker-calendar vn-calendar:nth-child(6) section:nth-child(18) > div',
|
secondThursdayOfMay: 'vn-worker-calendar vn-calendar:nth-child(6) section:nth-child(18) > div',
|
||||||
holidays: 'vn-worker-calendar > vn-side-menu div:nth-child(3) > vn-chip:nth-child(1)',
|
holidays: 'vn-worker-calendar > vn-side-menu [name="absenceTypes"] > vn-chip:nth-child(1)',
|
||||||
absence: 'vn-worker-calendar > vn-side-menu div:nth-child(3) > vn-chip:nth-child(2)',
|
absence: 'vn-worker-calendar > vn-side-menu [name="absenceTypes"] > vn-chip:nth-child(2)',
|
||||||
halfHoliday: 'vn-worker-calendar > vn-side-menu div:nth-child(3) > vn-chip:nth-child(3)',
|
halfHoliday: 'vn-worker-calendar > vn-side-menu [name="absenceTypes"] > vn-chip:nth-child(3)',
|
||||||
furlough: 'vn-worker-calendar > vn-side-menu div:nth-child(3) > vn-chip:nth-child(4)',
|
furlough: 'vn-worker-calendar > vn-side-menu [name="absenceTypes"] > vn-chip:nth-child(4)',
|
||||||
halfFurlough: 'vn-worker-calendar > vn-side-menu div:nth-child(3) > vn-chip:nth-child(5)',
|
halfFurlough: 'vn-worker-calendar > vn-side-menu [name="absenceTypes"] > vn-chip:nth-child(5)',
|
||||||
},
|
},
|
||||||
invoiceOutIndex: {
|
invoiceOutIndex: {
|
||||||
topbarSearch: 'vn-searchbar',
|
topbarSearch: 'vn-searchbar',
|
||||||
|
|
|
@ -11,7 +11,7 @@ import './style.scss';
|
||||||
* @property {String} valueField The data field name that should be used as value
|
* @property {String} valueField The data field name that should be used as value
|
||||||
* @property {Array} data Static data for the autocomplete
|
* @property {Array} data Static data for the autocomplete
|
||||||
* @property {Object} intialData An initial data to avoid the server request used to get the selection
|
* @property {Object} intialData An initial data to avoid the server request used to get the selection
|
||||||
* @property {Boolean} multiple Wether to allow multiple selection
|
* @property {Boolean} multiple Whether to allow multiple selection
|
||||||
* @property {Object} selection Current object selected
|
* @property {Object} selection Current object selected
|
||||||
*
|
*
|
||||||
* @event change Thrown when value is changed
|
* @event change Thrown when value is changed
|
||||||
|
|
|
@ -17,7 +17,7 @@ export default class Popup extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Boolean} Wether to show or hide the popup.
|
* @type {Boolean} Whether to show or hide the popup.
|
||||||
*/
|
*/
|
||||||
get shown() {
|
get shown() {
|
||||||
return this._shown;
|
return this._shown;
|
||||||
|
|
|
@ -102,7 +102,7 @@ vn-table {
|
||||||
& > vn-one {
|
& > vn-one {
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
font-size: 0.75rem;
|
font-size: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
& > vn-one:nth-child(2) h3 {
|
& > vn-one:nth-child(2) h3 {
|
||||||
|
|
|
@ -180,5 +180,6 @@
|
||||||
"This genus already exist": "Este genus ya existe",
|
"This genus already exist": "Este genus ya existe",
|
||||||
"This specie already exist": "Esta especie ya existe",
|
"This specie already exist": "Esta especie ya existe",
|
||||||
"Client assignment has changed": "He cambiado el comercial ~*\"<{{previousWorkerName}}>\"*~ por *\"<{{currentWorkerName}}>\"* del cliente [{{clientName}} ({{clientId}})]({{{url}}})",
|
"Client assignment has changed": "He cambiado el comercial ~*\"<{{previousWorkerName}}>\"*~ por *\"<{{currentWorkerName}}>\"* del cliente [{{clientName}} ({{clientId}})]({{{url}}})",
|
||||||
"None": "Ninguno"
|
"None": "Ninguno",
|
||||||
|
"The contract was not active during the selected date": "El contrato no estaba activo durante la fecha seleccionada"
|
||||||
}
|
}
|
|
@ -149,11 +149,13 @@
|
||||||
ng-model="ticket.checked">
|
ng-model="ticket.checked">
|
||||||
</vn-check>
|
</vn-check>
|
||||||
</vn-td>
|
</vn-td>
|
||||||
<vn-td number>{{::ticket.id}}</vn-td>
|
|
||||||
<vn-td number>
|
<vn-td number>
|
||||||
<span
|
<span class="link" ng-click="ticketDescriptor.show($event, ticket.id)">
|
||||||
ng-click="::$ctrl.showClientDescriptor($event, ticket.clientFk)"
|
{{::ticket.id}}
|
||||||
class="link">
|
</span>
|
||||||
|
</vn-td>
|
||||||
|
<vn-td number>
|
||||||
|
<span class="link" ng-click="clientDescriptor.show($event, ticket.clientFk)">
|
||||||
{{::ticket.nickname}}
|
{{::ticket.nickname}}
|
||||||
</span>
|
</span>
|
||||||
</vn-td>
|
</vn-td>
|
||||||
|
@ -180,3 +182,9 @@
|
||||||
vn-bind="+"
|
vn-bind="+"
|
||||||
fixed-bottom-right>
|
fixed-bottom-right>
|
||||||
</vn-float-button>
|
</vn-float-button>
|
||||||
|
<vn-ticket-descriptor-popover
|
||||||
|
vn-id="ticket-descriptor">
|
||||||
|
</vn-ticket-descriptor-popover>
|
||||||
|
<vn-client-descriptor-popover
|
||||||
|
vn-id="client-descriptor">
|
||||||
|
</vn-client-descriptor-popover>
|
||||||
|
|
|
@ -63,7 +63,7 @@ module.exports = Self => {
|
||||||
}, {
|
}, {
|
||||||
relation: 'address',
|
relation: 'address',
|
||||||
scope: {
|
scope: {
|
||||||
fields: ['street', 'city', 'provinceFk', 'phone', 'mobile'],
|
fields: ['street', 'city', 'provinceFk', 'phone', 'mobile', 'postalCode'],
|
||||||
include: {
|
include: {
|
||||||
relation: 'province',
|
relation: 'province',
|
||||||
scope: {
|
scope: {
|
||||||
|
|
|
@ -3,3 +3,4 @@ No delivery zone available for this shipping date: No hay una zona de reparto di
|
||||||
No delivery zone available for this parameters: No hay una zona de reparto disponible con estos parámetros
|
No delivery zone available for this parameters: No hay una zona de reparto disponible con estos parámetros
|
||||||
Deleted: Eliminado
|
Deleted: Eliminado
|
||||||
Zone: Zona
|
Zone: Zona
|
||||||
|
Edit address: Editar dirección
|
|
@ -16,10 +16,11 @@ class Controller extends Summary {
|
||||||
get formattedAddress() {
|
get formattedAddress() {
|
||||||
if (!this.summary) return '';
|
if (!this.summary) return '';
|
||||||
|
|
||||||
let address = this.summary.address;
|
const address = this.summary.address;
|
||||||
let province = address.province ? `(${address.province.name})` : '';
|
const postcode = address.postalCode;
|
||||||
|
const province = address.province ? `(${address.province.name})` : '';
|
||||||
|
|
||||||
return `${address.street} - ${address.city} ${province}`;
|
return `${address.street} - ${postcode} - ${address.city} ${province}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
loadData() {
|
loadData() {
|
||||||
|
|
|
@ -27,18 +27,19 @@ describe('Ticket', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('formattedAddress()', () => {
|
describe('formattedAddress()', () => {
|
||||||
it('should return a full fromatted address with city and province', () => {
|
it('should return the full fromatted address with city and province', () => {
|
||||||
controller.summary = {
|
controller.summary = {
|
||||||
address: {
|
address: {
|
||||||
province: {
|
province: {
|
||||||
name: 'Gotham'
|
name: 'Gotham'
|
||||||
},
|
},
|
||||||
street: '1007 Mountain Drive',
|
street: '1007 Mountain Drive',
|
||||||
|
postalCode: 46060,
|
||||||
city: 'Gotham'
|
city: 'Gotham'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(controller.formattedAddress).toEqual('1007 Mountain Drive - Gotham (Gotham)');
|
expect(controller.formattedAddress).toEqual('1007 Mountain Drive - 46060 - Gotham (Gotham)');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,32 +2,24 @@ const UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethodCtx('absences', {
|
Self.remoteMethodCtx('absences', {
|
||||||
description: 'Returns an array of absences from an specified worker',
|
description: 'Returns an array of absences from an specified contract',
|
||||||
accepts: [{
|
accepts: [{
|
||||||
arg: 'workerFk',
|
arg: 'businessFk',
|
||||||
type: 'Number',
|
type: 'number',
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
arg: 'started',
|
arg: 'year',
|
||||||
type: 'Date',
|
type: 'date',
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
arg: 'ended',
|
|
||||||
type: 'Date',
|
|
||||||
required: true,
|
required: true,
|
||||||
}],
|
}],
|
||||||
returns: [{
|
returns: [{
|
||||||
arg: 'calendar'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
arg: 'absences',
|
arg: 'absences',
|
||||||
type: 'Number'
|
type: 'number'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
arg: 'holidays',
|
arg: 'holidays',
|
||||||
type: 'Number'
|
type: 'number'
|
||||||
}],
|
}],
|
||||||
http: {
|
http: {
|
||||||
path: `/absences`,
|
path: `/absences`,
|
||||||
|
@ -35,25 +27,42 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.absences = async(ctx, workerFk, yearStarted, yearEnded) => {
|
Self.absences = async(ctx, businessFk, year, options) => {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const isSubordinate = await models.Worker.isSubordinate(ctx, workerFk);
|
|
||||||
|
|
||||||
if (!isSubordinate)
|
const started = new Date();
|
||||||
throw new UserError(`You don't have enough privileges`);
|
started.setFullYear(year);
|
||||||
|
started.setMonth(0);
|
||||||
|
started.setDate(1);
|
||||||
|
|
||||||
const calendar = {totalHolidays: 0, holidaysEnjoyed: 0};
|
const ended = new Date();
|
||||||
const holidays = [];
|
ended.setFullYear(year);
|
||||||
|
ended.setMonth(12);
|
||||||
|
ended.setDate(0);
|
||||||
|
|
||||||
// Get active contracts on current year
|
let myOptions = {};
|
||||||
const year = yearStarted.getFullYear();
|
|
||||||
const contracts = await models.WorkerLabour.find({
|
if (typeof options == 'object')
|
||||||
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
|
const contract = await models.WorkerLabour.findOne({
|
||||||
include: [{
|
include: [{
|
||||||
relation: 'holidays',
|
relation: 'holidays',
|
||||||
scope: {
|
scope: {
|
||||||
where: {year}
|
where: {year}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
relation: 'absences',
|
||||||
|
scope: {
|
||||||
|
include: {
|
||||||
|
relation: 'absenceType',
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
dated: {between: [started, ended]}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
relation: 'workCenter',
|
relation: 'workCenter',
|
||||||
scope: {
|
scope: {
|
||||||
|
@ -67,104 +76,39 @@ module.exports = Self => {
|
||||||
relation: 'type'
|
relation: 'type'
|
||||||
}],
|
}],
|
||||||
where: {
|
where: {
|
||||||
dated: {between: [yearStarted, yearEnded]}
|
dated: {between: [started, ended]}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}],
|
}],
|
||||||
where: {
|
where: {businessFk}
|
||||||
and: [
|
}, myOptions);
|
||||||
{workerFk: workerFk},
|
|
||||||
{or: [{
|
|
||||||
ended: {gte: [yearStarted]}
|
|
||||||
}, {ended: null}]}
|
|
||||||
],
|
|
||||||
|
|
||||||
}
|
if (!contract) return;
|
||||||
});
|
|
||||||
|
|
||||||
// Contracts ids
|
const isSubordinate = await models.Worker.isSubordinate(ctx, contract.workerFk, myOptions);
|
||||||
const contractsId = contracts.map(contract => {
|
if (!isSubordinate)
|
||||||
return contract.businessFk;
|
throw new UserError(`You don't have enough privileges`);
|
||||||
});
|
|
||||||
|
|
||||||
// Get absences of year
|
|
||||||
let absences = await Self.find({
|
|
||||||
include: {
|
|
||||||
relation: 'absenceType'
|
|
||||||
},
|
|
||||||
where: {
|
|
||||||
businessFk: {inq: contractsId},
|
|
||||||
dated: {between: [yearStarted, yearEnded]}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let entitlementRate = 0;
|
|
||||||
absences.forEach(absence => {
|
|
||||||
const absenceType = absence.absenceType();
|
|
||||||
const isHoliday = absenceType.code === 'holiday';
|
|
||||||
const isHalfHoliday = absenceType.code === 'halfHoliday';
|
|
||||||
|
|
||||||
if (isHoliday)
|
|
||||||
calendar.holidaysEnjoyed += 1;
|
|
||||||
if (isHalfHoliday)
|
|
||||||
calendar.holidaysEnjoyed += 0.5;
|
|
||||||
|
|
||||||
entitlementRate += absenceType.holidayEntitlementRate;
|
|
||||||
|
|
||||||
|
const absences = [];
|
||||||
|
for (let absence of contract.absences()) {
|
||||||
absence.dated = new Date(absence.dated);
|
absence.dated = new Date(absence.dated);
|
||||||
absence.dated.setHours(0, 0, 0, 0);
|
absence.dated.setHours(0, 0, 0, 0);
|
||||||
});
|
|
||||||
|
|
||||||
// Get number of worked days
|
absences.push(absence);
|
||||||
let workedDays = 0;
|
}
|
||||||
contracts.forEach(contract => {
|
|
||||||
const started = contract.started;
|
|
||||||
const ended = contract.ended;
|
|
||||||
const startedTime = started.getTime();
|
|
||||||
const endedTime = ended && ended.getTime() || yearEnded;
|
|
||||||
const dayTimestamp = 1000 * 60 * 60 * 24;
|
|
||||||
|
|
||||||
workedDays += Math.floor((endedTime - startedTime) / dayTimestamp);
|
|
||||||
|
|
||||||
if (workedDays > daysInYear())
|
|
||||||
workedDays = daysInYear();
|
|
||||||
|
|
||||||
// Workcenter holidays
|
// Workcenter holidays
|
||||||
let holidayList = contract.workCenter().holidays();
|
const holidays = [];
|
||||||
|
const holidayList = contract.workCenter().holidays();
|
||||||
for (let day of holidayList) {
|
for (let day of holidayList) {
|
||||||
day.dated = new Date(day.dated);
|
day.dated = new Date(day.dated);
|
||||||
day.dated.setHours(0, 0, 0, 0);
|
day.dated.setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
holidays.push(day);
|
holidays.push(day);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
const currentContract = contracts.find(contract => {
|
|
||||||
return contract.started <= new Date()
|
|
||||||
&& (contract.ended >= new Date() || contract.ended == null);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (currentContract) {
|
return [absences, holidays];
|
||||||
const maxHolidays = currentContract.holidays() && currentContract.holidays().days;
|
|
||||||
calendar.totalHolidays = maxHolidays;
|
|
||||||
|
|
||||||
workedDays -= entitlementRate;
|
|
||||||
|
|
||||||
if (workedDays < daysInYear())
|
|
||||||
calendar.totalHolidays = Math.round(2 * maxHolidays * (workedDays) / daysInYear()) / 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
function daysInYear() {
|
|
||||||
const year = yearStarted.getFullYear();
|
|
||||||
|
|
||||||
return isLeapYear(year) ? 366 : 365;
|
|
||||||
}
|
|
||||||
|
|
||||||
return [calendar, absences, holidays];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function isLeapYear(year) {
|
|
||||||
return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,78 +2,56 @@ const app = require('vn-loopback/server/server');
|
||||||
|
|
||||||
describe('Worker absences()', () => {
|
describe('Worker absences()', () => {
|
||||||
it('should get the absence calendar for a full year contract', async() => {
|
it('should get the absence calendar for a full year contract', async() => {
|
||||||
let ctx = {req: {accessToken: {userId: 106}}};
|
const ctx = {req: {accessToken: {userId: 106}}};
|
||||||
let workerFk = 106;
|
const businessId = 106;
|
||||||
|
|
||||||
const started = new Date();
|
const now = new Date();
|
||||||
started.setHours(0, 0, 0, 0);
|
const year = now.getFullYear();
|
||||||
started.setMonth(0);
|
|
||||||
started.setDate(1);
|
|
||||||
|
|
||||||
const monthIndex = 11;
|
const [absences] = await app.models.Calendar.absences(ctx, businessId, year);
|
||||||
const ended = new Date();
|
|
||||||
ended.setHours(0, 0, 0, 0);
|
|
||||||
ended.setMonth(monthIndex + 1);
|
|
||||||
ended.setDate(0);
|
|
||||||
|
|
||||||
let result = await app.models.Calendar.absences(ctx, workerFk, started, ended);
|
const firstType = absences[0].absenceType().name;
|
||||||
let calendar = result[0];
|
const sixthType = absences[5].absenceType().name;
|
||||||
let absences = result[1];
|
|
||||||
|
|
||||||
expect(calendar.totalHolidays).toEqual(27.5);
|
|
||||||
expect(calendar.holidaysEnjoyed).toEqual(5);
|
|
||||||
|
|
||||||
let firstType = absences[0].absenceType().name;
|
|
||||||
let sixthType = absences[5].absenceType().name;
|
|
||||||
|
|
||||||
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
|
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
|
||||||
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
|
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should get the absence calendar for a permanent contract', async() => {
|
it('should get the absence calendar for a permanent contract', async() => {
|
||||||
let workerFk = 106;
|
const businessId = 106;
|
||||||
let worker = await app.models.WorkerLabour.findById(workerFk);
|
const ctx = {req: {accessToken: {userId: 9}}};
|
||||||
let endedDate = worker.ended;
|
|
||||||
|
const now = new Date();
|
||||||
|
const year = now.getFullYear();
|
||||||
|
|
||||||
|
const tx = await app.models.Calendar.beginTransaction({});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const options = {transaction: tx};
|
||||||
|
|
||||||
|
const worker = await app.models.WorkerLabour.findById(businessId, null, options);
|
||||||
|
|
||||||
await app.models.WorkerLabour.rawSql(
|
await app.models.WorkerLabour.rawSql(
|
||||||
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`,
|
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`,
|
||||||
[null, worker.businessFk]
|
[null, worker.businessFk], options);
|
||||||
);
|
|
||||||
|
|
||||||
let ctx = {req: {accessToken: {userId: 9}}};
|
const [absences] = await app.models.Calendar.absences(ctx, businessId, year, options);
|
||||||
|
|
||||||
const started = new Date();
|
|
||||||
started.setHours(0, 0, 0, 0);
|
|
||||||
started.setMonth(0);
|
|
||||||
started.setDate(1);
|
|
||||||
|
|
||||||
const monthIndex = 11;
|
|
||||||
const ended = new Date();
|
|
||||||
ended.setHours(0, 0, 0, 0);
|
|
||||||
ended.setMonth(monthIndex + 1);
|
|
||||||
ended.setDate(0);
|
|
||||||
|
|
||||||
let result = await app.models.Calendar.absences(ctx, workerFk, started, ended);
|
|
||||||
let calendar = result[0];
|
|
||||||
let absences = result[1];
|
|
||||||
|
|
||||||
expect(calendar.totalHolidays).toEqual(27.5);
|
|
||||||
expect(calendar.holidaysEnjoyed).toEqual(5);
|
|
||||||
|
|
||||||
let firstType = absences[0].absenceType().name;
|
let firstType = absences[0].absenceType().name;
|
||||||
let sixthType = absences[5].absenceType().name;
|
let sixthType = absences[5].absenceType().name;
|
||||||
|
|
||||||
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
|
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
|
||||||
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
|
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
|
||||||
|
await tx.rollback();
|
||||||
// restores the contract end date
|
} catch (e) {
|
||||||
await app.models.WorkerLabour.rawSql(
|
await tx.rollback();
|
||||||
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`,
|
throw e;
|
||||||
[endedDate, worker.businessFk]
|
}
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should give the same holidays as worked days since the holidays amount matches the amount of days in a year', async() => {
|
it('should give the same holidays as worked days since the holidays amount matches the amount of days in a year', async() => {
|
||||||
|
const businessId = 106;
|
||||||
|
const userId = 106;
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
|
|
||||||
// getting how many days in a year
|
// getting how many days in a year
|
||||||
|
@ -94,21 +72,22 @@ describe('Worker absences()', () => {
|
||||||
|
|
||||||
const daysInYear = Math.round((endedTime - startedTime) / dayTimestamp);
|
const daysInYear = Math.round((endedTime - startedTime) / dayTimestamp);
|
||||||
|
|
||||||
|
const tx = await app.models.Calendar.beginTransaction({});
|
||||||
|
try {
|
||||||
|
const options = {transaction: tx};
|
||||||
|
|
||||||
// sets the holidays per year to the amount of days in the current year
|
// sets the holidays per year to the amount of days in the current year
|
||||||
let holidaysConfig = await app.models.WorkCenterHoliday.findOne({
|
const holidaysConfig = await app.models.WorkCenterHoliday.findOne({
|
||||||
where: {
|
where: {
|
||||||
workCenterFk: 1,
|
workCenterFk: 1,
|
||||||
year: today.getFullYear()
|
year: today.getFullYear()
|
||||||
}});
|
}
|
||||||
|
}, options);
|
||||||
|
|
||||||
let originalHolidaysValue = holidaysConfig.days;
|
await holidaysConfig.updateAttribute('days', daysInYear, options);
|
||||||
|
|
||||||
await holidaysConfig.updateAttribute('days', daysInYear);
|
|
||||||
|
|
||||||
// normal test begins
|
// normal test begins
|
||||||
const userId = 106;
|
const contract = await app.models.WorkerLabour.findById(businessId, null, options);
|
||||||
const contract = await app.models.WorkerLabour.findById(userId);
|
|
||||||
const contractStartDate = contract.started;
|
|
||||||
|
|
||||||
const startingContract = new Date();
|
const startingContract = new Date();
|
||||||
startingContract.setHours(0, 0, 0, 0);
|
startingContract.setHours(0, 0, 0, 0);
|
||||||
|
@ -117,47 +96,23 @@ describe('Worker absences()', () => {
|
||||||
|
|
||||||
await app.models.WorkerLabour.rawSql(
|
await app.models.WorkerLabour.rawSql(
|
||||||
`UPDATE postgresql.business SET date_start = ?, date_end = ? WHERE business_id = ?`,
|
`UPDATE postgresql.business SET date_start = ?, date_end = ? WHERE business_id = ?`,
|
||||||
[startingContract, yearEnd, contract.businessFk]
|
[startingContract, yearEnd, contract.businessFk], options
|
||||||
);
|
);
|
||||||
|
|
||||||
let ctx = {req: {accessToken: {userId: userId}}};
|
const ctx = {req: {accessToken: {userId: userId}}};
|
||||||
|
|
||||||
let result = await app.models.Calendar.absences(ctx, userId, yearStart, yearEnd);
|
const [absences] = await app.models.Calendar.absences(ctx, businessId, currentYear);
|
||||||
let calendar = result[0];
|
|
||||||
let absences = result[1];
|
|
||||||
|
|
||||||
let remainingDays = 0;
|
const firstType = absences[0].absenceType().name;
|
||||||
for (let i = today.getMonth(); i < 12; i++) {
|
const sixthType = absences[5].absenceType().name;
|
||||||
today.setDate(1);
|
|
||||||
today.setMonth(i + 1);
|
|
||||||
today.setDate(0);
|
|
||||||
|
|
||||||
remainingDays += today.getDate();
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(calendar.totalHolidays).toEqual(remainingDays);
|
|
||||||
expect(calendar.holidaysEnjoyed).toEqual(5);
|
|
||||||
|
|
||||||
let firstType = absences[0].absenceType().name;
|
|
||||||
let sixthType = absences[5].absenceType().name;
|
|
||||||
|
|
||||||
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
|
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
|
||||||
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
|
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
|
||||||
|
|
||||||
// resets the holidays per year with originalHolidaysValue and the contract starting date
|
await tx.rollback();
|
||||||
await app.models.WorkCenterHoliday.updateAll(
|
} catch (e) {
|
||||||
{
|
await tx.rollback();
|
||||||
workCenterFk: 1,
|
throw e;
|
||||||
year: today.getFullYear()
|
|
||||||
},
|
|
||||||
{
|
|
||||||
days: originalHolidaysValue
|
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
await app.models.WorkerLabour.rawSql(
|
|
||||||
`UPDATE postgresql.business SET date_start = ? WHERE business_id = ?`,
|
|
||||||
[contractStartDate, contract.businessFk]
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
|
module.exports = Self => {
|
||||||
|
Self.remoteMethodCtx('activeContract', {
|
||||||
|
description: 'Returns an array of contracts from an specified worker',
|
||||||
|
accepts: [{
|
||||||
|
arg: 'id',
|
||||||
|
type: 'number',
|
||||||
|
required: true,
|
||||||
|
description: 'The worker id',
|
||||||
|
http: {source: 'path'}
|
||||||
|
}],
|
||||||
|
returns: {
|
||||||
|
type: ['object'],
|
||||||
|
root: true
|
||||||
|
},
|
||||||
|
http: {
|
||||||
|
path: `/:id/activeContract`,
|
||||||
|
verb: 'GET'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Self.activeContract = async(ctx, id) => {
|
||||||
|
const models = Self.app.models;
|
||||||
|
const isSubordinate = await models.Worker.isSubordinate(ctx, id);
|
||||||
|
|
||||||
|
if (!isSubordinate)
|
||||||
|
throw new UserError(`You don't have enough privileges`);
|
||||||
|
|
||||||
|
const now = new Date();
|
||||||
|
|
||||||
|
return models.WorkerLabour.findOne({
|
||||||
|
where: {
|
||||||
|
and: [
|
||||||
|
{workerFk: id},
|
||||||
|
{started: {lte: now}},
|
||||||
|
{
|
||||||
|
or: [
|
||||||
|
{ended: {gte: now}},
|
||||||
|
{ended: null}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
|
@ -0,0 +1,43 @@
|
||||||
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
|
module.exports = Self => {
|
||||||
|
Self.remoteMethodCtx('contracts', {
|
||||||
|
description: 'Returns an array of contracts from an specified worker',
|
||||||
|
accepts: [{
|
||||||
|
arg: 'id',
|
||||||
|
type: 'number',
|
||||||
|
required: true,
|
||||||
|
description: 'The worker id',
|
||||||
|
http: {source: 'path'}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
arg: 'filter',
|
||||||
|
type: 'Object',
|
||||||
|
description: 'Filter defining where and paginated data',
|
||||||
|
required: true
|
||||||
|
}],
|
||||||
|
returns: {
|
||||||
|
type: ['object'],
|
||||||
|
root: true
|
||||||
|
},
|
||||||
|
http: {
|
||||||
|
path: `/:id/contracts`,
|
||||||
|
verb: 'GET'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Self.contracts = async(ctx, id, filter) => {
|
||||||
|
const models = Self.app.models;
|
||||||
|
const isSubordinate = await models.Worker.isSubordinate(ctx, id);
|
||||||
|
|
||||||
|
if (!isSubordinate)
|
||||||
|
throw new UserError(`You don't have enough privileges`);
|
||||||
|
|
||||||
|
if (!filter.where) filter.where = {};
|
||||||
|
|
||||||
|
const where = filter.where;
|
||||||
|
where['workerFk'] = id;
|
||||||
|
|
||||||
|
return models.WorkerLabour.find(filter);
|
||||||
|
};
|
||||||
|
};
|
|
@ -5,19 +5,24 @@ module.exports = Self => {
|
||||||
description: 'Creates a new worker absence',
|
description: 'Creates a new worker absence',
|
||||||
accepts: [{
|
accepts: [{
|
||||||
arg: 'id',
|
arg: 'id',
|
||||||
type: 'Number',
|
type: 'number',
|
||||||
description: 'The worker id',
|
description: 'The worker id',
|
||||||
http: {source: 'path'}
|
http: {source: 'path'}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
arg: 'businessFk',
|
||||||
|
type: 'number',
|
||||||
|
required: true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
arg: 'absenceTypeId',
|
arg: 'absenceTypeId',
|
||||||
type: 'Number',
|
type: 'number',
|
||||||
required: true
|
required: true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
arg: 'dated',
|
arg: 'dated',
|
||||||
type: 'Date',
|
type: 'date',
|
||||||
required: false
|
required: true
|
||||||
}],
|
}],
|
||||||
returns: {
|
returns: {
|
||||||
type: 'Object',
|
type: 'Object',
|
||||||
|
@ -29,55 +34,70 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.createAbsence = async(ctx, id, absenceTypeId, dated) => {
|
Self.createAbsence = async(ctx, id, options) => {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const $t = ctx.req.__; // $translate
|
const $t = ctx.req.__; // $translate
|
||||||
|
const args = ctx.args;
|
||||||
const userId = ctx.req.accessToken.userId;
|
const userId = ctx.req.accessToken.userId;
|
||||||
const isSubordinate = await models.Worker.isSubordinate(ctx, id);
|
|
||||||
const isTeamBoss = await models.Account.hasRole(userId, 'teamBoss');
|
let tx;
|
||||||
|
let myOptions = {};
|
||||||
|
|
||||||
|
if (typeof options == 'object')
|
||||||
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
|
if (!myOptions.transaction) {
|
||||||
|
tx = await Self.beginTransaction({});
|
||||||
|
myOptions.transaction = tx;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const isSubordinate = await models.Worker.isSubordinate(ctx, id, myOptions);
|
||||||
|
const isTeamBoss = await models.Account.hasRole(userId, 'teamBoss', myOptions);
|
||||||
|
|
||||||
if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss))
|
if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss))
|
||||||
throw new UserError(`You don't have enough privileges`);
|
throw new UserError(`You don't have enough privileges`);
|
||||||
|
|
||||||
const labour = await models.WorkerLabour.findOne({
|
const labour = await models.WorkerLabour.findById(args.businessFk, {
|
||||||
include: {relation: 'department'},
|
include: {relation: 'department'}
|
||||||
where: {
|
}, myOptions);
|
||||||
and: [
|
|
||||||
{workerFk: id},
|
if (args.dated < labour.started || (labour.ended != null && args.dated > labour.ended))
|
||||||
{or: [{
|
throw new UserError(`The contract was not active during the selected date`);
|
||||||
ended: {gte: [dated]}
|
|
||||||
}, {ended: null}]}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const absence = await models.Calendar.create({
|
const absence = await models.Calendar.create({
|
||||||
businessFk: labour.businessFk,
|
businessFk: labour.businessFk,
|
||||||
dayOffTypeFk: absenceTypeId,
|
dayOffTypeFk: args.absenceTypeId,
|
||||||
dated: dated
|
dated: args.dated
|
||||||
});
|
}, myOptions);
|
||||||
|
|
||||||
const department = labour.department();
|
const department = labour.department();
|
||||||
if (department && department.notificationEmail) {
|
if (department && department.notificationEmail) {
|
||||||
const absenceType = await models.AbsenceType.findById(absenceTypeId);
|
const absenceType = await models.AbsenceType.findById(args.absenceTypeId, null, myOptions);
|
||||||
const account = await models.Account.findById(userId);
|
const account = await models.Account.findById(userId, null, myOptions);
|
||||||
const subordinated = await models.Account.findById(id);
|
const subordinated = await models.Account.findById(id, null, myOptions);
|
||||||
const origin = ctx.req.headers.origin;
|
const origin = ctx.req.headers.origin;
|
||||||
const body = $t('Created absence', {
|
const body = $t('Created absence', {
|
||||||
author: account.nickname,
|
author: account.nickname,
|
||||||
employee: subordinated.nickname,
|
employee: subordinated.nickname,
|
||||||
absenceType: absenceType.name,
|
absenceType: absenceType.name,
|
||||||
dated: formatDate(dated),
|
dated: formatDate(args.dated),
|
||||||
workerUrl: `${origin}/#!/worker/${id}/calendar`
|
workerUrl: `${origin}/#!/worker/${id}/calendar`
|
||||||
});
|
});
|
||||||
await models.Mail.create({
|
await models.Mail.create({
|
||||||
subject: $t('Absence change notification on the labour calendar'),
|
subject: $t('Absence change notification on the labour calendar'),
|
||||||
body: body,
|
body: body,
|
||||||
sender: department.notificationEmail
|
sender: department.notificationEmail
|
||||||
});
|
}, myOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (tx) await tx.commit();
|
||||||
|
|
||||||
return absence;
|
return absence;
|
||||||
|
} catch (e) {
|
||||||
|
if (tx) await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function formatDate(date) {
|
function formatDate(date) {
|
||||||
|
|
|
@ -5,13 +5,13 @@ module.exports = Self => {
|
||||||
description: 'Deletes a worker absence',
|
description: 'Deletes a worker absence',
|
||||||
accepts: [{
|
accepts: [{
|
||||||
arg: 'id',
|
arg: 'id',
|
||||||
type: 'Number',
|
type: 'number',
|
||||||
description: 'The worker id',
|
description: 'The worker id',
|
||||||
http: {source: 'path'}
|
http: {source: 'path'}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
arg: 'absenceId',
|
arg: 'absenceId',
|
||||||
type: 'Number',
|
type: 'number',
|
||||||
required: true
|
required: true
|
||||||
}],
|
}],
|
||||||
returns: 'Object',
|
returns: 'Object',
|
||||||
|
@ -21,31 +21,45 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.deleteAbsence = async(ctx, id, absenceId) => {
|
Self.deleteAbsence = async(ctx, id, options) => {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const $t = ctx.req.__; // $translate
|
const $t = ctx.req.__; // $translate
|
||||||
|
const args = ctx.args;
|
||||||
const userId = ctx.req.accessToken.userId;
|
const userId = ctx.req.accessToken.userId;
|
||||||
const isSubordinate = await models.Worker.isSubordinate(ctx, id);
|
|
||||||
const isTeamBoss = await models.Account.hasRole(userId, 'teamBoss');
|
let tx;
|
||||||
|
let myOptions = {};
|
||||||
|
|
||||||
|
if (typeof options == 'object')
|
||||||
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
|
if (!myOptions.transaction) {
|
||||||
|
tx = await Self.beginTransaction({});
|
||||||
|
myOptions.transaction = tx;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const isSubordinate = await models.Worker.isSubordinate(ctx, id, myOptions);
|
||||||
|
const isTeamBoss = await models.Account.hasRole(userId, 'teamBoss', myOptions);
|
||||||
|
|
||||||
if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss))
|
if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss))
|
||||||
throw new UserError(`You don't have enough privileges`);
|
throw new UserError(`You don't have enough privileges`);
|
||||||
|
|
||||||
const absence = await models.Calendar.findById(absenceId, {
|
const absence = await models.Calendar.findById(args.absenceId, {
|
||||||
include: {
|
include: {
|
||||||
relation: 'labour',
|
relation: 'labour',
|
||||||
scope: {
|
scope: {
|
||||||
include: {relation: 'department'}
|
include: {relation: 'department'}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}, myOptions);
|
||||||
const result = await absence.destroy();
|
const result = await absence.destroy(myOptions);
|
||||||
const labour = absence.labour();
|
const labour = absence.labour();
|
||||||
const department = labour && labour.department();
|
const department = labour && labour.department();
|
||||||
if (department && department.notificationEmail) {
|
if (department && department.notificationEmail) {
|
||||||
const absenceType = await models.AbsenceType.findById(absence.dayOffTypeFk);
|
const absenceType = await models.AbsenceType.findById(absence.dayOffTypeFk, null, myOptions);
|
||||||
const account = await models.Account.findById(userId);
|
const account = await models.Account.findById(userId, null, myOptions);
|
||||||
const subordinated = await models.Account.findById(labour.workerFk);
|
const subordinated = await models.Account.findById(labour.workerFk, null, myOptions);
|
||||||
const origin = ctx.req.headers.origin;
|
const origin = ctx.req.headers.origin;
|
||||||
const body = $t('Deleted absence', {
|
const body = $t('Deleted absence', {
|
||||||
author: account.nickname,
|
author: account.nickname,
|
||||||
|
@ -58,10 +72,16 @@ module.exports = Self => {
|
||||||
subject: $t('Absence change notification on the labour calendar'),
|
subject: $t('Absence change notification on the labour calendar'),
|
||||||
body: body,
|
body: body,
|
||||||
sender: department.notificationEmail
|
sender: department.notificationEmail
|
||||||
});
|
}, myOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (tx) await tx.commit();
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
} catch (e) {
|
||||||
|
if (tx) await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function formatDate(date) {
|
function formatDate(date) {
|
||||||
|
|
|
@ -0,0 +1,176 @@
|
||||||
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
|
module.exports = Self => {
|
||||||
|
Self.remoteMethodCtx('holidays', {
|
||||||
|
description: 'Returns the holidays available whitin a contract or a year',
|
||||||
|
accepts: [{
|
||||||
|
arg: 'id',
|
||||||
|
type: 'number',
|
||||||
|
required: true,
|
||||||
|
description: 'The worker id',
|
||||||
|
http: {source: 'path'}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
arg: 'year',
|
||||||
|
type: 'date',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
arg: 'businessFk',
|
||||||
|
type: 'number',
|
||||||
|
required: false
|
||||||
|
}],
|
||||||
|
returns: [{
|
||||||
|
type: 'object',
|
||||||
|
root: true
|
||||||
|
}],
|
||||||
|
http: {
|
||||||
|
path: `/:id/holidays`,
|
||||||
|
verb: 'GET'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Self.holidays = async(ctx, id, options) => {
|
||||||
|
const models = Self.app.models;
|
||||||
|
const args = ctx.args;
|
||||||
|
|
||||||
|
let myOptions = {};
|
||||||
|
|
||||||
|
if (typeof options == 'object')
|
||||||
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
|
const isSubordinate = await models.Worker.isSubordinate(ctx, id, myOptions);
|
||||||
|
if (!isSubordinate)
|
||||||
|
throw new UserError(`You don't have enough privileges`);
|
||||||
|
|
||||||
|
const started = new Date();
|
||||||
|
started.setFullYear(args.year);
|
||||||
|
started.setMonth(0);
|
||||||
|
started.setDate(1);
|
||||||
|
started.setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
|
const ended = new Date();
|
||||||
|
ended.setFullYear(args.year);
|
||||||
|
ended.setMonth(12);
|
||||||
|
ended.setDate(0);
|
||||||
|
ended.setHours(23, 59, 59, 59);
|
||||||
|
|
||||||
|
const filter = {
|
||||||
|
include: [{
|
||||||
|
relation: 'holidays',
|
||||||
|
scope: {
|
||||||
|
where: {year: args.year}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
relation: 'absences',
|
||||||
|
scope: {
|
||||||
|
include: {
|
||||||
|
relation: 'absenceType',
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
dated: {between: [started, ended]}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
relation: 'workCenter',
|
||||||
|
scope: {
|
||||||
|
include: {
|
||||||
|
relation: 'holidays',
|
||||||
|
scope: {
|
||||||
|
include: [{
|
||||||
|
relation: 'detail'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
relation: 'type'
|
||||||
|
}],
|
||||||
|
where: {
|
||||||
|
dated: {between: [started, ended]}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}],
|
||||||
|
where: {
|
||||||
|
and: [
|
||||||
|
{workerFk: id},
|
||||||
|
{
|
||||||
|
or: [
|
||||||
|
{started: {between: [started, ended]}},
|
||||||
|
{ended: {between: [started, ended]}},
|
||||||
|
{and: [{started: {lt: started}}, {ended: {gt: ended}}]},
|
||||||
|
{and: [{started: {lt: started}}, {ended: null}]}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if (args.businessFk)
|
||||||
|
filter.where.and.push({businessFk: args.businessFk});
|
||||||
|
|
||||||
|
const contracts = await models.WorkerLabour.find(filter, myOptions);
|
||||||
|
|
||||||
|
let totalHolidays = 0;
|
||||||
|
let holidaysEnjoyed = 0;
|
||||||
|
|
||||||
|
for (let contract of contracts) {
|
||||||
|
const contractStarted = contract.started;
|
||||||
|
contractStarted.setHours(0, 0, 0, 0);
|
||||||
|
const contractEnded = contract.ended;
|
||||||
|
if (contractEnded)
|
||||||
|
contractEnded.setHours(23, 59, 59, 59);
|
||||||
|
|
||||||
|
let startedTime;
|
||||||
|
if (contractStarted < started)
|
||||||
|
startedTime = started.getTime();
|
||||||
|
else startedTime = contractStarted.getTime();
|
||||||
|
|
||||||
|
let endedTime;
|
||||||
|
if (!contractEnded || (contractEnded && contractEnded > ended))
|
||||||
|
endedTime = ended.getTime();
|
||||||
|
else endedTime = contractEnded.getTime();
|
||||||
|
|
||||||
|
const dayTimestamp = 1000 * 60 * 60 * 24;
|
||||||
|
|
||||||
|
// Get number of worked days between dates
|
||||||
|
let workedDays = Math.floor((endedTime - startedTime) / dayTimestamp);
|
||||||
|
workedDays += 1; // 1 day inclusion
|
||||||
|
|
||||||
|
// Calculates absences
|
||||||
|
let entitlementRate = 0;
|
||||||
|
for (let absence of contract.absences()) {
|
||||||
|
const absenceType = absence.absenceType();
|
||||||
|
const isHoliday = absenceType.code === 'holiday';
|
||||||
|
const isHalfHoliday = absenceType.code === 'halfHoliday';
|
||||||
|
|
||||||
|
if (isHoliday) holidaysEnjoyed += 1;
|
||||||
|
if (isHalfHoliday) holidaysEnjoyed += 0.5;
|
||||||
|
|
||||||
|
entitlementRate += absenceType.holidayEntitlementRate;
|
||||||
|
}
|
||||||
|
|
||||||
|
workedDays -= entitlementRate;
|
||||||
|
|
||||||
|
// Max holidays for the selected year
|
||||||
|
const maxHolidays = contract.holidays() && contract.holidays().days;
|
||||||
|
|
||||||
|
if (workedDays < daysInYear())
|
||||||
|
totalHolidays += Math.round(2 * maxHolidays * (workedDays) / daysInYear()) / 2;
|
||||||
|
else totalHolidays = maxHolidays;
|
||||||
|
}
|
||||||
|
|
||||||
|
function daysInYear() {
|
||||||
|
const year = started.getFullYear();
|
||||||
|
|
||||||
|
return isLeapYear(year) ? 366 : 365;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {totalHolidays, holidaysEnjoyed};
|
||||||
|
};
|
||||||
|
|
||||||
|
function isLeapYear(year) {
|
||||||
|
return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0);
|
||||||
|
}
|
||||||
|
};
|
|
@ -23,16 +23,21 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.isSubordinate = async(ctx, id) => {
|
Self.isSubordinate = async(ctx, id, options) => {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const myUserId = ctx.req.accessToken.userId;
|
const myUserId = ctx.req.accessToken.userId;
|
||||||
|
|
||||||
const mySubordinates = await Self.mySubordinates(ctx);
|
let myOptions = {};
|
||||||
|
|
||||||
|
if (typeof options == 'object')
|
||||||
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
|
const mySubordinates = await Self.mySubordinates(ctx, myOptions);
|
||||||
const isSubordinate = mySubordinates.find(subordinate => {
|
const isSubordinate = mySubordinates.find(subordinate => {
|
||||||
return subordinate.workerFk == id;
|
return subordinate.workerFk == id;
|
||||||
});
|
});
|
||||||
|
|
||||||
const isHr = await models.Account.hasRole(myUserId, 'hr');
|
const isHr = await models.Account.hasRole(myUserId, 'hr', myOptions);
|
||||||
if (isHr || isSubordinate)
|
if (isHr || isSubordinate)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
|
|
@ -20,17 +20,22 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.mySubordinates = async ctx => {
|
Self.mySubordinates = async(ctx, options) => {
|
||||||
const conn = Self.dataSource.connector;
|
const conn = Self.dataSource.connector;
|
||||||
const userId = ctx.req.accessToken.userId;
|
const userId = ctx.req.accessToken.userId;
|
||||||
const stmts = [];
|
const stmts = [];
|
||||||
|
|
||||||
|
let myOptions = {};
|
||||||
|
|
||||||
|
if (typeof options == 'object')
|
||||||
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
stmts.push(new ParameterizedSQL('CALL vn.subordinateGetList(?)', [userId]));
|
stmts.push(new ParameterizedSQL('CALL vn.subordinateGetList(?)', [userId]));
|
||||||
const queryIndex = stmts.push('SELECT * FROM tmp.subordinate') - 1;
|
const queryIndex = stmts.push('SELECT * FROM tmp.subordinate') - 1;
|
||||||
stmts.push('DROP TEMPORARY TABLE tmp.subordinate');
|
stmts.push('DROP TEMPORARY TABLE tmp.subordinate');
|
||||||
|
|
||||||
const sql = ParameterizedSQL.join(stmts, ';');
|
const sql = ParameterizedSQL.join(stmts, ';');
|
||||||
const result = await conn.executeStmt(sql);
|
const result = await conn.executeStmt(sql, myOptions);
|
||||||
|
|
||||||
return result[queryIndex];
|
return result[queryIndex];
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,18 +5,34 @@ describe('Worker createAbsence()', () => {
|
||||||
const workerId = 18;
|
const workerId = 18;
|
||||||
|
|
||||||
it('should return an error for a user without enough privileges', async() => {
|
it('should return an error for a user without enough privileges', async() => {
|
||||||
const ctx = {req: {accessToken: {userId: 18}}};
|
const ctx = {
|
||||||
const absenceTypeId = 1;
|
req: {accessToken: {userId: 18}},
|
||||||
const dated = new Date();
|
args: {
|
||||||
|
businessFk: 18,
|
||||||
|
absenceTypeId: 1,
|
||||||
|
dated: new Date()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const tx = await app.models.Calendar.beginTransaction({});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const options = {transaction: tx};
|
||||||
|
|
||||||
let error;
|
let error;
|
||||||
await app.models.Worker.createAbsence(ctx, workerId, absenceTypeId, dated).catch(e => {
|
await app.models.Worker.createAbsence(ctx, workerId, options).catch(e => {
|
||||||
error = e;
|
error = e;
|
||||||
}).finally(() => {
|
}).finally(() => {
|
||||||
expect(error.message).toEqual(`You don't have enough privileges`);
|
expect(error.message).toEqual(`You don't have enough privileges`);
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(error).toBeDefined();
|
expect(error).toBeDefined();
|
||||||
|
|
||||||
|
await tx.rollback();
|
||||||
|
} catch (e) {
|
||||||
|
await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a new absence', async() => {
|
it('should create a new absence', async() => {
|
||||||
|
@ -24,7 +40,14 @@ describe('Worker createAbsence()', () => {
|
||||||
accessToken: {userId: 19},
|
accessToken: {userId: 19},
|
||||||
headers: {origin: 'http://localhost'}
|
headers: {origin: 'http://localhost'}
|
||||||
};
|
};
|
||||||
const ctx = {req: activeCtx};
|
const ctx = {
|
||||||
|
req: activeCtx,
|
||||||
|
args: {
|
||||||
|
businessFk: 18,
|
||||||
|
absenceTypeId: 1,
|
||||||
|
dated: new Date()
|
||||||
|
}
|
||||||
|
};
|
||||||
ctx.req.__ = value => {
|
ctx.req.__ = value => {
|
||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
|
@ -32,9 +55,12 @@ describe('Worker createAbsence()', () => {
|
||||||
active: activeCtx
|
active: activeCtx
|
||||||
});
|
});
|
||||||
|
|
||||||
const absenceTypeId = 1;
|
const tx = await app.models.Calendar.beginTransaction({});
|
||||||
const dated = new Date();
|
|
||||||
const createdAbsence = await app.models.Worker.createAbsence(ctx, workerId, absenceTypeId, dated);
|
try {
|
||||||
|
const options = {transaction: tx};
|
||||||
|
|
||||||
|
const createdAbsence = await app.models.Worker.createAbsence(ctx, workerId, options);
|
||||||
|
|
||||||
const expectedBusinessId = 18;
|
const expectedBusinessId = 18;
|
||||||
const expectedAbsenceTypeId = 1;
|
const expectedAbsenceTypeId = 1;
|
||||||
|
@ -42,7 +68,10 @@ describe('Worker createAbsence()', () => {
|
||||||
expect(createdAbsence.businessFk).toEqual(expectedBusinessId);
|
expect(createdAbsence.businessFk).toEqual(expectedBusinessId);
|
||||||
expect(createdAbsence.dayOffTypeFk).toEqual(expectedAbsenceTypeId);
|
expect(createdAbsence.dayOffTypeFk).toEqual(expectedAbsenceTypeId);
|
||||||
|
|
||||||
// Restores
|
await tx.rollback();
|
||||||
await app.models.Calendar.destroyById(createdAbsence.id);
|
} catch (e) {
|
||||||
|
await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -12,45 +12,68 @@ describe('Worker deleteAbsence()', () => {
|
||||||
ctx.req.__ = value => {
|
ctx.req.__ = value => {
|
||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
let createdAbsence;
|
|
||||||
|
|
||||||
beforeEach(async() => {
|
beforeEach(async() => {
|
||||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
||||||
active: activeCtx
|
active: activeCtx
|
||||||
});
|
});
|
||||||
createdAbsence = await app.models.Calendar.create({
|
|
||||||
businessFk: businessId,
|
|
||||||
dayOffTypeFk: 1,
|
|
||||||
dated: new Date()
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(async() => {
|
|
||||||
await app.models.Calendar.destroyById(createdAbsence.id);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return an error for a user without enough privileges', async() => {
|
it('should return an error for a user without enough privileges', async() => {
|
||||||
activeCtx.accessToken.userId = 106;
|
activeCtx.accessToken.userId = 106;
|
||||||
|
const tx = await app.models.Calendar.beginTransaction({});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const options = {transaction: tx};
|
||||||
|
const createdAbsence = await app.models.Calendar.create({
|
||||||
|
businessFk: businessId,
|
||||||
|
dayOffTypeFk: 1,
|
||||||
|
dated: new Date()
|
||||||
|
}, options);
|
||||||
|
|
||||||
|
ctx.args = {absenceId: createdAbsence.id};
|
||||||
|
|
||||||
let error;
|
let error;
|
||||||
await app.models.Worker.deleteAbsence(ctx, 18, createdAbsence.id).catch(e => {
|
await app.models.Worker.deleteAbsence(ctx, workerId).catch(e => {
|
||||||
error = e;
|
error = e;
|
||||||
}).finally(() => {
|
}).finally(() => {
|
||||||
expect(error.message).toEqual(`You don't have enough privileges`);
|
expect(error.message).toEqual(`You don't have enough privileges`);
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(error).toBeDefined();
|
expect(error).toBeDefined();
|
||||||
|
|
||||||
|
await tx.rollback();
|
||||||
|
} catch (e) {
|
||||||
|
await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a new absence', async() => {
|
it('should successfully delete an absence', async() => {
|
||||||
activeCtx.accessToken.userId = 19;
|
activeCtx.accessToken.userId = 19;
|
||||||
|
|
||||||
expect(createdAbsence.businessFk).toEqual(businessId);
|
const tx = await app.models.Calendar.beginTransaction({});
|
||||||
|
|
||||||
await app.models.Worker.deleteAbsence(ctx, workerId, createdAbsence.id);
|
try {
|
||||||
|
const options = {transaction: tx};
|
||||||
|
const createdAbsence = await app.models.Calendar.create({
|
||||||
|
businessFk: businessId,
|
||||||
|
dayOffTypeFk: 1,
|
||||||
|
dated: new Date()
|
||||||
|
}, options);
|
||||||
|
|
||||||
const deletedAbsence = await app.models.Calendar.findById(createdAbsence.id);
|
ctx.args = {absenceId: createdAbsence.id};
|
||||||
|
|
||||||
|
await app.models.Worker.deleteAbsence(ctx, workerId, options);
|
||||||
|
|
||||||
|
const deletedAbsence = await app.models.Calendar.findById(createdAbsence.id, null, options);
|
||||||
|
|
||||||
expect(deletedAbsence).toBeNull();
|
expect(deletedAbsence).toBeNull();
|
||||||
|
|
||||||
|
await tx.rollback();
|
||||||
|
} catch (e) {
|
||||||
|
await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
const app = require('vn-loopback/server/server');
|
||||||
|
const LoopBackContext = require('loopback-context');
|
||||||
|
|
||||||
|
describe('Worker holidays()', () => {
|
||||||
|
const businessId = 106;
|
||||||
|
const workerId = 106;
|
||||||
|
const activeCtx = {
|
||||||
|
accessToken: {userId: workerId},
|
||||||
|
headers: {origin: 'http://localhost'}
|
||||||
|
};
|
||||||
|
const ctx = {req: activeCtx};
|
||||||
|
|
||||||
|
beforeEach(async() => {
|
||||||
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
||||||
|
active: activeCtx
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should get the absence calendar for a full year contract', async() => {
|
||||||
|
const now = new Date();
|
||||||
|
const year = now.getFullYear();
|
||||||
|
|
||||||
|
ctx.args = {businessFk: businessId, year: year};
|
||||||
|
|
||||||
|
const result = await app.models.Worker.holidays(ctx, workerId);
|
||||||
|
|
||||||
|
expect(result.totalHolidays).toEqual(27.5);
|
||||||
|
expect(result.holidaysEnjoyed).toEqual(5);
|
||||||
|
});
|
||||||
|
});
|
|
@ -12,10 +12,10 @@
|
||||||
"type": "Number"
|
"type": "Number"
|
||||||
},
|
},
|
||||||
"started": {
|
"started": {
|
||||||
"type": "Date"
|
"type": "date"
|
||||||
},
|
},
|
||||||
"ended": {
|
"ended": {
|
||||||
"type": "Date"
|
"type": "date"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"relations": {
|
"relations": {
|
||||||
|
@ -38,6 +38,11 @@
|
||||||
"type": "belongsTo",
|
"type": "belongsTo",
|
||||||
"model": "WorkCenterHoliday",
|
"model": "WorkCenterHoliday",
|
||||||
"foreignKey": "workCenterFk"
|
"foreignKey": "workCenterFk"
|
||||||
|
},
|
||||||
|
"absences": {
|
||||||
|
"type": "hasMany",
|
||||||
|
"model": "Calendar",
|
||||||
|
"foreignKey": "businessFk"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,4 +10,7 @@ module.exports = Self => {
|
||||||
require('../methods/worker/active')(Self);
|
require('../methods/worker/active')(Self);
|
||||||
require('../methods/worker/activeWithRole')(Self);
|
require('../methods/worker/activeWithRole')(Self);
|
||||||
require('../methods/worker/activeWithInheritedRole')(Self);
|
require('../methods/worker/activeWithInheritedRole')(Self);
|
||||||
|
require('../methods/worker/contracts')(Self);
|
||||||
|
require('../methods/worker/holidays')(Self);
|
||||||
|
require('../methods/worker/activeContract')(Self);
|
||||||
};
|
};
|
||||||
|
|
|
@ -22,13 +22,22 @@
|
||||||
</div>
|
</div>
|
||||||
<vn-side-menu side="right">
|
<vn-side-menu side="right">
|
||||||
<div class="vn-pa-md">
|
<div class="vn-pa-md">
|
||||||
<div class="totalBox" style="text-align: center;">
|
<div class="totalBox vn-mb-sm" style="text-align: center;">
|
||||||
<h6 translate>Holidays</h6>
|
<h6>{{'Contract' | translate}} ID: {{$ctrl.businessId}}</h6>
|
||||||
<div>
|
<div>
|
||||||
{{'Used' | translate}} {{$ctrl.calendar.holidaysEnjoyed}}
|
{{'Used' | translate}} {{$ctrl.contractHolidays.holidaysEnjoyed}}
|
||||||
{{'of' | translate}} {{$ctrl.calendar.totalHolidays || 0}} {{'days' | translate}}
|
{{'of' | translate}} {{$ctrl.contractHolidays.totalHolidays || 0}} {{'days' | translate}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="totalBox" style="text-align: center;">
|
||||||
|
<h6>{{'Year' | translate}} {{$ctrl.year}}</h6>
|
||||||
|
<div>
|
||||||
|
{{'Used' | translate}} {{$ctrl.yearHolidays.holidaysEnjoyed}}
|
||||||
|
{{'of' | translate}} {{$ctrl.yearHolidays.totalHolidays || 0}} {{'days' | translate}}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="vn-pt-md">
|
<div class="vn-pt-md">
|
||||||
<vn-autocomplete label="Year"
|
<vn-autocomplete label="Year"
|
||||||
data="$ctrl.yearFilter"
|
data="$ctrl.yearFilter"
|
||||||
|
@ -37,8 +46,23 @@
|
||||||
value-field="year"
|
value-field="year"
|
||||||
order="DESC">
|
order="DESC">
|
||||||
</vn-autocomplete>
|
</vn-autocomplete>
|
||||||
|
<vn-autocomplete label="Contract"
|
||||||
|
url="Workers/{{$ctrl.$params.id}}/contracts"
|
||||||
|
fields="['started', 'ended']"
|
||||||
|
ng-model="$ctrl.businessId"
|
||||||
|
search-function="{businessFk: $search}"
|
||||||
|
value-field="businessFk"
|
||||||
|
order="businessFk DESC"
|
||||||
|
limit="5">
|
||||||
|
<tpl-item>
|
||||||
|
<div>ID: {{businessFk}}</div>
|
||||||
|
<div class="text-caption text-secondary">
|
||||||
|
{{started | date: 'dd/MM/yyyy'}} - {{ended ? (ended | date: 'dd/MM/yyyy') : 'Indef.'}}
|
||||||
</div>
|
</div>
|
||||||
<div class="input vn-py-md" style="overflow: hidden;">
|
</tpl-item>
|
||||||
|
</vn-autocomplete>
|
||||||
|
</div>
|
||||||
|
<div name="absenceTypes" class="input vn-py-md" style="overflow: hidden;">
|
||||||
<vn-chip ng-repeat="absenceType in absenceTypes" ng-class="::{'selectable': $ctrl.isSubordinate}"
|
<vn-chip ng-repeat="absenceType in absenceTypes" ng-class="::{'selectable': $ctrl.isSubordinate}"
|
||||||
ng-click="$ctrl.pick(absenceType)">
|
ng-click="$ctrl.pick(absenceType)">
|
||||||
<vn-avatar
|
<vn-avatar
|
||||||
|
|
|
@ -20,7 +20,24 @@ class Controller extends Section {
|
||||||
|
|
||||||
this.date = newYear;
|
this.date = newYear;
|
||||||
|
|
||||||
this.refresh().then(() => this.repaint());
|
this.refresh()
|
||||||
|
.then(() => this.repaint())
|
||||||
|
.then(() => this.getContractHolidays())
|
||||||
|
.then(() => this.getYearHolidays());
|
||||||
|
}
|
||||||
|
|
||||||
|
get businessId() {
|
||||||
|
return this._businessId;
|
||||||
|
}
|
||||||
|
|
||||||
|
set businessId(value) {
|
||||||
|
this._businessId = value;
|
||||||
|
if (value) {
|
||||||
|
this.refresh()
|
||||||
|
.then(() => this.repaint())
|
||||||
|
.then(() => this.getContractHolidays())
|
||||||
|
.then(() => this.getYearHolidays());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get date() {
|
get date() {
|
||||||
|
@ -31,16 +48,6 @@ class Controller extends Section {
|
||||||
this._date = value;
|
this._date = value;
|
||||||
value.setHours(0, 0, 0, 0);
|
value.setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
const started = new Date(value.getTime());
|
|
||||||
started.setMonth(0);
|
|
||||||
started.setDate(1);
|
|
||||||
this.started = started;
|
|
||||||
|
|
||||||
const ended = new Date(value.getTime());
|
|
||||||
ended.setMonth(12);
|
|
||||||
ended.setDate(0);
|
|
||||||
this.ended = ended;
|
|
||||||
|
|
||||||
this.months = new Array(12);
|
this.months = new Array(12);
|
||||||
|
|
||||||
for (let i = 0; i < this.months.length; i++) {
|
for (let i = 0; i < this.months.length; i++) {
|
||||||
|
@ -59,26 +66,51 @@ class Controller extends Section {
|
||||||
this._worker = value;
|
this._worker = value;
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
this.refresh().then(() => this.repaint());
|
|
||||||
this.getIsSubordinate();
|
this.getIsSubordinate();
|
||||||
|
this.getActiveContract();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buildYearFilter() {
|
buildYearFilter() {
|
||||||
const currentYear = new Date().getFullYear();
|
const now = new Date();
|
||||||
const minRange = currentYear - 5;
|
now.setFullYear(now.getFullYear() + 1);
|
||||||
|
|
||||||
|
const maxYear = now.getFullYear();
|
||||||
|
const minRange = maxYear - 5;
|
||||||
|
|
||||||
const years = [];
|
const years = [];
|
||||||
for (let i = currentYear; i > minRange; i--)
|
for (let i = maxYear; i > minRange; i--)
|
||||||
years.push({year: i});
|
years.push({year: i});
|
||||||
|
|
||||||
this.yearFilter = years;
|
this.yearFilter = years;
|
||||||
}
|
}
|
||||||
|
|
||||||
getIsSubordinate() {
|
getIsSubordinate() {
|
||||||
this.$http.get(`Workers/${this.worker.id}/isSubordinate`).then(res =>
|
this.$http.get(`Workers/${this.worker.id}/isSubordinate`)
|
||||||
this.isSubordinate = res.data
|
.then(res => this.isSubordinate = res.data);
|
||||||
);
|
}
|
||||||
|
|
||||||
|
getActiveContract() {
|
||||||
|
this.$http.get(`Workers/${this.worker.id}/activeContract`)
|
||||||
|
.then(res => this.businessId = res.data.businessFk);
|
||||||
|
}
|
||||||
|
|
||||||
|
getContractHolidays() {
|
||||||
|
this.getHolidays({
|
||||||
|
businessFk: this.businessId,
|
||||||
|
year: this.year
|
||||||
|
}, data => this.contractHolidays = data);
|
||||||
|
}
|
||||||
|
|
||||||
|
getYearHolidays() {
|
||||||
|
this.getHolidays({
|
||||||
|
year: this.year
|
||||||
|
}, data => this.yearHolidays = data);
|
||||||
|
}
|
||||||
|
|
||||||
|
getHolidays(params, cb) {
|
||||||
|
this.$http.get(`Workers/${this.worker.id}/holidays`, {params})
|
||||||
|
.then(res => cb(res.data));
|
||||||
}
|
}
|
||||||
|
|
||||||
onData(data) {
|
onData(data) {
|
||||||
|
@ -155,9 +187,6 @@ class Controller extends Section {
|
||||||
if (!this.absenceType)
|
if (!this.absenceType)
|
||||||
return this.vnApp.showMessage(this.$t('Choose an absence type from the right menu'));
|
return this.vnApp.showMessage(this.$t('Choose an absence type from the right menu'));
|
||||||
|
|
||||||
if (this.year != new Date().getFullYear())
|
|
||||||
return this.vnApp.showMessage(this.$t('You can just add absences within the current year'));
|
|
||||||
|
|
||||||
const day = $days[0];
|
const day = $days[0];
|
||||||
const stamp = day.getTime();
|
const stamp = day.getTime();
|
||||||
const event = this.events[stamp];
|
const event = this.events[stamp];
|
||||||
|
@ -176,7 +205,8 @@ class Controller extends Section {
|
||||||
const absenceType = this.absenceType;
|
const absenceType = this.absenceType;
|
||||||
const params = {
|
const params = {
|
||||||
dated: dated,
|
dated: dated,
|
||||||
absenceTypeId: absenceType.id
|
absenceTypeId: absenceType.id,
|
||||||
|
businessFk: this.businessId
|
||||||
};
|
};
|
||||||
|
|
||||||
const path = `Workers/${this.$params.id}/createAbsence`;
|
const path = `Workers/${this.$params.id}/createAbsence`;
|
||||||
|
@ -190,7 +220,10 @@ class Controller extends Section {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.repaintCanceller(() =>
|
this.repaintCanceller(() =>
|
||||||
this.refresh().then(calendar.repaint())
|
this.refresh()
|
||||||
|
.then(calendar.repaint())
|
||||||
|
.then(() => this.getContractHolidays())
|
||||||
|
.then(() => this.getYearHolidays())
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -208,7 +241,10 @@ class Controller extends Section {
|
||||||
event.type = absenceType.code;
|
event.type = absenceType.code;
|
||||||
|
|
||||||
this.repaintCanceller(() =>
|
this.repaintCanceller(() =>
|
||||||
this.refresh().then(calendar.repaint())
|
this.refresh()
|
||||||
|
.then(calendar.repaint())
|
||||||
|
.then(() => this.getContractHolidays())
|
||||||
|
.then(() => this.getYearHolidays())
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -220,7 +256,10 @@ class Controller extends Section {
|
||||||
delete this.events[day.getTime()];
|
delete this.events[day.getTime()];
|
||||||
|
|
||||||
this.repaintCanceller(() =>
|
this.repaintCanceller(() =>
|
||||||
this.refresh().then(calendar.repaint())
|
this.refresh()
|
||||||
|
.then(calendar.repaint())
|
||||||
|
.then(() => this.getContractHolidays())
|
||||||
|
.then(() => this.getYearHolidays())
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -237,9 +276,8 @@ class Controller extends Section {
|
||||||
|
|
||||||
refresh() {
|
refresh() {
|
||||||
const params = {
|
const params = {
|
||||||
workerFk: this.worker.id,
|
businessFk: this.businessId,
|
||||||
started: this.started,
|
year: this.year
|
||||||
ended: this.ended
|
|
||||||
};
|
};
|
||||||
return this.$http.get(`Calendars/absences`, {params})
|
return this.$http.get(`Calendars/absences`, {params})
|
||||||
.then(res => this.onData(res.data));
|
.then(res => this.onData(res.data));
|
||||||
|
|
|
@ -41,35 +41,31 @@ describe('Worker', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('started property', () => {
|
describe('businessId() setter', () => {
|
||||||
it(`should return first day and month of current year`, () => {
|
it(`should set the contract id and then call to the refresh method`, () => {
|
||||||
let started = new Date(year, 0, 1);
|
jest.spyOn(controller, 'refresh').mockReturnValue(Promise.resolve());
|
||||||
|
|
||||||
expect(controller.started).toEqual(started);
|
controller.businessId = 106;
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('ended property', () => {
|
expect(controller.refresh).toHaveBeenCalledWith();
|
||||||
it(`should return last day and month of current year`, () => {
|
|
||||||
let ended = new Date(year, 11, 31);
|
|
||||||
|
|
||||||
expect(controller.ended).toEqual(ended);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('months property', () => {
|
describe('months property', () => {
|
||||||
it(`should return an array of twelve months length`, () => {
|
it(`should return an array of twelve months length`, () => {
|
||||||
|
const started = new Date(year, 0, 1);
|
||||||
const ended = new Date(year, 11, 1);
|
const ended = new Date(year, 11, 1);
|
||||||
|
|
||||||
expect(controller.months.length).toEqual(12);
|
expect(controller.months.length).toEqual(12);
|
||||||
expect(controller.months[0]).toEqual(controller.started);
|
expect(controller.months[0]).toEqual(started);
|
||||||
expect(controller.months[11]).toEqual(ended);
|
expect(controller.months[11]).toEqual(ended);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('worker() setter', () => {
|
describe('worker() setter', () => {
|
||||||
it(`should perform a get query and set the reponse data on the model`, () => {
|
it(`should perform a get query and set the reponse data on the model`, () => {
|
||||||
jest.spyOn(controller, 'getIsSubordinate').mockReturnValue(true);
|
controller.getIsSubordinate = jest.fn();
|
||||||
|
controller.getActiveContract = jest.fn();
|
||||||
|
|
||||||
let today = new Date();
|
let today = new Date();
|
||||||
let tomorrow = new Date(today.getTime());
|
let tomorrow = new Date(today.getTime());
|
||||||
|
@ -78,49 +74,60 @@ describe('Worker', () => {
|
||||||
let yesterday = new Date(today.getTime());
|
let yesterday = new Date(today.getTime());
|
||||||
yesterday.setDate(yesterday.getDate() - 1);
|
yesterday.setDate(yesterday.getDate() - 1);
|
||||||
|
|
||||||
$httpBackend.whenRoute('GET', 'Calendars/absences')
|
controller.worker = {id: 107};
|
||||||
.respond({
|
|
||||||
holidays: [
|
expect(controller.getIsSubordinate).toHaveBeenCalledWith();
|
||||||
{dated: today, detail: {name: 'New year'}},
|
expect(controller.getActiveContract).toHaveBeenCalledWith();
|
||||||
{dated: tomorrow, detail: {name: 'Easter'}}
|
});
|
||||||
],
|
|
||||||
absences: [
|
|
||||||
{dated: today, absenceType: {name: 'Holiday', rgb: '#aaa'}},
|
|
||||||
{dated: yesterday, absenceType: {name: 'Leave', rgb: '#bbb'}}
|
|
||||||
]
|
|
||||||
});
|
});
|
||||||
|
|
||||||
controller.worker = {id: 107};
|
describe('getIsSubordinate()', () => {
|
||||||
|
it(`should return whether the worker is a subordinate`, () => {
|
||||||
|
$httpBackend.expect('GET', `Workers/106/isSubordinate`).respond(true);
|
||||||
|
controller.getIsSubordinate();
|
||||||
$httpBackend.flush();
|
$httpBackend.flush();
|
||||||
|
|
||||||
let events = controller.events;
|
expect(controller.isSubordinate).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
expect(events[today.getTime()].name).toEqual('New year, Holiday');
|
describe('getActiveContract()', () => {
|
||||||
expect(events[tomorrow.getTime()].name).toEqual('Easter');
|
it(`should return the current contract and then set the businessId property`, () => {
|
||||||
expect(events[yesterday.getTime()].name).toEqual('Leave');
|
jest.spyOn(controller, 'refresh').mockReturnValue(Promise.resolve());
|
||||||
expect(events[yesterday.getTime()].color).toEqual('#bbb');
|
|
||||||
expect(controller.getIsSubordinate).toHaveBeenCalledWith();
|
$httpBackend.expect('GET', `Workers/106/activeContract`).respond({businessFk: 106});
|
||||||
|
controller.getActiveContract();
|
||||||
|
$httpBackend.flush();
|
||||||
|
|
||||||
|
expect(controller.businessId).toEqual(106);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getContractHolidays()', () => {
|
||||||
|
it(`should return the worker holidays amount and then set the contractHolidays property`, () => {
|
||||||
|
const today = new Date();
|
||||||
|
const year = today.getFullYear();
|
||||||
|
|
||||||
|
const serializedParams = $httpParamSerializer({year});
|
||||||
|
$httpBackend.expect('GET', `Workers/106/holidays?${serializedParams}`).respond({totalHolidays: 28});
|
||||||
|
controller.getContractHolidays();
|
||||||
|
$httpBackend.flush();
|
||||||
|
|
||||||
|
expect(controller.contractHolidays).toEqual({totalHolidays: 28});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('formatDay()', () => {
|
describe('formatDay()', () => {
|
||||||
it(`should set the day element style`, () => {
|
it(`should set the day element style`, () => {
|
||||||
jest.spyOn(controller, 'getIsSubordinate').mockReturnThis();
|
const today = new Date();
|
||||||
|
|
||||||
let today = new Date();
|
controller.events[today.getTime()] = {
|
||||||
|
name: 'Holiday',
|
||||||
|
color: '#000'
|
||||||
|
};
|
||||||
|
|
||||||
$httpBackend.whenRoute('GET', 'Calendars/absences')
|
const dayElement = angular.element('<div><section></section></div>')[0];
|
||||||
.respond({
|
const dayNumber = dayElement.firstElementChild;
|
||||||
absences: [
|
|
||||||
{dated: today, absenceType: {name: 'Holiday', rgb: '#000'}}
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
controller.worker = {id: 1};
|
|
||||||
$httpBackend.flush();
|
|
||||||
|
|
||||||
let dayElement = angular.element('<div><section></section></div>')[0];
|
|
||||||
let dayNumber = dayElement.firstElementChild;
|
|
||||||
|
|
||||||
controller.formatDay(today, dayElement);
|
controller.formatDay(today, dayElement);
|
||||||
|
|
||||||
|
@ -160,28 +167,6 @@ describe('Worker', () => {
|
||||||
expect(controller.vnApp.showMessage).toHaveBeenCalledWith('Choose an absence type from the right menu');
|
expect(controller.vnApp.showMessage).toHaveBeenCalledWith('Choose an absence type from the right menu');
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should show an snackbar message if the selected day is not within the current year`, () => {
|
|
||||||
jest.spyOn(controller.vnApp, 'showMessage').mockReturnThis();
|
|
||||||
|
|
||||||
const selectedDay = new Date();
|
|
||||||
const $event = {
|
|
||||||
target: {
|
|
||||||
closest: () => {
|
|
||||||
return {$ctrl: {}};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const $days = [selectedDay];
|
|
||||||
const pastYear = new Date();
|
|
||||||
pastYear.setFullYear(pastYear.getFullYear() - 1);
|
|
||||||
|
|
||||||
controller.date = pastYear;
|
|
||||||
controller.absenceType = {id: 1};
|
|
||||||
controller.onSelection($event, $days);
|
|
||||||
|
|
||||||
expect(controller.vnApp.showMessage).toHaveBeenCalledWith('You can just add absences within the current year');
|
|
||||||
});
|
|
||||||
|
|
||||||
it(`should call to the create() method`, () => {
|
it(`should call to the create() method`, () => {
|
||||||
jest.spyOn(controller, 'create').mockReturnThis();
|
jest.spyOn(controller, 'create').mockReturnThis();
|
||||||
|
|
||||||
|
@ -342,20 +327,8 @@ describe('Worker', () => {
|
||||||
it(`should make a HTTP GET query and then call to the onData() method`, () => {
|
it(`should make a HTTP GET query and then call to the onData() method`, () => {
|
||||||
jest.spyOn(controller, 'onData').mockReturnThis();
|
jest.spyOn(controller, 'onData').mockReturnThis();
|
||||||
|
|
||||||
const dated = controller.date;
|
|
||||||
const started = new Date(dated.getTime());
|
|
||||||
started.setMonth(0);
|
|
||||||
started.setDate(1);
|
|
||||||
|
|
||||||
const ended = new Date(dated.getTime());
|
|
||||||
ended.setMonth(12);
|
|
||||||
ended.setDate(0);
|
|
||||||
|
|
||||||
controller.started = started;
|
|
||||||
controller.ended = ended;
|
|
||||||
|
|
||||||
const expecteResponse = [{id: 1}];
|
const expecteResponse = [{id: 1}];
|
||||||
const expectedParams = {workerFk: 106, started: started, ended: ended};
|
const expectedParams = {year: year};
|
||||||
const serializedParams = $httpParamSerializer(expectedParams);
|
const serializedParams = $httpParamSerializer(expectedParams);
|
||||||
$httpBackend.expect('GET', `Calendars/absences?${serializedParams}`).respond(200, expecteResponse);
|
$httpBackend.expect('GET', `Calendars/absences?${serializedParams}`).respond(200, expecteResponse);
|
||||||
controller.refresh();
|
controller.refresh();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
Calendar: Calendario
|
Calendar: Calendario
|
||||||
Holidays: Vacaciones
|
Contract: Contrato
|
||||||
Festive: Festivo
|
Festive: Festivo
|
||||||
Used: Utilizados
|
Used: Utilizados
|
||||||
Year: Año
|
Year: Año
|
||||||
|
|
|
@ -13,6 +13,19 @@ class Controller extends Section {
|
||||||
this.date = new Date();
|
this.date = new Date();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get worker() {
|
||||||
|
return this._worker;
|
||||||
|
}
|
||||||
|
|
||||||
|
set worker(value) {
|
||||||
|
this._worker = value;
|
||||||
|
|
||||||
|
if (value) {
|
||||||
|
this.getActiveContract()
|
||||||
|
.then(() => this.getAbsences());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The current selected date
|
* The current selected date
|
||||||
*/
|
*/
|
||||||
|
@ -70,6 +83,11 @@ class Controller extends Section {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getActiveContract() {
|
||||||
|
return this.$http.get(`Workers/${this.worker.id}/activeContract`)
|
||||||
|
.then(res => this.businessId = res.data.businessFk);
|
||||||
|
}
|
||||||
|
|
||||||
fetchHours() {
|
fetchHours() {
|
||||||
const params = {workerFk: this.$params.id};
|
const params = {workerFk: this.$params.id};
|
||||||
const filter = {
|
const filter = {
|
||||||
|
@ -80,7 +98,6 @@ class Controller extends Section {
|
||||||
};
|
};
|
||||||
this.$.model.applyFilter(filter, params).then(() => {
|
this.$.model.applyFilter(filter, params).then(() => {
|
||||||
this.getWorkedHours(this.started, this.ended);
|
this.getWorkedHours(this.started, this.ended);
|
||||||
this.getAbsences();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,10 +106,10 @@ class Controller extends Section {
|
||||||
}
|
}
|
||||||
|
|
||||||
getAbsences() {
|
getAbsences() {
|
||||||
|
const fullYear = this.started.getFullYear();
|
||||||
let params = {
|
let params = {
|
||||||
workerFk: this.$params.id,
|
businessFk: this.businessId,
|
||||||
started: this.started,
|
year: fullYear
|
||||||
ended: this.ended
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.$http.get(`Calendars/absences`, {params})
|
return this.$http.get(`Calendars/absences`, {params})
|
||||||
|
@ -257,5 +274,8 @@ Controller.$inject = ['$element', '$scope', 'vnWeekDays'];
|
||||||
|
|
||||||
ngModule.vnComponent('vnWorkerTimeControl', {
|
ngModule.vnComponent('vnWorkerTimeControl', {
|
||||||
template: require('./index.html'),
|
template: require('./index.html'),
|
||||||
controller: Controller
|
controller: Controller,
|
||||||
|
bindings: {
|
||||||
|
worker: '<'
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue