var itemsTable = 'itemsTable';
var items = new Array();
var displayArray = new Array();
var searchedProducts = new Array();
var ajax_url = "index.php?module=EcmStockDocCorrects&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 += '
';
}
}
// 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 calculateAll() {
var count = $('#' + itemsTable + '_T tr').length - 1; // -1 - thead row
for (var index = 0; index != count; index++) {
calculateRow(index);
}
}
function calculateRow(index) {
if (index == -1)
return;
items[index].quantity_consignment_id = $('#quantity_consignment_id_'+index).val();
items[index].quantity_consignment_doc = $('#quantity_consignment_doc_'+index).val();
getConsignmentsDiv(index);
if( ($('#type').val()=='2dcd99ce-c2fe-6bbd-6d1a-5512a569c4e0') || ($('#type').val()=='1')){
getConsignmentsDiv(index);
}
// liczone według najlepszych zasad księgowości
var price = UnformatNumber($('#price_' + index).val());
var quantity = UnformatNumber($('#quantity_' + index).val());
//alert(palet);
var total = price * quantity;
// put data
$('#product_total_' + index).val(FormatNumber(total));
// put data to items array
items[index].price = toFixed(price, 2);
items[index].product_total = total;
items[index].quantity = toFixed(quantity, 4);
items[index].name = $('#name_' + index).val();
items[index].unit_name = $('#unit_name_' + index).val();
console.log(items[index]);
// calculate total
calculateTotal();
}
function calculateTotal() {
var count = $('#' + itemsTable + '_T tr').length - 1; // -1 - thead row
var vats = new Array();
var all_subtotal = 0;
var all_total = 0;
var sum_vats = 0;
var sum_discounts = 0;
var weight_total=0;
for (var index = 0; index != count; index++) {
subtotal = UnformatNumber($('#product_total_' + 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 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) {
var precision = precision || 2;
// make string..
number = number + '';
var minus = false;
if (number.charAt(0) == '-') {
minus = true;
number = number.slice(1);
}
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("");
// minus
if (minus)
c = '-' + c;
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 minus = false;
value+='';
if (value.charAt(0) == '-') {
minus = true;
value = value.slice(1);
}
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');
// minus
if (minus)
integral = '-' + integral;
return precision ? integral + '.' + padding + fraction : integral;
}
// draw table
function DrawHeaders() {
var html = '';
html += '