Skip to content

Instantly share code, notes, and snippets.

@tuanpmt
Created October 10, 2014 04:49
Show Gist options
  • Save tuanpmt/294839fb118547764c45 to your computer and use it in GitHub Desktop.
Save tuanpmt/294839fb118547764c45 to your computer and use it in GitHub Desktop.
report
exports.attendanceToday = function(day, _callback) {
var beginDay = moment(day).tz("Asia/Ho_Chi_Minh").startOf('day');
var endDay = moment(day).tz("Asia/Ho_Chi_Minh").endOf('day');
async.waterfall([
/*Get all employers*/
function(cb){
Employer
.find({enable:true})
.populate('attendance', {where: {createdAt: {'>': beginDay.toDate(), '<': endDay.toDate()}}, sort: {_id: 1}})
.populate('timetables')
.exec(function(err, employers){
cb(err, employers);
});
},
/*Get current timetable task apply for user*/
function(employers, cb){
var thisMonth = beginDay.month();
var thisDay = beginDay.isoWeekday() - 1;
async.map(employers, function(employer, callback){
var employerTimetable = _.find(employer.timetables, function(timetable){
return timetable.monthApply.indexOf(thisMonth+1) != -1;
});
var employerTask = [];
var timetable = {};
if(employerTimetable && employerTimetable.table && employerTimetable.table.length > 0){
employerTask = _.find(employerTimetable.table, function(table){
return table.order == thisDay;
});
timetable = {name: employerTimetable.name, table: employerTask};
}
var newEmployer = _.clone(employer);
//newEmployer.id = uuid.v4();
newEmployer.timetable = timetable;
callback(null,newEmployer);
}, function(err, employers){
cb(err, employers);
});
},
/*Report*/
function(employers, cb){
async.map(employers, function(employer, callback){
var timetable = employer.timetable;
var employerTasks = [];
if(typeof timetable.table != 'undefined' && timetable.table.tasks.length > 0 && employer.attendance.length == 2){
var enterTask = false;
for(var i=0; i<timetable.table.tasks.length; i++){
var task = _.clone(timetable.table.tasks[i]);
var dateTaskTo = moment(task.to).tz("Asia/Ho_Chi_Minh").date(beginDay.date()).month(beginDay.month()).year(beginDay.year()).toDate();
var dateTaskFrom = moment(task.from).tz("Asia/Ho_Chi_Minh").date(beginDay.date()).month(beginDay.month()).year(beginDay.year()).toDate();
//sails.log.info(employer.username, dateTaskFrom, dateTaskTo,employer.attendance[0].createdAt,employer.attendance[1].createdAt);
if(employer.attendance[0].createdAt < dateTaskTo) {
if (employer.attendance[0].createdAt > dateTaskFrom) {
task.from = employer.attendance[0].createdAt;
}
else {
task.from = dateTaskFrom;
}
} else
continue;
if(employer.attendance[1].createdAt > dateTaskFrom){
if(employer.attendance[1].createdAt > dateTaskTo){
task.to = dateTaskTo;
} else {
task.to = employer.attendance[1].createdAt;
}
if(moment(task.to).diff(moment(task.from), 'minutes') >= 30){
var minutesFrom = moment(task.from).minutes();
minutesFrom = Math.round(minutesFrom/10)*10;
task.from = moment(task.from).minutes(minutesFrom).second(0);
var minutesTo = moment(task.to).minutes();
minutesTo = Math.round(minutesTo/10)*10;
task.to = moment(task.to).minutes(minutesTo).second(0);//.tz('Africa/Abidjan').format("YYYY-MM-DDTHH:mm:ss");
employerTasks.push(task);
}
}
}
}
var newEmployer = _.clone(employer);
newEmployer.employerTasks = employerTasks;
callback(null, newEmployer);
}, function(err, result){
cb(err, result);
});
},
/*create timeline data*/
function(employers, cb){
var order = 1;
var tables = _.map(employers, function(employer){
var table = {id: employer.id, description: employer.username, order: order++, tasks: employer.employerTasks, attendance: employer.attendance};
return table;
});
cb(null, employers, tables);
}
], function(err, employers, tables){
_callback(err, employers, tables);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment