<?php 
if (isset($_GET["csv"]) && $_GET["csv"]="true") {
	$csv=true;
	$EXPORT_GLOBAL=true;
	}
else $csv=false;
if (!$csv) include ("header.php");
else {

	include ("../master.php");
	}

statistique_display();

function statistique_display(){
	global $csv;
	$sql_more="WHERE 1  AND id_contrat='0' AND remplace_par=''";
	

	

	if (!isset($_GET["dfc"])) $_GET["dfc"]="";//date_field_contrat
	switch ($_GET["dfc"]) {
		case "so" :	$date_order="date_souscription"; break;
		case "aj" : $date_order="date_add"; break;
		case "ef" : $date_order="date_effet_debut"; break;
		case "fe" : $date_order="date_effet_fin"; break;
		case "ec" : $date_order="date_echeance"; break;
		default   : $date_order="date_effet_debut"; break;
		}
	if (isset($_GET["ct"]) && $_GET["ct"]!="")  //contrat_type
		$sql_more.=" AND id_type='".sn($_GET["ct"])."'";
	if (isset($_GET["ida"]) && $_GET["ida"]!="") 
		$sql_more.=" AND id_apporteur='".sn($_GET["ida"])."'";
	else $sql_more.=" AND id_apporteur!='0'";
	
	
	
	if (!isset($_GET["date_type"])) $sql_more.=" AND YEAR(".$date_order.")='".date("Y")."'";
	if (isset($_GET["date_type"]) && $_GET["date_type"]=="0") {
		if (isset($_GET["dlannee"])) $sql_more.=" AND YEAR(".$date_order.")='".sn($_GET["dlannee"])."'";
		if (isset($_GET["dlmois"]) && $_GET["dlmois"]!="") $sql_more.=" AND MONTH(".$date_order.")='".sn($_GET["dlmois"])."'";
		}
	elseif (isset($_GET["date_type"]) && $_GET["date_type"]=="1") {
		$sql_more.=" AND ".$date_order.">='".sn($_GET["dannee"])."-".sn($_GET["dmois"])."-".sn($_GET["djour"])." 00:00:00'";
		$sql_more.=" AND ".$date_order."<='".sn($_GET["fannee"])."-".sn($_GET["fmois"])."-".sn($_GET["fjour"])." 00:00:00'";
		}
	
	$orderby=$date_order;
	$way="ASC";
	$way_new="d";
	if (isset($_GET["ob"]) && $_GET["ob"]=="t") $orderby='c.id_type';
	if (isset($_GET["ob"]) && $_GET["ob"]=="m") $orderby='(c.montant+0)';
	if (isset($_GET["ob"]) && $_GET["ob"]=="c") $orderby='(c.prime+0)';
	if (isset($_GET["ob"]) && $_GET["ob"]=="d") $orderby=$date_order;
	if (isset($_GET["way"]) && $_GET["way"]=="a") {
		$way="ASC";
		$way_new="d";
		}
	if (isset($_GET["way"]) && $_GET["way"]=="d"){
		$way="DESC";
		$way_new="a";
		}
	
	$sql="SELECT c.*, ".$date_order." as my_date FROM contrats c ".$sql_more." 
		ORDER BY ".$orderby.' '.$way;
	$result=mysql_query($sql); echo mysql_error();
	$echo_me="";
	$total=0;
	$totalc=0;
	$nb=0;
	
	while ($row=mysql_fetch_array($result)){
		
		$total+=$row["montant"];
		$totalc+=$row["prime"];
		$nb++;
		if ($row["incomplet"]==1) $color="#FFEEEE";
		elseif ($row["prime_paye"]!="0000-00-00 00:00:00") $color="#EEEEEE";
		else $color="#FFFFFF";
		if (!$csv){
			$echo_me.='<tr>
			<td bgcolor="'.$color.'">'.(($row["prime_paye"]=="0000-00-00 00:00:00")?'<input
			onclick="update_somme('.$row["id"].', this.checked, this.value);"
			 type="checkbox" name="ids['.$row["id"].']" value="'.html($row["prime"]).'">':""). '</td>
			<td bgcolor="'.$color.'" align="center">'.ucwords(strtolower(contrat_type_nom($row["id_type"]) )).'
			</td>
			<td bgcolor="'.$color.'" width="300"><a target="_blank" href="../collaborateurs/contrat.php?req=liste&id_axa='.$row["id_axa"].'">'.$row["id_axa"].'</a>
			'.client_nom($row["id_client"]).'</td>
			<td bgcolor="'.$color.'" align="center">'.$row["montant"].'€</td>
			<td bgcolor="'.$color.'" align="center">'.$row["prime"].'€</td>
			<td bgcolor="'.$color.'" align="center">'.dateshort($row["my_date"]).'</td>
			<td bgcolor="'.$color.'" align="center">'.(($row["prime_paye"]!="0000-00-00 00:00:00")?dateshort($row["prime_paye"]):"Non&nbsp;payé").'</td>
			</tr>';
			}
			
		else {
			$echo_me.='"'.ucwords(strtolower(contrat_type_nom($row["id_type"]) )).'";';
			$echo_me.='"'.$row["id_axa"].'";';
			$echo_me.='"'.client_nom($row["id_client"]).'";';
			$echo_me.='"'.$row["montant"].'";"'.$row["prime"].'";"'.dateshort($row["my_date"]).'";"'.(($row["prime_paye"]!="0000-00-00 00:00:00")?dateshort($row["prime_paye"]):"Non&nbsp;payé").'"';
			$echo_me.="\n";
			}
			
		}
	if (!$csv){
		$qr=$_SERVER["QUERY_STRING"];
		$qr=str_replace("&ob=".((isset($_GET["ob"]))?$_GET["ob"]:"")."&way=".((isset($_GET["way"]))?$_GET["way"]:""), "", $qr);
		
		echo '<center>
		
		<table border=0 cellpadding="4" cellspacing="1" bgcolor="#eeeeee" width="100%">
		
		
		<tr class="white">
			<td bgcolor="#000000" width="1%"></td>
			<td bgcolor="#000000" align="center"><a class="white" href="apporteur.php?'.$qr.'&ob=t&way='.$way_new.'"><u><b>Type</b></u></a></td>
			<td bgcolor="#000000"><b>Contrat</b></td>
			<td bgcolor="#000000" align="center"><a class="white" href="apporteur.php?'.$qr.'&ob=m&way='.$way_new.'"><u><b>Montant</b></u></a></td>
			<td bgcolor="#000000" align="center"><a class="white" href="apporteur.php?'.$qr.'&ob=c&way='.$way_new.'"><u><b>Commission</b></u></a></td>
			<td bgcolor="#000000" align="center"><a class="white" href="apporteur.php?'.$qr.'&ob=d&way='.$way_new.'"><u><b>Date</b></u></a></td>
			<td bgcolor="#000000" align="center"><a class="white" href="apporteur.php?'.$qr.'&ob=d&way='.$way_new.'"><u><b>Payé&nbsp;le</b></u></a></td>
		</tr>';
		
		echo $echo_me;
		echo '
		<tr class="white" bgcolor="#999999">
			<td width="1%"></td>
			<td><b>TOTAUX</b></td>
			<td><b>'.$nb.' contrats</b></td>
			<td align="center"><b>'.$total.'€</b></td>
			<td align="center"><b>'.$totalc.'€</b></td>
			<td> </td>
			<td> </td>
		</tr>
		</table></center>
		<br />
		&nbsp;<b>A payer :</b>&nbsp;<div style="display:inline;" id="somme_html"><b>0</b></div>&nbsp;€
		<br /><br />
		<form target="_blank" method="get" action="print_apporteur.php">
		<input type="hidden" name="id_all" id="id_all" value="">
		<input type="hidden" name="total" id="total" value="">
		<input type="submit" value="Imprimer la facture">
		</form>
		<script>
		somme=0;
		function update_somme(my_id, add, value){
			if (add) somme+=parseFloat(value);
			else somme-=parseFloat(value);
			document.getElementById("somme_html").innerHTML="<b>"+somme+"</b>";
			document.getElementById("total").value=somme;

			if (add) document.getElementById("id_all").value+=my_id+"-";
			else {
				ids=document.getElementById("id_all").value;
				tmp=ids.split("-");
				id_all_new="";
				
				for (i=0; i<tmp.length; i++){
					if (tmp[i]!=my_id && tmp[i]!="") id_all_new+=tmp[i]+"-";
					}
				document.getElementById("id_all").value=id_all_new;
				}
			}
		
		</script>
		';
		}
	else {
		$dh = opendir("./"); 
	   	while ($file = readdir($dh)) {
			if($file!='..' && $file!='.' && $file!='' && strrchr($file, ".")==".csv"){ 
				@unlink($file);
				}
			}
		closedir($dh);
		$uid=uniqid("");
		$file=fopen("export_".$uid.".csv", "w");
		fwrite($file, $echo_me);
		fclose($file);
		kick_me("export_".$uid.".csv");
		}
	}




if (!$csv) include ("footer.php");
?>