var itemsTable = 'itemsTable';
var items = new Array();
var displayArray = new Array();
var searchedProducts = new Array();
var ajax_url = "index.php?module=EcmStockDocMoves&action=javahelper&to_pdf=1";
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 += '
';
// sort? why not!
/*
* html += 'Sortowanie: '; html += ''; html += 'Nazwa '; html += 'Kod ';
* html += 'Kategoria '; html += 'Kategoria '; html += 'Cena po upuście '; html += 'Wartość '; 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 searchAllProducts() {
var searchKey = '%';
$("#searchResultDiv").html(
' ');
setTimeout(function() {
var params = {
job : 'searchProducts',
searchKey : searchKey,
searchCategory : $("#productSearchCategory :selected").val(),
searchStock : $("#productSearchStock :selected").val(),
searchSort : $("#productSearchSort :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 += 'Ilość:  ';
html += ' ';
$('#searchResultDiv').html(html);
}
function unsetAllCheckboxes() {
$("#searchResultDiv input[type=checkbox]").each(function() {
$(this).prop("checked", false);
});
}
function addProducts() {
$(".loading_panel").css("display", "block");
clearEmpty();
var products = new Array();
$.each(searchedProducts, function(index, value) {
if ($('#prod_' + value).is(':checked'))
products[value] = true;
});
for ( var key in products) {
var params = {
job : 'getProduct',
id : key,
stockId : $("#stock_out_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_precision = data.unit_precision;
p.unit_precision = data.unit_precision;
p.product_is_consignment = data.is_consignment;
if ($("#searchInputQty").val() != ''
&& !isNaN(parseFloat(UnformatNumber($(
"#searchInputQty").val()))))
p.quantity = $("#searchInputQty").val();
items.push(p);
}
},
data : params
});
}
AddSearchRecord();
FillTable(items);
checkProductIsConsignment();
// clear results
$('#searchResultDiv').html('');
searchedProducts = new Array();
$('#searchProductsInput').val('');
$(".loading_panel").css("display", "none");
}
function AddProduct(index,record){
$(".loading_panel").css("display", "block");
var record;
var pricebook = $("#pricebook_id :selected").val();
var params = {
job : 'getProduct',
id : record,
stockId : $("#stock_out_id :selected").val(),
};
$.ajax({
type : "POST",
url : ajax_url,
dataType : "json",
async : false,
success : function(data) {
if (data != '-1') {
items[index].product_id = data.id;
items[index].name = data.name;
items[index].product_code = data.code;
items[index].product_id = data.id;
items[index].ecmvat_id = data.ecmvat_id;
items[index].ecmvat_value = data.ecmvat_value;
items[index].ecmvat_name = data.ecmvat_name;
items[index].product_ean = data.ean;
items[index].discount = data.discount;
items[index].product_ean2 = data.ean2;
items[index].unit_name = data.unit_name;
items[index].unit_id = data.unit_id;
items[index].price_start = data.price_start;
items[index].recipient_code = data.recipient_code;
items[index].product_precision = data.unit_precision;
items[index].unit_precision = data.unit_precision;
items[index].stock_state = data.stock_state;
items[index].product_is_consignment = data.is_consignment;
if ($("#searchInputPrice").val() != ''
&& !isNaN(parseFloat(UnformatNumber($(
"#searchInputPrice").val()))))
items[index].price_start = $("#searchInputPrice").val();
if ($("#searchInputQty").val() != ''
&& !isNaN(parseFloat(UnformatNumber($(
"#searchInputQty").val()))))
items[index].quantity = $("#searchInputQty").val();
}
},
data : params
});
AddSearchRecord();
FillTable(items);
checkProductIsConsignment();
$('#searchResultDiv').html('');
$(".loading_panel").css("display", "none");
$("#quantity_"+index).focus().select();
}
function getItems(editview) {
var record = '';
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,
};
$.ajax({
type : "POST",
url : ajax_url,
dataType : "json",
success : function(data) {
items = data;
if (editview)
FillTable(items, false);
else {
FillTable(items, true);
var count = $('#' + itemsTable + '_T tr').length - 1; // -1 - thead row
for (var index = 0; index != count; index++) {
console.log(items[index]);
if (items[index].product_is_consignment) {
$('#product_code_'+index).parent().append($( ""+items[index].part_no+"
" ));
}
}
}
$(".loading_panel").css("display", "none");
},
data : params
});
}
function DrawDetailSummary() {
$("#result_table").html('');
html = '';
html += ' ';
html += 'Suma: ';
html += ' ';
html += ' ';
$("#result_table").html(html);
}
function generateNumber() {
var params = {
job : 'generateNumber',
stock: $("#stock_in_id").val(),
date : $("#register_date").val(),
};
$.ajax({
type : "POST",
url : ajax_url,
dataType : "json",
success : function(data) {
if (data == '-1') {
// try loading again
generateNumber();
} else {
$("#document_no").val(data.document_no);
$("#number").val(data.number);
$(".loading_panel").css("display", "none");
}
},
data : params
});
}
function getCategoriesList() {
var params = {
job : 'getCategoriesList'
};
$.ajax({
type : "POST",
url : ajax_url,
dataType : "json",
success : function(data) {
var html = ' ';
$.each(data, function(index, value) {
html += '' + value.name
+ ' ';
$("#productSearchCategory").html(html);
});
},
data : params
});
}
function refreshStock(index) {
var params = {
job : 'getStockState',
id : $("#product_id_" + index).val(),
stockId : $("#stock_out_id").val(),
};
$.ajax({
type : "POST",
url : ajax_url,
dataType : "json",
async : false,
success : function(data) {
$("#stock_state_" + index).val(FormatNumber(data));
},
data : params
});
}
function setRowDate(index){
var qty = UnformatNumber($("#quantity_" + index).val());
items[index].quantity = qty;
}
function checkProducts() {
var count = $('#' + itemsTable + '_T tr').length - 1; // -1 - thead row
var error = false; // hope :)
for (var index = 0; index != count; index++) {
if($("#product_id_" + index).val()!='' && items[index]){
refreshStock(index);
var qty = UnformatNumber($("#quantity_" + index).val());
var cat_id = $("#product_category_id_" + index).val();
var con_qty= $("#product_consignment_qty_" + index).val();
// refresh items array
if ($('#product_is_consignment_' + index).val() != '1') {
con_qty=qty;
}
items[index].quantity = qty;
var stock = UnformatNumber($("#stock_state_" + index).val());
if ((qty > stock || qty == 0 || qty > con_qty) && cat_id!='d7f876b0-1a3d-43a1-7c9b-511ba40df3d1') {
error = true;
$("#quantity_" + index).css("color", "red");
} else
$("#quantity_" + index).css("color", "black");
}
}
if (error)
return false;
else
return true;
}
function getComponents(index) {
$(".loading_panel").css("display", "block");
var product_id = items[index].product_id;
var qty = items[index].quantity;
var params = {
job : 'getComponents',
product_id : product_id,
};
$.ajax({
type : "POST",
url : ajax_url,
dataType : "json",
async : false,
success : function(data) {
if (data.length > 0) {
items.splice(index, 1);
$.each(data, function(index, value) {
var p = new Object();
p.product_id = value.id;
p.name = value.name;
p.product_code = value.code;
p.unit_name = value.unit_name;
p.unit_id = value.unit_id;
p.quantity = qty * parseInt(value.quantity);
p.product_category_id = value.product_category_id;
if (p.product_category_id=='d7f876b0-1a3d-43a1-7c9b-511ba40df3d1')
return;
items.push(p);
});
};
},
data : params
});
FillTable(items);
$(".loading_panel").css("display", "none");
}
function createProduct() {
var newProdId = document.getElementById("newProductId");
newProduct = window.open('index.php?module=EcmProducts&action=EditView&IamPopup=1', 'newProduct', 'toolbar=no,menubar=no,scrollbars=yes,scrollbars=1');
newProduct.newProdId = newProdId;
newProduct.focus();
}
// sortable rows in main table
$("#" + itemsTable + " tbody").sortable();