<?php

    $id = intval($_REQUEST['id']);
    include '../rdb/conn.php';
    include '../comunes_php/cors.php';
    include "../comunes_php/Log.class.php"; 
    $log = new Log("app_trace", "./");
    $parametros = print_r($_REQUEST,TRUE); 
    $log->insert($parametros, false, false, false);
    
    $sql= " select count(*) from producto_materia_prima where producto_id = $id";
    
    $rs = mysqli_query( $conn,$sql);
    $row = mysqli_fetch_row($rs);
    $result["total"] = $row[0];
    

    $result = array();
    
    $sql="select ap.producto_id,ap.materia_prima_id,m.nombre materia_prima, ap.cantidad 
    from producto_materia_prima ap , 
    materia_prima m where  ap.materia_prima_id =m.id and  ap.materia_prima_id=m.id and ap.producto_id=$id ";
    
    $sql .= "order by ap.materia_prima_id";
    
    $rs = mysqli_query( $conn,$sql);

    $log->insert($sql, false, false, false);

    
     $items = array();
    while($row = mysqli_fetch_object($rs)){
        array_push($items, $row);
    }
    $result["rows"] = $items;
    $result["sql"] = $sql;

    echo json_encode($result);
  
    
?>