var itemsTable = 'itemsTable';
var items = new Array();
var displayArray = new Array();
var validation = new Object();
var shipping_addresses = new Array();
var searchedProducts = new Array();
var ajax_url = "index.php?module=EcmStockDocOuts&action=javahelper&to_pdf=1";
function FillTable(data, allReadOnly) {
html = '';
// loop throw data rows
$
.each(
data,
function(row_index, row) {
html += '
';
// 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 {
hide='';
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();
else{
calculateTotal();
}
}
function deleteRow(index) {
items.splice(index, 1);
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) {
hide='';
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 :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 setAddresses() {
if (shipping_addresses.length == 0)
return;
var s = $("");
var opt=' ';
$.each(shipping_addresses, function (index, value) {
opt+=''+value.name+' ';
});
s.html(opt);
$("#addresses").html("");
$("#addresses").append(s);
//set on change
$("#shipping_addresses").change(function(){
putShippingAddress($( "#shipping_addresses option:selected" ).val());
});
}
function putShippingAddress(number) {
if (number == '') {
$("#shipping_address_name").val("");
$("#shipping_address_street").val("");
$("#shipping_address_postalcode").val("");
$("#shipping_address_city").val("");
$("#shipping_address_country").val("");
$("#shipping_iln").val("");
$("#shipping_nip").val("");
$("#shipping_supplier_code").val("");
} else {
$("#shipping_address_name").val(shipping_addresses[number].name);
$("#shipping_address_street").val(shipping_addresses[number].street);
$("#shipping_address_postalcode").val(shipping_addresses[number].postalcode);
$("#shipping_address_city").val(shipping_addresses[number].city);
$("#shipping_address_country").val(shipping_addresses[number].country);
$("#shipping_iln").val(shipping_addresses[number].iln);
$("#shipping_nip").val(shipping_addresses[number].nip);
$("#shipping_supplier_code").val(shipping_addresses[number].supplier_code);
}
}
function getParentInfo(parent_id, parent_type) {
var params = {
id : parent_id,
type : parent_type,
job : 'getParentInfo'
};
$.ajax({
type: "POST",
url: ajax_url,
dataType : "json",
success: function(data) {
if (data == '-1') {
alert('Błąd');
$("#parent_name").val('');
$("#parent_id").val('');
} else {
setParentInfo(data);
}
},
data: params
});
}
//insert parent info in fields
function setParentInfo(data) {
//pricebooks
if ($("#new_number").val() == false) return; //if edit exists load only pricebooks
if (data.name)
$("#parent_name_copy").val(data.name);
if (data.parent_nip)
$("#parent_nip").val(data.parent_nip);
if (data.parent_iln)
$("#parent_iln").val(data.parent_iln);
if (data.parent_supplier_code)
$("#parent_supplier_code").val(data.parent_supplier_code);
if (data.parent_address_street)
$("#parent_address_street").val(data.parent_address_street);
if (data.parent_address_city)
$("#parent_address_city").val(data.parent_address_city);
if (data.parent_address_postalcode)
$("#parent_address_postalcode").val(data.parent_address_postalcode);
if (data.parent_address_country)
$("#parent_address_country").val(data.parent_address_country);
if (data.ecmpaymentcondition_id && data.ecmpaymentcondition_name) {
$("#ecmpaymentcondition_id").val(data.ecmpaymentcondition_id);
$("#ecmpaymentcondition_name").val(data.ecmpaymentcondition_name);
}
if (data.ecmdeliverycondition_id && data.ecmdeliverycondition_name) {
$("#ecmdeliverycondition_id").val(data.ecmdeliverycondition_id);
$("#ecmdeliverycondition_name").val(data.ecmdeliverycondition_name);
}
if (data.currency_id)
$("#currency_id option[value=\""+data.currency_id+"\"]").attr('selected', 'selected');
$.each(validation, function(index, value) {
validation.index = false;
});
//validation
if (data.document_recipient_code=='1')
validation.recipient_code = true;
if (data.document_parent_order_no=='1')
validation.parent_order_no = true;
if (data.document_delivery_address=='1')
validation.delivery_address=true;
if (data.document_parent_iln=='1')
validation.parent_iln=true;
if (data.document_shipping_iln=='1')
validation.shipping_iln=true;
if (data.document_parent_nip=='1')
validation.parent_nip=true;
if (data.document_shipping_nip=='1')
validation.shipping_nip=true;
setAccountValidation();
//clear prevoius validation data
shipping_addresses.length = 0; //clear array
shipping_addresses = data.addresses;
setAddresses();
//hide loading
$(".loading_panel").css("display", "none");
}
function unsetAllCheckboxes() {
$("#searchResultDiv input[type=checkbox]").each(function() {
$(this).prop("checked", false);
});
}
function setAccountValidation() {
//first clear validation
removeFromValidate('EditView', 'parent_order_no');
removeFromValidate('EditView', 'parent_shipping_address_name');
removeFromValidate('EditView', 'parent_shipping_address_street');
removeFromValidate('EditView', 'parent_shipping_address_postalcode');
removeFromValidate('EditView', 'parent_shipping_address_city');
removeFromValidate('EditView', 'parent_iln');
removeFromValidate('EditView', 'shipping_iln');
//now add what we need
if (validation.parent_order_no==true)
addToValidate('EditView', 'parent_document_no', 'id', 'true','');
if (validation.parent_order_no==true)
addToValidate('EditView', 'parent_document_no', 'id', 'true','');
if (validation.delivery_address==true) {
addToValidate('EditView', 'shipping_address_name', 'id', 'true','');
addToValidate('EditView', 'shipping_address_street', 'id', 'true','');
addToValidate('EditView', 'shipping_address_postalcode', 'id', 'true','');
addToValidate('EditView', 'shipping_address_city', 'id', 'true','');
}
if (validation.parent_iln==true)
addToValidate('EditView', 'parent_iln', 'id', 'true','');
if (validation.shipping_iln==true)
addToValidate('EditView', 'shipping_iln', 'id', 'true','');
if (validation.parent_nip==true)
addToValidate('EditView', 'parent_nip', 'id', 'true','');
if (validation.shipping_nip==true)
addToValidate('EditView', 'shipping_nip', 'id', 'true','');
}
function addProducts() {
$(".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) {
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.unit_name = data.unit_name;
p.unit_id = data.unit_id;
p.product_ean = data.ean;
p.stock_state = data.stock_state;
p.product_category_id = data.product_category_id;
p.product_pieces_per_carton = data.pieces_per_carton;
if ($("#searchInputQty").val() != ''
&& !isNaN(parseFloat(UnformatNumber($(
"#searchInputQty").val()))))
p.quantity = $("#searchInputQty").val();
p.product_cartons = 0;
if (p.product_pieces_per_carton > 0) {
p.product_cartons = Math.ceil(p.quantity / p.product_pieces_per_carton);
}
items.push(p);
}
},
data : params
});
}
FillTable(items);
// clear results
$('#searchResultDiv').html('');
searchedProducts = new Array();
$('#searchProductsInput').val('');
$(".loading_panel").css("display", "none");
}
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);
$(".loading_panel").css("display", "none");
},
data : params
});
}
function getItemsFromEcmSale(editview) {
var record = '';
if ($("#duplicate").val() == true)
record = $("input[name='return_id']").val();
else
record = $("input[name='ecmsale_id']").val();
if (isNaN(editview))
editview = true;
var params = {
job : 'getItemsFromEcmSale',
record : record,
};
$.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 += ' ';
html += 'Suma dokumentu ';
html += ' ';
html += ' ';
html += ' ';
html += 'Suma operacji: wyjście ';
html += ' ';
html += ' ';
html += ' ';
html += 'Suma operacji: wyjście ';
html += ' ';
html += ' ';
html += ' ';
html += 'Podgląd operacji magazynowych ';
html += ' ';
html += ' ';
$("#result_table").html(html);
}
function generateDocNumberStock() {
var params = {
job : 'generateDocNumberStock',
stock_id: $("#stock_id").val(),
};
$.ajax({
type : "POST",
url : ajax_url,
dataType : "json",
success : function(data) {
if (data == '-1') {
// try loading again
generateDocNumberStock();
} 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_id").val(),
};
$.ajax({
type : "POST",
url : ajax_url,
dataType : "json",
async : false,
success : function(data) {
items[index].stock_state = data;
$("#stock_state_" + index).val(FormatNumber(data));
},
data : params
});
}
function checkProducts() {
var count = $('#' + itemsTable + '_T tr').length - 1; // -1 - thead row
var error = false; // hope :)
for (var index = 0; index != count; index++) {
refreshStock(index);
var qty = UnformatNumber($("#quantity_" + index).val());
var cat_id = $("#product_category_id_" + index).val();
// refresh items array
items[index].quantity = qty;
var stock = UnformatNumber($("#stock_state_" + index).val());
if ((qty > stock || qty == 0) && cat_id!='d7f876b0-1a3d-43a1-7c9b-511ba40df3d1') {
error = true;
$("#quantity_" + index).css("color", "red");
} else
$("#quantity_" + index).css("color", "black");
}
if (error){
alert('Brak produktów na stanie!');
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.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 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 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 calculateRow(index) {
if (index == -1)
return;
var price = UnformatNumber($('#price_sell_' + index).val());
var quantity = UnformatNumber($('#quantity_' + index).val());
var total_sell = toFixed(price * quantity,2);
$('#price_total_sell_' + index).val(FormatNumber(total_sell));
//format quantity
$('#price_quantity_' + index).val(FormatNumber(quantity,2));
// put data to items array
items[index].price_sell = toFixed(price, 2);
items[index].price_total_sell = total_sell;
items[index].quantity = toFixed(quantity, 2);
// calculate total
calculateTotal();
//update cartons
var pieces_per_carton = UnformatNumber($('#product_pieces_per_carton_' + index).val());
var cartons = 0;
if (pieces_per_carton > 0) {
cartons = Math.ceil(quantity / pieces_per_carton);
}
$('#product_cartons_' + index).val(FormatNumber(cartons,2));
items[index].product_pieces_per_carton = toFixed(pieces_per_carton, 2);
items[index].product_cartons = toFixed(cartons, 2);
}
function updatePartNo(index) {
items[index].part_no = $('#part_no_' + index).val();
}
function calculateTotal() {
var count = $('#' + itemsTable + '_T tr').length - 1; // -1 - thead row
var all_subtotal = 0;
var all_total = 0;
var weight_total=0;
for (var index = 0; index != count; index++) {
subtotal = UnformatNumber($('#price_total_sell_' + index).val());
if(!isNaN(subtotal)){
all_subtotal += subtotal;
}
}
all_total = all_subtotal;
DrawSummary();
$("#t_netto").val(FormatNumber(all_total));
}
function DrawSummary() {
$("#result_table").html('');
html = '';
html += ' ';
html += 'Suma ';
html += ' ';
html += ' ';
$("#result_table").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);
}
$.event.special.inputchange = {
setup: function() {
var self = this, val;
$.data(this, 'timer', window.setInterval(function() {
val = self.value;
if ( $.data( self, 'cache') != val ) {
$.data( self, 'cache', val );
$( self ).trigger( 'inputchange' );
}
}, 20));
},
teardown: function() {
window.clearInterval( $.data(this, 'timer') );
},
add: function() {
$.data(this, 'cache', this.value);
}
};
// sortable rows in main table
$("#" + itemsTable + " tbody").sortable();