<?php /*
    header("Content-Type:application/json");
    include("connection.php");
    include("utils.php");
    
    // Vamos a consultar los colegios privado
    
    $sql = "SELECT * FROM schools WHERE private = 1";
    if (!$rs=$db->query($sql))
        badEnd("500", array("msg"=>$db->error.$sql));
    
    // Verificamos que tengan pagos
    while($row = $rs->fetch_assoc()){
        $sql = "SELECT COUNT(id) AS qty FROM payments WHERE schoolid = ".$row['id'];
        if (!$resVe=$db->query($sql))
            badEnd("500", array("msg"=>$db->error.$sql));
        
        $verify = $resVe->fetch_assoc();
        // Si tiene pagos, seguimos
        if($verify['qty'] > 0){
            
            // Obtenemos la suma de sus pagos(por si acaso)
            $sql = "SELECT SUM(amount) AS qty FROM payments WHERE schoolid = ".$row['id'];
            if (!$res=$db->query($sql))
                badEnd("500", array("msg"=>$db->error.$sql));
                
            $totalamount = $res->fetch_assoc();
                
            // validamos si tiene 1 o mas pagos
            if($verify == 1){
                // Si tiene 1 solo pago, actualizamos todo en un query
                
                // Actualizamos el registro
                $sql = "UPDATE payments SET ".
                "       due = ".$totalamount['qty'].", ".
                "       pupilqty = ".$row['pupilqty'].
                "       WHERE schoolid = ".$row['id'];
                if (!$db->query($sql))
                    badEnd("500", array("msg"=>$db->error));
            }else{
                // SI tiene mas de uno, tomamos uno y agregamos a ese la cantida de alumnos
                $sql = "SELECT id, date FROM payments WHERE schoolid = ".$row['id'].
                "       ORDER BY id ASC".
                "       LIMIT 1";
                if (!$res=$db->query($sql))
                    badEnd("500", array("msg"=>$db->error.$sql));
                
                $firstpayments = $res->fetch_assoc();
                
                // Actualizamos los registros
                $sql = "UPDATE payments SET ".
                "       due = ".$totalamount['qty'].", ".
                "       date = '".$firstpayments['date']."' ".
                "       WHERE schoolid = ".$row['id'];
                if (!$db->query($sql))
                    badEnd("500", array("msg"=>$db->error.$sql));
                
                // Actualizamos el primer pago con la cantidad
                $sql = "UPDATE payments SET ".
                "       pupilqty = ".$row['allowedpupils'].
                "       WHERE schoolid = ".$row['id'].
                "       AND id = ".$firstpayments['id'];
                if (!$db->query($sql))
                    badEnd("500", array("msg"=>$db->error.$sql));
            }
        }
    }
    
    header("HTTP/1.1 200");
    echo (json_encode($out));
    die();*/
?>