Resulta que quiero hacer una factura donde se introduce el Nombre del Producto, Descripción del Producto, Precio, Unidades e IVA. En el campo de IVA se introduce uno de estos tres valores: 21%, 9% y 1%. El problema es que tengo que separar las cantidades de los IVAS en diferentes casillas según se seleccione.
Os paso el código por si me podeis ayudar. Espero hacerlo bien ya que nunca he puesto código.
example.js
Cita:
Factura.phpfunction print_today() {
var now = new Date();
var months = new Array('01','02','03','04','05','06','07','08','09' ,'10','11','12');
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;
}
var today = date + "/"+ months[now.getMonth()] + "/" + (fourdigits(now.getYear()));
return today;
}
function roundNumber(number,decimals) {
var newString;
decimals = Number(decimals);
if (decimals < 1) {
newString = (Math.round(number)).toString();
} else {
var numString = number.toString();
if (numString.lastIndexOf(".") == -1) {
numString += ".";
}
var cutoff = numString.lastIndexOf(".") + decimals;
var d1 = Number(numString.substring(cutoff,cutoff+1));
var d2 = Number(numString.substring(cutoff+1,cutoff+2));
if (d2 >= 5) {
if (d1 == 9 && cutoff > 0) {
while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
if (d1 != ".") {
cutoff -= 1;
d1 = Number(numString.substring(cutoff,cutoff+1));
} else {
cutoff -= 1;
}
}
}
d1 += 1;
}
if (d1 == 10) {
numString = numString.substring(0, numString.lastIndexOf("."));
var roundedNum = Number(numString) + 1;
newString = roundedNum.toString() + '.';
} else {
newString = numString.substring(0,cutoff) + d1.toString();
}
}
if (newString.lastIndexOf(".") == -1) {
newString += ".";
}
var decs = (newString.substring(newString.lastIndexOf(".")+1) ).length;
for(var i=0;i<decimals-decs;i++) newString += "0";
return newString;
}
function update_total() {
var total = 0;
$('.price').each(function(i){
price = $(this).html().replace("€","");
if (!isNaN(price)) total += Number(price);
});
total = roundNumber(total,2);
var subtotal=0;
$('.cost').each(function(n){
cost = $(this).html().replace("€","");
if (!isNaN(cost)) subtotal += Number(cost);
});
subtotal = roundNumber(subtotal,2);
var t_siniva=0;
var t_siniva21=0;
var t_siniva9=0;
var t_siniva1=0;
$('.siniva').each(function(z){
siniva = $(this).html().replace("€","");
if (!isNaN(siniva)) t_siniva21 += Number(siniva);
});
t_siniva21 = roundNumber(t_siniva21,2);
t_siniva9 = roundNumber(t_siniva9,2);
t_siniva1 = roundNumber(t_siniva1,2);
$('#iva21').html(t_siniva21.replace(".",",")+"€");
$('#iva9').html(t_siniva9.replace(".",",")+"€");
$('#iva1').html(t_siniva1.replace(".",",")+"€");
$('#subtotal').html(subtotal.replace(".",",")+"€") ;
$('#total').html(total.replace(".",",")+"€")
update_balance();
}
function update_balance() {
var due = $("#total").html().replace("€","").replace(",","." ) - $("#paid").val().replace("€","").replace(",","." );
due = roundNumber(due,2);
$('.due').html(due.replace(".",",")+"€");
document.getElementById("totalfactura").style.text Align="center";
var subt=document.getElementById("totalfactura");
subt.value=due;
}
function update_price() {
var row = $(this).parents('.item-row');
var price = (row.find('.cost').val().replace("€","").replace(" ,",".") * row.find('.qty').val() * row.find('.iva').val().replace("%","").replace("," ,".") / 100) + (row.find('.cost').val().replace("€","").replace(" ,",".") * row.find('.qty').val());
price = roundNumber(price,2);
isNaN(price) ? row.find('.price').html("N/A") : row.find('.price').html(price+"€");
var siniva = (row.find('.cost').val().replace("€","").replace(" ,",".") * row.find('.qty').val() * row.find('.iva').val().replace("%","").replace("," ,".") / 100);
siniva = roundNumber(siniva,2);
isNaN(siniva) ? row.find('.siniva').html("N/A") : row.find('.siniva').html(siniva+"€");
var iva = row.find('.iva').val();
var cost = row.find('.cost').val().replace("€","").replace(", ",".") * row.find('.qty').val();
cost = roundNumber(cost,2);
row.find('.cost').html(cost+"€");
update_total();
}
function bind() {
$(".cost").blur(update_price);
$(".qty").blur(update_price);
$(".iva").blur(update_price);
}
$(document).ready(function() {
$('input').click(function(){
$(this).select();
});
$("#paid").blur(update_balance);
$("#addrow").click(function(){
$(".item-row:last").after('<tr class="item-row"><td class="item-name"><div class="delete-wpr"><textarea name=producto[]></textarea><a class="delete" href="javascript:;" title="Remove row"><img src="imagenes/eliminar_concepto.jpg" width="22" height="23" alt="Eliminar" border=0 /></a></div></td><td class="description"><textarea name=descripcion[]></textarea></td><td><textarea class="cost" name="precio[]">0€</textarea></td><td><textarea class="qty" name="cantidad[]">0</textarea></td><td> <textarea name="iva[]" class="iva" id="iva[]">9%</textarea></textarea></td><td><span class="siniva">0€</span></td><td><span class="price">0€</span></td></tr>');
if ($(".delete").length > 0) $(".delete").show();
bind();
});
bind();
$(".delete").live('click',function(){
$(this).parents('.item-row').remove();
update_total();
if ($(".delete").length < 2) $(".delete").hide();
});
$("#cancel-logo").click(function(){
$("#logo").removeClass('edit');
});
$("#delete-logo").click(function(){
$("#logo").remove();
});
$("#change-logo").click(function(){
$("#logo").addClass('edit');
$("#imageloc").val($("#image").attr('src'));
$("#image").select();
});
$("#save-logo").click(function(){
$("#image").attr('src',$("#imageloc").val());
$("#logo").removeClass('edit');
});
$("#date").val(print_today());
});
var now = new Date();
var months = new Array('01','02','03','04','05','06','07','08','09' ,'10','11','12');
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;
}
var today = date + "/"+ months[now.getMonth()] + "/" + (fourdigits(now.getYear()));
return today;
}
function roundNumber(number,decimals) {
var newString;
decimals = Number(decimals);
if (decimals < 1) {
newString = (Math.round(number)).toString();
} else {
var numString = number.toString();
if (numString.lastIndexOf(".") == -1) {
numString += ".";
}
var cutoff = numString.lastIndexOf(".") + decimals;
var d1 = Number(numString.substring(cutoff,cutoff+1));
var d2 = Number(numString.substring(cutoff+1,cutoff+2));
if (d2 >= 5) {
if (d1 == 9 && cutoff > 0) {
while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
if (d1 != ".") {
cutoff -= 1;
d1 = Number(numString.substring(cutoff,cutoff+1));
} else {
cutoff -= 1;
}
}
}
d1 += 1;
}
if (d1 == 10) {
numString = numString.substring(0, numString.lastIndexOf("."));
var roundedNum = Number(numString) + 1;
newString = roundedNum.toString() + '.';
} else {
newString = numString.substring(0,cutoff) + d1.toString();
}
}
if (newString.lastIndexOf(".") == -1) {
newString += ".";
}
var decs = (newString.substring(newString.lastIndexOf(".")+1) ).length;
for(var i=0;i<decimals-decs;i++) newString += "0";
return newString;
}
function update_total() {
var total = 0;
$('.price').each(function(i){
price = $(this).html().replace("€","");
if (!isNaN(price)) total += Number(price);
});
total = roundNumber(total,2);
var subtotal=0;
$('.cost').each(function(n){
cost = $(this).html().replace("€","");
if (!isNaN(cost)) subtotal += Number(cost);
});
subtotal = roundNumber(subtotal,2);
var t_siniva=0;
var t_siniva21=0;
var t_siniva9=0;
var t_siniva1=0;
$('.siniva').each(function(z){
siniva = $(this).html().replace("€","");
if (!isNaN(siniva)) t_siniva21 += Number(siniva);
});
t_siniva21 = roundNumber(t_siniva21,2);
t_siniva9 = roundNumber(t_siniva9,2);
t_siniva1 = roundNumber(t_siniva1,2);
$('#iva21').html(t_siniva21.replace(".",",")+"€");
$('#iva9').html(t_siniva9.replace(".",",")+"€");
$('#iva1').html(t_siniva1.replace(".",",")+"€");
$('#subtotal').html(subtotal.replace(".",",")+"€") ;
$('#total').html(total.replace(".",",")+"€")
update_balance();
}
function update_balance() {
var due = $("#total").html().replace("€","").replace(",","." ) - $("#paid").val().replace("€","").replace(",","." );
due = roundNumber(due,2);
$('.due').html(due.replace(".",",")+"€");
document.getElementById("totalfactura").style.text Align="center";
var subt=document.getElementById("totalfactura");
subt.value=due;
}
function update_price() {
var row = $(this).parents('.item-row');
var price = (row.find('.cost').val().replace("€","").replace(" ,",".") * row.find('.qty').val() * row.find('.iva').val().replace("%","").replace("," ,".") / 100) + (row.find('.cost').val().replace("€","").replace(" ,",".") * row.find('.qty').val());
price = roundNumber(price,2);
isNaN(price) ? row.find('.price').html("N/A") : row.find('.price').html(price+"€");
var siniva = (row.find('.cost').val().replace("€","").replace(" ,",".") * row.find('.qty').val() * row.find('.iva').val().replace("%","").replace("," ,".") / 100);
siniva = roundNumber(siniva,2);
isNaN(siniva) ? row.find('.siniva').html("N/A") : row.find('.siniva').html(siniva+"€");
var iva = row.find('.iva').val();
var cost = row.find('.cost').val().replace("€","").replace(", ",".") * row.find('.qty').val();
cost = roundNumber(cost,2);
row.find('.cost').html(cost+"€");
update_total();
}
function bind() {
$(".cost").blur(update_price);
$(".qty").blur(update_price);
$(".iva").blur(update_price);
}
$(document).ready(function() {
$('input').click(function(){
$(this).select();
});
$("#paid").blur(update_balance);
$("#addrow").click(function(){
$(".item-row:last").after('<tr class="item-row"><td class="item-name"><div class="delete-wpr"><textarea name=producto[]></textarea><a class="delete" href="javascript:;" title="Remove row"><img src="imagenes/eliminar_concepto.jpg" width="22" height="23" alt="Eliminar" border=0 /></a></div></td><td class="description"><textarea name=descripcion[]></textarea></td><td><textarea class="cost" name="precio[]">0€</textarea></td><td><textarea class="qty" name="cantidad[]">0</textarea></td><td> <textarea name="iva[]" class="iva" id="iva[]">9%</textarea></textarea></td><td><span class="siniva">0€</span></td><td><span class="price">0€</span></td></tr>');
if ($(".delete").length > 0) $(".delete").show();
bind();
});
bind();
$(".delete").live('click',function(){
$(this).parents('.item-row').remove();
update_total();
if ($(".delete").length < 2) $(".delete").hide();
});
$("#cancel-logo").click(function(){
$("#logo").removeClass('edit');
});
$("#delete-logo").click(function(){
$("#logo").remove();
});
$("#change-logo").click(function(){
$("#logo").addClass('edit');
$("#imageloc").val($("#image").attr('src'));
$("#image").select();
});
$("#save-logo").click(function(){
$("#image").attr('src',$("#imageloc").val());
$("#logo").removeClass('edit');
});
$("#date").val(print_today());
});
Cita:
Desde ya, muchas gracias <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type='text/javascript' src='example.js'></script>
<script type='text/javascript' src='jquery-1.3.2.min.js'></script>
</head>
<body>
<form action="factura.php" method="get" name="form1">
<div id="page-wrap">
<div id="page-wrap2">
<div id="identity">
<table id="items" AL>
<tr>
<th>Producto</th>
<th>Descripcion</th>
<th>Precio</th>
<th>Unidades</th>
<th>IVA</th>
<th>SIN IVA</th>
<th>Subtotal</th>
</tr>
<tr class="item-row">
<td class="item-name"><div class="delete-wpr">
<textarea name="producto[]"></textarea>
<a class="delete" href="javascript:;" title="Remove row"><img src="imagenes/eliminar_concepto.jpg" width="22" height="23" alt="Eliminar" border=0 /></a></div></td>
<td class="description"><textarea name="descripcion[]"></textarea></td>
<td><textarea name="precio[]" class="cost" id="precio[]">0€</textarea></td>
<td><textarea name="cantidad[]" class="qty" id="cantidad[]">0</textarea></td>
<td>
<? /*
<select name="iva[]" class="iva" id="iva[]" onChange="bind()">
<option value="21">21%</option>
<option value="9">9%</option>
<option value="1">1%</option>
</select>
ç*/?>
<textarea name="iva[]" class="iva" id="iva[]"><?=$iva_usr?>%</textarea>
</td>
<td><span class="siniva">0€</span></td>
<td><span class="price">0€</span></td>
</tr>
<tr id="hiderow">
<td colspan="7"><a id="addrow" href="javascript:;" title="Add a row"><img src="imagenes/nuevo_concepto.jpg" width="34" height="32" alt="Nuevo Concepto" /></a></td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Subtotal</td>
<td class="total-value"> </td>
<td class="total-value"> </td>
<td class="total-value"><div id="subtotal">0.00€</div></td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Total</td>
<td class="total-value"> </td>
<td class="total-value"> </td>
<td class="total-value"><div id="total">0.00€</div></td>
</tr>
<tr>
<td colspan="7" class="blank"><table border="0" align="right" cellpadding="5" cellspacing="5">
<tr>
<td>IVA 21% </td>
<td><div id="iva21">0%</div></td>
<td>IVA 9%</td>
<td><div id="iva9">0%</div></td>
<td>IVA 1%</td>
<td><div id="iva1">0%</div></td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line balance">Total factura:</td>
<td class="total-value balance"> </td>
<td class="total-value balance"> </td>
<td class="total-value balance"><div class="due">0.00€</div></td>
</tr>
</table>
</form>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type='text/javascript' src='example.js'></script>
<script type='text/javascript' src='jquery-1.3.2.min.js'></script>
</head>
<body>
<form action="factura.php" method="get" name="form1">
<div id="page-wrap">
<div id="page-wrap2">
<div id="identity">
<table id="items" AL>
<tr>
<th>Producto</th>
<th>Descripcion</th>
<th>Precio</th>
<th>Unidades</th>
<th>IVA</th>
<th>SIN IVA</th>
<th>Subtotal</th>
</tr>
<tr class="item-row">
<td class="item-name"><div class="delete-wpr">
<textarea name="producto[]"></textarea>
<a class="delete" href="javascript:;" title="Remove row"><img src="imagenes/eliminar_concepto.jpg" width="22" height="23" alt="Eliminar" border=0 /></a></div></td>
<td class="description"><textarea name="descripcion[]"></textarea></td>
<td><textarea name="precio[]" class="cost" id="precio[]">0€</textarea></td>
<td><textarea name="cantidad[]" class="qty" id="cantidad[]">0</textarea></td>
<td>
<? /*
<select name="iva[]" class="iva" id="iva[]" onChange="bind()">
<option value="21">21%</option>
<option value="9">9%</option>
<option value="1">1%</option>
</select>
ç*/?>
<textarea name="iva[]" class="iva" id="iva[]"><?=$iva_usr?>%</textarea>
</td>
<td><span class="siniva">0€</span></td>
<td><span class="price">0€</span></td>
</tr>
<tr id="hiderow">
<td colspan="7"><a id="addrow" href="javascript:;" title="Add a row"><img src="imagenes/nuevo_concepto.jpg" width="34" height="32" alt="Nuevo Concepto" /></a></td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Subtotal</td>
<td class="total-value"> </td>
<td class="total-value"> </td>
<td class="total-value"><div id="subtotal">0.00€</div></td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Total</td>
<td class="total-value"> </td>
<td class="total-value"> </td>
<td class="total-value"><div id="total">0.00€</div></td>
</tr>
<tr>
<td colspan="7" class="blank"><table border="0" align="right" cellpadding="5" cellspacing="5">
<tr>
<td>IVA 21% </td>
<td><div id="iva21">0%</div></td>
<td>IVA 9%</td>
<td><div id="iva9">0%</div></td>
<td>IVA 1%</td>
<td><div id="iva1">0%</div></td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line balance">Total factura:</td>
<td class="total-value balance"> </td>
<td class="total-value balance"> </td>
<td class="total-value balance"><div class="due">0.00€</div></td>
</tr>
</table>
</form>
</body>
</html>