<?php

       require_once '../app/model/db.class.php';

        $obj = new database();
        $obj->conectar(); $obj->consulta("set names 'utf8'");

        //$obj->consulta("drop table repetidos");
        $obj->consulta("create table repetidos select boletin.grado , boletin.curso ,detalleboletin.alumno ,boletin.periodo,boletin.asignatura , COUNT(*) Total from boletin inner join detalleboletin on boletin.numero = detalleboletin.boletin  GROUP BY detalleboletin.alumno ,boletin.periodo,boletin.asignatura HAVING COUNT(*) > 1");

          $reg = $obj->consulta("select * from  repetidos");

            while($row = $obj->fetch_assoc($reg)){

                   $reg1 = $obj->consulta("select boletin.numero , detalleboletin.secuencial ,detalleboletin.alumno, boletin.asignatura, boletin.periodo, boletin.grado, boletin.curso, detalleboletin.definitiva
                                          from boletin inner join detalleboletin on boletin.numero = detalleboletin.boletin
                                          where detalleboletin.alumno='".$row['alumno']."' and boletin.asignatura='".$row['asignatura']."' and boletin.periodo='".$row['periodo']."'");

                   $reg2 = $obj->consulta("select * from matriculas where codigo='".$row['alumno']."'");
                   $row2 = $obj->fetch_assoc($reg2);

                   while($row1 = $obj->fetch_assoc($reg1)){

                         if (($row1['grado']!=$row2['grado']) || ($row1['curso']!=$row2['curso'])){
                             
                             $obj->consulta("delete from indicadores where detalle_boletin='".$row1['secuencial']."'");
                             $obj->consulta("delete from detalleboletin where secuencial='".$row1['secuencial']."'");
                             
                         }

                   }
            }


         $obj->disconnect();

         echo "<script languaje='javascript'>alert('PROCESO TERMINADO')</script>";
         echo "<script languaje='javascript'>document.location.href='../menu.php';</script>";
?>