var itemsTable = 'itemsTable';
var items = new Array();
var displayArray = new Array();
var searchedProducts = new Array();
var ajax_url = "index.php?module=EcmInsideOrders&action=javahelper&to_pdf=1";
function FillTable(data, allReadOnly) {
html = '';
// loop throw data rows
var comp_count = 0;
var color;
$.each(
data,
function (row_index, row) {
if (row.product_component == 0) {
comp_count++;
color = '#D4D9DB';
} else {
color = '#ffffff';
}
html += '
';
//alert(row_index);
//alert(comp_count);
// and insert columns
$
.each(
columns,
function (col_index, column) {
// row must have code and name
if (!row.product_code
|| row.product_code == ''
|| !row.name
|| row.name == '')
return; // return in each =
// continue in php for
// loop
// special types
if (column.name == 'number') {
html += '
';
} else if (column.name == 'name') {
html += '
';
} else if (column.name == 'options') {
if (allReadOnly)
html += '
';
else {
html += '
';
// move up
html += '';
html += ' ';
// move down
html += '';
html += ' ';
// delete row
html += '';
html += ' ';
// get components
html += '';
html += '
';
}
}
// other types
else {
html += '
';
$
.each(
column.content,
function (
cell_index,
cell) {
var cellname = column.name
+ cell.name;
if (cellname == 'product_link') {
html += ''
+ row.product_code
+ '';
} else if (cellname == 'price_start_div'
&& !allReadOnly) {
html += ' ';
html += '';
} else {
if (cell.label
&& cell.label != '')
html += '
'
+ cell.label
+ '
';
html += '';
}
});
html += '
';
}
});
html += '';
});
$('#' + itemsTable + '_T > tbody').html(html);
if (allReadOnly) {
DrawDetailSummary();
}
}
function deleteRow(index) {
var count = $('#' + itemsTable + '_T tr').length - 1; // -1 - thead row
var error = false; // hope :)
var id = $("#product_id_" + index).val();
var howmany = 1;
for (var row = index+1; row != count; row++) {
comp_id = $("#product_product_id_" + row).val();
if (comp_id == id) howmany++;
}
items.splice(index, howmany);
FillTable(items);
}
function moveUpRow(index) {
if (index == 0)
return; // Can't go upper.. :(
var new_index = index - 1;
var old_index = index;
if (new_index >= items.length) {
var k = new_index - items.length;
while ((k--) + 1) {
items.push(undefined);
}
}
items.splice(new_index, 0, items.splice(old_index, 1)[0]);
FillTable(items);
}
function moveDownRow(index) {
if (index == items.length - 1)
return; // Can't go lower.. :(
var new_index = index + 1;
var old_index = index;
if (new_index >= items.length) {
var k = new_index - items.length;
while ((k--) + 1) {
items.push(undefined);
}
}
items.splice(new_index, 0, items.splice(old_index, 1)[0]);
FillTable(items);
}
function FormatNumber(number, precision) {
if (number < 0)
number = 0;
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('.', '');
// 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');
return precision ? integral + '.' + padding + fraction : integral;
}
// draw table
function DrawHeaders() {
var html = '';
html += '
';
html += '
';
html += '';
html += '
';
// draw columns headers
$.each(columns, function (index, column) {
html += '
' + column.label + '
';
});
html += '
';
html += '
';
// totals table
html += '
';
html += '
';
html += '
';
html += ' ';
html += '
';
html += '
';
html += ' ';
html += '
';
html += '
';
html += '
';
html += '
';
html += '
';
html += '
';
// sort? why not!
/*
* html += 'Sortowanie: '; html += '';
*/
$('#' + itemsTable).html(html);
}
function searchProducts() {
var searchKey = $('#searchProductsInput').val();
if (searchKey.length < 2 && searchKey != '%') {
return;
}
$("#searchResultDiv").html(
'');
setTimeout(function () {
if (searchKey == $('#searchProductsInput').val()) {
var params = {
job: 'searchProducts',
searchKey: searchKey,
searchCategory: $("#productSearchCategory :selected").val(),
searchStock: $("#productSearchStock :selected").val(),
searchSort: $("#productSearchSort :selected").val(),
searchStockId: $("#stock_out_id :selected").val(),
};
$.ajax({
type: "POST",
url: ajax_url,
dataType: "json",
success: function (data) {
if (data != '-1')
if (data.length == 0)
$('#searchResultDiv').html(
'MOD.LBL_SEARCH_NO_RESULT');
else
createSearchResult(data);
},
data: params
});
}
}, 1000);
}
function createSearchResult(data) {
var html = '
';
html += '
';
html += '
 
';
html += '
Kod
';
html += '
Nazwa
';
if ($("#productSearchStock :selected").val() != '1')
html += '
Stan
';
html += '
 
';
html += '
';
var counter;
$
.each(
data,
function (index, value) {
if (counter % 2 == 0) {
html += '
';
} else {
html += '
';
}
html += '
';
html += '
'
+ value.code + '
';
html += '
'
+ value.name + '
';
if ($("#productSearchStock :selected").val() != '1')
html += '
'
+ value.stock_state + '
';
html += '
';
html += '';
html += '';
html += '
';
html += '
';
searchedProducts.push(value.id);
counter++;
});
// add button
html += '
';
html += 'Cena:  Ilość: ';
html += '
';
$('#searchResultDiv').html(html);
}
function unsetAllCheckboxes() {
$("#searchResultDiv input[type=checkbox]").each(function () {
$(this).prop("checked", false);
});
}
function searchIn(id) {
var error = 0;
for (var item in items) {
if (items[item].product_id == id) {
error = 1
}
}
return error;
}
function addProducts() {
var i = 0;
$(".loading_panel").css("display", "block");
var products = new Array();
$.each(searchedProducts, function (index, value) {
if ($('#prod_' + value).is(':checked'))
products[value] = true;
});
for (var key in products) {
if (searchIn(key) == 0) {
var params = {
job: 'getProduct',
id: key,
stockId: $("#pw_stock_id").val()
};
$.ajax({
type: "POST",
url: ajax_url,
dataType: "json",
async: false,
success: function (data) {
if (data != '-1') {
var p = new Object();
p.product_id = data.id;
p.name = data.name;
p.product_code = data.code;
p.unit_name = data.unit_name;
p.unit_id = data.unit_id;
p.stock_state = data.stock_state;
p.product_category_id = data.product_category_id;
p.product_component = '0';
p.product_product_id = '';
if ($("#searchInputQty").val() != ''
&& !isNaN(parseFloat(UnformatNumber($(
"#searchInputQty").val()))))
p.quantity = $("#searchInputQty").val();
items.push(p);
i++;
}
},
data: params
});
var params = {
job: 'getComponents',
product_id: key,
};
$.ajax({
type: "POST",
url: ajax_url,
dataType: "json",
async: false,
success: function (data) {
$.each(data, function (id, value) {
var a = new Object();
a.product_id = value.id;
a.name = value.name;
a.product_code = value.code;
a.unit_name = value.unit_name;
a.unit_id = value.unit_id;
a.stock_state = refreshStock2(value.id, 1);
a.product_category_id = value.product_category_id;
if ($("#searchInputQty").val() != ''
&& !isNaN(parseFloat(UnformatNumber($(
"#searchInputQty").val()))))
a.quantity = value.quantity * $("#searchInputQty").val();
a.quantity_comp = value.quantity;
a.unit_id = value.unit_id;
a.unit_name = value.unit_name;
a.product_product_id = key;
a.product_component = '1';
items.push(a);
i++;
});
// items.push(p);
},
data: params
});
} else {
alert('Wybrany produkt już znajduje się na liście!');
}
}
FillTable(items, false);
// clear results
$('#searchResultDiv').html('');
searchedProducts = new Array();
$('#searchProductsInput').val('');
$(".loading_panel").css("display", "none");
}
function getItems(editview) {
var record = '';
var stock = $("#stock_rw :selected").val();
stock += ',' + $("#stock_pw :selected").val();
if ($("#duplicate").val() == true)
record = $("input[name='return_id']").val();
else
record = $("input[name='record']").val();
if (isNaN(editview))
editview = true;
var params = {
job: 'getItems',
record: record,
stockId: stock,
};
$.ajax({
type: "POST",
url: ajax_url,
dataType: "json",
success: function (data) {
items = data;
if (editview)
FillTable(items, false);
else
FillTable(items, true);
$(".loading_panel").css("display", "none");
},
data: params
});
}
function DrawDetailSummary() {
$("#result_table").html('');
html = '';
html += '