<?php
    require('../conexion.php');
    $from_date = $_GET['dateIni'];
    $to_date = $_GET['dateFin'];
   
    /* $unions="";

    $qry = query("SELECT vpn_version FROM versiones_productos_nota");
    if($qry->num_rows > 0){
        while($res=mysqli_fetch_assoc($qry))
        {
                $ver = $res['vpn_version'];
                $union = "UNION SELECT * FROM productos_nota_$ver";
                $unions = $unions. " " . $union;
        }
    } */
    $timestamp = strtotime($to_date);
    $year = date("Y", $timestamp);

    
    $timestamp2 = strtotime($from_date);
    $year2 = date("Y", $timestamp);
    if ($year == $year2) {
        $query = query("SHOW TABLES LIKE 'historico_$year';");
        if ($query->num_rows > 0 ) {
            $nota=query("SELECT COUNT(*) 'RES', SUM(ntv_tarjeta+ntv_efectivo) TOTALTAREFE, SUM(nta.ntv_no_personas) PERSONAS FROM notas_venta as nta 
            WHERE nta.ntv_fecha_creacion BETWEEN '$from_date' AND '$to_date' AND ntv_estatus IN ('finalizado','impreso')");
            $resultado=mysqli_fetch_assoc($nota);

            $qry_mesas=query("SELECT COUNT(*) 'RES' FROM notas_venta as nta WHERE nta.ntv_fecha_creacion BETWEEN '$from_date' AND '$to_date' AND ntv_estatus IN ('finalizado','impreso') AND ntv_no_mesa!=''"); 
            $mesas=mysqli_fetch_assoc($qry_mesas);

            $qry_efect=query("SELECT SUM(ntv_efectivo) 'RES' FROM notas_venta as nta 
            WHERE nta.ntv_fecha_creacion BETWEEN '$from_date' AND '$to_date' 
            AND ntv_estatus IN ('finalizado','impreso') and ntv_id_tipo_pago!=1"); 
            $efect=mysqli_fetch_assoc($qry_efect);


            $qry_tarj=query("SELECT  SUM(ntv_tarjeta) 'RES' FROM notas_venta as nta 
            WHERE nta.ntv_fecha_creacion BETWEEN '$from_date' AND '$to_date' 
            AND ntv_estatus IN ('finalizado','impreso') AND ntv_id_tipo_pago!=2");
            $tarj=mysqli_fetch_assoc($qry_tarj);

            $notas= $resultado['RES'];
            $total_efe_tar= $resultado['TOTALTAREFE'];
            $personas = $resultado['PERSONAS'];
            $mesas=$mesas['RES'];
            $efect= $efect['RES'];
            $tarj= $tarj['RES'];
            
            $datos = [
                'notas' => $notas,
                'total_efe_tar' =>  number_format($total_efe_tar),
                'personas' => $personas,
                'mesas' => $mesas,
                'efect' =>  number_format($efect),
                'tarj' =>  number_format($tarj),
            ];
        }
    }else{
        $datos = [
            'notas' => 0,
            'total_efe_tar' =>  number_format(0),
            'personas' => 0,
            'mesas' => 0,
            'efect' =>  number_format(0),
            'tarj' =>  number_format(0),
        ];
    }
    
   
    echo json_encode($datos);
?>