var startCat; var startIndex; var cost_netto = 764; var cost_netto_s = cost_netto/3600; // koszt netto / 1h = koszt na sekunde $(document).ready(function () { initializeTime(); if($('#cost_hour').val()==''){ $('#cost_hour').val("15,20"); } //$('#cost_hour').attr('disabled','disabled'); startCat = $('#category').find(":selected").val(); startIndex = $('#indeks').val(); if ($('#indeks').val() == '') { categoryChange(); } $("#cost_action").change(function () { cost_actionChange(); }); $("#cost_hour").change(function () { cost_hourChange(); }); $('#category').change(function () { categoryChange(); }); $('#indeks').change(function () { indeksChange(); }); $('#cost_other').change(function () { cost_otherChange(); }); $("#EditView").submit(function (event) { indeksChek(); }); }); function initializeTime() { var time = parseInt($("#time").val()); if (isNaN(time)) { time = 0; $("#time").val(0); } var hours = 0; var minutes = 0; var seconds = 0; minutes = Math.floor(time / 60); hours = Math.floor(minutes / 60); seconds = time - (minutes * 60); minutes = minutes - (hours * 60); $("#seconds").val(seconds); $("#minutes").val(minutes); $("#hours").val(hours); } function secondsChange() { var tmp = parseInt($("#seconds").val()); if (isNaN(tmp)) { tmp = 0; } if (tmp < 0) { tmp = 0; } if (tmp > 59) { tmp = 59; } $("#seconds").val(tmp); updateTime(); } function minutesChange() { var tmp = parseInt($("#minutes").val()); if (isNaN(tmp)) { tmp = 0; } if (tmp < 0) { tmp = 0; } if (tmp > 59) { tmp = 59; } $("#minutes").val(tmp); updateTime(); } function hoursChange() { var tmp = parseInt($("#hours").val()); if (isNaN(tmp)) { tmp = 0; } if (tmp < 0) { tmp = 0; } $("#hours").val(tmp); updateTime(); } function cost_actionChange() { var tmp = parseFloat($("#cost_action").val().replace(",", ".")); if (isNaN(tmp) || tmp == 'NaN,NaN') { tmp = 0; } $("#cost_action").val(FormatNumber(tmp)); var cost_hour = UnformatNumber($("#cost_hour").val()); var proporcja = tmp/cost_hour; var cost_other = (proporcja*cost_netto/100).toFixed(2); $("#cost_other").val(FormatNumber(cost_other)); time = Math.round(3600*proporcja); $('#time').val(time); initializeTime(); } function cost_hourChange() { var tmp = parseFloat($("#cost_hour").val().replace(",", ".")); if (isNaN(tmp) || tmp == 'NaN,NaN') { tmp = 0; } $("#cost_hour").val(FormatNumber(tmp)); } function cost_otherChange() { var tmp = parseFloat($("#cost_other").val().replace(",", ".")); if (isNaN(tmp) || tmp == 'NaN,NaN') { tmp = 0; } $("#cost_other").val(FormatNumber(tmp)); //var time = $("#time").val(); var proporcja = tmp/cost_netto; time = Math.round(3600*proporcja*100); $('#time').val(time); initializeTime(); var cost_hour = UnformatNumber($("#cost_hour").val())*100; var cost_action = (cost_hour*proporcja).toFixed(2); $("#cost_action").val(FormatNumber(cost_action)); } function categoryChange() { var category = $('#category').find(":selected").val(); console.log(category); if (startIndex == '') { var sufix; $.ajax({ type: "POST", url: "index.php?module=EcmActions&action=indeksIncrement&to_pdf=1", dataType: "json", async: false, data: { job: "getIndeks", category: category, }, success: function (data) { sufix = data; } }); var text = $('#category').find(":selected").text(); var prefix = ''; if (text.indexOf(' ') === -1) { prefix = text.substring(0, 5).toUpperCase(); } else { prefix = text.substring(0, 2).toUpperCase(); prefix = prefix + text.substring(text.indexOf(' ') + 1, text.indexOf(' ') + 4) .toUpperCase(); } var indeks = prefix + sufix; $("#indeks").val(indeks); }else{ if(category==startCat){ $("#indeks").val(startIndex); }else{ var sufix; $.ajax({ type: "POST", url: "index.php?module=EcmActions&action=indeksIncrement&to_pdf=1", dataType: "json", async: false, data: { job: "getIndeks", category: category, }, success: function (data) { sufix = data; } }); var text = $('#category').find(":selected").text(); var prefix = ''; if (text.indexOf(' ') === -1) { prefix = text.substring(0, 5).toUpperCase(); } else { prefix = text.substring(0, 2).toUpperCase(); prefix = prefix + text.substring(text.indexOf(' ') + 1, text.indexOf(' ') + 4) .toUpperCase(); } var indeks = prefix + sufix; $("#indeks").val(indeks); } } } function indeksChange() { var checkindeks = $.trim($("#indeks").val()).toUpperCase(); if (checkindeks.length > 0) { if (checkindeks.length > 10) { checkindeks = checkindeks.substring(0, 10); } $("#indeks").val(checkindeks); var condition = false; var category = $('#category').find(":selected").val(); $.ajax({ type: "POST", url: "index.php?module=EcmActions&action=indeksIncrement&to_pdf=1", dataType: "json", async: false, data: { job: "checkIndeks", indeks: checkindeks, category: category, }, success: function (data) { condition = data; } }); if (condition) { alert("Podany indeks dla danej kategorii już istnieje"); $("#indeks").val(''); } } else { alert("Indeks nie może być pusty"); } } function indeksChek(){ var checkindeks = $.trim($("#indeks").val()).toUpperCase(); if (checkindeks.length > 0) { if (checkindeks.length > 10) { checkindeks = checkindeks.substring(0, 10); } $("#indeks").val(checkindeks); var condition = false; var category = $('#category').find(":selected").val(); $.ajax({ type: "POST", url: "index.php?module=EcmActions&action=indeksIncrement&to_pdf=1", dataType: "json", async: false, data: { job: "checkIndeks", indeks: checkindeks, category: category, }, success: function (data) { condition = data; } }); if (condition) { categoryChange(); } } else { alert("Indeks nie może być pusty"); } } function updateTime() { var hours = parseInt($("#hours").val()) * 3600; var minutes = parseInt($("#minutes").val()) * 60; var seconds = parseInt($("#seconds").val()); var tmp = hours + minutes + seconds; $("#time").val(tmp); var time = tmp; var cost_hour = UnformatNumber($("#cost_hour").val())*100; if (isNaN(cost_hour) || cost_hour == 'NaN,NaN') { cost_hour = 0; } var cost_hour_h = parseFloat(cost_hour / 3600); //*time + cost_netto_s*time if (isNaN(cost_hour_h)) { cost_hour_h = 0; } var tmp = ((cost_hour_h*time)/100).toFixed(2); $("#cost_action").val(FormatNumber(tmp)); var tmp2 = (((cost_netto_s*time)/100)).toFixed(2); $("#cost_other").val(FormatNumber(tmp2)); } function setCost_Hour() { var tmp = $("#time").val(); var cost_action = UnformatNumber($("#cost_action").val()); console.log($("#cost_action").val()); console.log(cost_action); console.log(tmp); if (isNaN(cost_action) || cost_action == 'NaN,NaN') { cost_action = 0; } var value = parseFloat((3600 / tmp) * cost_action); if (isNaN(value) || value == 'Infinity') { value = 0; $("#cost_hour").val("Nieprawidłowa wartość czasu"); } else { $("#cost_hour").val(FormatNumber(value)); } } function setCost_Action() { var time = $("#time").val(); var cost_hour = UnformatNumber($("#cost_hour").val())*100; if (isNaN(cost_hour) || cost_hour == 'NaN,NaN') { cost_hour = 0; } var cost_hour_h = parseFloat(cost_hour / 3600); //*time + cost_netto_s*time if (isNaN(cost_hour_h)) { cost_hour_h = 0; } var tmp = ((cost_hour_h*time)/100).toFixed(2); $("#cost_action").val(FormatNumber(tmp)); } function setCost_Other() { var time = $("#time").val(); var cost_other = UnformatNumber($("#cost_other").val())*100; var tmp2 = (((cost_netto_s*time)/100)).toFixed(2); $("#cost_other").val(FormatNumber(tmp2)); } function FormatNumber(number, precision) { var precision = precision || 2; // make string.. number = number + ''; number = number.replace(',', '.'); // round number = toFixed(number, precision); // add 1000 sep var tmp = number.split("."); var c = ''; for (var i = tmp[0].length; i != -1; i--) { c += tmp[0].charAt(i); if ((tmp[0].length - i) == 0 || i == 0) continue; if ((tmp[0].length - i) % 3 == 0) c += '.'; } // reverse c c = c.split("").reverse().join(""); return c + ',' + tmp[1]; } function UnformatNumber(number) { // make string.. number = number + ''; // remove 1000 sep number = number.replace(/\./g, ''); // change ',' to '.' number = number.replace(',', '.'); return parseFloat(number); } // round with precision function toFixed(value, precision) { var precision = precision || 0, neg = value < 0, power = Math.pow(10, precision), value = Math.round(value * power), integral = String((neg ? Math.ceil : Math.floor)(value / power)), fraction = String((neg ? -value : value) % power), padding = new Array(Math.max(precision - fraction.length, 0) + 1).join('0'); //fix problem with Math.floor and Math.ceil with result zero (lose sign) if (neg && integral=="0") integral='-'+integral; return precision ? integral + '.' + padding + fraction : integral; }