mercredi 20 juin 2018

Enter data to 2 tables

I'm doing a shopping module. The code is already made to enter the sales table and sales detail. When I enter a product it works correctly but when I want to enter more than two products at the same time I get this error:

Uninitialized string offset: 1

It is worth mentioning that the data is sent through a matrix with js and in the controller I receive it through the while loop. This is the JavaScript code:

<script>

$(document).ready(function(){
  $("#bt_add").click(function(){
        agregar();
  });
});
   var cont=0;
   total = 0;
   subtotal=[];
   precioIva=[];

  $("#guardar").hide();
  $("#pidConcepto").change(mostrarValores);

  function mostrarValores() {
    datosArticulo=document.getElementById('pidConcepto').value.split("_");
    $('#pprecio').val(datosArticulo[1]);
    $('#pimpuesto').val(datosArticulo[2]);
  }

  function agregar() {

        datosArticulo=document.getElementById('pidConcepto').value.split('_');

        idConceptosPagos=datosArticulo[0];
        consepto=$("#pidConcepto option:selected").text();
        precio=$("#pprecio").val();
        impuesto=$("#pimpuesto").val();
        cantidad=$("#pcantidad").val();

        if(consepto!=" " &&  precio!=" " && impuesto!=" " && cantidad!= " ")
        {

              precioCantidad = precio * cantidad;
              iva = precioCantidad * 16 / 100;
              subtotal[cont] = precioCantidad;
              precioIva[cont] = iva;
              total1=precioIva[cont]+subtotal[cont];
              total=total+total1;

              var fila ='<tr class="selected" id="fila' + cont +'" ><td><button type="button" class="btn btn-warning" onclick="eliminar('+cont+');">X</button></td><td><input type="hidden" name="idConceptosPagos[]" value="'+idConceptosPagos+'">'+consepto+'</td><td><input type="text" name="precio[]" readonly="readonly" style="text-align: right;" value="$/. '+precio+'"></td><td><input type="text" name="cantidad[]" disabled  value="'+cantidad+'"></td><td type="text" name=precioCantidad[] style="text-align: right;" readonly>$/.'+precioCantidad+'</td><td type="number" style="text-align: right;" name=iva[]>$/.'+iva+'</td><td style="text-align: right;">$/.'+ total1+'</td></tr>';

              cont++;
              limpiar();
              $("#total").html("$/. " + total);
              $("#total_venta").val(total);
              evaluar();
              $("#detalles").append(fila);

        }else {


              alert("Error al ingresar el detalle de la venta, revise los datos del articulo");

        }
  }

function limpiar() {
  $("#pfechaPago").val("");
  $("#pprecio_venta").val("");   
 }

  function evaluar(){

  if (total>0) {
        $("#guardar").show();
  }
  else {
       $("#guardar").hide();  
  }
}

function eliminar(index) {

 total=total-subtotal[index];
 $("#total").html("$/. " + total);
 $("#total_venta").val(total);
 $("#fila" + index).remove();
 evaluar();
}

and in the controller I do it this way:

$venta = new VentaUtp;
$venta->idAlumno=$request->get('idAlumno');
$venta->idCiclo=$request->get('idCiclo');
$venta->clave=$request->get('clave');
$venta->anio=$request->get('anio');
$venta->mes=$request->get('mes');
$venta->pagado= 'N';
$venta->cantidadProgramada = $request->get('total_venta');
$venta->cantidadPagada = null;

$venta->save();


$idAlumno = $request->get('idAlumno');
$idCiclo = $request->get('idCiclo');
$idConceptosPagos = $request->get('idConceptosPagos');
$precio = $request->get('precio');
$pcantidad = $request->get('pcantidad');

$cont=0;

while ($cont < count($idConceptosPagos)) 
{
    $detalle = new DetalleVentaUtp;
    $detalle->idAlumnoCxC= $venta->idAlumnosCxC;
    $detalle->idAlumno = $idAlumno[$cont];
    $detalle->idCiclo = $idCiclo[$cont];
    $detalle->idConseptoPago= $idConceptosPagos[$cont];
    $detalle->cantidad=$pcantidad[$cont];
    $detalle->save();
    $cont= $cont + 1;
}

I think the error comes from this assignment:

$cont = 0;

But I do not know why. How can I solve this?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2M79yFO
via IFTTT

Aucun commentaire:

Enregistrer un commentaire