var itemsTable = 'itemsTable';
var items = new Array();
var displayArray = new Array();
var searchedProducts = new Array();
var ajax_url = "index.php?module=EcmStockDocIns&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 {
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
checkProducts();
}
function deleteRow(index) {
items.splice(index, 1);
FillTable(items);
}
function calculateRow(index) {
if (index == -1)
return;
// liczone według najlepszych zasad księgowości
var price = UnformatNumber($('#price_' + index).val());
var price_fk = UnformatNumber($('#price_fk_' + index).val());
var vat_value = $('#ecmvat_value_' + index).val();
var quantity = UnformatNumber($('#quantity_' + index).val());
var palet= UnformatNumber($('#palet_' + index).val());
var waga= UnformatNumber($('#waga_' + index).val());
var clo= UnformatNumber($('#clo_' + index).val());
//alert(palet);
var total = price * quantity;
// put data
$('#total_' + index).val(FormatNumber(total));
// put data to items array
items[index].price = toFixed(price, 2);
items[index].price_fk = toFixed(price_fk, 2);
items[index].palet = palet;
items[index].total = total;
items[index].quantity = toFixed(quantity, 2);
items[index].ecmvat_id = $('#ecmvat_id_' + index).val();
items[index].ecmvat_name = $('#ecmvat_name_' + index).val();
items[index].ecmvat_value = vat_value;
items[index].waga = waga;
items[index].clo = clo;
// put other data
items[index].recipient_code = $('#recipient_code_' + index).val();
items[index].remarks = $('#remarks_' + index).val();
// change name or language?
items[index].name = $('#name_' + index).val();
items[index].unit_name = $('#unit_name_' + index).val();
console.log(items[index]);
// calculate total
calculateTotal();
}
function calculatePrices(){
var count = $('#' + itemsTable + '_T tr').length - 1; // -1 - thead row
if($("#kind").val()=='import'){
var TZ =0;
if ($("#foreign_transport_cur_id").val()=='PLN')
TZ = UnformatNumber($("#foreign_transport_val").val());
else
TZ = UnformatNumber($("#foreign_transport_val").val())*UnformatNumber($("#foreign_transport_cur_val").val());
var TK =0;
if ($("#country_transport_cur_id").val()=='PLN')
TK = UnformatNumber($("#country_transport_val").val());
else
TK = UnformatNumber($("#country_transport_val").val())*UnformatNumber($("#country_transport_cur_val").val());
var KD =0;
if ($("#addition_cost_cur_id").val()=='PLN')
KD = UnformatNumber($("#addition_cost_val").val());
else
KD = UnformatNumber($("#addition_cost_val").val())*UnformatNumber($("#addition_cost_cur_val").val());
var inv_total=0;
var weight_total=0;
var error = false; // hope :)
for (var index = 0; index != count; index++) {
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].price_fk = UnformatNumber($("#price_fk_" + index).val());
items[index].palet = UnformatNumber($("#palet_" + index).val());
inv_total += UnformatNumber($("#price_fk_" + index).val())*qty;
weight_total += UnformatNumber($("#waga_" + index).val());
}
alert(weight_total);
var sum=0;
var participation = new Array();
for (var index = 0; index != count; index++) {
if (index != count-1) {
participation[i] = UnformatNumber($("#waga_" + index).val()) / weight_total;
sum+=participation[i];
} else
participation[i] = 1 - sum;
}
var currency_id = $("#currency_id").val();
var exchange;
var sad_exchange;
if (currency_id=='PLN') exchange=1; else {
exchange= UnformatNumber($("#currency_value").val());
sad_exchange=UnformatNumber($("#currency_sad_value").val());
}
for (var index = 0; index != count; index++) {
var pprice = 0;
//alert(document.forms.EditView.currency_value.value);
var duty = UnformatNumber($("#clo_" + index).val())/100;
//pprice = (participation[i]*TZ/pf(data.quantity))+(data.invoice_price*exchange * (1+duty)) + (participation[i]*TK/pf(data.quantity)) + (participation[i]*KD/pf(data.quantity));
//data.price = pprice;
foreign_costs = (participation[i]*TZ/UnformatNumber($("#quantity_" + index).val()));
country_costs = (participation[i]*TK/UnformatNumber($("#quantity_" + index).val()));
add_costs = (participation[i]*KD/UnformatNumber($("#quantity_" + index).val()));
//alert(duty+' '+pf(data.quantity));
//alert(foreign_costs+' '+country_costs+' '+add_costs);
price = (UnformatNumber($("#price_fk_" + index).val())*exchange + (UnformatNumber($("#price_fk_" + index).val())*sad_exchange+foreign_costs)*duty);
pprice = foreign_costs+country_costs+add_costs+price;
$('#price_' + index).val(FormatNumber(pprice));
var total = pprice * UnformatNumber($("#quantity_" + index).val());
// put data
$('#total_' + index).val(FormatNumber(total));
console.log(pprice);
//N.row(i).cells.item(6).setData(data);
//N.row(i).calculateTotal();
}
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($('#total_' + index).val());
all_subtotal += subtotal;
// font color to black
$("#ecmvat_name_" + index).css("color", "black");
if (!($('#ecmvat_id_' + index).val() in vats)) {
vats[$('#ecmvat_id_' + index).val()] = new Object();
vats[$('#ecmvat_id_' + index).val()].value_name = $(
'#ecmvat_name_' + index).val(); // ex. 23
vats[$('#ecmvat_id_' + index).val()].value = subtotal
* ($('#ecmvat_value_' + index).val() / 100);
} else
vats[$('#ecmvat_id_' + index).val()].value += subtotal
* ($('#ecmvat_value_' + index).val() / 100);
sum_vats += subtotal * ($('#ecmvat_value_' + index).val() / 100);
}
all_total = all_subtotal + sum_vats;
DrawSummary();
$("#t_netto").val(FormatNumber(all_subtotal));
$("#t_brutto").val(FormatNumber(all_total));
// add vats
var html = '';
var vats_summary = '';
for ( var x in vats) {
html += '
';
$("#result_table").html(html);
}
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 += '