Added absence selection
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Joan Sánchez 2020-07-07 14:29:57 +02:00
parent 778c38df89
commit dd8e03dd0f
7 changed files with 274 additions and 21 deletions

View File

@ -0,0 +1,11 @@
USE `vn`;
CREATE
OR REPLACE ALGORITHM = UNDEFINED
DEFINER = `root`@`%`
SQL SECURITY DEFINER
VIEW `workerCalendar2` AS
SELECT
`ce`.`business_id` AS `businessFk`,
`ce`.`calendar_state_id` AS `absenceTypeFk`,
`ce`.`date` AS `dated`
FROM `postgresql`.`calendar_employee` `ce`;

View File

@ -0,0 +1,101 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('createAbsences', {
description: 'Returns an array of absences from an specified worker',
accepts: [{
arg: 'id',
type: 'Number',
description: 'The worker id',
http: {source: 'path'}
},
{
arg: 'absenceTypeId',
type: 'Number',
required: true
},
{
arg: 'dated',
type: 'Date',
required: false
},
{
arg: 'started',
type: 'Date',
required: false
},
{
arg: 'ended',
type: 'Date',
required: false
},
{
arg: 'type',
type: 'String',
required: true
}],
returns: 'Object',
http: {
path: `/:id/createAbsences`,
verb: 'POST'
}
});
Self.createAbsences = async(ctx, id, absenceTypeId, dated, started, ended, type) => {
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 absences = [];
if (type == 'day') {
const contracts = await models.WorkerLabour.find({
where: {
and: [
{workerFk: id},
{or: [{
ended: {gte: [yearStarted]}
}, {ended: null}]}
]
}
});
absences.push({
businessFk: 106,
absenceTypeFk: absenceTypeId,
dated: dated
});
} else if (type == 'range')
console.log(true);
return models.WorkerCalendar.create(absences);
};
};
/* include: [{
relation: 'holidays',
scope: {
where: {year}
}
},
{
relation: 'workCenter',
scope: {
include: {
relation: 'holidays',
scope: {
include: [{
relation: 'detail'
},
{
relation: 'type'
}],
where: {
dated: {between: [yearStarted, yearEnded]}
}
}
}
}
}], */

View File

@ -3,7 +3,7 @@
"base": "VnModel",
"options": {
"mysql": {
"table": "workerCalendar"
"table": "workerCalendar2"
}
},
"properties": {
@ -11,21 +11,12 @@
"id": 1,
"type": "Number"
},
"workerFk": {
"id": 2,
"type": "Number"
},
"dated": {
"id": 3,
"type": "Date"
}
},
"relations": {
"worker": {
"type": "belongsTo",
"model": "Worker",
"foreignKey": "workerFk"
},
"absenceType": {
"type": "belongsTo",
"model": "AbsenceType",

View File

@ -4,4 +4,5 @@ module.exports = Self => {
require('../methods/worker/isSubordinate')(Self);
require('../methods/worker/getWorkedHours')(Self);
require('../methods/worker/uploadFile')(Self);
require('../methods/worker/createAbsences')(Self);
};

View File

@ -12,7 +12,8 @@
format-day="$ctrl.formatDay($day, $element)"
display-controls="false"
hide-contiguous="true"
hide-year="true">
hide-year="true"
on-selection="$ctrl.onSelection($days, $type, $weekday)">
</vn-calendar>
</vn-card>
</div>
@ -26,12 +27,74 @@
</div>
</div>
<div class="vn-pt-md" style="overflow: hidden;">
<vn-chip ng-repeat="absenceType in absenceTypes">
<vn-chip ng-repeat="absenceType in absenceTypes" class="selectable"
ng-click="$ctrl.pick(absenceType)">
<vn-avatar
ng-style="{backgroundColor: absenceType.rgb}">
<vn-icon icon="check" ng-if="absenceType.id == $ctrl.absenceType.id"></vn-icon>
</vn-avatar>
{{absenceType.name}}
</vn-chip>
</div>
</div>
</vn-side-menu>
</vn-side-menu>
<vn-dialog
vn-id="dialog"
on-response="$ctrl.onAbsenceResponse($response)"
message="{{$ctrl.isNew ? 'Add absence' : 'Edit absence'}}">
<tpl-body>
<vn-vertical>
<vn-vertical class="vn-pb-md">
<vn-radio
ng-model="$ctrl.selected.type"
label="One day"
val="day">
</vn-radio>
<vn-radio
ng-model="$ctrl.selected.type"
label="Range of dates"
val="range">
</vn-radio>
</vn-vertical>
<vn-date-picker
ng-if="$ctrl.selected.type == 'day'"
label="Day"
ng-model="$ctrl.selected.dated">
</vn-date-picker>
<vn-horizontal
ng-if="$ctrl.selected.type == 'range'">
<vn-date-picker
label="From"
ng-model="$ctrl.selected.started">
</vn-date-picker>
<vn-date-picker
label="To"
ng-model="$ctrl.selected.ended">
</vn-date-picker>
</vn-horizontal>
</vn-vertical>
</tpl-body>
<tpl-buttons>
<input
type="button"
response="cancel"
translate-attr="{value: 'Cancel'}">
</input>
<input
type="button"
ng-if="!$ctrl.isNew"
response="delete"
translate-attr="{value: 'Delete'}">
</input>
<button response="accept">
<span ng-if="$ctrl.isNew" translate>Add</span>
<span ng-if="!$ctrl.isNew" translate>Save</span>
</button>
</tpl-buttons>
</vn-dialog>
<vn-confirm
vn-id="confirm"
message="This item will be deleted"
question="Are you sure you want to continue?">
</vn-confirm>

View File

@ -43,15 +43,8 @@ class Controller extends Section {
set worker(value) {
this._worker = value;
if (!value) return;
let params = {
workerFk: this.worker.id,
started: this.started,
ended: this.ended
};
this.$http.get(`WorkerCalendars/absences`, {params})
.then(res => this.onData(res.data));
if (value) this.refresh();
}
onData(data) {
@ -102,6 +95,86 @@ class Controller extends Section {
dayNumber.style.backgroundColor = event.color;
dayNumber.style.color = 'rgba(0, 0, 0, 0.7)';
}
pick(absenceType) {
if (absenceType == this.absenceType)
absenceType = null;
this.absenceType = absenceType;
}
onSelection($days, $type, $weekday) {
if (!this.absenceType)
return this.vnApp.showMessage(this.$t('Choose an absence type'));
let $events = [];
for (let day of $days) {
const stamp = day.getTime();
$events = $events.concat(this.events[stamp] || []);
}
if ($events.length)
this.edit($events[0]);
else
this.create($type, $days, $weekday);
}
create(type, days, weekday) {
this.isNew = true;
this.selected = {
type: 'day',
dated: days[0]
};
this.$.dialog.show();
}
edit() {
this.isNew = false;
}
onAbsenceResponse(response) {
switch (response) {
case 'accept': {
const selected = this.selected;
const params = Object.assign(selected, {
absenceTypeId: this.absenceType.id
});
/* const selected = {
businessFk: 106,
absenceTypeFk: this.absenceType.id,
dated: $days[0]
}; */
let req;
if (this.isNew) {
const path = `Workers/${this.$params.id}/createAbsences`;
req = this.$http.post(path, params);
} else
req = this.$http.put(`${this.path}/${selected.id}`, selected);
return req.then(() => {
this.selected = null;
this.isNew = null;
this.refresh();
});
}
case 'delete':
return this.onDelete(this.selected.id)
.then(response => response == 'accept');
}
}
refresh() {
const params = {
workerFk: this.worker.id,
started: this.started,
ended: this.ended
};
this.$http.get(`WorkerCalendars/absences`, {params})
.then(res => this.onData(res.data));
}
}
ngModule.component('vnWorkerCalendar', {

View File

@ -16,4 +16,17 @@ vn-worker-calendar {
max-width: 288px;
}
}
vn-chip.selectable {
cursor: pointer
}
vn-chip.selectable:hover {
opacity: 0.8
}
vn-chip vn-avatar {
text-align: center;
color: white
}
}