var itemsTable = 'itemsTable';
var items = new Array();
var displayArray = new Array();
var searchedProducts = new Array();
var ajax_url = "index.php?module=EcmStockDocInsideIns&action=javahelper&to_pdf=1";
// 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 += '
';
// 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 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_id :selected").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.price = data.price;
p.unit_name = data.unit_name;
p.price = data.price;
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].price = data.price;
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].product_is_consignment = data.is_consignment;
items[index].stock_state = data.stock_state;
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",
async: false,
success : function(data) {
items = data;
if (editview)
FillTable(items, false);
else
FillTable(items, true);
$(".loading_panel").css("display", "none");
},
data : params
});
}
/*
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 DrawDetailSummary() {
$("#result_table").html('');
html = '';
html += ' ';
html += 'Suma dokumentu ';
html += ' ';
html += ' ';
$("#result_table").html(html);
}
function generateNumber() {
var params = {
job : 'generateNumber',
stock: $("#stock_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 getPricesInfo(index) {
$("#price_start_div_" + index).show('slow');
$("#price_start_div_" + index).html(
' ');
var params = {
job : 'getPricesInfo',
product_id : $("#product_id_" + index).val(),
pricebook_id : $("#pricebook_id :selected").val(),
account_id : $("#parent_id").val(),
};
$
.ajax({
type : "POST",
url : ajax_url,
dataType : "json",
async : false,
success : function(data) {
html = '';
$
.each(
data,
function(key, value) {
if (value.name == 'pricebook') {
html += ''
+ $(
"#pricebook_id :selected")
.html()
+ ': '
+ FormatNumber(value.price)
+ ' ';
} else if (value.name == $(
"#ecmprice_name").val()) {
html += ''
+ value.name
+ ': '
+ FormatNumber(value.price)
+ ' ';
} else {
if(value.visible=='1'){
html += ''
+ value.name
+ ': ';
if(value.module=='EcmPrices'){
html+=''+FormatNumber(value.price)+' ';
}
if(value.module=='EcmInvoiceOuts'){
html+=''+FormatNumber(value.price)+' ';
}
html+ ' ';
}
}
});
html += ' ';
},
data : params
});
$("#price_start_div_" + index).html(html);
}
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_id").val(),
};
$.ajax({
type : "POST",
url : ajax_url,
dataType : "json",
async : false,
success : function(data) {
$("#stock_state_" + index).val(FormatNumber(data,$("#product_precision_" + index).val()));
},
data : params
});
}
function checkProducts() {
var count = $('#' + itemsTable + '_T tr').length - 1; // -1 - thead row
var error = false; // hope :)
var total_netto=0;
for (var index = 0; index != count; index++) {
if($("#product_id_" + index).val()!=''){
var qty = UnformatNumber($("#quantity_" + index).val());
var cat_id = $("#product_category_id_" + index).val();
// refresh items array
items[index].quantity = qty;
items[index].price = UnformatNumber($("#price_" + index).val());
items[index].total=items[index].quantity*items[index].price;
$("#total_" + index).val(FormatNumber(items[index].total,2));
total_netto+=items[index].total;
var stock = UnformatNumber($("#stock_state_" + index).val());
if(!(qty>0)){
error=true;
$("#quantity_" + index).css("color", "red");
} else
$("#quantity_" + index).css("color", "black");
}
}
$("#total_netto").val(total_netto);
DrawDetailSummary();
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 price = items[index].price;
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.price = value.price;
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 getSearchInfo2(product_id, product_code) {
$("#search_info_div2_" + product_id).show().draggable();
var html = ' ';
html += '' + product_code + ' ';
html +='Magazyn Ilość ';
var params = {
job : 'getStockArray',
product_id : product_id,
};
$.ajax({
type : "POST",
url : ajax_url,
dataType : "json",
async : false,
success : function(data) {
$.each(data, function(key, value) {
html += ''+key + ' ' + FormatNumber(value)
+ ' ';
});
},
data : params
});
html += '
';
$("#search_info_div2_" + product_id).html(html);
}
/*
function getSearchInfo(product_id, product_code) {
$("#search_info_div_" + product_id).show().draggable();
var html = ' ';
html += '' + product_code + ' ';
// get prices info
html +='Nazwa Ilość Cena Data dok ';
var params = {
job : 'getPricesInfo',
product_id : product_id,
pricebook_id : $("#pricebook_id :selected").val(),
account_id : $("#parent_id").val(),
};
$.ajax({
type : "POST",
url : ajax_url,
dataType : "json",
async : false,
success : function(data) {
$.each(data, function(key, value) {
if (value.name == 'pricebook')
html += $("#pricebook_id :selected").html() + ': '
+ FormatNumber(value.price) + ' ';
if (value.name == $("#ecmprice_name").val())
html += '' + value.name + ': '
+ FormatNumber(value.price) + ' ';
else
if(value.module=='EcmStockDocIns'){
html+=''+value.name + ' '+value.quantity+' '+FormatNumber(value.price)+' '+value.register_date+' ';
}
//html += value.name + ': ' + FormatNumber(value.price)
// + ' ';
});
},
data : params
});
html+='
';
$("#search_info_div_" + product_id).html(html);
}
*/
function getSearchInfo3(product_id, product_code) {
$("#search_info_div3_" + product_id).show().draggable();
var html = ' ';
html += '' + product_code + ' ';
// get prices info
html +='Nazwa Cena ';
var params = {
job : 'getPricesInfo',
product_id : product_id,
pricebook_id : $("#pricebook_id :selected").val(),
account_id : $("#parent_id").val(),
};
$.ajax({
type : "POST",
url : ajax_url,
dataType : "json",
async : false,
success : function(data) {
$.each(data, function(key, value) {
if (value.name == 'pricebook')
html += $("#pricebook_id :selected").html() + ': '
+ FormatNumber(value.price) + ' ';
if (value.name == $("#ecmprice_name").val())
html += '' + value.name + ': '
+ FormatNumber(value.price) + ' ';
else
if(value.module=='EcmPrices'){
html+=''+value.name + ': '+FormatNumber(value.price)+' ';
}
if(value.module=='EcmInvoiceOuts'){
html+=''+value.name + ': '+FormatNumber(value.price)+' ';
}
//html += value.name + ': ' + FormatNumber(value.price)
// + ' ';
});
},
data : params
});
html+='
';
$("#search_info_div3_" + product_id).html(html);
}
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();