<?php
    require('../conexion.php');
    $from_date = $_POST['from_date'];
    $to_date = $_POST['to_date'];
    $i=0;

    $result = query("SELECT pd.id, SUM(pdn.pdn_cantidad) cant_productos, pd.pro_nombre, pdn.pdn_precio,
        SUM(pdn.pdn_cantidad * pdn.pdn_precio) total FROM productos_nota AS pdn 
        INNER JOIN productos AS pd ON pd.id = pdn.id_producto
        INNER JOIN notas_venta AS nv ON nv.id = pdn.id_nota_venta
        WHERE DATE(pdn.created_at) BETWEEN '$from_date' AND '$to_date' 
        AND pdn.pdn_estatus NOT IN ('eliminado','cancelado')
        AND ntv_estatus IN ('finalizado','impreso')
        GROUP BY pd.id ORDER BY pd.pro_nombre; ");
        
    while($row = mysqli_fetch_assoc($result)){
        $rowdata[$i] = $row;
        $i++;
    }

    if ($i>0){
        $res =  json_encode($rowdata,JSON_UNESCAPED_UNICODE);
    } else {
        $res =  '[{"id":"0"}]';	
    }
    echo $res;
?>