<?php

require('../conexion.php');
   
    $from_date=$_POST['dateIni'];
    $to_date=$_POST['dateFin'];
    $timestamp = strtotime($to_date);
    $year = date("Y", $timestamp);
    $i = 0;
    
    $timestamp2 = strtotime($from_date);
    $year2 = date("Y", $timestamp);
    if ($year == $year2) {
        $query = query("SHOW TABLES LIKE 'historico_$year';");
        //if ($query->num_rows > 0 ) {
            $query = "SELECT nta.id nta_id, nta.ntv_no_nota, nta.ntv_fecha_creacion, nta.ntv_hora_ini, nta.ntv_subtotal, 
                nta.ntv_total_nota, nta.ntv_no_mesa, nta.ntv_no_personas, nta.ntv_tarjeta, nta.ntv_efectivo
                FROM notas_venta nta
                WHERE  nta.ntv_fecha_creacion BETWEEN  '$from_date' AND '$to_date' AND ntv_estatus IN ('finalizado','impreso')
                GROUP BY nta.ntv_no_nota";
            $result =query($query);
            
            while($row = mysqli_fetch_assoc($result)){
                $rowdata[$i] = $row;
                $i++;
            }
        //}
    }
    
    if ($i>0){
        $res =  json_encode($rowdata,JSON_UNESCAPED_UNICODE);
    } else {
        $res =  '[{"nta_id":"0"}]';	
    }
    echo $res;
    
?>