Ver Mensaje Individual
  #1 (permalink)  
Antiguo 23/01/2012, 08:14
Avatar de kimmy
kimmy
 
Fecha de Ingreso: julio-2008
Mensajes: 841
Antigüedad: 15 años, 9 meses
Puntos: 15
Pregunta Funciones que calculan al principio pero luego dan resultados errados

Buenos días:

Tengo unos calculos con varias funciones y al introducir los datos ordenadamente los realiza perfectamente, pero si después cambio algunos ya no da los resultados adecuados. Cómo puedo evitar esto????.

Este es el código:

Código PHP:
Ver original
  1. <script type="text/javascript">
  2. // dias es el total de días trabajados
  3. // sueldo es el sueldo básico del empleado
  4. // indice corresponde al input text que se debe actualizar
  5. var sq;
  6. var dd;
  7. var dv;
  8.  
  9. function calcula(dias, sueldo_diario, indice) {
  10.        sq = document.getElementById('sueldo_quinc' + indice).value = (parseFloat(dias) * parseFloat(sueldo_diario)).toFixed(2);
  11. }
  12.  
  13. function deducciones(otros_desc, ivss, paro_for, lph, indice2) {
  14.     dd= document.getElementById('total_deducc' + indice2).value = (parseFloat(otros_desc) + parseFloat(ivss) + parseFloat(paro_for) + parseFloat(lph)).toFixed(2);
  15. }
  16.  
  17. function total_dev(otros_ing, sq, dd, indice3) {
  18.     dv = document.getElementById('total_deveng' + indice3).value = ((parseFloat(otros_ing) + parseFloat(sq)) - parseFloat(dd)).toFixed(2);
  19.    
  20. }
  21.  
  22. function Mostrar()
  23.  
  24. {
  25.  
  26. msg="Opcion 1:"+sq+"\n";
  27.  
  28. msg+="Opcion 2:"+dd+"\n";
  29.  
  30. alert(msg);
  31.  
  32. }
  33.  
  34. function totalizar(){
  35.     var chks = document.getElementsByName('total_deveng[]');
  36.     var totales = 0;
  37.     for(var i = 0; i < chks.length; i++) {
  38.         var valor = parseFloat(chks[i].value);
  39.         if(isNaN(valor) == false) {
  40.             totales += parseFloat(chks[i].value);
  41.         }
  42.     }
  43.     document.getElementById("total").value = totales;
  44.      
  45. }
  46.  
  47. </script>
  48.     <br />
  49.     <?php if($_SESSION['user']==true){ ?>
  50.     <form action="nomina_reg.php" method="post" name="form1" >
  51.       <table width="750" border="0" class="tabla2">
  52.         <tr>
  53.           <td colspan="5" class="td2" bgcolor="#95bce2">UNIDAD EDUCATIVA EL MILAGRO DE LA PERLA</td>
  54.           </tr>
  55.         <tr>
  56.           <td colspan="5">NOMINA PARA EL PAGO DE SUELDOS EMPLEADOS</td>
  57.           </tr>
  58.       <tr>
  59.       <td width="26%">
  60.       <div class="demo">
  61.       <p>Fecha Desde: <input type="text" id="datepicker_1" name="desde" class="fechador"></p>
  62.       </div>
  63.       </td>
  64.       <td width="26%">
  65.       <div class="demo">
  66.       <p>Fecha Hasta: <input type="text" id="datepicker_2" name="hasta" class="fechador"></p>
  67.       </div>
  68.       </td>
  69.       </tr>
  70.       </table>
  71.       <br />
  72.       <table width="750" border="1"  bordercolor="#95bce2"  class="tabla3">
  73.   <tr bgcolor="#95bce2 ">
  74.     <td width="3%" class="td3">Nº</td>
  75.     <td width="3%" class="td3">CI</td>
  76.     <td width="10%" class="td3">APELLIDOS</td>
  77.     <td width="11%" class="td3">NOMBRES</td>
  78.     <td width="4%" class="td3">DIAS</td>
  79.     <td width="8%" class="td3">SUELDO BÁSICO</td>
  80.     <td width="8%" class="td3">SUELDO DIARIO</td>    
  81.     <td width="8%" class="td3">SUELDO B. QUINCENA</td>
  82.     <td width="8%" class="td3">IVSS</td>
  83.     <td width="8%" class="td3">PARO FORSOZO</td>
  84.     <td width="8%" class="td3">LPH</td>
  85.     <td width="8%" class="td3">OTROS DESCUENT</td>
  86.     <td width="8%" class="td3">OTROS INGRESOS</td>
  87.     <td width="8%" class="td3">TOTAL DEDUCCIONES</td>
  88.     <td width="8%" class="td3">TOTAL DEVENGADO</td>
  89.   </tr>
  90.  
  91.   <?php
  92. $indice = 1;
  93. $indice2 = 1;
  94. $indice3 = 1;
  95.  
  96. $rs = mysql_query("SELECT * FROM empleados ORDER BY id_empleado ASC");
  97. while($result = mysql_fetch_array($rs)) {
  98. }
  99.     $ivss = number_format(($result['sueldo_basico']*4/100),2,'.','');
  100.     $paro_for = number_format(($result['sueldo_basico']*0.5/100),2,'.','');
  101.     $lph = number_format(($result['sueldo_basico']*1/100),2,'.','');
  102.     $sueldo_diario = number_format(($result['sueldo_basico']/30),2,'.','');
  103.  
  104. ?>
  105.     <tr>
  106.         <td><?php echo $result['id_empleado']; ?><input type="hidden" name="id_empleado[]" value="<?php echo $result['id_empleado']; ?>" /></td>
  107.         <td><?php echo $result['ci']; ?><input type="hidden" name="ci[]" value="<?php echo $result['ci']; ?>" /></td>
  108.         <td><?php echo "{$result['papellido']} {$result['sapellido']}"; ?><input type="hidden" name="papellido[]" value="<?php echo $result['papellido']; ?>" /></td>
  109.         <td><?php echo "{$result['pnombre']} {$result['snombre']}"; ?><input type="hidden" name="pnombre[]" value="<?php echo $result['pnombre']; ?>" /></td>
  110.        
  111.         <td><input name="dias[]" type="text" id="dias<?php echo $indice; ?>" size="2" value="" onBlur="calcula(this.value, '<?php echo $sueldo_diario; ?>', <?php echo $indice; ?>);" /></td>
  112.         <td><label name="sueldo_basico" id="sueldo_basico" size="2" readonly="readonly" value="" ><?php echo $result['sueldo_basico']; ?><input type="hidden" name="sueldo_basico[]" value="<?php echo $result['sueldo_basico']; ?>" /></label></td>
  113.         <td><input type="text" name="sueldo_diario[]" id="sueldo_diario" size="5" readonly="readonly" value="<?php echo $sueldo_diario; ?>" ></td>
  114.         <td><input name="sueldo_quinc[]" type="text" id="sueldo_quinc<?php echo $indice; ?>" size="5" value="" readonly="readonly" /></td>
  115.  
  116.         <td><input type="text" name="ivss[]" id="ivss" size="3" readonly="readonly" value="<?php echo $ivss; ?>"  /></td>
  117.         <td><input type="text" name="paro_for[]" id="paro_for" size="3" readonly="readonly" value="<?php echo $paro_for; ?>" ></td>
  118.         <td><input type="text" name="lph[]" id="lph" size="3" readonly="readonly" value="<?php echo $lph; ?>" ></td>
  119.        
  120.         <td><input name="otros_desc[]" type="text" id="otros_desc<?php echo $indice2; ?>" size="5" value="" onBlur="deducciones(this.value, '<?php echo $ivss; ?>', '<?php echo $paro_for; ?>', '<?php echo $lph; ?>', <?php echo $indice2; ?>);" /></td>
  121.         <td><input name="otros_ing[]" type="text" id="otros_ing<?php echo $indice3; ?>" size="5" value="" onChange="total_dev(this.value, sq, dd, <?php echo $indice3; ?>);" /></td>
  122.  
  123.         <td><input name="total_deducc[]" type="text" id="total_deducc<?php echo $indice2; ?>" size="5" value="" readonly="readonly" /></td>
  124.        <td><input name="total_deveng[]" type="text" id="total_deveng<?php echo $indice3; ?>" size="5" value="" readonly="readonly"  /></td>
  125.     </tr>
  126. <?php
  127.     $indice ++;
  128.     $indice2 ++;
  129.     $indice3 ++;
  130. } // fin de while
  131. ?>  <tr>
  132.       <td>&nbsp;</td>
  133.  
  134.     <td>&nbsp;</td>
  135.     <td>&nbsp;</td>
  136.     <td>&nbsp;</td>
  137.     <td>&nbsp;</td>
  138.     <td>&nbsp;</td>
  139.     <td>&nbsp;</td>
  140.     <td>&nbsp;</td>
  141.     <td>&nbsp;</td>
  142.     <td>&nbsp;</td>
  143.     <td>&nbsp;</td>
  144.     <td>&nbsp;</td>
  145.     <td>&nbsp;</td>
  146.     <td class="td3">TOTAL</td>
  147.     <td><input type="text" id="total" name="total"  size="5" value="" readonly="readonly" onMouseOver="javascript:totalizar();" /></td>
  148.   </tr>
  149. </table>
  150.   <tr>
  151.   <th colspan="2"><input type="submit" name="button" id="button" value="Guardar" /> - <input type="reset" name="Reset" id="button" value="Borrar" />
  152.   </th>
  153.   </tr>
  154. </form>

Cómo puedo evitar esto???? Gracias
__________________
Caminando con el corazón partío