Calcolo Hours
This commit is contained in:
@@ -2,9 +2,11 @@ var mongoose = require('mongoose');
|
||||
|
||||
const _ = require('lodash');
|
||||
|
||||
const { ObjectID } = require('mongodb');
|
||||
const tools = require('../tools/general');
|
||||
|
||||
var { Project } = require('./project');
|
||||
const Hours = require('./hours');
|
||||
|
||||
var server_constants = require('../tools/server_constants');
|
||||
|
||||
@@ -98,12 +100,14 @@ TodoSchema.methods.toJSON = function () {
|
||||
return _.pick(todoObject, tools.allfieldTodoWithId());
|
||||
};
|
||||
|
||||
|
||||
TodoSchema.statics.findByUserIdAndIdParent = function (userId, category, phase = '') {
|
||||
var Todo = this;
|
||||
|
||||
let tofind = {
|
||||
'category': category,
|
||||
category: ObjectID(category),
|
||||
$or:
|
||||
[{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
|
||||
|
||||
};
|
||||
|
||||
if (userId !== '') {
|
||||
@@ -293,6 +297,7 @@ class CalcTodo {
|
||||
constructor(phase) {
|
||||
|
||||
this.mydata = {
|
||||
_id: '',
|
||||
phase: phase,
|
||||
numitem: 0
|
||||
};
|
||||
@@ -306,17 +311,21 @@ class CalcTodo {
|
||||
this.mydata.progressCalc = 0;
|
||||
}
|
||||
|
||||
addDataProj(datain) {
|
||||
async addDataProj(datain) {
|
||||
if (!!datain) {
|
||||
if (datain.actualphase === this.mydata.phase) {
|
||||
if (datain._id)
|
||||
datain.hoursworked = await Hours.calculateHoursTodo(datain._id.toString());
|
||||
CalcTodo.addFields(this.mydata, datain, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addDataTodo(datain) {
|
||||
async addDataTodo(datain) {
|
||||
if (!!datain) {
|
||||
if (datain.phase === this.mydata.phase) {
|
||||
if (datain._id)
|
||||
datain.hoursworked = await Hours.calculateHoursTodo(datain._id.toString());
|
||||
CalcTodo.addFields(this.mydata, datain, false);
|
||||
}
|
||||
}
|
||||
@@ -324,6 +333,7 @@ class CalcTodo {
|
||||
|
||||
static addFields(recout, recin, isproj) {
|
||||
// console.log('addFields', recin);
|
||||
|
||||
recout.hoursworked += recin.hoursworked;
|
||||
recout.hoursplanned += recin.hoursplanned;
|
||||
let hoursleft = (recin.hoursplanned - recin.hoursworked);
|
||||
@@ -351,7 +361,6 @@ class CalcTodo {
|
||||
else
|
||||
recout.progressCalc = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
setValuesToRecord(objout) {
|
||||
@@ -393,12 +402,12 @@ TodoSchema.statics.calculateTreeTodo = async function (actualphase, userId, idpr
|
||||
promiseChain = promiseChain.then(() => {
|
||||
return Todo.calculateTreeTodo(actualphase, userId, subproj._id, calcalsoUpper, masterproj, true)
|
||||
.then((subobjdata) => {
|
||||
objdata.addDataProj(subobjdata);
|
||||
return objdata.addDataProj(subobjdata);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
promiseChain = promiseChain.then(() => {
|
||||
objdata.addDataProj(subproj);
|
||||
return objdata.addDataProj(subproj);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -420,19 +429,19 @@ TodoSchema.statics.calculateTreeTodo = async function (actualphase, userId, idpr
|
||||
if (!!myrecproj) {
|
||||
if (myrecproj.actualphase === actualphase) {
|
||||
// console.log('objdatatodos', objdatatodos);
|
||||
objdata.addDataTodo(objdatatodos);
|
||||
return objdata.addDataTodo(objdatatodos)
|
||||
.then(() => {
|
||||
// End Calculate
|
||||
objdata.endDataCalc();
|
||||
|
||||
// End Calculate
|
||||
objdata.endDataCalc();
|
||||
|
||||
// Update into the DB:
|
||||
return Project.updateCalc(userId, idproj, objdata, null)
|
||||
.then((ris) => {
|
||||
if (ris)
|
||||
return objdata.getData();
|
||||
else
|
||||
return null;
|
||||
// Update into the DB:
|
||||
return Project.updateCalc(userId, idproj, objdata, null)
|
||||
.then((ris) => (ris) ? objdata.getData() : null);
|
||||
});
|
||||
|
||||
} else {
|
||||
return Project.updateCalc(userId, idproj, objdata, null)
|
||||
.then((ris) => (ris) ? objdata.getData() : null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -449,15 +458,17 @@ TodoSchema.statics.calculateTreeTodo = async function (actualphase, userId, idpr
|
||||
});
|
||||
} else {
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve()
|
||||
resolve(ris)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve()
|
||||
resolve(ris)
|
||||
});
|
||||
}).catch((e) => {
|
||||
console.log('error', e.message)
|
||||
})
|
||||
};
|
||||
|
||||
@@ -469,9 +480,16 @@ TodoSchema.statics.calculateTodoHoursAndProgress = async function (userId, idpro
|
||||
return await Todo.findByUserIdAndIdParent(userId, idproj, actualphase)
|
||||
.then(arrtodo => {
|
||||
// console.log(' calculateTodo *', arrtodo.length, '* FOUND');
|
||||
for (let itemtodo of arrtodo) {
|
||||
objdata.addDataTodo(itemtodo);
|
||||
|
||||
let promiseChain = Promise.resolve();
|
||||
|
||||
for (const itemtodo of arrtodo) {
|
||||
promiseChain = promiseChain.then(() => {
|
||||
return objdata.addDataTodo(itemtodo);
|
||||
})
|
||||
}
|
||||
return promiseChain;
|
||||
}).then(() => {
|
||||
|
||||
objdata.endDataCalc();
|
||||
|
||||
@@ -485,6 +503,7 @@ TodoSchema.statics.calculateTodoHoursAndProgress = async function (userId, idpro
|
||||
|
||||
};
|
||||
|
||||
|
||||
TodoSchema.pre('save', function (next) {
|
||||
// var todo = this;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user