kods
Paloma.controller('CRM/AGGIRScales', {
new: function() {
this._setupCalculator();
},
create: function() {
this._setupCalculator();
},
edit: function() {
this._setupCalculator();
},
update: function() {
this._setupCalculator();
},
_setupCalculator: function() {
function Group(modeC, modeB, rankFunc) {
this.C = modeC;
this.B = modeB;
this.rankFunc = rankFunc;
}
Group.prototype = {
calculateRank: function(values) {
var total = this._calculateTotal(values);
return this.rankFunc(total);
},
_calculateTotal: function(values) {
var l = values.length,
total = 0,
i = 0,
currentMode;
for(; i < l; i++) {
currentMode = values[i];
if(this.hasOwnProperty(currentMode)) {
total += this[currentMode][i];
}
}
return total;
}
}
GroupA = new Group(
[2000, 1200, 40, 40, 60, 100, 800, 200, 0, 0],
[0, 0, 16, 16, 20, 16, 120, 32, 0, 0],
function(value) {
if(value >= 4380) { return 1; }
if(value >= 4140) { return 2; }
if(value >= 3390) { return 3; }
return 0;
}
)
GroupB = new Group(
[1500, 1200, 40, 40, 60, 100, 800, -80, 0, 0],
[320, 120, 16, 16, 0, 16, 120, -40, 0, 0],
function(value) {
if(value >= 2016) { return 4; }
return 0;
}
);
GroupC = new Group(
[0, 0, 40, 40, 60, 160, 1000, 400, 0, 0],
[0, 0, 16, 16, 20, 20, 200, 40, 0, 0],
function(value) {
if(value >= 1700) { return 5; }
if(value >= 1432) { return 6; }
return 0;
}
);
GroupD = new Group(
[0, 0, 0, 0, 2000, 400, 2000, 200, 0, 0],
[0, 0, 0, 0, 200, 200, 200, 0, 0, 0],
function(value) {
if(value >= 2400) { return 7; }
return 0;
}
);
GroupE = new Group(
[400, 400, 400, 400, 400, 800, 800, 200, 0, 0],
[0, 0, 100, 100, 100, 100, 100, 0, 0, 0],
function(value) {
if(value >= 1200) { return 8; }
return 0;
}
);
GroupF = new Group(
[200, 200, 500, 500, 500, 500, 500, 200, 0, 0],
[100, 100, 100, 100, 100, 100, 100, 0, 0, 0],
function(value) {
if(value >= 800) { return 9; }
return 0;
}
);
GroupG = new Group(
[150, 150, 300, 300, 500, 500, 400, 200, 0, 0],
[0, 0, 200, 200, 200, 200, 200, 100, 0, 0],
function(value) {
if(value >= 650) { return 10; }
return 0;
}
);
GroupH = new Group(
[0, 0, 3000, 3000, 3000, 3000, 1000, 1000, 0, 0],
[0, 0, 2000, 2000, 2000, 2000, 2000, 1000, 0, 0],
function(value) {
if(value >= 4000) { return 11; }
if(value >= 2000) { return 12; }
return 13;
}
);
function calculate() {
var inputNames = [
'consistency', 'orientation', 'toilet',
'dressing', 'supply', 'elimination',
'transfer', 'internal_displacement'
];
var ranks = [0,1,2,2,2,2,2,2,3,3,4,4,5,6],
values = [],
currentValue;
$.each(inputNames, function(i, name) {
currentValue = $("input[name='resident_scale_aggir[" + name + "]']:checked").val();
if (currentValue != '' && currentValue != null) {
values.push(currentValue);
} else {
values.push("A");
}
});
var groups = [GroupA, GroupB, GroupC, GroupD, GroupE, GroupE, GroupF, GroupG, GroupH],
i = 0,
l = groups.length,
rank;
for(; i < l; i++) {
rank = groups[i].calculateRank(values);
if (rank > 0) {
updateGIR(ranks[rank]);
break;
}
}
};
function bindCriteria(from, to, valueFunc) {
$(document).on('change', from, function() {
var result = [];
$(from).filter(":checked").each(function(i, el) {
result.push($(el).val());
});
$(to).val([valueFunc(result.join(""))]);
});
$(document).on('change', to, function() {
var selected = $(to).filter(":checked").val();
$(from).val([selected]);
});
};
function updateGIR(newValue) {
$('#gir').html(newValue);
$('#resident_scale_aggir_gir').val(newValue);
}
$(document).ready(function() {
bindCriteria(
"input[name='resident_scale_aggir[toilet_top]'], input[name='resident_scale_aggir[toilet_bottom]']",
"input[name='resident_scale_aggir[toilet]']",
function(val) {
switch(val) {
case "AA": return "A"
case "CC": return "C"
default: return "B"
}
}
);
bindCriteria(
"input[name='resident_scale_aggir[dressing_top]'], input[name='resident_scale_aggir[dressing_middle]'], input[name='resident_scale_aggir[dressing_bottom]']",
"input[name='resident_scale_aggir[dressing]']",
function(val) {
switch(val) {
case "AAA": return "A"
case "CCC": return "C"
default: return "B"
}
}
);
bindCriteria(
"input[name='resident_scale_aggir[supply_serving]'], input[name='resident_scale_aggir[supply_food]']",
"input[name='resident_scale_aggir[supply]']",
function(val) {
switch(val) {
case "AA": return "A"
case "CC": return "C"
case "CB": return "C"
case "BC": return "C"
default: return "B"
}
}
);
bindCriteria(
"input[name='resident_scale_aggir[elimination_urination]'], input[name='resident_scale_aggir[elimination_feces]']",
"input[name='resident_scale_aggir[elimination]']",
function(val) {
switch(true) {
case val === "AA": return "A"
case /C/.test(val): return "C"
default: return "B"
}
}
);
$(document).on("change", ":radio", function() {
calculate();
});
$(document).on("click", "#reset-aggir-scale", function(e) {
e.preventDefault();
$(":radio").val(["A"]);
updateGIR(6);
});
$(document).on("change", ".resident-select2", function(e) {
$.get("/resident_scale_aggirs/calculate.js", {
resident_scale_aggir: {resident_id: e.val}
});
});
$(document).on("click", "#save-aggir-scale", function(e) {
if ($('#resident_scale_aggir_resident_id').val() === "") {
e.preventDefault();
new PNotify({
text: I18n.t('views.crm.aggir_scales.you_have_not_selected_resident'),
addclass: 'bg-warning'
});
}
});
});
}
});