<?php
require_once('../Web_Admin/Conexion.php');
require_once('Class_Fecha.php');
class Admin_P_Alt
{
  function __construct()
  {
  }

  public function Gan_Mov($Merc, $Fecha_Ini, $Fecha_Fin)
  {
    $db = new Conexion();
    if ($Merc != 0) {
      $Sql = "SELECT B.nombre, B.id from C_Mpio B INNER JOIN (SELECT Id from O_Folios_Asig where Act=1 and Mercancia=$Merc and Fecha_Emi BETWEEN '$Fecha_Ini' and '$Fecha_Fin' GROUP BY Id) A on A.id=B.id ";
    } else {
      $Sql = "SELECT B.nombre, B.id from C_Mpio B INNER JOIN (SELECT Id from O_Folios_Asig where Act=1 and Fecha_Emi BETWEEN '$Fecha_Ini' and '$Fecha_Fin' GROUP BY Id) A on A.id=B.Id ";
    }

    $resultado = $db->query($Sql);
    echo "Municipio : <select name='Muni' id='Muni' required>";
    echo '<option value="0" >- - Todos - -</option>';
    while ($row = $resultado->fetch_assoc()) {
      echo '<option value="' . $row['id'] . '">' . $row['nombre'] . '</option>';
    }
    echo '</select>';
    //echo '<script>console.log("'."SELECT B.nombre, B.id from C_Mpio B INNER JOIN (SELECT Id from O_Folios_Asig where Act=1 and Mercancia=$Merc and Fecha_Emi BETWEEN '$Fecha_Ini' and '$Fecha_Fin' GROUP BY Id) A on A.id=B.id ".'")</script>';
  }

  public function Fecha_MM()
  {
    $db = new Conexion();
    $sql = $db->query("SELECT min(Fecha_Emi) As Fecha_Min, MAX(Fecha_Emi) As Fecha_Max FROM O_Folios_Asig");
    $dato = $db->recorrer($sql);
    return ($dato);
  }

  public function Display_Res($Merc, $Muni, $Fecha_Ini, $Fecha_Fin)
  {
    $db = new Conexion();
    $Camp_Merc = ($Merc != 0 ? ' and A.Mercancia=' . $Merc . ' and ' : ' and ');
    if ($Muni == 0) {
      $SSql = "select A.Mercancia, A.Ruta, A.Cantidad, A.Folio, A.Fecha_Cap, A.Fecha_Emi, B.nombre as Mpio_Origen, concat(C.nombre,' - ',D.nombre) As Destino from O_Folios_Asig A INNER JOIN C_Mpio B on A.Id=B.id INNER JOIN C_Estados C on A.Estado_Des=C.id INNER JOIN C_Mpio D on A.Mpio_Des=D.id where A.Act=1 $Camp_Merc Fecha_Emi between '$Fecha_Ini' and '$Fecha_Fin' ";
    } else {
      $SSql = "select A.Mercancia, A.Ruta, A.Cantidad, A.Folio, A.Fecha_Cap, A.Fecha_Emi, B.nombre as Mpio_Origen, concat(C.nombre,' - ',D.nombre) As Destino from O_Folios_Asig A INNER JOIN C_Mpio B on A.Id=B.id INNER JOIN C_Estados C on A.Estado_Des=C.id INNER JOIN C_Mpio D on A.Mpio_Des=D.id where A.Act=1 $Camp_Merc A.Fecha_Emi between '$Fecha_Ini' and '$Fecha_Fin' and A.Id = $Muni";
    }
    //echo '<script>console.log("'.$SSql.'")</script>';

    $resultado = $db->query($SSql);
    echo '<table width="100%" border="1"><tbody><thead><tr><th width="5%">#</th><th width="25%">Origen</th><th width="37%">Destino</th><th width="18%">Fecha</th><th width="5%">Cant</th><th width="10%">Folio</th></tr></thead>';
    $Cont = 1;

    while ($row = $resultado->fetch_assoc()) {
      $Fecha = new Fechas_Form($row['Fecha_Cap']);
      $Ruta = is_null($row['Ruta']) ? $row['Folio'] : '<button type="button" style="background-color:#9ACD32; cursor: pointer;" onClick="View_Fol(' . $row['Folio'] . ')">' . $row['Folio'] . '</button>';
      echo '<tr><td>' . $Cont . '</td><td>' . $row['Mpio_Origen'] . '</td><td>' . $row['Destino'] . '</td><td>' . $Fecha->Fecha_DM() . '</td><td>' . $row['Cantidad'] . '</td><td>' . $Ruta . '</td></tr>';
      $Cont++;
    }
    echo '</tbody></table>';
  }
  function Datos_Gen()
  {
    $db = new Conexion();
    $resultado = $db->query("SELECT A.Id as Id_Reg, B.nombre, count(A.Folio) as Folio, A.Act, sum(if(A.Ruta is null,1,0)) as Sin_Guia, sum(if(A.Ruta is NOT Null,1,0)) as Con_Guia FROM Prod_Guias_Union.O_Folios_Asig A INNER JOIN Prod_Guias_Union.C_Municipios B on A.Id=B.id GROUP by A.Act, A.Id ORDER by A.Id, A.Act");
    $Cont = -1;
    $Nombre = '';
    while ($row = $resultado->fetch_assoc()) {
      if ($Nombre != $row['nombre']) {
        $Folios_0 = 0;
        $Folios_1 = 0;
        $Folios_2 = 0;
        $Folios_3 = 0;
        $Con_G_0 = 0;
        $Con_G_1 = 0;
        $Con_G_2 = 0;
        $Con_G_3 = 0;
        $Cont++;
      }
      if ($row['Act'] == 0) {
        $Folios_0 = $row['Folio'];
        $Sin_G_0 = $row['Sin_Guia'];
        $Con_G_0 = $row['Con_Guia'];
      } elseif ($row['Act'] == 1) {
        $Folios_1 = $row['Folio'];
        $Sin_G_1 = $row['Sin_Guia'];
        $Con_G_1 = $row['Con_Guia'];
      } elseif ($row['Act'] == 2) {
        $Folios_2 = $row['Folio'];
        $Sin_G_2 = $row['Sin_Guia'];
        $Con_G_2 = $row['Con_Guia'];
      } elseif ($row['Act'] == 3) {
        $Folios_3 = $row['Folio'];
        $Sin_G_3 = $row['Sin_Guia'];
        $Con_G_3 = $row['Con_Guia'];
      } elseif ($row['Act'] == 5) {
        $Folios_3 = $row['Folio'];
        $Sin_G_3 = $row['Sin_Guia'];
        $Con_G_3 = $row['Con_Guia'];
      }
      $Nombre = $row['nombre'];
      $Tot = $Folios_0 + $Folios_1 + $Folios_2 + $Folios_3;
      $Usados = $Folios_1 + $Folios_2 + $Folios_3;
      $Porcent = ($Usados * 100) / ($Tot + $Usados);
      $C_Guia = $Con_G_0 + $Con_G_1 + $Con_G_2 + $Con_G_3;
      $Tabla[$Cont] = array('Id_Mpio' => $row['Id_Reg'], 'Nombre' => $row['nombre'], 'Usados' => $Usados, 'Folios_0' => $Folios_0, 'Folios_1' => $Folios_1, 'Folios_2' => $Folios_2, 'Folios_3' => $Folios_3, 'Sin_G_0' => $Sin_G_0, 'Sin_G_01' => $Sin_G_1, 'Sin_G_02' => $Sin_G_2, 'Sin_G_03' => $Sin_G_3, 'Con_G_0' => $Con_G_0, 'Con_G_01' => $Con_G_1, 'Con_G_02' => $Con_G_2, 'Con_G_3' => $Con_G_3, 'C_Guia' => $C_Guia, 'Total_F' => $Tot, 'Porc' => $Porcent);
    }
    echo '<table width="100%" border="1"><tbody><thead><tr><th width="5%">#</th><th width="23%">Municipio</th><th width="10%">Sin Usar</th><th width="10%">Usados</th><th width="10%">Correc</th><th width="10%">Cancel</th><th width="10%">Incomp</th><th width="10%">Archivo Guia</th><th width="10%">Total</th><th width="10%">% Usa</th><th "2%">Ver</th></tr></thead>';
    $Cont = 1;
    foreach ($Tabla as $Reg => $Key) {
      echo '<tr><td>' . $Cont . '</td><td><div onclick="Bus_Mpio(' . $Key['Id_Mpio'] . ')">' . $Key['Nombre'] . '</div></td><td>' . $Key['Folios_0'] . '</td><td>' . $Key['Usados'] . '</td><td>' . $Key['Folios_1'] . '</td><td>' . $Key['Folios_2'] . '</td><td>' . $Key['Folios_3'] . '</td><td>' . $Key['C_Guia'] . '</td><td>' . $Key['Total_F'] . '</td><td>' . number_format($Key['Porc'], 2) . '</td><td><img src="../Diseno/ver.jpg" width="30" height="26" onClick="detaFol(' . $Key['Id_Mpio'] . ')"/></td></tr>';
      $Cont++;
    }
    echo '</tbody></table>';
  }

  public function Busca_Folio($Folio)
  {
    $db = new Conexion();
    $sql = $db->query("select A.Id, A.Fecha_Emi, A.Fecha_Cap, A.Fecha_EntComite, A.Fecha_EntUnion, B.nombre as Mpio_Origen, C.nombre as Est_Destino, D.nombre as Mpio_Destino, E.Descripcion as Merca, A.Folio_Zoo, A.Cantidad, F.Descripcion, A.Motivo, A.Observaciones, A.Ruta, A.Act from Prod_Guias_Union.O_Folios_Asig A LEFT JOIN Prod_Guias_Comite.C_Mpio B on A.Id=B.id LEFT JOIN Prod_Guias_Comite.C_Estados C on A.Estado_Des=C.id LEFT JOIN Prod_Guias_Comite.C_Mpio D on A.Mpio_Des=D.id LEFT JOIN Prod_Guias_Comite.C_Mercancia E on A.Mercancia=E.Id_mercancia LEFT JOIN Prod_Guias_Comite.C_Motivos F on A.Id_motivo=F.Id_motivo where A.Folio=$Folio");
    $dato = $db->recorrer($sql);
    return ($dato);
  }

  public function Cambio_Fecha($Valor, $Opc)
  {
    $Mes_ = array('Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');
    $Fecha = new DateTime($Valor);
    if ($Opc == 0) {
      $Fecha = $Fecha->format('d') . ' - ' . $Mes_[($Fecha->format('m') - 1)] . ' - ' . $Fecha->format('Y');
    } else {
      $Fecha = $Fecha->format('d') . ' - ' . $Mes_[($Fecha->format('m') - 1)] . ' - ' . $Fecha->format('Y') . ' | Hr ' . $Fecha->format('H') . ':' . $Fecha->format('i');
    }
    return ($Fecha);
  }

  public function Aretes_Rep($Opc, $Nom_Aret)
  {
    $db = new Conexion();
    if ($Opc == 0) {
      $Sql = "select D.No_arete,C.Folio, C.Id, C.Ruta, C.Fecha_Emi, C.Fecha_Cap, E.nombre as Mpio_Origen, concat(F.nombre, ' | ',G.nombre) as Estado_Des, H.Descripcion from O_Folios_Asig C INNER JOIN (SELECT A.Id_folios, A.No_arete from O_Historial_Aretes A INNER JOIN (SELECT No_arete FROM O_Historial_Aretes GROUP by No_arete HAVING count(No_arete)>1) B on A.No_arete=B.No_arete) D on C.Folio=D.Id_folios INNER JOIN C_Municipios E on C.Id= E.id INNER JOIN C_Estados F on C.Estado_Des=F.id INNER JOIN C_Municipios G on G.id=C.Mpio_Des INNER JOIN C_Mercancia H on H.Id_mercancia=C.Mercancia ORDER by D.No_arete * 1";
    } else {
      $Sql = "select D.No_arete,C.Folio, C.Id, C.Ruta, C.Fecha_Emi, C.Fecha_Cap, E.nombre as Mpio_Origen, concat(F.nombre, ' | ',G.nombre) as Estado_Des, H.Descripcion from O_Folios_Asig C INNER JOIN (SELECT A.Id_folios, A.No_arete from O_Historial_Aretes A INNER JOIN (SELECT No_arete FROM O_Historial_Aretes GROUP by No_arete HAVING count(No_arete)>1) B on A.No_arete=B.No_arete) D on C.Folio=D.Id_folios INNER JOIN C_Municipios E on C.Id= E.id INNER JOIN C_Estados F on C.Estado_Des=F.id INNER JOIN C_Municipios G on G.id=C.Mpio_Des INNER JOIN C_Mercancia H on H.Id_mercancia=C.Mercancia   where D.No_arete = '$Nom_Aret' ORDER by D.No_arete * 1";
    }
    $resultado = $db->query($Sql);
    echo '<table width="100%"><tbody><thead><tr><th width="5%">#</th><th width="15%">Arete</th><th width="7%">Folio</th><th width="18%">Origen</th><th width="5%">Mercancia</th><th width="20%">Destino</th><th width="15%">Fecha Emi</th><th width="15%">Fecha Cap</th></tr></thead>';
    $Cont = 1;

    while ($row = $resultado->fetch_assoc()) {
      $Fecha_Cap = new Fechas_Form($row['Fecha_Cap']);
      $Fecha_Emi = new Fechas_Form($row['Fecha_Emi']);
      $Ruta = is_null($row['Ruta']) ? $row['Folio'] : '<button type="button" style="background-color:#9ACD32;  cursor: pointer;" onClick="View_Fol(' . $row['Folio'] . ')">' . $row['Folio'] . '</button>';
      echo '<tr><td>' . $Cont . '</td><td>' . $row['No_arete'] . '</td><td>' . $Ruta . '</td><td>' . $row['Mpio_Origen'] . '</td><td>' . $row['Descripcion'] . '</td><td>' . $row['Estado_Des'] . '</td><td>' . $Fecha_Emi->Fecha_DMAC() . '</td><td>' . $Fecha_Cap->Fecha_DMAC() . '</td></tr>';
      $Cont++;
    }
    echo '</tbody></table>';
  }

  public function Hist_Blocks($Mpio)
  {
    $db = new Conexion();
    $Sql = "SELECT B.nombre, A.Fecha_Asig, A.Id_block, C.Id_blocks, C.F_inicio, C.F_final, (SELECT sum(if(Act>0,1,0)) from O_Folios_Asig where Folio BETWEEN C.F_inicio and C.F_final) as Folios_Usd, (SELECT sum(if(Act=0,1,0)) from O_Folios_Asig where Folio BETWEEN C.F_inicio and C.F_final) as Folios_Disp, (SELECT sum(if(EntComite=1,1,0)) from O_Folios_Asig where Folio BETWEEN C.F_inicio and C.F_final) as EntComite, (SELECT sum(if(EntUnion=1,1,0)) from O_Folios_Asig where Folio BETWEEN C.F_inicio and C.F_final) as EntUnion from O_Asig_block_Gan A INNER JOIN C_Municipios B on A.Id_cliente=B.id INNER JOIN Prod_Guias_Comite.O_Blocks C on A.Id_block=C.Id_blocks where A.Id_cliente = {$Mpio}";
    $resultado = $db->query($Sql);
    echo '<p>Solicitud de blocks :.</p><hr>';
    echo '<table width="100%" border="1"><tbody><thead><tr><th width="5%">#</th><th width="20%">Fecha de Compra</th><th width="10%">Block</th><th width="10%">Folio Inicio</th><th width="10%">Folio Final</th><th width="10%">Folios Usd</th><th width="12%">Folios Disp</th><th width="13%">% Usados</th><th width="10%">F EntCom</th></tr></thead>';
    $Cont = 1;

    while ($row = $resultado->fetch_assoc()) {
      $Fecha_Com = new Fechas_Form($row['Fecha_alta']);
      echo '<tr><td>' . $Cont . '</td><td>' . $Fecha_Com->Fecha_DMAC() . '</td><td>' . $row['Id_blocks'] . '</td><td>' . $row['F_inicio'] . '</td><td>' . $row['F_final'] . '</td><td>' . $row['Folios_Usd'] . '</td><td>' . $row['Folios_Disp'] . '</td><td>' . $row['Folios_Usd'] * 100 / ($row['Folios_Usd'] + $row['Folios_Disp']) . '</td><td>' . $row['EntComite'] . '</td></tr>';
      $Cont++;
    }
    echo '</tbody></table><hr>';
  }

  public function Municipios()
  {
    $db = new Conexion();
    $Sql = "SELECT B.nombre, B.id from Prod_Guias_Comite.C_Mpio B INNER JOIN (SELECT Id from Prod_Guias_Union.O_Folios_Asig GROUP BY Id) A on A.id=B.id ";
    $resultado = $db->query($Sql);
    echo "Municipio : <select name='Muni' id='Muni' required>";
    echo '<option value="0" >-- Todos --</option>';
    while ($row = $resultado->fetch_assoc()) {
      echo '<option value="' . $row['id'] . '">' . $row['nombre'] . '</option>';
    }
    echo '</select>';
  }

  public function Status_Fol($Status, $Muni)
  {
    if ($Status == 1) {
      $Titulo = "Folios Correctos";
    } elseif ($Status == 2) {
      $Titulo = "Folios Cancelados";
    } elseif ($Status == 3) {
      $Titulo = "Folios Incompletos";
    }
    $db = new Conexion();
    $resultado = $db->query("SELECT B.nombre, A.Folio, A.Fecha_Emi, A.Fecha_Cap, C.Descripcion as Mercancia, concat(D.nombre,' | ',E.nombre) as Destino from Prod_Guias_Union.O_Folios_Asig A LEFT JOIN Prod_Guias_Union.C_Municipios B on A.Id= B.id LEFT JOIN Prod_Guias_Comite.C_Mercancia C on A.Mercancia=C.Id_mercancia LEFT JOIN Prod_Guias_Comite.C_Estados D on A.Estado_Des=D.id LEFT JOIN Prod_Guias_Comite.C_Mpio E on A.Mpio_Des=E.id WHERE A.Act=$Status and A.Id=$Muni");
    echo '<br><table width="100%" border="1"><tbody><thead><tr><th colspan="7">.: Lista de ' . $Titulo . ' :.</th></tr></thead><thead><tr><th width="5%">#</th><th width="20%">Municipio</th><th width="12%">Fecha Emi</th><th width="12%">Fecha Cap</th><th width="25%">Mpio. Destino</th><th width="20%">Mercancia</th><th width="6%">Folio</th></tr></thead>';
    $Cont = 1;
    while ($row = $resultado->fetch_assoc()) {
      $Fecha_Emi = new Fechas_Form($row['Fecha_Emi']);
      $Fecha_Cap = new Fechas_Form($row['Fecha_Cap']);
      echo '<tr><td>' . $Cont . '</td><td>' . $row['nombre'] . '</td><td>' . $Fecha_Emi->Fecha_DMAAC() . '</td><td>' . $Fecha_Cap->Fecha_DMAAC() . '</td><td>' . $row['Destino'] . '</td><td>' . $row['Mercancia'] . '</td><td>' . $row['Folio'] . '</td></tr>';
      $Cont++;
    }
    echo '</tbody></table>';
    //echo '<script>console.log("'."SELECT B.nombre, A.Fecha_Emi, A.Fecha_Cap, C.Descripcion as Mercancia, concat(D.nombre,' | ',E.nombre) as Destino from O_Folios_Asig A INNER JOIN C_Municipios B on A.Id= B.id INNER JOIN C_Mercancia C on A.Mercancia=C.Id_mercancia INNER JOIN C_Estados D on A.Estado_Des=D.id INNER JOIN C_Mpio E on A.Mpio_Des=E.id WHERE A.Act=$Status and A.Id=$Muni".'")</script>';

  }

  public function Hist_Aretes($Fol)
  {
    $db = new Conexion();
    $Sql = $db->query("Select count(*) as Hay_Reg from Prod_Guias_Union.O_Historial_Aretes where Id_folios={$Fol}");
    $dato = $db->recorrer($Sql);
    if ($dato['Hay_Reg'] > 0) {
      $resultado = $db->query("SELECT Id_folios, No_arete from Prod_Guias_Union.O_Historial_Aretes WHERE Id_folios={$Fol}");
      echo '<table width="100%" border="1"><tbody><thead><tr><td colspan="6" align="center"><h3>.: Lista de Aretes :.</h3></th></tr><tr><th width="7%">#</th><th width="17%">1</th><th width="17%">2</th><th width="17%">3</th><th width="17%">4</th><th width="17%">5</th></tr></thead>';
      $Cont_C = 1;
      $Cont_F = 1;
      $Coor_F = 1;
      while ($row = $resultado->fetch_assoc()) {
        if ($Cont_C <= 4) {
          if ($Cont_F == 1) {
            echo '<tr><td>' . $Coor_F . '</td><td>' . $row['No_arete'] . '</td>';
          } else {
            echo '<td>' . $row['No_arete'] . '</td>';
          }
          $Cont_C++;
          $Cont_F++;
        } else {
          echo '<td>' . $row['No_arete'] . '</td>';
          echo '</tr>';
          $Cont_C = 1;
          $Cont_F = 1;
          $Coor_F++;
        }
      }
      if ($Cont_C != 1) {
        for ($Sal = 1; $Sal < (7 - $Cont_C); $Sal++) {
          if ($Sal < 5) {
            echo '<td>**********</td>';
          } else {
            echo '</tr>';
          }
        }
      }
      echo '</tbody></table>';
    }
  }

  public function Reset_Fol($Folio)
  {
    $db = new Conexion();
    $Sql = $db->query("Select Ruta from Prod_Guias_Union.O_Folios_Asig where Folio={$Folio}");
    $dato = $db->recorrer($Sql);
    if (!is_null($dato['Ruta'])) {
      if (file_exists("../Hist_Folios/" . $dato['Ruta'])) {
        unlink("../Hist_Folios/" . $dato['Ruta']);
      }
    }
    $Sql = "Update Prod_Guias_Union.O_Folios_Asig set Ruta=NULL, Estado_Des=NULL, Mpio_Des=NULL, Mercancia=NULL, Folio_Zoo=NULL, Cantidad=NULL, Id_motivo=NULL, Motivo=NULL, Observaciones=NULL, Act=0 where Folio={$Folio}";
    if ($db->query($Sql)) {
      $Sql = "Delete from Prod_Guias_Union.O_Historial_Aretes where Id_folios={$Folio}";
      $db->query($Sql);
    }
  }

  public function Cancel_Fol($Folio)
  {
    $db = new Conexion();
    $Sql = "Update Prod_Guias_Union.O_Folios_Asig set Act=2 where Folio={$Folio}";
    if ($db->query($Sql)) {
      $Sql = "Delete from Prod_Guias_Union.O_Historial_Aretes where Id_folios={$Folio}";
      $db->query($Sql);
    }
  }

  public function List_Reportes($Rep)
  {
    $db = new Conexion();
    if (is_null($Rep)) {
      $Sql = "SELECT A.Id_Rep_F, B.nombre, C.Folio, A.Solicitud, A.Fecha_Rep, A.Observaciones FROM Prod_Guias_Union.O_Folio_Rep A INNER JOIN Prod_Guias_Union.O_Folios_Asig C on A.Id_Fol_Asig=C.Id_Fol_Asig INNER JOIN Prod_Guias_Union.C_Municipios B on C.Id=B.id WHERE A.Act=0";
    } else {
      $Sql = "SELECT A.Id_Rep_F, B.nombre, C.Folio, A.Solicitud, A.Fecha_Rep, A.Observaciones FROM Prod_Guias_Union.O_Folio_Rep A INNER JOIN Prod_Guias_Union.O_Folios_Asig C on A.Id_Fol_Asig=C.Id_Fol_Asig INNER JOIN Prod_Guias_Union.C_Municipios B on C.Id=B.id WHERE A.Id_Rep_F=$Rep";
    }
    $resultado = $db->query($Sql);
    echo '<table width="100%" border="1"><tbody><thead><tr align="center"><th colspan="8">.: Lista de reportes :.</th></tr></thead><thead><tr><th width="5%">#</th><th width="8%">No Rep</th><th width="24%">Municipio</th><th width="8%">Folio</th><th width="17%">Solicitud</th><th width="15%">Fecha</th><th width="18%">Obs</th><th width="5%">A</th></tr></thead>';
    $Cont = 1;
    while ($row = $resultado->fetch_assoc()) {
      $Fecha = new Fechas_Form($row['Fecha_Rep']);
      if ($row['Solicitud'] == 0) {
        $Solicitud = 'Restaurar Folio';
      } elseif ($row['Solicitud'] == 1) {
        $Solicitud = 'Cancelar Folio';
      } elseif ($row['Solicitud'] == 2) {
        $Solicitud = 'Activar Folio';
      } elseif ($row['Solicitud'] == 3) {
        $Solicitud = 'Cambio de Arete';
      } elseif ($row['Solicitud'] == 4) {
        $Solicitud = 'Borrar Archivo';
      }
      echo '<tr><td>' . $Cont . '</td><td>' . $row['Id_Rep_F'] . '</td><td>' . $row['nombre'] . '</td><td>' . $row['Folio'] . '</td><td>' . $Solicitud . '</td><td>' . $Fecha->Fecha_DMA() . '</td><td>' . $row['Observaciones'] . '</td>
      <td><img src="../Diseno/Paloma.jpg" width="30" height="26" onClick="Procesar(' . $row['Id_Rep_F'] . ',' . $row['Solicitud'] . ')"/></td></tr>';
      $Cont++;
    }
    echo '</tbody></table>';
  }
  function Cancela_Pro($Rep)
  {
    $db = new Conexion();
    $sql = "Update Prod_Guias_Union.O_Folios_Asig set Act=0, Fecha_Emi=NULL, Estado_Des=NULL, Mpio_Des=NULL, Folio_Zoo=NULL, Mercancia=NULL, Cantidad=NULL, Id_motivo=NULL, Observaciones=NULL where Id_Fol_Asig=(SELECT Id_Fol_Asig from Prod_Guias_Union.O_Folio_Rep where Id_Rep_F=$Rep)";
    if ($db->query($sql)) {
      $sql = "Update Prod_Guias_Union.O_Folio_Rep Set Fecha_Resp = now(), Act=1 where Id_Rep_F=$Rep";
      $db->query($sql);
      $sql = "Delete from Prod_Guias_Union.O_Historial_Aretes where Id_folios=(SELECT B.Folio from Prod_Guias_Union.O_Folio_Rep A INNER JOIN Prod_Guias_Union.O_Folios_Asig B on A.Id_Fol_Asig=B.Id_Fol_Asig where Id_Rep_F=$Rep)";
      $db->query($sql);
    }
  }

  function Cancela_File($Rep)
  {
    $db = new Conexion();
    $Sql = $db->query("SELECT concat('../Hist_Folios/',B.Ruta, C.Ruta) as Ruta FROM Prod_Guias_Union.O_Folio_Rep A INNER JOIN Prod_Guias_Union.O_Acceso B on A.Id=B.Id INNER JOIN Prod_Guias_Union.O_Folios_Asig C on A.Id_Fol_Asig=C.Id_Fol_Asig WHERE A.Id_Rep_F=$Rep");
    $dato = $db->recorrer($Sql);
    if (file_exists($dato['Ruta'])) {
      unlink($dato['Ruta']);
      $sql = "Update Prod_Guias_Union.O_Folio_Rep Set Fecha_Resp = now(), Act=1 where Id_Rep_F=$Rep";
      $db->query($sql);
      $sql = "Update Prod_Guias_Union.O_Folios_Asig Set Ruta = NULL where Folio=(SELECT B.Folio from Prod_Guias_Union.O_Folio_Rep A INNER JOIN Prod_Guias_Union.O_Folios_Asig B on A.Id_Fol_Asig=B.Id_Fol_Asig where Id_Rep_F=$Rep)";
      $db->query($sql);
    }
  }

  function Cancela_Fol($Rep)
  {
    $db = new Conexion();
    $sql = "Update Prod_Guias_Union.O_Folio_Rep Set Fecha_Resp = now(), Act=1 where Id_Rep_F=$Rep";
    $db->query($sql);
    $sql = "Update Prod_Guias_Union.O_Folios_Asig Set Act = 2 where Id_Fol_Asig=(Select Id_Fol_Asig from Prod_Guias_Union.O_Folio_Rep where Id_Rep_F=$Rep)";
    $db->query($sql);
  }

  function Visualiza_Fol($Folio)
  {
    $db = new Conexion();
    $sql = $db->query("Select concat(B.Ruta,A.Ruta) as Ruta from Prod_Guias_Union.O_Folios_Asig A INNER JOIN Prod_Guias_Union.O_Acceso B on A.Id=B.Id where A.Folio=$Folio ORDER by B.Ruta DESC LIMIT 1");
    $dato = $db->recorrer($sql);
    return ($dato);
  }


  function Folio_inicio()
  {
    $db = new Conexion();
    $resultado = $db->query("SELECT A.Id, B.nombre, Min(A.Folio) as Fol_Ini, Max(A.Folio) as Fol_Fin, C.Folio FROM Prod_Guias_Union.O_Folios_Asig A INNER JOIN (SELECT Id, MIN(Folio) as Folio from Prod_Guias_Union.O_Folios_Asig where Act=0 group by Id) C on A.Id=C.Id INNER join Prod_Guias_Union.C_Municipios B on A.Id=B.id WHERE A.Id <> 2493 group by A.Id ORDER BY `B`.`nombre` ASC ");
    $Cont = -1;
    echo '<table width="100%" border="1"><tbody><thead><tr><th width="5%">#</th><th width="25%">Municipio</th><th width="10%">Folio Inicio</th><th width="10%">Folio Final</th><th width="10%">Inicio Captura</th><th width="10%">Comenzar en</th><th width="10%"></th></tr></thead>';
    $Cont = 1;
    while ($row = $resultado->fetch_assoc()) {
      echo '<tr><td>' . $Cont . '</td><td><div onclick="Bus_Mpio(' . $row['Id'] . ')">' . $row['nombre'] . '</div></td><td>' . $row['Fol_Ini'] . '</td><td>' . $row['Fol_Fin'] . '</td><td>' . $row['Folio'] . '</td><td><input type="text" name="Fol_Pro" id="Fol_Pro" onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;" placeholder="Folio inicio" class="TM_Alto" max="10" maxlength="10" autocomplete="off" /></td><td><button onClick="iniGuia(' . $row['Id'] . ',' . ($Cont - 1) . ')">Aceptar</button></td></tr>';
      $Cont++;
    }
    echo '</tbody></table>';
  }

  function fol_Ini($idCliente, $folInicio)
  {
    $db = new Conexion();
    $sql = "Update Prod_Guias_Union.O_Folios_Asig Set Act = 4 where id=$idCliente and Act=0 and Folio < $folInicio";
    if ($db->query($sql) === true) {
      echo "De lujo";
    } else {
      echo "Error";
    }
    mysqli_close($db);
  }

  function ListBlock($Mun)
  {
    $db = new Conexion();
    $Sql = "SELECT A.Id_block, C.nombre, B.F_inicio, B.F_final FROM Prod_Guias_Union.O_Asig_block_Gan A INNER JOIN Prod_Guias_Comite.O_Blocks B on A.Id_block=B.Id_blocks INNER JOIN Prod_Guias_Union.C_Municipios C on A.Id_cliente=C.id where A.Id_cliente=$Mun";
    $resultado = $db->query($Sql);
    return ($resultado);
  }
}// Fin de La Classe
