var tabAction = $('#tableEcmActions'); $(document).ready(function () { InitializeTableEcmActions(); loadData(); //$("#tableEcmActions_list").val(''); $("#EditView").submit(function (event) { $('#tableEcmActions').appendGrid('removeEmptyRows'); var tmp = $('#tableEcmActions').appendGrid('getAllValue'); var tmp2 = JSON.stringifyNoSecurity(tmp); $("#tableEcmActions_list").val(utf8_to_b64(tmp2)); }); $('#tableEcmActions').enableCellNavigation(); lockEnter(); updateSum(); }); $["ui"]["autocomplete"].prototype["_renderItem"] = function (ul, item) { return $("
  • ").data("item.autocomplete", item).append( $("").html(item.label)).appendTo(ul); }; function InitializeTableEcmActions() { tabAction.appendGrid({ hideRowNumColumn: true, initRows: 1, columns: [ { name: 'code', display: 'Indeks', type: 'ui-autocomplete', ctrlClass: 'inputs', displayCss: {'width': '10%', 'vertical-align': 'middle'}, uiOption: { source: function (request, response) { getProductIndeks(request, response); }, minLength: 1, delay: 1500, select: function (event, ui) { if (ui.item.id != '') { var rowIndex = event.target.id.split("_").pop(); rowIndex = $('#tableEcmActions').appendGrid('getRowIndex', rowIndex) setProductActions(rowIndex, ui.item); } }, }, }, { name: 'ecmproductname', display: 'Nazwa produktu', type: 'ui-autocomplete', ctrlClass: 'inputs', displayCss: {'width': '32%', 'vertical-align': 'middle'}, uiOption: { source: function (request, response) { getProductName(request, response); }, delay: 1500, minLength: 1, select: function (event, ui) { if (ui.item.id != '') { var rowIndex = event.target.id.split("_").pop(); rowIndex = $('#tableEcmActions').appendGrid('getRowIndex', rowIndex) setProductActions(rowIndex, ui.item); } }, }, }, { name: 'ecmaction', display: 'Indeks - Nazwa czynności', type: 'select', ctrlOptions: {0: 'Wyszukaj produkt'}, ctrlAttr: {disabled: 'disabled'}, ctrlClass: 'inputs', displayCss: {'width': '32%', 'vertical-align': 'middle'}, emptyCriteria: function (value) { return (value <= 0); }, onChange: function (evt, rowIndex) { selectChange(evt, rowIndex); }, }, { name: 'quantity', display: 'Ilość', type: 'text', ctrlClass: 'inputs', value: '0', ctrlCss: {'text-align': 'right'}, onChange: function (evt, rowIndex) { var quantity = (isNaN(parseFloat(evt.target.value))) ? '0' : parseFloat(evt.target.value); var costvalue = $('#tableEcmActions').appendGrid('getCtrlValue', 'ecmactioncost', rowIndex); var bruttovalue = $('#tableEcmActions').appendGrid('getCtrlValue', 'costbrutto', rowIndex); $('#tableEcmActions').appendGrid('setCtrlValue', 'quantity', rowIndex, quantity); $('#tableEcmActions').appendGrid('setCtrlValue', 'ecmactioncost_display', rowIndex, FormatNumber(quantity * costvalue)); $('#tableEcmActions').appendGrid('setCtrlValue', 'costbrutto_display', rowIndex, FormatNumber(quantity * bruttovalue)); updateSum(evt, rowIndex); }, displayCss: {'width': '50px', 'vertical-align': 'middle'}, emptyCriteria: function (value) { // A value lesser than zero will consider as empty. return (value <= 0); } }, { name: 'ecmactioncost_single_netto', type: 'text', ctrlClass: 'inputs', display: 'Netto', ctrlCss: {'text-align': 'right'}, ctrlAttr: {readonly: 'readonly', 'disabled': 'disabled'}, displayCss: {'width': '40px', 'vertical-align': 'middle'}, invisible: true, },{ name: 'ecmactioncost_single_brutto', type: 'text', ctrlClass: 'inputs', display: 'Brutto', ctrlCss: {'text-align': 'right'}, ctrlAttr: {readonly: 'readonly', 'disabled': 'disabled'}, displayCss: {'width': '40px', 'vertical-align': 'middle'}, invisible: true, }, { name: 'ecmactioncost_display', display: 'Wartość netto', type: 'text', ctrlClass: 'inputs', value: '0,00', ctrlCss: {'text-align': 'right'}, ctrlAttr: {readonly: 'readonly', 'disabled': 'disabled'}, displayCss: {'width': '90px', 'vertical-align': 'middle'}, }, { name: 'costbrutto_display', display: 'Wartość brutto', type: 'text', ctrlClass: 'inputs', value: '0,00', invisible: false, ctrlCss: {'text-align': 'right'}, ctrlAttr: {readonly: 'readonly', 'disabled': 'disabled'}, displayCss: {'width': '90px', 'vertical-align': 'middle'}, }, { name: 'ecmproductid', type: 'text', display: 'ecmproductid', invisible: true, }, { name: 'ecmactioncost', type: 'text', display: 'ecmactioncost', invisible: true, }, { name: 'costbrutto', type: 'text', display: 'costbrutto', invisible: true, } ], i18n: { rowEmpty: 'Brak rekordów', append: 'Dodaj rekord', insert: 'Skopiuj rekord', remove: 'Usuń rekord', }, customGridButtons: { insert: function () { var button = document.createElement('img'); button.src = 'modules/EcmQuotes/images/add_position.gif'; return button; }, 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, } }); $('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 getProductName(request, response) { $.ajax({ type: "POST", url: "index.php?module=EcmWorkCards&action=dbpost&to_pdf=1", dataType: "json", async: false, data: { job: "ecmProductNameAutocomplite", name: request.term, }, success: function (data) { response(data); } }); } function getProductIndeks(request, response) { $.ajax({ type: "POST", url: "index.php?module=EcmWorkCards&action=dbpost&to_pdf=1", dataType: "json", async: false, data: { job: "ecmProductIndeksAutocomplite", indeks: request.term, }, success: function (data) { response(data); } }); } function selectChange(evt, rowIndex) { var jr = $('#' + evt.target.id + '').find(":selected").val(); var cost = ''; $.ajax({ type: "POST", url: "index.php?module=EcmWorkCards&action=dbpost&to_pdf=1", dataType: "json", async: false, data: { job: "getEcmActionsCost", id: jr, }, success: function (data) { cost = data; } }); tabAction.appendGrid('setCtrlValue', 'ecmactioncost', rowIndex, cost['cost_other']); tabAction.appendGrid('setCtrlValue', 'costbrutto', rowIndex, cost['cost_action']); var quantity = $('#tableEcmActions').appendGrid('getCtrlValue', 'quantity', rowIndex); quantity = parseFloat(quantity); tabAction.appendGrid('setCtrlValue', 'ecmactioncost_display', rowIndex, FormatNumber(quantity * cost)); } function setProductActions(rowIndex, item) { var czynnosci = ''; $.ajax({ type: "POST", url: "index.php?module=EcmWorkCards&action=dbpost&to_pdf=1", dataType: "json", async: false, data: { job: "getProductActions", id: item.id, }, success: function (data) { czynnosci = data; } }); if (czynnosci.length > 0) { $.each(czynnosci, function (key, value) { tabAction.appendGrid('setCtrlValue', 'ecmproductid', rowIndex, item.id); tabAction.appendGrid('setCtrlValue', 'ecmproductname', rowIndex, item.ecmproductname); tabAction.appendGrid('setCtrlValue', 'code', rowIndex, item.code); tabAction.appendGrid('setCtrlValue', 'ecmactioncost', rowIndex, value.ecmactioncost); tabAction.appendGrid('setCtrlValue', 'costbrutto', rowIndex, value.costbrutto); tabAction.appendGrid('setCtrlValue', 'ecmactioncost_single_netto', rowIndex, FormatNumber(value.ecmactioncost)); tabAction.appendGrid('setCtrlValue', 'ecmactioncost_single_brutto', rowIndex, FormatNumber(value.costbrutto)); var selekt = tabAction.appendGrid('getCellCtrl', 'ecmaction', rowIndex); selekt.options.length = 1; $.each(czynnosci, function (key2, value2) { if (value.id != value2.id) { selekt.options[key2] = new Option(value2.name, value2.id); } else { selekt.options[key2] = new Option(value2.name, value2.id, true, true); } }); selekt.disabled = false; if (key != czynnosci.length - 1) { tabAction.appendGrid('insertRow', 1, rowIndex); } }); } else { tabAction.appendGrid('setCtrlValue', 'ecmproductid', rowIndex, item.id); tabAction.appendGrid('setCtrlValue', 'ecmproductname', rowIndex, item.ecmproductname); tabAction.appendGrid('setCtrlValue', 'code', rowIndex, item.code); tabAction.appendGrid('setCtrlValue', 'ecmactioncost', rowIndex, item.ecmactioncost); tabAction.appendGrid('setCtrlValue', 'costbrutto', rowIndex, item.costbrutto); tabAction.appendGrid('setCtrlValue', 'ecmactioncost_single_netto', rowIndex, FormatNumber(item.ecmactioncost)); tabAction.appendGrid('setCtrlValue', 'ecmactioncost_single_brutto', rowIndex, FormatNumber(item.costbrutto)); var selekt = tabAction.appendGrid('getCellCtrl', 'ecmaction', rowIndex); if (selekt != null) { selekt.options.length = 1; selekt.options[0] = new Option('Brak przypisanych czynności', 0); selekt.disabled = true; } } tabAction.appendGrid('removeEmptyRows'); tabAction.appendGrid('appendRow', 1); $("input[type='text']").on("focus", function () { $(this).select(); }); tabAction.appendGrid('getCellCtrl', 'quantity', rowIndex).focus(); } function loadData() { var action_list = $.parseJSON($("input[name='tableEcmActions_list']").val()); if(action_list != '[]'){ action_list = b64_to_utf8(action_list); action_list = $.parseJSON(action_list); } if (action_list.length > 0 && action_list != '[]') { $.each(action_list, function (idx2, obj2) { tabAction.appendGrid('insertRow', [{ ecmproductid: obj2.ecmproductid, quantity: obj2.quantity, ecmproductname: obj2.ecmproductname, code: obj2.code, ecmactioncost: obj2.ecmactioncost, ecmactioncost_display: obj2.ecmactioncost_display, costbrutto: obj2.costbrutto, costbrutto_display: obj2.costbrutto_display, ecmactioncost_single_netto: obj2.ecmactioncost_single_netto, ecmactioncost_single_brutto: obj2.ecmactioncost_single_brutto, }], 0); setDisabled(0); var tmp; var elem2 = tabAction.appendGrid('getCellCtrl', 'ecmaction', 0); $.ajax({ type: "POST", url: "index.php?module=EcmWorkCards&action=dbpost&to_pdf=1", dataType: "json", async: false, data: { job: "getProductActionsLoad", id: obj2.ecmproductid, }, success: function (data) { tmp = data; } }); //wstawianie do selecta elem2.options.length = 1; elem2.options[0] = new Option('Brak przypisanych czynności', 0); if (0 < tmp.length) { // Generate options for elem2 $.each(tmp, function (key, value) { if (value.id != obj2.ecmaction) { elem2.options[key] = new Option(value.name, value.id); } else { elem2.options[key] = new Option(value.name, value.id, true, true); } }); elem2.disabled = true; } else { elem2.disabled = true; } }); } } function updateSum(evt, rowIndex) { var listanetto = $('input[name^=tableEcmActions_ecmactioncost_display_]'); var listabrutto = $('input[name^=tableEcmActions_costbrutto_display_]'); var sumanetto = 0; var sumabrutto = 0; $.each(listanetto, function (key, value) { var tmp = $('#' + value.id); sumanetto = sumanetto + UnformatNumber(tmp.val()); }); $.each(listabrutto, function (key, value) { var tmp = $('#' + value.id); sumabrutto = sumabrutto + UnformatNumber(tmp.val()); }); $('#sum_netto').text(FormatNumber(sumanetto)); $('#sum_brutto').text(FormatNumber(sumabrutto)); } function setDisabled(id) { var rekord = tabAction.appendGrid('getCellCtrl', 'code', id); var jr = $('#' + rekord.id + ''); jr.attr("disabled", "disabled"); rekord = tabAction.appendGrid('getCellCtrl', 'ecmproductname', id); jr = $('#' + rekord.id + ''); jr.attr("disabled", "disabled"); } function showHideSingleCost(){ var txt = $('#showPrice').val(); if(txt == "Pokaż jednostkowe ceny"){ $('#showPrice').val("Schowaj jednostkowe ceny"); tabAction.appendGrid('showColumn', 'ecmactioncost_single_netto'); tabAction.appendGrid('showColumn', 'ecmactioncost_single_brutto'); }else{ $('#showPrice').val("Pokaż jednostkowe ceny"); tabAction.appendGrid('hideColumn', 'ecmactioncost_single_netto'); tabAction.appendGrid('hideColumn', 'ecmactioncost_single_brutto'); } } function utf8_to_b64(str) { return window.btoa(unescape(encodeURIComponent(str))); } function b64_to_utf8(str) { return decodeURIComponent(escape(window.atob(str))); } 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 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; } function UnformatNumber(number) { // make string.. number = number + ''; // remove 1000 sep number = number.replace(/\./g, ''); // change ',' to '.' number = number.replace(',', '.'); return parseFloat(number); } function lockEnter() { // prevent default $(window).keydown(function (event) { if (event.keyCode == 13 && $(":focus").prop('tagName') != 'input') { event.preventDefault(); return false; } }); }