Files
crm.e5.pl/modules/EcmWorkCards/js/EditViewDocumentReady6.js
2024-04-27 09:23:34 +02:00

563 lines
18 KiB
JavaScript

var tabAction = $('#tableEcmActions');
$(document).ready(function () {
//addToValidate('EditView','quantity','income_cycle_c',true,'');
addToValidate('EditView','date_from','income_cycle_c',true,'');
addToValidate('EditView','date_to','income_cycle_c',true,'');
$('#time_from').timepicker({ 'timeFormat': 'H:i:s','step':15 });
$('#time_to').timepicker({ 'timeFormat': 'H:i:s' ,'step':15});
$('#worker_name').attr('tabindex',201);
$('#date').attr('tabindex',202);
$('#product_name').attr('tabindex',203);
$('#action_name').attr('tabindex',204);
$('#time_from').attr('tabindex',205);
$('#time_to').attr('tabindex',206);
$('#quantity').attr('tabindex',207);
$('#description').attr('tabindex',208);
$('#worker_name').focus();
lockEnter();
$( "#product_name" ).autocomplete({
source: function (request, response)
{
$.ajax(
{
url: 'index.php',
dataType: "json",
data:
{
query: request.term,
to_pdf: 'true',
type: 'EcmProducts',
module: 'Home',
action: 'customSearch',
},
success: function (data)
{
response($.map(data.fields, function (item) {
return {
label: item.code,
value: item.code,
id: item.id,
};
}));
}
});
},
minLength: 2,
select: function( event, ui ) {
$('#product_id').val(ui.item.id);
}
});
$( "#action_name" ).autocomplete({
source: function (request, response)
{
$.ajax(
{
url: 'index.php',
dataType: "json",
data:
{
query: request.term,
to_pdf: 'true',
type: 'EcmActions',
module: 'Home',
action: 'customSearch',
},
success: function (data)
{
response($.map(data.fields, function (item) {
return {
label: item.name,
value: item.name,
id: item.id,
};
}));
}
});
},
minLength: 2,
select: function( event, ui ) {
$('#action_id').val(ui.item.id);
}
});
$(document).keypress(function(event) {
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '9') { //onEnter
var tabindex= $(this.activeElement).attr("tabindex");
if(tabindex==209){
$('#worker_name').select();
return false;
}
}
});
});
$["ui"]["autocomplete"].prototype["_renderItem"] = function (ul, item) {
return $("<li></li>").data("item.autocomplete", item).append(
$("<a></a>").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").attr('id') != 'save_and_next') {
event.preventDefault();
return false;
}
});
}