You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
5.4 KiB
156 lines
5.4 KiB
$(function() {
|
|
var nds = '';
|
|
|
|
var fix_float_input = function (e) {
|
|
var el = $(this);
|
|
var new_val = fix_float_string(el.val());
|
|
if (new_val != el.val())
|
|
el.val(new_val);
|
|
};
|
|
|
|
var calc_summa = function (e) {
|
|
console.log('calc_summa');
|
|
console.log($('#id_nds_method input:checked').val());
|
|
|
|
var el = $(this);
|
|
var qty = parseFloat(el.closest('tr.row').find('td.qty input').val());
|
|
var price = parseFloat(el.closest('tr.row').find('td.price input').val());
|
|
var total_price;
|
|
|
|
total_price = (qty && price) ? qty * price : 0;
|
|
|
|
if (total_price) {
|
|
el.closest('tr.row').find('td.total_price input').val(total_price.toFixed(2));
|
|
} else {
|
|
el.closest('tr.row').find('td.total_price input').val('');
|
|
}
|
|
|
|
};
|
|
|
|
var calc_itogo_nds = function() {
|
|
var itogo_sum = 0;
|
|
$('#tbl_items').find('.total_price input:visible').each(function() {
|
|
var itogo_sum_row = $(this).val() ? parseFloat($(this).val()) : 0;
|
|
itogo_sum += itogo_sum_row;
|
|
});
|
|
|
|
if ($('#id_nds_value').val() !== '0') {
|
|
$('#nds_method').show();
|
|
$('#decryption').show();
|
|
}
|
|
|
|
if ($('#id_nds_value').val() && $('#itogo').html()) {
|
|
|
|
console.log('----','NDS %',$('#id_nds_value').val());
|
|
console.log('----','Total %',$('#itogo').html());
|
|
console.log('----','Metod %', parseInt($('#id_nds_method input:checked').val()));
|
|
|
|
if (parseInt($('#id_nds_method input:checked').val())) {
|
|
nds = parseFloat($('#id_nds_value').val()) * parseFloat(itogo_sum) / 100;
|
|
} else {
|
|
nds = (parseFloat(itogo_sum) / (100 + parseFloat($('#id_nds_value').val()))) * parseFloat($('#id_nds_value').val());
|
|
}
|
|
// console.log(nds.toFixed(2))
|
|
$('#totalNds').html(nds.toFixed(2));
|
|
} else {
|
|
$('#totalNds').html('');
|
|
}
|
|
|
|
};
|
|
|
|
window.calc_itogo = function (e) {
|
|
var itogo_sum = 0;
|
|
$('#tbl_items').find('.total_price input:visible').each(function() {
|
|
var itogo_sum_row = $(this).val() ? parseFloat($(this).val()) : 0;
|
|
itogo_sum += itogo_sum_row;
|
|
});
|
|
if (itogo_sum) {
|
|
if (parseInt($('#id_nds_method input:checked').val())) {
|
|
console.log('add');
|
|
nds = parseFloat($('#id_nds_value').val()) * parseFloat(itogo_sum) / 100;
|
|
$('#itogo').html(parseFloat(itogo_sum.toFixed(2)) + parseFloat(nds.toFixed(2)));
|
|
} else {
|
|
nds = (parseFloat(itogo_sum) / (100 + parseFloat($('#id_nds_value').val()))) * parseFloat($('#id_nds_value').val());
|
|
var totalWithoutNds = parseFloat(itogo_sum.toFixed(2)) - parseFloat(nds).toFixed(2);
|
|
$('#decryption').html(totalWithoutNds + ' + НДС');
|
|
$('#itogo').html(itogo_sum.toFixed(2));
|
|
}
|
|
|
|
} else {
|
|
$('#itogo').html('');
|
|
}
|
|
|
|
calc_itogo_nds();
|
|
};
|
|
|
|
window.set_events = function set_events() {
|
|
$("#tbl_items tr.row td.qty input").on('keyup', fix_float_input);
|
|
$("#tbl_items tr.row td.price input").on('keyup', fix_float_input);
|
|
|
|
$("#tbl_items tr.row td.debit input").on('keyup', fix_float_input);
|
|
$("#tbl_items tr.row td.credit input").on('keyup', fix_float_input);
|
|
|
|
// not formset fields though...
|
|
$("div#saldo_debit input").on('keyup', fix_float_input);
|
|
$("div#saldo_credit input").on('keyup', fix_float_input);
|
|
|
|
$("#tbl_items tr.row td.qty input").on('keyup', calc_summa);
|
|
$("#tbl_items tr.row td.price input").on('keyup', calc_summa);
|
|
|
|
$("#tbl_items tr.row td.qty input").blur(window.calc_itogo);
|
|
$("#tbl_items tr.row td.price input").blur(window.calc_itogo);
|
|
$("#tbl_items tr.row td.total_price input").blur(window.calc_itogo);
|
|
|
|
$('#id_nds_value').on('change', function(){
|
|
|
|
calc_itogo_nds();
|
|
calc_itogo();
|
|
if (!parseInt($('#id_nds_method input:checked').val())) {
|
|
$('#decryption').show();
|
|
} else {
|
|
$('#decryption').hide();
|
|
}
|
|
});
|
|
$('#id_nds_method').on('change', function () {
|
|
console.log('id_nds_method change');
|
|
calc_itogo();
|
|
if (!parseInt($('#id_nds_method input:checked').val())) {
|
|
$('#decryption').show();
|
|
} else {
|
|
$('#decryption').hide();
|
|
}
|
|
});
|
|
|
|
};
|
|
window.set_events();
|
|
window.calc_itogo();
|
|
calc_itogo_nds();
|
|
});
|
|
|
|
|
|
function fix_float_string(val) {
|
|
try {
|
|
var part1, part2, lastpos;
|
|
|
|
lastpos = val.lastIndexOf('.');
|
|
if (lastpos != -1) { // string have at least one dot
|
|
part1 = val.substr(0, lastpos);
|
|
part2 = val.substr(lastpos+1);
|
|
val = part1.replace('.', '').replace(',', '') + '.' + part2.replace('.', '').replace(',', '');
|
|
}
|
|
else {
|
|
lastpos = val.lastIndexOf(',');
|
|
if (lastpos != -1) { // string have at least one comma
|
|
part1 = val.substr(0, lastpos);
|
|
part2 = val.substr(lastpos+1);
|
|
val = part1.replace('.', '').replace(',', '') + '.' + part2.replace('.', '').replace(',', '');
|
|
}
|
|
}
|
|
}
|
|
catch (e) {
|
|
if (!e instanceof TypeError)
|
|
throw(e);
|
|
}
|
|
|
|
return val;
|
|
}
|
|
|