Index: trunk/admin/modules/Elezioni/controllo_aff.php
===================================================================
--- trunk/admin/modules/Elezioni/controllo_aff.php	(revision 263)
+++ trunk/admin/modules/Elezioni/controllo_aff.php	(revision 263)
@@ -0,0 +1,126 @@
+<?php
+/************************************************************************/
+/* Eleonline - Raccolta e diffusione dei dati elettorali                */
+/************************************************************************/
+/* Modulo controlla affluenze                                               */
+/* Amministrazione                                                      */
+/************************************************************************/
+
+function controllo_aff($id_cons,$id_sez,$id_parz){
+	global $prefix,$dbi,$id_sede,$id_con_gen;
+
+	$err=0; 
+	$sql="select maschi,femmine from ".$prefix."_ele_sezioni where id_sez='$id_sez'";
+	$res = $dbi->prepare("$sql");
+	$res->execute();
+	list($maschi,$femmine)=$res->fetch(PDO::FETCH_NUM);
+
+	$sql="select voti_uomini,voti_donne,voti_complessivi from ".$prefix."_ele_voti_parziale where id_sez='$id_sez' and id_parz='$id_parz'";
+	$res = $dbi->prepare("$sql");
+	$res->execute();
+	while(list($voti_u,$voti_d,$voti_t)=$res->fetch(PDO::FETCH_NUM))
+		if((($voti_u+$voti_d!=$voti_t && $voti_u+$voti_d>0) || $voti_u>$maschi || $voti_d>$femmine || $voti_t>$maschi+$femmine) ) {$err=1; break;}
+
+	$tipo='affluenze';
+	$sql="select id from ".$prefix."_ele_controlli where tipo='$tipo' and id_sez='$id_sez' ";
+	$res = $dbi->prepare("$sql");
+	$res->execute();
+	$righe=$res->rowCount();
+	if($righe){
+		while(list($id)=$res->fetch(PDO::FETCH_NUM)){
+			if($id==$id_parz){
+				if(!$err){ 
+					$sql="delete from ".$prefix."_ele_controlli where tipo='$tipo' and id='$id_parz' ";
+					$res = $dbi->prepare("$sql");
+					$res->execute();
+					
+				}
+				$err=0;
+				break;
+			}					
+		}
+	}
+	if($err){
+		$sql="insert into ".$prefix."_ele_controlli value('$id_cons','$id_sez','$tipo','$id_parz')";
+		$res = $dbi->prepare("$sql");
+		$res->execute();		
+	}
+
+	$sql="SELECT validi,nulli,bianchi,contestati,voti_nulli FROM ".$prefix."_ele_sezioni as t1 where t1.id_sez='$id_sez'";
+	$res = $dbi->prepare("$sql");
+	$res->execute();
+	list($validi, $nulli, $bianchi,$contestati,$votinulli)=$res->fetch(PDO::FETCH_NUM);
+	if (($validi+$nulli+$bianchi+$contestati+$votinulli)>0)
+		controllo_voti($id_cons,$id_sez,'aff');
+
+#	include("ele_colora_sez.php");
+}
+
+
+	
+function controllo_voti($id_cons,$id_sez,$da){
+	global $prefix,$dbi,$id_sede,$id_con_gen,$genere;
+		##############################
+	$err=0;
+	$sql="SELECT id_gruppo FROM ".$prefix."_ele_gruppo where id_cons='$id_cons'";
+	$resref = $dbi->prepare("$sql");
+	$resref->execute();	
+	if($genere==0){ 
+		$numscru=$resref->rowCount(); $rifscru=0;
+		while(list($idrefgruppo)=$resref->fetch(PDO::FETCH_NUM)) {
+			$sql="SELECT si,no,validi,nulli,bianchi,contestati FROM ".$prefix."_ele_voti_ref where id_sez='$id_sez' and id_gruppo='$idrefgruppo'";
+			$res2 = $dbi->prepare("$sql");
+			$res2->execute();
+			$refscru=$res2->rowCount(); 
+			$sql="SELECT max(voti_complessivi) FROM ".$prefix."_ele_voti_parziale where id_sez='$id_sez' and id_gruppo='$idrefgruppo'";
+			$res3 = $dbi->prepare("$sql");
+			$res3->execute();
+			list($voti)=$res3->fetch(PDO::FETCH_NUM);
+			$rifscru++;
+			list($si,$no,$validi,$nulli,$bianchi,$contestati)=$res2->fetch(PDO::FETCH_NUM);
+			if(($si+$no==$validi) and ($validi+$nulli+$bianchi+$contestati==$voti)) 
+				continue;
+			else {$err=1; break;}	
+		}
+	}else{
+		#per le altre consultazione
+		$sql="SELECT max(voti_complessivi) FROM ".$prefix."_ele_voti_parziale where id_sez='$id_sez'";
+		$res3 = $dbi->prepare("$sql");
+		$res3->execute();
+		list($voti)=$res3->fetch(PDO::FETCH_NUM);
+		$sql="SELECT validi FROM ".$prefix."_ele_sezioni where id_sez='$id_sez' and id_cons='$id_cons' ";
+		$res2 = $dbi->prepare("$sql");
+		$res2->execute();
+		list($validi) = $res2->fetch(PDO::FETCH_NUM);
+		if($validi) {
+			$status=0;
+			$query="SELECT validi,nulli,bianchi,contestati,voti_nulli,solo_gruppo,validi_lista,contestati_lista,voti_nulli_lista,solo_lista FROM ".$prefix."_ele_sezioni as t1 where t1.id_sez='$id_sez'";
+			$sql="$query";
+			$res4 = $dbi->prepare("$sql");
+			$res4->execute();
+			list($validi, $nulli, $bianchi,$contestati,$votinulli)=$res4->fetch(PDO::FETCH_NUM);
+			if (($validi+$nulli+$bianchi+$contestati+$votinulli)!=$voti)
+				{$err=1;}
+		} 
+	}
+	$tipo='votanti';
+	$sql="select id from ".$prefix."_ele_controlli where tipo='$tipo' and id_sez='$id_sez' ";
+	$res = $dbi->prepare("$sql");
+	$res->execute();
+	$righe=$res->rowCount();
+	if($righe){
+		if(!$err){ 
+			$sql="delete from ".$prefix."_ele_controlli where tipo='$tipo' and id='$id_sez' ";
+			$res = $dbi->prepare("$sql");
+			$res->execute();
+		}
+		$err=0;
+	}
+	if($err){
+		$sql="insert into ".$prefix."_ele_controlli value('$id_cons','$id_sez','$tipo','$id_sez')";
+		$res = $dbi->prepare("$sql");
+		$res->execute();		
+	}
+	include("ele_colora_sez.php");
+}
+?>
Index: trunk/admin/modules/Elezioni/ele_colora_sez.php
===================================================================
--- trunk/admin/modules/Elezioni/ele_colora_sez.php	(revision 262)
+++ trunk/admin/modules/Elezioni/ele_colora_sez.php	(revision 263)
@@ -15,8 +15,8 @@
 	$res->execute();	
 	}else{ #candidati
-	$sql="SELECT t2.id_lista FROM ".$prefix."_ele_voti_candidati as t1 left join ".$prefix."_ele_candidati as t2 on t1.id_cand=t2.id_cand where t1.id_cons='$id_cons' and t1.id_sez='$id_sez' group by t2.id_lista";
+	$sql="SELECT count(t2.id_lista) FROM ".$prefix."_ele_voti_candidati as t1 left join ".$prefix."_ele_candidati as t2 on t1.id_cand=t2.id_cand where t1.id_cons='$id_cons' and t1.id_sez='$id_sez' group by t2.id_lista";
 	$res = $dbi->prepare("$sql");
 	$res->execute();
-	$liste=$res->rowCount();
+	list($liste)=$res->fetch(PDO::FETCH_NUM);
 	
 	$sql="SELECT count(id_lista) FROM ".$prefix."_ele_lista where id_cons='$id_cons'";
