Shiftplan
by Abhishek Kumar
HTML
<div class="form">
<div>
<label>Start date</label>
<input type="date" id="startdate">
</div>
<div>
<label>End date</label>
<input type="date" id="enddate">
</div>
<button id="genshiftplan">Generate</button>
</div>
<div class="report">
<table id="shiftplan"></table>
</div>
CSS
table {
border-collapse: collapse;
}
table,
th,
td {
border: 1px solid black;
}
th,
td {
padding: 0 5px 0 5px;
font-family: Calibri;
}
label {
font-family: Calibri;
}
th {
color: #eee;
background-color: #333;
}
.form {
padding: 5px 5px;
}
.report {
padding: 5px 5px;
}
JavaScript
var team = {
primary: ['AK', 'PK', 'RG'],
secondary: ['AV', 'AG', 'KN', 'AGL'],
tertiary: ['RGC']
};
const holidays = [
'2018-12-24',
'2018-12-25',
'2019-01-26',
'2019-05-21',
'2019-08-15',
'2019-10-02'
];
var startDate, endDate;
const _MS_PER_DAY = 1000 * 60 * 60 * 24;
function dateDiffInDays(a, b) {
const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}
function stringify(n) {
return (((n > 9) ? '' : '0') + n);
}
function isHoliday(d) {
let bool = false;
let dstr = d.getFullYear() + '-' + stringify(d.getMonth() + 1) + '-' + stringify(d.getDate());
if (holidays.indexOf(dstr) >= 0) {
bool = true;
}
return bool;
}
function render(tbl) {
let row = [];
let colh = [];
for (let j in tbl[0]) {
colh.push(j);
}
row.push('<th>' + colh.join('</th><th>') + '</th>');
for (let i = 0; i < tbl.length; i++) {
let col = [];
for (let j in tbl[i]) {
var txt = (typeof tbl[i][j] == 'object') ? tbl[i][j].join(', ') : tbl[i][j];
col.push(txt);
}
row.push('<td>' + col.join('</td><td>') + '</td>');
}
$('#shiftplan').html('<tr>' + row.join('</tr><tr>') + '</tr>');
}
function genShiftPlan() {
startDate = new Date($('#startdate').val());
endDate = new Date($('#enddate').val());
let dayCount = dateDiffInDays(startDate, endDate);
console.log(dayCount)
let shiftDays = [],
shiftCount = 0;
for (let i = 0; i <= dayCount; i++) {
let shift = {
Day: 0,
A: [],
B: [],
C: [],
D: []
};
let shiftDay = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() + i);
shift.Day = shiftDay.toDateString();
if (shiftDay.getDay() != 0 && shiftDay.getDay() != 6 && !isHoliday(shiftDay)) {
shiftCount++;
shift.A.push('-');
shift.B.push(team.primary[shiftCount % team.primary.length]);
...