<?

echo 'iiiiiiinicio con exito<br>';

require('/home/opshmfm/public_html/agencias/webs/cnt/tissels.php');

///////////////////////////////////////////////////////////////////////////////

$update_ai="UPDATE `thllistarb` SET estado='Pendiente' where estado='Standby' and locexterno not like 'HLL%'";
mysql_query($update_ai);
//echo '<font color="blue">'.$update_ai.'</font><br><br>';



$update_ai2="UPDATE `thllistarb` SET estado='Pendiente' where estado='Aprovechada' ";
mysql_query($update_ai2);
//echo '<font color="blue">'.$update_ai2.'</font><br><br>';
///////////////////////////////////////////////////////////////////////////////



$req="SELECT * from thllistarb where
(estado='Pendiente' or estado ='Standby' )
and fechasalida>=CURDATE()
group by aeropuertosalida,aeropuertollegada,nvuelo,tarifa,fechasalida
ORDER BY laststandby  asc  ";
$exe=mysql_query($req);
echo '<font color="red" size="4">'.$req.'</font><br><br>';
while($res = mysql_fetch_array($exe))
{

  $porcentage_bloc_dia="";
  $porcentaje_espacio="";
  $minespacio_ai ="";
  $ctrl_espacio ="";
  $_4_standby_pendiente_final="";


  $id=$res['id'];
  $nvuelo=$res['nvuelo'];
  $tarifa=$res['tarifa'];
  $origen=$res['aeropuertosalida'];
  $destino=$res['aeropuertollegada'];
  $fechasalida=$res['fechasalida'];


  ////////////////////////////////////////////////////////////////////////////
  // $insert="INSERT INTO t_hl_last_all set
  // tipo= 'laststandby' ,
  // idlista= '$id_lista' ,
  // aeropuertosalida= '$aeropuertosalida' ,
  // aeropuertollegada= '$aeropuertollegada' ,
  // fechasalida= '$fechasalida' ,
  // nvuelo= '$nvuelo' ,
  // tarifa= '$tarifa' ,
  // pdhnow='$fecha_hora_actual' ,
  // laststandby='$fecha_hora_actual' ,
  // pdhuti='robot-inventory-hl-bloc-reserve-api-eh-server-24-v1.php' ";
  // //echo '<font color="green" size="4">'.$insert.'</font><br><br>';
  // mysql_query($insert);

  ////////////////////////////////////////////////////////////////////////////


  ///////////////////////////////////////////////////////////////////////////////

  // $update_ai="UPDATE thllistarb set laststandby=NOW() WHERE
  //     fechasalida='$fechasalida'
  // and aeropuertosalida='$origen'
  // and aeropuertollegada='$destino'
  // and nvuelo='$nvuelo'
  // and tarifa='$tarifa' ";
  // mysql_query($update_ai);
  //echo '<font color="blue">'.$update_ai.'</font><br><br>';

  ///////////////////////////////////////////////////////////////////////////////

echo '<hr color="#F300FE" > </hr>';

  echo '<br><br><center><font size="6" color="#F300FE">'.$origen.'  -  '.$destino.' - '.$nvuelo.' - '.$tarifa.' - '.$fechasalida.'</font></center><br><br>';

echo '<hr color="#F300FE" > </hr>';
  ///////////////////////////////////////////////////////////////////////////////

  //and  ai !='0000-00-00 00:00:00'
  $req_rows_7="SELECT * FROM thllistarb WHERE
  fechasalida='$fechasalida'
  and  aeropuertosalida ='$origen'
  and  aeropuertollegada ='$destino'
  and  nvuelo ='$nvuelo'
  and  tarifa ='$tarifa'
  and  hl !='199'
  ORDER BY ai DESC LIMIT 1  ";
  $exe_rows_7 = mysql_query($req_rows_7);
  echo '<br><font size="3" color="green">'.$req_rows_7.'</font><br>';
  $res_rows_7 = mysql_fetch_array($exe_rows_7);
  $horalanzamiento=$res_rows_7['horalanzamiento'];
  $lastctrlespacio=$res_rows_7['hl'];


  //and  ai !='0000-00-00 00:00:00'
  $req_rows_8="SELECT * FROM t_hl_lista_rb_updater WHERE
  hl is not null
  and  fechasalida='$fechasalida'
  and  aeropuertosalida ='$origen'
  and  aeropuertollegada ='$destino'
  and  nvuelo ='$nvuelo'
  and  tarifa ='$tarifa'
  ORDER BY id  DESC LIMIT 1  ";
  $exe_rows_8 = mysql_query($req_rows_8);
  echo '<br><font size="3" color="green">'.$req_rows_8.'</font><br>';
  $res_rows_8 = mysql_fetch_array($exe_rows_8);
  $hl=$res_rows_8['hl'];


  //////////////////////////////////
  /////////////// Vuelos Anulados ////////////////

  $req_vuelo_cancelado = "SELECT * from tvuelosanulados where  estado='Cancelado' and  fechadesde<='$fechasalida'  and fechahasta>='$fechasalida'  and    aeropuertosalida='$origen' and aeropuertollegada='$destino' and nvuelo='$nvuelo'  ";
  $exe_vuelo_cancelado = mysql_query($req_vuelo_cancelado);
  echo $req_vuelo_cancelado.'<br><br>';
  if($res_vuelo_cancelado = mysql_fetch_array($exe_vuelo_cancelado))
  {
    echo '<center><font size="30" color="red">El vuelo esta acancelado</font></center><br><br>';


    $update11="UPDATE thllistarb set estado='Vuelo Cancelado' WHERE  fechasalida='$fechasalida'  and    aeropuertosalida='$origen' and aeropuertollegada='$destino' and nvuelo='$nvuelo'  and (estado='Pendiente' or estado='Standby') and locexterno like 'HLL%'  ";
    mysql_query($update11);
    echo '<font color="blue">'.$update11.'</font><br><br>';


    $update22="UPDATE thllistarb set estado='Voidar' WHERE  fechasalida='$fechasalida'  and    aeropuertosalida='$origen' and aeropuertollegada='$destino' and nvuelo='$nvuelo'  and (estado='Pendiente' or estado='Standby') and locexterno not like 'HLL%'  ";
    mysql_query($update22);
    echo '<font color="blue">'.$update22.'</font><br><br>';


    continue;
  }
  /////////////// Vuelos Anulados ////////////////
  //////////////////////////////////


  if($tarifa=='Todos'){$tarifa='Y';}

  $tarifa_hl=$tarifa.'HL';


  ///////////////// ctrl espacio ////////////////
  $req_espacio = "SELECT count from  tamacmdespacio where   estadoverificacion='' and  aeropuertosalida='$origen' and aeropuertollegada='$destino' and nvuelo='$nvuelo' and fechasalida='$fechasalida' and tarifa='$tarifa'   order by pdhnow desc limit 1 ";
  $exe_espacio = mysql_query($req_espacio);
  echo $req_espacio.'<br><br>';
  $res_espacio = mysql_fetch_array($exe_espacio);
  $ctrl_espacio_actual=$res_espacio['count'];


  ///////////////// hora salida llegada ////////////////
  $req_pend2 = "SELECT horasalida,horallegada from thllistarb where   horasalida!='' and hl!='199' and  aeropuertosalida='$origen' and aeropuertollegada='$destino' and nvuelo='$nvuelo' and fechasalida='$fechasalida'    order by horasalida desc limit 1 ";
  $exe_pend2 = mysql_query($req_pend2);
  echo $req_pend2.'<br><br>';
  $res_pend2 = mysql_fetch_array($exe_pend2);
  $horasalida=$res_pend2['horasalida'];
  $horallegada=$res_pend2['horallegada'];



  ///////////////// TOTAL VENTA ////////////////
  $req_venta = "SELECT count(*) from treservasaereas where   tarifapvp like '$tarifa%'   and fechasalida='$fechasalida' and aeropuertosalida='$origen' and aeropuertollegada='$destino' and nvuelo='$nvuelo' and estadoservicio ='Confirmado'   ";
  $exe_venta = mysql_query($req_venta);
  echo '<font color="red" size="4">'.$req_venta.'</font><br>';
  $res_venta = mysql_result($exe_venta,0,0);


  ///////////////// NO LISTADOS ////////////////
  $req_no_listados = "SELECT count(*) from treservasaereas where   tarifapvp like '$tarifa_hl'  AND statu='' and fechasalida='$fechasalida' and aeropuertosalida='$origen' and aeropuertollegada='$destino' and nvuelo='$nvuelo' and estadoservicio ='Confirmado'    ";
  $exe_no_listados = mysql_query($req_no_listados);
  echo '<font color="blue" size="4">'.$req_no_listados.'</font><br>';
  $res_no_listados = mysql_result($exe_no_listados,0,0);



  ///////////////// standby ////////////////
  $req_standby_hll = "SELECT COUNT(*) from thllistarb where  locexterno like  'HLL%' and   aeropuertosalida='$origen' and aeropuertollegada='$destino' and nvuelo='$nvuelo' and fechasalida='$fechasalida'  and  tarifa='$tarifa'  and estado= 'standby'    ";
  $exe_standby_hll = mysql_query($req_standby_hll);
  //echo $req_standby_hll.'<br><br>';
  $res_standby_hll = mysql_result($exe_standby_hll,0,0);


  ///////////////// PENDIENTE HLL ////////////////
  $req_pend_hll = "SELECT COUNT(*) from thllistarb where  locexterno like  'HLL%' and   aeropuertosalida='$origen' and aeropuertollegada='$destino' and nvuelo='$nvuelo' and fechasalida='$fechasalida'  and  tarifa='$tarifa'  and estado= 'Pendiente'    ";
  $exe_pend_hll = mysql_query($req_pend_hll);
  //echo $req_pend_hll.'<br><br>';
  $res_pend_hll = mysql_result($exe_pend_hll,0,0);


  ///////////////// PENDIENTE PNR ////////////////
  $req_pend_pnr = "SELECT COUNT(*) from thllistarb where   locexterno not like  'HLL%' and    aeropuertosalida='$origen' and aeropuertollegada='$destino' and nvuelo='$nvuelo' and fechasalida='$fechasalida'  and  tarifa='$tarifa'  and estado= 'Pendiente'     ";
  $exe_pend_pnr = mysql_query($req_pend_pnr);
  //echo $req_pend_pnr.'<br><br>';
  $res_pend_pnr = mysql_result($exe_pend_pnr,0,0);


  $total_negativo =  $res_pend_hll  + $res_pend_pnr  ;




  ///////////////// Dias ////////////////
  $fecha_actual=date('Y-m-d');

  echo 'fechasalida : '.$fechasalida.'<br><br>';
  echo 'fecha_actual : '.$fecha_actual.'<br><br>';

  $diff = abs(strtotime($fechasalida) - strtotime($fecha_actual));
  $j_dias = round($diff / (60*60*24));

  if($j_dias<13)

  {    $dia_antelacion='dia'.$j_dias;   }
  else
  {    $dia_antelacion='masdias';       }

  // dia esta usada como max_por_dia sera error ?
  /////////////////////////////////////////////////////////////////////////////////////////////


  ///////////////// PENDIENTE ////////////////
  // espaciode<='$ctrl_espacio_actual' and espacioa>='$ctrl_espacio_actual'  and
  $req_max = ("SELECT $dia_antelacion as porcentage_bloc_dia,startbloc,minespacio,maxventa,lunes,martes,miercoles,jueves,viernes,sabado,domingo  from thlblocconfigai  where
    origen='$origen'
    and destino='$destino'
    and (nvuelo='$nvuelo' or nvuelo='Todos')
    and tarifa='$tarifa'
    and estado= 'Configurado'
    limit 1 ");
    $exe_max = mysql_query($req_max);
    echo '<font color="orange" size="4">'.$req_max.'</font><br>';
    $res_max = mysql_fetch_array($exe_max);

    $porcentage_bloc_dia=$res_max['porcentage_bloc_dia'].'%';
    $minespacio_ai=$res_max['minespacio'];
    $maxventa=$res_max['maxventa'];
    $startbloc=$res_max['startbloc'];
    $lunes=$res_max['lunes'];
    $martes=$res_max['martes'];
    $miercoles=$res_max['miercoles'];
    $jueves=$res_max['jueves'];
    $viernes=$res_max['viernes'];
    $sabado=$res_max['sabado'];
    $domingo=$res_max['domingo'];

    if($startbloc < $ctrl_espacio_actual)
    {
      echo '<hr color="gray" > </hr>';

      echo '<center><font size="30" color="red"> '.$startbloc.'  -  '.$ctrl_espacio_actual.'  -    Ctrl espacio menos que el minimo </font></center>';

      echo '<hr color="gray" > </hr>';

      continue;
    }

    $nombredia=date('N', strtotime($fechasalida));


    if($nombredia=='1'){$nombredia=$lunes;}
    if($nombredia=='2'){$nombredia=$martes;}
    if($nombredia=='3'){$nombredia=$miercoles;}
    if($nombredia=='4'){$nombredia=$jueves;}
    if($nombredia=='5'){$nombredia=$viernes;}
    if($nombredia=='6'){$nombredia=$sabado;}
    if($nombredia=='7'){$nombredia=$domingo;}

    echo $nombredia.'<br>';

    if($nombredia =='NO')
    {
      $update1199="UPDATE thllistarb set estado='Vuelo No Existe' WHERE
      '$nombredia'='NO'
      and fechasalida='$fechasalida'
      and aeropuertosalida='$origen'
      and aeropuertollegada='$destino' and nvuelo='$nvuelo'
      and (estado='Pendiente' or estado='Standby')
      and locexterno like 'HLL%'  ";
      //ysql_query($update1199);
      echo '<font color="blue">'.$update1199.'</font><br><br>';
      continue;
    }


    ///////////////// hl bloc mixer ////////////////
    $req_mixer = "SELECT * from thlblocmixer where
        ( origen='$origen' or origen='ALL')
    and ( destino='$destino' or destino='ALL')
    and ( tarifa='$tarifa' or tarifa='ALL')
    and ( nvuelo='$nvuelo' or nvuelo='ALL')
    and fechaaplicaciondesde <='$fechasalida'
    and fechaaplicacionhasta>='$fechasalida'
    and estado= 'Configurado'

    and ventade<='$res_venta'
    and ventaa>='$res_venta'

    and espaciode<='$hl'
    and espacioa>='$hl'

    limit 1 ";

    $exe_mixer = mysql_query($req_mixer);
    echo $req_mixer.'<br><br>';
    $res_mixer = mysql_fetch_array($exe_mixer);

    $bloc_accion_mixer=$res_mixer['bloc_accion'];
    $vacaciones_accion_mixer=$res_mixer['vacaciones_accion'];


    $resultado_1_mixer = ($maxventa * $bloc_accion_mixer)/100 ;


    ///////////////// vacaciones ////////////////
    $req_vac = "SELECT total from tvacaciones where
    origen='$origen'
    and destino='$destino'
    and (nvuelo='$nvuelo' or nvuelo='Todos')
    and fechaaplicaciondesde <='$fechasalida'
    and fechaaplicacionhasta>='$fechasalida'
    and tarifa='$tarifa'
    and estado= 'Autorizado'  limit 1 ";
    $exe_vac = mysql_query($req_vac);
    echo $req_vac.'<br><br>';
    if($res_vac = mysql_fetch_array($exe_vac))
    {

      $resultado_2_vacaciones = ($maxventa * $vacaciones_accion_mixer)/100 ;

    }
    else
    {
      $resultado_2_vacaciones='0';
    }


    $resultado_3_mixer_vacaciones = $resultado_1_mixer + $resultado_2_vacaciones ;

    $_4_standby_pendiente_final = ( $resultado_3_mixer_vacaciones * $porcentage_bloc_dia)/100  ;



    /////////////////////////////////////////////////////////////////////////////////////////////

    echo '<hr color="orange" > </hr>';

    echo 'max venta config ai  : '.$maxventa.'<br><br>';

    echo '<font color="orange">dia :    '.$dia_antelacion.'</font><br>';

    echo '<hr  align="left"  width="50%" color="gray" > </hr>';

    echo '<font color="black">min espacio : '.$minespacio_ai.'</font><br>';
    echo '<font color="black">hl  : '.$hl.'</font><br>';
    echo '<font color="green">plazas bloquear por dia   '.$porcentage_bloc_dia.' </font><br>';

    echo '<hr align="left"  width="50%" color="gray" > </hr>';

    echo '<font color="blue">hl no_list :  '.$res_no_listados.'</font><br>';
    echo '<font color="blue">hl standby_hll :  '.$res_standby_hll.'</font><br>';
    echo '<font color="blue">hl pend :  '.$res_pend_hll.'</font><br>';
    echo '<font color="blue">hl pend_pnr :  '.$res_pend_pnr.'</font><br>';

    echo '<hr  align="left"  width="50%" color="gray" > </hr>';

    echo 'bloc_accion_mixer   :  '.$bloc_accion_mixer.'<br><br>';
    echo 'vacaciones_accion_mixer   :  '.$vacaciones_accion_mixer.'<br><br>';

    echo '<hr  align="left"  width="50%" color="gray" > </hr>';

    echo 'resultado_1_mixer   :  '.$resultado_1_mixer.'<br><br>';
    echo 'resultado_2_vacaciones   :  '.$resultado_2_vacaciones.'<br><br>';

    echo '<hr  align="left"  width="50%" color="gray" > </hr>';

    echo 'resultado_3_mixer_vacaciones   :  '.$resultado_3_mixer_vacaciones.'<br><br>';

    echo '_4_standby_pendiente_final   :  '.$_4_standby_pendiente_final.'<br><br>';

    echo '<hr color="orange" > </hr>';
    /////////////////////////////////////////////////////////////////////////////////////////////


    if( $_4_standby_pendiente_final < 1 ) // para que haya siempre una plaza a vender en caso de error precio ram
    {
      $_4_standby_pendiente_final='6';
    }
    else
    {
      $_4_standby_pendiente_final=$_4_standby_pendiente_final;
    }

    $_4_standby_pendiente_final = $_4_standby_pendiente_final - ( $res_pend_hll  + $res_pend_pnr + $res_no_listados) ;

    $_4_standby_pendiente_final=round($_4_standby_pendiente_final, 0);


    /////////////////////////////////////////////////////////////////////////////////////////////
    if($maxventa<=$res_no_listados)
    {
      $_4_standby_pendiente_final='0';
      echo '<br><br><center><font size="6" color="red">============== Hay  '.$res_no_listados.'  pasajeros no Listados stop sales ==============</font></center><br><br>';
    }
    else
    {
      echo '<font color="green"  size="8" >_4_standby_pendiente_final +  '.$_4_standby_pendiente_final.' </font><br>';
    }

    /////////////////////////////////////////////////////////////////////////////////////////////

    if($_4_standby_pendiente_final >0 )
    {

      ////////////////////////////////////////////////

      $update_c="UPDATE thllistarb SET
      estado ='Pendiente'
      WHERE
      locexterno like  'HLL%'
      and aeropuertosalida='$origen'
      and aeropuertollegada='$destino'
      and nvuelo='$nvuelo'
      and fechasalida='$fechasalida'
      and tarifa='$tarifa'
      and estado= 'Standby'
      order by id asc limit $_4_standby_pendiente_final  ";
      mysql_query($update_c);
      echo '<br><br><font color="green">'.$update_c.'</font><br><br>';

    }

    ////////////////////////////////////////////////

    if($_4_standby_pendiente_final < 1 )
    {
      $_4_standby_pendiente_final = $_4_standby_pendiente_final * (-1);

      echo '<center><font size="6" color="green">No Hay que Activar mas plazas</font></center><br><br>';

      $update_c="UPDATE thllistarb SET
      estado ='Standby'
      WHERE
      locexterno like  'HLL%'
      and aeropuertosalida='$origen'
      and aeropuertollegada='$destino'
      and nvuelo='$nvuelo'
      and fechasalida='$fechasalida'
      and tarifa='$tarifa'
      and estado= 'Pendiente'
      order by id asc limit $_4_standby_pendiente_final  ";
      mysql_query($update_c);
      echo '<br><br><font color="green">'.$update_c.'</font><br><br>';

    }

    echo '<br><br><center><font size="6" color="black">=======================        FIN         FIN        FIN      =====================</font></center><br><br>';

  }




  mysql_close($connsht);
  ?>
