$(document).ready(function () { structureEdit(); InitializeTable(); //$("#account_type").attr("disabled", true); $("#account_code").keydown(function (e) { // Allow: backspace, delete, tab, escape, enter if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 || // Allow: Ctrl+A (e.keyCode == 65 && e.ctrlKey === true) || // Allow: home, end, left, right, down, up (e.keyCode >= 35 && e.keyCode <= 40)) { // let it happen, don't do anything return; } // Ensure that it is a number and stop the keypress if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) { e.preventDefault(); } }); $("#account_code").change(function (e) { var kod = $("#account_code").val(); if (kod.length == 1) { kod = '00' + kod; } if (kod.length == 2) { kod = '0' + kod; } if (kod.length > 3) { kod = kod.substring(0, 3) } $("#account_code").val(kod); var test; $.ajax({ type: "POST", url: "index.php?module=EcmSyntheticAccounts&action=helper&to_pdf=1", dataType: "json", async: false, data: { job: "checkCode", kod: kod, }, success: function (data) { test = data; } }); if (test) { $("#account_code").val(''); alert("Podany kod już istnieje"); } }); $("#EditView").submit(function (event) { odblokuj(); $('#tableAnalyticalAccounts').appendGrid('removeEmptyRows'); for (var row = 0; row < $("#tableAnalyticalAccounts").appendGrid('getRowCount'); row++) { value = $("#tableAnalyticalAccounts").appendGrid('getCtrlValue', 'code', row); for (var i = 0; i < $("#tableAnalyticalAccounts").appendGrid('getRowCount'); i++) { var cosik = $("#tableAnalyticalAccounts").appendGrid('getCtrlValue', 'code', i); if (cosik == value && row != i) { alert("Duplikat kodu w: " + (i + 1) + " i "+(row+1) +"\nProszę podać inny"); console.log($("#tableAnalyticalAccounts").appendGrid('getCtrlValue', 'code', i)); event.preventDefault(); zablokuj(); } } } }); }); function structureEdit() { var MaxInputs = 50; // maximum input boxes allowed var InputsWrapper = $("#InputsWrapper"); // Input boxes wrapper ID var AddButton = $("#AddMore"); // Add button ID var x = InputsWrapper.length; // initlal text box count var FieldCount = 1; // to keep track of text box added var structureanalyticalpart_list = $.parseJSON($("input[name='structureanalyticalpart_list']").val()); if (structureanalyticalpart_list != '[]') $.each(structureanalyticalpart_list, function (idx, obj) { // add existing elements FieldCount++; // text box added increment // add input box if (idx == 0) { // first element $("div input[name='structureanalyticalpart[]']").first().val(htmlspecialchars_decode(obj.value)); } else { // rest of elementsec $(InputsWrapper).append('\ \ \ \ \ \  \ \ ' ); } x++; // text box increment }); $(AddButton).click(function (e) { // on add input button click if (x <= MaxInputs) { // max input box allowed if (x == 1) { x = 2; } FieldCount++; // text box added increment // add input box $(InputsWrapper).append('\ \ \ \ \ \  '; return buttons; } function inputCode(event) { var field = event.target.name; var value = event.target.value; var struktura = new Array(); $("input[name='structureanalyticalpart[]']").each(function (indeks, obj) { struktura[indeks] = obj.value; }); if (value.length < 4) { var tmp2 = value; value = $("#account_code").val() + '-' + tmp2; } value = value.replace(/\s+/g, '-'); //value = value.replace(/\s+|\,+/g, ''); dla kilku wartosci var lista = value.split('-', struktura.length + 1); var tmp = $("#account_code").val(); for (var i = 1; i < lista.length; i++) { if (lista[i].length > struktura[i - 1]) { lista[i] = lista[i].substr(0, struktura[i - 1]); } tmp += '-' + lista[i]; } $("#" + field).val(tmp); } function codeChange(event, row) { var field = event.target.name; var value = event.target.value; var struktura = new Array(); $("input[name='structureanalyticalpart[]']").each(function (indeks, obj) { struktura[indeks] = obj.value; }); var lista = value.split('-', struktura.length + 1); var tmp = $("#account_code").val(); for (var i = 1; i <= struktura.length; i++) { if (lista[i] === undefined) { lista [i] = ''; } while (lista[i].length < struktura[i - 1]) { lista[i] = '0' + lista[i]; } tmp += '-' + lista[i]; } $("#" + field).val(tmp); for (var i = 0; i < $("#tableAnalyticalAccounts").appendGrid('getRowCount'); i++) { var cosik = $("#tableAnalyticalAccounts").appendGrid('getCtrlValue', 'code', i); if (cosik == value && row != i) { alert("Podany kod jest już używany w wierszu: " + (i + 1) + "\nProszę podać inny"); console.log($("#tableAnalyticalAccounts").appendGrid('getCtrlValue', 'code', i)); } } } function InitializeTable() { $('#tableAnalyticalAccounts').appendGrid({ hideRowNumColumn: true, initRows: 0, columns: [ { name: 'code', display: 'Konto', type: 'text', ctrlClass: 'inputs', onChange: function (evt, rowIndex) { codeChange(evt, rowIndex); }, displayCss: {'width': '30%', 'vertical-align': 'middle'} }, { name: 'name', display: 'Nazwa', type: 'text', ctrlClass: 'inputs', displayCss: {'width': '65%', 'vertical-align': 'middle'} }, ], afterRowAppended: function (caller, parentRowIndex, addedRowIndex) { zablokuj(); $("input[name^='tableAnalyticalAccounts_code_']").on('input', function (event) { inputCode(event) }); }, afterRowRemoved: function (caller, rowIndex) { //usunięcie ostatniego wiersza włącza edycje var count = $(caller).appendGrid('getRowCount'); if (count == 0) { odblokuj(); } }, i18n: { rowEmpty: 'Brak rekordów' }, customGridButtons: { // Use a function that create DOM element moveUp: function () { var button = document.createElement('img'); button.src = 'modules/EcmQuotes/images/moverowup.gif'; return button; }, moveDown: function () { var button = document.createElement('img'); button.src = 'modules/EcmQuotes/images/moverowdown.gif'; return button; }, removeLast: function () { var button = document.createElement('img'); button.src = 'modules/EcmQuotes/images/deleterow.gif'; return button; }, append: function () { var button = document.createElement('img'); button.src = 'modules/EcmQuotes/images/insertrow.gif'; return button; }, remove: function () { var button = document.createElement('img'); button.src = 'modules/EcmQuotes/images/deleterow.gif'; return button; }, }, hideButtons: { removeLast: true, // append: true, insert: true, // remove : true, moveUp: true, moveDown: true } }); LoadDataFromAnalyticalAccountsList(); $('thead').removeClass('ui-widget-header'); $('td').removeClass('ui-widget-header'); $('tbody').removeClass('ui-widget-content'); $('td').removeClass('ui-widget-content'); $('tfoot').removeClass('ui-widget-header'); } function LoadDataFromAnalyticalAccountsList() { var action_list = $.parseJSON($("input[name='analyticalaccounts_list']").val()); if (action_list != '[]') if (action_list.length > 0) { $.each(action_list, function (idx2, obj2) { $('#tableAnalyticalAccounts').appendGrid('insertRow', [{ code: obj2.code, name: obj2.name, }], 0); }); } } function zablokuj() { $("input[name='structureanalyticalpart[]']").attr("disabled", true); $("#accounts_balance").attr("disabled", true); $("#AddMore").attr("disabled", true); $("#account_type").attr("disabled", true); $("#account_code").attr("disabled", true); $("button[name='delbutton']").attr("disabled",true); } function odblokuj() { $("input[name='structureanalyticalpart[]']").attr("disabled", false); $("#accounts_balance").attr("disabled", false); $("#AddMore").attr("disabled", false); $("#account_type").attr("disabled", false); $("#account_code").attr("disabled", false); $("button[name='delbutton']").attr("disabled",false); } function htmlspecialchars_decode(string, quote_style) { // discuss at: http://phpjs.org/functions/htmlspecialchars_decode/ // original by: Mirek Slugen // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // bugfixed by: Mateusz "loonquawl" Zalega // bugfixed by: Onno Marsman // bugfixed by: Brett Zamir (http://brett-zamir.me) // bugfixed by: Brett Zamir (http://brett-zamir.me) // input by: ReverseSyntax // input by: Slawomir Kaniecki // input by: Scott Cariss // input by: Francois // input by: Ratheous // input by: Mailfaker (http://www.weedem.fr/) // revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // reimplemented by: Brett Zamir (http://brett-zamir.me) // example 1: htmlspecialchars_decode("

this -> "

", // 'ENT_NOQUOTES'); // returns 1: '

this -> "

' // example 2: htmlspecialchars_decode("&quot;"); // returns 2: '"' var optTemp = 0, i = 0, noquotes = false; if (typeof quote_style === 'undefined') { quote_style = 2; } string = string.toString() .replace(/</g, '<') .replace(/>/g, '>'); var OPTS = { 'ENT_NOQUOTES': 0, 'ENT_HTML_QUOTE_SINGLE': 1, 'ENT_HTML_QUOTE_DOUBLE': 2, 'ENT_COMPAT': 2, 'ENT_QUOTES': 3, 'ENT_IGNORE': 4 }; if (quote_style === 0) { noquotes = true; } if (typeof quote_style !== 'number') { // Allow for a single string or an // array of string flags quote_style = [].concat(quote_style); for (i = 0; i < quote_style.length; i++) { // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4 if (OPTS[quote_style[i]] === 0) { noquotes = true; } else if (OPTS[quote_style[i]]) { optTemp = optTemp | OPTS[quote_style[i]]; } } quote_style = optTemp; } if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) { string = string.replace(/�*39;/g, "'"); // PHP doesn't currently escape // if more than one 0, but it // should // string = string.replace(/'|�*27;/g, "'"); // This would also be // useful here, but not a part of PHP } if (!noquotes) { string = string.replace(/"/g, '"'); } // Put this in last place to avoid escape being double-decoded string = string.replace(/&/g, '&'); return string; }