How to get the total sum of all the subtotals?

-1
//Creo una función donde voy a sumar todos los valores totales de la última columna
var sumaTotales = function(){
  //Inicializo en 0 el valor a establecer indicando el resultado de la sumatoria
  var totales = 0;
  //Recorro todas las filas de la tabla
  $('tr','#tablaDetalleMaterialEinsumo').each(function(){
     //Obtengo el campo del cual quiero tomar el valor para sumarlo
     var campo_total = $(this).find('input[type="text"]:eq(4)');
     //Agrego a totales el nuevo valor
     var numero = parseInt(campo_total.val()); totales += isNaN(numero) ? 0 : numero; 
   });
  //Establezco el resultado de la suma
  $('#valor_unitario').val(totales);
  $('#suma_1').val(totales);
};



//Esta tabla es para agregar Detalle materiales, repuestos e insumos
var sumaValores = function(){
    //Obtengo la última fila ingresada en la tabla
    var fila = $("#tablaDetalleMaterialEinsumo").find('tr:last');
    //Obtengo el campo referente a la cantidad de insumo ':eq(2)'
    var campo1 = fila.find('input[type="text"]:eq(2)'); 
    //Obtengo el campo referente al precio_unitario_insumo ':eq(3)'
    var campo2 = fila.find('input[type="text"]:eq(3)');
    //Obtengo el campo donde se escribirá el resultado
    var destino = fila.find('input[type="text"]:eq(4)');

    campo1.on('change', function(){
      destino.val(parseInt(campo1.val())*parseInt(campo2.val()));
        //Llamo a la función para que se actualice cada vez que se cambie algo en este campo
        sumaTotales();

      });

    campo2.on('change', function(){
      destino.val(parseInt(campo1.val())*parseInt(campo2.val()));
        //Llamo a la función para que se actualice cada vez que se cambie algo en este campo
        sumaTotales();

      }); 
  };

  function funcNuevaDetalleMaterialEinsumo() 
  {
    $("#tablaDetalleMaterialEinsumo")
    .append
    (
      $('<tr>')
      .append
      (
        $('<td>')
        .append
        (
          $('<input>').attr('type', 'text').addClass('form-control').attr('name', 'detalle_insumo[]')
          )
        )
      .append
      (
        $('<td>')
        .append
        (
          $('<input>').attr('type', 'text').addClass('form-control').attr('name', 'unidad_insumo[]')
          )
        )
      .append
      (
        $('<td>')
        .append
        (
          $('<input>').attr('type', 'text').addClass('form-control').attr('name', 'cantidad_insumo[]')
          )
        )
      .append
      (
       $('<td>')
       .append
       (
        $('<input>').attr('type', 'text').addClass('form-control').attr('name', 'precio_unitario_insumo[]')
        )
       )
      .append
      (
       $('<td>')
       .append
       (
        $('<input>').attr('type', 'text').addClass('form-control').attr('name', 'valor_total_insumo[]').prop('readonly', true)
        )
       )
      .append
      (
        $('<td>').addClass('text-center')

        .append
        (
          $('<div>').addClass('btn btn-danger').text('Eliminar')
          )            
        )        
      );
    sumaValores();

  }
<div class="caja8">
  <div class="alcance">
    <h1>GASTOS GENERALES</h1> 
  </div>
  <div class="container theme-showcase" role="main">
    <br>
    <div class="form-group">
     <table class='table table-bordered table-hover' id="gastos_totales_generales">
      <tr>
        <th>DESCRIPCION</th>
        <th>PORCENTAJE</th>
        <th>SUB TOTAL</th>
        <th>TOTAL</th>
      </tr>
      <tr>
        <th>GASTOS GENERALES</th>
        <td><input type="text" class="form-control" name="porcentaje_gasto_generales" value=""></td>
        <td><input type="text" class="form-control" name="sub_total_gasto_generales"  id="sub_total_gasto_generales" value="" readonly></td>
        <td><input type="text" class="form-control" name="total_gasto_generales" value="" readonly></td>
      </tr>
      <tr>
        <th>UTILIDAD</th>
        <td><input type="text" class="form-control" name="porcentaje_utilidad" value=""></td>
        <td><input type="text" class="form-control" name="sub_total_utilidad"  value="" readonly></td>
        <td><input type="text" class="form-control" name="total_utilidad" value="" readonly></td>
      </tr>
    </table>                 
  </div>  
</div>

    
asked by Monsines 06.10.2017 в 05:17
source

1 answer

0

This is the answer: What I did was to go through all the tables that are separated and I made a function that would add all the subtotals, and then they are shown in the subtotal of general expenses

//esta funcion es para sumar todo los subtotales de los detalles
var sumaTotalGeneral = function(){
  //Inicializo en 0 el valor a establecer indicando el resultado de la sumatoria
  var totalgeneralsuma0 = 0;
  var totalgeneralsuma1 = 0;
  var totalgeneralsuma2 = 0;
  //Recorro todas las filas de la tabla
  $('tr','#tablaDetalleMaterialEinsumo').each(function(){
     //Obtengo el campo del cual quiero tomar el valor para sumarlo
     var campo_total0 = $(this).find('input[type="text"]:eq(4)');
     //Agrego a totales el nuevo valor
     var numero0 = parseInt(campo_total0.val()); 
     totalgeneralsuma0 += isNaN(numero0) ? 0 : numero0; 
   });


  $('tr','#tablaDetallePersonaEmaquina').each(function(){
     //Obtengo el campo del cual quiero tomar el valor para sumarlo
     var campo_total2 = $(this).find('input[type="text"]:eq(5)');
     //Agrego a totales el nuevo valor
     var numero1 = parseInt(campo_total2.val()); 
     totalgeneralsuma1 += isNaN(numero1) ? 0 : numero1; 
   });

  $('tr','#tablaDetalleGastoGeneral').each(function(){
     //Obtengo el campo del cual quiero tomar el valor para sumarlo
     var campo_total3 = $(this).find('input[type="text"]:eq(4)');
     //Agrego a totales el nuevo valor
     var numero2 = parseInt(campo_total3.val()); 
     totalgeneralsuma2 += isNaN(numero2) ? 0 : numero2; 
   });


  var to= totalgeneralsuma0+totalgeneralsuma1+totalgeneralsuma2;
  //Establezco el resultado de la suma
  $('#sub_total_gasto_generales').val(to);

  };
    
answered by 06.10.2017 в 16:15