Index: trunk/admin/modules/Elezioni/ele_voti.php
===================================================================
--- trunk/admin/modules/Elezioni/ele_voti.php	(revision 218)
+++ trunk/admin/modules/Elezioni/ele_voti.php	(revision 221)
@@ -165,36 +165,52 @@
 			list($votil) = mysql_fetch_row($res3);
 			if($votil) {
-						
-					$query="SELECT sum(voti) FROM ".$prefix."_ele_voti_lista where id_sez='$id_sez'";
+					$query="SELECT sum(voti) FROM ".$prefix."_ele_voti_lista where id_sez='$id_sez'";	
 					$res4 = mysql_query("$query", $dbi);
 					list($votils)=mysql_fetch_row($res4);
-					if($votils>0) {
-						$query="SELECT id_lista FROM ".$prefix."_ele_lista where id_cons='$id_cons'";
+#					if($votils>0) {
+					if(mysql_num_rows($res4)>0) {
+						$query="SELECT stato_aff, stato_val,stato_gru,stato_lis,stato_can FROM ".$prefix."_ele_controllosez where id_sez='$id_sez' and id_cons='$id_cons'";
 						$res3 = mysql_query("$query", $dbi);
-						$testcand=0;
-						$tutte=1;
-						while(list($testlista)=mysql_fetch_row($res3)){
-							$res4= mysql_query("SELECT sum(voti) FROM ".$prefix."_ele_voti_lista where id_cons='$id_cons' and id_sez='$id_sez' and id_lista='$testlista'", $dbi);
-							if($res4) list($testvotil)=mysql_fetch_row($res4); else $testvotil=0;
-							$res4= mysql_query("SELECT sum(t1.voti) 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' and t2.id_lista='$testlista'", $dbi);
-							if($res4) list($testvotic)=mysql_fetch_row($res4); else $testvotic=0;
+							$res4= mysql_query("SELECT count(0) 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' and t2.id_lista='$testlista'", $dbi);
+							list($contains)=mysql_fetch_row($res4);
+							if($contains==0) $tutte=1; else $tutte=0;
+						if(mysql_num_rows($res3)){
+							list($aff,$val,$gru,$lis,$can)=mysql_fetch_row($res3); 
+						}else{
+							$query="SELECT id_lista FROM ".$prefix."_ele_lista where id_cons='$id_cons'";
+							$res3 = mysql_query("$query", $dbi); 
+							$testcand=0;
+							
 							$res4= mysql_query("SELECT preferenze FROM ".$prefix."_ele_cons_comune where id_cons='$id_cons'", $dbi);
 							if($res4) list($testpref)=mysql_fetch_row($res4); else $testpref=0;
-							$res4= mysql_query("SELECT count(0) 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' and t2.id_lista='$testlista'", $dbi);
-							list($contains)=mysql_fetch_row($res4);
-                                			if($contains==0) $tutte=0;
-
-							if(($testvotil*$testpref)<$testvotic) { $testcand=1;break;}
-							$res4= mysql_query("SELECT t1.voti 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' and t2.id_lista='$testlista'", $dbi);
-							while(list($testvotic)=mysql_fetch_row($res4))
-								if($testvotil<$testvotic) { $testcand=1;break;}
+							while(list($testlista)=mysql_fetch_row($res3)){
+								$res4= mysql_query("SELECT sum(voti) FROM ".$prefix."_ele_voti_lista where id_cons='$id_cons' and id_sez='$id_sez' and id_lista='$testlista'", $dbi);
+								if($res4) list($testvotil)=mysql_fetch_row($res4); else $testvotil=0;
+								$res4= mysql_query("SELECT sum(t1.voti) 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' and t2.id_lista='$testlista'", $dbi);
+								if($res4) list($testvotic)=mysql_fetch_row($res4); else $testvotic=0;
+
+								if(($testvotil*$testpref)<$testvotic) { $testcand=1;break;}
+								$res4= mysql_query("SELECT t1.voti 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' and t2.id_lista='$testlista'", $dbi);
+								while(list($testvotic)=mysql_fetch_row($res4))
+									if($testvotil<$testvotic) { $testcand=1;break;}
+							}
 						}
+						if(!isset($can)){ 
+							$can=($testcand==0) ? 1:-1;
+							if($genere==4 or $votog) $contestatilista=0;
+							$val=(($votils+$sologruppo+$contestatilista+$votinullilista)==($validi+$sololista))?1:-1;
+							$lis=($validilista==$votils) ? 1:-1;
+							$res4=mysql_query("insert into ".$prefix."_ele_controllosez values('$id_sez','$id_cons','0','$val','0','$lis','$can')",$dbi);
+						}
+						#		$res4= mysql_query("SELECT sum(voti) FROM ".$prefix."_ele_voti_lista where  id_sez='$id_sez' and id_lista='$testlista' and id_cons='$id_cons' ", $dbi);
+				#		$res4= mysql_query("SELECT sum(t1.voti) 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' and t2.id_lista='$testlista'", $dbi);
 						if($genere==4 or $votog) $contestatilista=0;
-						if (($votils+$sologruppo+$contestatilista+$votinullilista)==($validi+$sololista) and $validilista==$votils and $testcand==0)
+#						if (($votils+$sologruppo+$contestatilista+$votinullilista)==($validi+$sololista) and $validilista==$votils and $testcand==0)
+						if ($val==1 and $lis==1 and $can==1)
 							{$status=$tutte;$ops=-3;}
 						else {return 3;}
 					} #else return 0;
 			}elseif($genere==5 and $fascia<=$limite){
-				$testcand=0;
+/*				$testcand=0;
 				$query="SELECT id_lista,id_gruppo FROM ".$prefix."_ele_lista where id_cons='$id_cons'";
 				$res3 = mysql_query("$query", $dbi);
@@ -214,5 +230,5 @@
 				while( list($testvotic)=mysql_fetch_row($res4))
 					if(($testvotit)<$testvotic) {$testcand=1;break;}
-				}
+				}*/
 				if($testcand==0)
 					{$status=$tutte;$ops=-3;}
@@ -823,5 +839,6 @@
 	}
 	if ((($genere==3) OR ($genere==5)) and !$id_lista and !$votog and ($fascia>$limite or !$id_conf)) $y = (count($arg)-4);
-	else $y=count($arg);
+	else $y=count($arg);#die ("delete from ".$prefix."_ele_controllosez where id_cons='$id_cons' and id_sez='$id_sez'");
+	mysql_query("delete from ".$prefix."_ele_controllosez where id_cons='$id_cons' and id_sez='$id_sez'", $dbi);
 	if($do==1) {
 			mysql_query("delete from ".$prefix."_ele_voti_lista where id_cons='$id_cons' and id_sez='$id_sez'", $dbi);
@@ -1037,4 +1054,5 @@
     $data=date("Y/m/d");
 	$tempo=date("H:i:s");
+	mysql_query("delete from ".$prefix."_ele_controllosez where id_cons='$id_cons' and id_sez='$id_sez'", $dbi);
 	$query="select * from ".$prefix."_ele_voti_parziale where data='$giorno' and orario='$ora' and id_sez='$id_sez'";
 	if ($genere==0){
@@ -1167,4 +1185,5 @@
 $log_ora=getdate(time());	
 $orario=($log_ora['hours'].":".$log_ora['minutes'].":".$log_ora['seconds']);
+mysql_query("delete from ".$prefix."_ele_controllosez where id_cons='$id_cons' and id_sez='$id_sez'", $dbi);
 
 $result= mysql_query("select voti_complessivi from ".$prefix."_ele_voti_parziale where id_cons='$id_cons' and id_sez='$id_sez' order by id_parz desc", $dbi);
@@ -1392,4 +1411,5 @@
 $do = intval($arg[5]);
 $y=count($arg)-1; 
+	mysql_query("delete from ".$prefix."_ele_controllosez where id_cons='$id_cons' and id_sez='$id_sez'", $dbi);
 	if($do==1) {
 			mysql_query("delete from ".$prefix."_ele_voti_gruppo where id_cons='$id_cons' and id_sez='$id_sez'", $dbi);
