Foros del Web » Programando para Internet » Javascript »

Facturas con 3 IVAS distintos

Estas en el tema de Facturas con 3 IVAS distintos en el foro de Javascript en Foros del Web. Necesito de vuestra ayuda ya que Javascript no es mi fuerte y llevo dias peleandome con un código. Resulta que quiero hacer una factura donde ...
  #1 (permalink)  
Antiguo 10/06/2013, 02:57
 
Fecha de Ingreso: agosto-2007
Mensajes: 9
Antigüedad: 16 años, 7 meses
Puntos: 0
Facturas con 3 IVAS distintos

Necesito de vuestra ayuda ya que Javascript no es mi fuerte y llevo dias peleandome con un código.

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:
function 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());

});
Factura.php
Cita:
<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">&nbsp;</td>
<td class="total-value">&nbsp;</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">&nbsp;</td>
<td class="total-value">&nbsp;</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%&nbsp;&nbsp;</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">&nbsp;</td>
<td class="total-value balance">&nbsp;</td>
<td class="total-value balance"><div class="due">0.00€</div></td>
</tr>
</table>

</form>

</body>

</html>
Desde ya, muchas gracias

Última edición por vinsen; 10/06/2013 a las 03:11
  #2 (permalink)  
Antiguo 11/06/2013, 02:53
 
Fecha de Ingreso: agosto-2007
Mensajes: 9
Antigüedad: 16 años, 7 meses
Puntos: 0
Respuesta: Facturas con 3 IVAS distintos

Os pongo una imagen para que veais como lo tengo. El problema es separar los ivas correspondientes a cada casilla abajo marcada en rojo.

Gracias

  #3 (permalink)  
Antiguo 11/06/2013, 03:25
Avatar de dontexplain  
Fecha de Ingreso: junio-2012
Mensajes: 536
Antigüedad: 11 años, 10 meses
Puntos: 127
Respuesta: Facturas con 3 IVAS distintos

Dónde está el problema exactamente. En hacer el cálculo del total con cada valor de IVA o cuál es.
__________________
21añero.
HTML/CSS, PHP, JS/jQuery, Mysql; NodeJS/Socket.io (& V8); C++ ; Python (wxpy); Ensamblador.
Músico (clarinetista/pianista) y compositor
  #4 (permalink)  
Antiguo 11/06/2013, 04:12
 
Fecha de Ingreso: agosto-2007
Mensajes: 9
Antigüedad: 16 años, 7 meses
Puntos: 0
Respuesta: Facturas con 3 IVAS distintos

Muchisimas gracias por contestar.

Exactamente. Como tu bien dices dices, ese es mi problema. Te indico url de imagen ampliada:

http://www.subirimagenes.com/otros-factuacion-8485495.html

No consigo hacer el sumatorio del total de IVA´s agrupados por 21%, 9% y 1% y meterlo en la caja de texto correspondiente. Quiero que en cada cambio que se haga en las cajas de texto vuelva a realizar la operación que te indico.

Muchas gracias
  #5 (permalink)  
Antiguo 11/06/2013, 04:32
 
Fecha de Ingreso: agosto-2007
Mensajes: 9
Antigüedad: 16 años, 7 meses
Puntos: 0
Respuesta: Facturas con 3 IVAS distintos

Te concreto un poco mas con lo que he avanzado.

Código:
  $('.siniva').each(function(z){
    siniva = $(this).html().replace("€","");
	
//	alert(document.getElementById('iva'));
	//alert(t_siniva);
  });
  
  $('.iva').each(function(r){
    t_siniva = $(this).val().replace("%","");
	if (t_siniva == 21) {
		if (!isNaN(t_siniva)) t_siniva21 += Number($('.siniva').val());
		alert($('.siniva').html());
	}
	if (t_siniva == 9) {
		if (!isNaN(t_siniva)) t_siniva9 +=  Number($('.siniva').val());
	}
	if (t_siniva == 1) {
		if (!isNaN(t_siniva)) t_siniva1 += Number($('.siniva').val());
	}

  });

Ahora recorro bien los IVA´s y me los detecta, lo que necesito ahora es sumar la casilla "siniva" correspondiente a esa linea. Dicha casilla no es caja de texto sino utilizo la etiqueta span con una clase: "<span class="siniva">0€</span>"

No se si me vas entendiendo
  #6 (permalink)  
Antiguo 12/06/2013, 02:07
 
Fecha de Ingreso: agosto-2007
Mensajes: 9
Antigüedad: 16 años, 7 meses
Puntos: 0
Respuesta: Facturas con 3 IVAS distintos

Un caso ejemplo sería:

PRECIO---------UNIDADES--------IVA-------------SIN IVA-----------------TOTAL
100€-------------1-------------21%------------21,00€-----------------121,00€
100€-------------1-------------9%--------------9,00€-----------------109,00€
100€-------------1-------------1&-------------1,00€------------------101,00€
100€-------------1-------------21%------------21,00€-----------------121,00€

TOTAL IVA 21% = 42,00€
TOTAL IVA 9% = 9,00€
TOTAL IVA 1% = 1,00€

Quiero recorrer la tabla y en el campo IVA identificar el tipo de iva es (21%, 9%, 1%) e ir haciendo un sumatorio del campo SIN IVA

Así recorro la columna IVA, pero no consigo sacar dentro de la misma matriz el campo SIN IVA.
Código:
  $('.iva').each(function(r){
    t_siniva = $(this).val().replace("%","");
	if (t_siniva == 21) {
		t_siniva21 += Number($('.siniva').val());
	}
	if (t_siniva == 9) {
		if (!isNaN(t_siniva)) t_siniva9 +=  Number($('.siniva').val());
	}
	if (t_siniva == 1) {
		if (!isNaN(t_siniva)) t_siniva1 += Number($('.siniva').val());
	}

  });

¿Alguna sugerencia?

Gracias

Última edición por vinsen; 12/06/2013 a las 02:15

Etiquetas: distintos, facturas, html, input, js, php, select
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:12.