<?php
include ("header.php");


function client_new_mod($id, $id_axa) { 
	global $link, $CLIENTS, $GLOBAL_TITRE;

	$color_table="#FFFFFF";
	$color_left="#FAFAFA";
	$color_right="#FFFFFF";
		
	if ($id=='' && $id_axa==''){
		$civilite="";
		$nom="";
		$prenom="";
		$date_naissance="0000-00-00";
		$adresse1="";
		$adresse2="";
		$adresse3="";
		$code_postal="";
		$ville="";
		$telephone_domicile="";
		$telephone_pro="";
		$portable="";
		$fax="";
		$email="";
		$information="";
		
		$titre_page="Ajout d'un ";
		$info="";
		$rel_visible="none";	
		
		$id="";
		$id_axa="";
		}
		
	else {
		if ($id_axa!="") $result=mysql_query("SELECT * FROM clients WHERE id_axa='$id_axa'", $link);
		else $result=mysql_query("SELECT * FROM clients WHERE id='$id'", $link);
		if (mysql_num_rows($result)==0) {echo '<center><br />Aucun enregistrement ne correspond à cet identifiant !<br /><br /></center>'; return false;}
		$row=mysql_fetch_array($result); 
		foreach ($row as $key=>$value) ${$key}=html($value);
			
		
		$titre_page="Modification du ";
		$info="&nbsp;&nbsp;&nbsp;Ajouté le ".datetime_short($date_add);
		if ($date_mod!="0000-00-00 00:00:00") $info.="<br>&nbsp;&nbsp;&nbsp;Modifié le ".datetime_short($date_mod);
		}
	
	$title=$titre_page.'client '.$nom.' '.$prenom;
	$GLOBAL_TITRE.=$title;
	
	
	echo '
	
	
	<table cellpadding="2" cellspacing="0" bgcolor="000000" width="100%">
	<form method="post" action="client.php" name="form1">
    <tr> 
       <td valign="top" class="white" height="22">
		<b>&raquo;&nbsp;</b><a class="white" href="client.php?req=liste"><b>Clients</b></a><b>&nbsp;&raquo;&nbsp;'.$title.'</b>
	</td></tr>
	<tr> 
       <td valign="top" class="white" height="22" bgcolor="#999999">'.$info.'</td>
	</tr>
	
	<tr>
    <td valign="top" width="100%" bgcolor="ffffff">
	
	
	
	<table cellpadding="5" cellspacing="1" border="0" bgcolor="'.$color_table.'">
	
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Id</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="id_axa" size="50" maxlength="250" value="'.$id_axa.'"></td>
	</tr>
	
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Civilité</b></td>
		<td bgcolor="'.$color_right.'"><select name="civilite">';
		foreach ($CLIENTS["civilite"] as $key=>$value) {
			echo '<option value="'.$key.'"'.(($key==$civilite)?'SELECTED':"").'>'.$value;
			}
		echo '
			</select></td>
	</tr>
	
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Nom</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="nom" size="50" maxlength="250" value="'.$nom.'"></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Prénom</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="prenom" size="50" maxlength="250" value="'.$prenom.'"></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Date de naissance</b></td>
		<td bgcolor="'.$color_right.'">'.html_form_date($date_naissance, "naissance_").'</td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Adresse</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="adresse1" size="50" maxlength="250" value="'.$adresse1.'"></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Adresse</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="adresse2" size="50" maxlength="250" value="'.$adresse2.'"></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Adresse</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="adresse3" size="50" maxlength="250" value="'.$adresse3.'"></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Code&nbsp;Postal</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="code_postal" size="50" maxlength="250" value="'.$code_postal.'"></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Ville</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="ville" size="50" maxlength="250" value="'.$ville.'"></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Téléphone Domicile</b></td>
		<td bgcolor="'.$color_right.'" ><input type="text" name="telephone_domicile" size="50" maxlength="50" value="'.$telephone_domicile.'"></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Téléphone Bureau</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="telephone_pro" size="50" maxlength="50" value="'.$telephone_pro.'"></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Téléphone Portable</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="portable" size="50" maxlength="50" value="'.$portable.'"></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Fax</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="fax" size="50" maxlength="50" value="'.$fax.'"></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" align="right"><b>Email</b></td>
		<td bgcolor="'.$color_right.'"><input type="text" name="email" size="50" maxlength="100" value="'.$email.'"></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'" valign="top" align="right"><b>Infos</b></td>
		<td bgcolor="'.$color_right.'"><textarea name="information" cols="49" rows="6" style="overflow:auto;">'.$information.'</textarea></td>
	</tr>
	<tr>
		<td bgcolor="'.$color_left.'"></td>
		<td bgcolor="'.$color_right.'"><br/></td>
	</tr>
	
	</table>
	
	</td></tr>
    <tr>
	<td align="center"><br/>
	<input type="hidden" name="req" value="save">
	<input type="hidden" name="id" value="'.$id.'">
	<input type="submit" value="Enregistrer"><br /><br /></td></tr>
	</form></table>
	';
	}



function client_save() {
    global $link, $GLOBAL_TITRE;

	foreach ($_POST as $key=>$value) {
		if (is_array($value)) {
			foreach ($value as $akey=>$avalue) {
				if (!is_array($avalue)) $_POST[$key][$akey]=var_post_post($avalue);
				else $_POST[$key][$akey]=$avalue;
				}
			}
		else $_POST[$key]=var_post_post($value);
		}
		
	$error=0;
	echo '<center>';	
	
	if ($_POST["id_axa"]=="") {
        $error=1;
        echo '<b>Le champ identifiant doit être saisi !</b><br><br>';
    	}
	
	if ($_POST["nom"]=="" || $_POST["prenom"]=="") {
        $error=1;
        echo '<b>Les nom et prénom doivent être saisis !</b><br><br>';
    	}
		
	$sql="SELECT id FROM clients WHERE id_axa='".mysql_escape_string($_POST["id_axa"])."'";
	$test_res=mysql_query($sql, $link);
	if (mysql_num_rows($test_res)!=0){
		$test_id=mysql_result($test_res, 0, "id");
		if ($_POST["id"]!=""){
			if ($test_id!=$_POST["id"]) {
				$error=1;
				echo 'Ce client est déjà inscrit !';
				}
			}
		else {
			$error=1;
			echo 'Ce client est déjà inscrit !';
			}
		}
			
	if ($error!=1){
	 	$sql_more="";
		$_POST["date_naissance"]=$_POST["naissance_annee"]."-".$_POST["naissance_mois"]."-".$_POST["naissance_jour"];
		
		foreach ($_POST as $key=>$value){
			if ($key!="req" && $key!="id" && substr($key, 0, 9)!="naissance") $sql_more.=",".$key."='".$value."'";
			}
		
		
		if ($_POST["id"]=="") {
			$sql_action="INSERT INTO";
			$sql_more="date_add=NOW() ".$sql_more;
			}
		else {
			$sql_action="UPDATE";
			$sql_more="date_mod=NOW() ".$sql_more." WHERE id='".$_POST["id"]."'";
			}
			
		
		$sql=$sql_action." clients SET ".$sql_more;
		mysql_query($sql, $link);
		if ($_POST["id"]=="") $_POST["id"]=mysql_insert_id();
		echo '
			<br><font class="normal">Merci !</font><br>
			<font class="normal">Les informations ont été enregistrées !
			<br><font class="normal">La fiche <a class="normal" href="client.php?req=mod&id='.$_POST["id"].'">'.stripslashes($_POST["nom"].' '.$_POST["prenom"]).'</a> est à jour</font>
			<br><br><font class="normal"><a class="normal" href="client.php?req=addone">Ajouter une personne</a></font><br>
			<font class="normal"><a class="normal" href="client.php?req=liste">Retour à la liste</a></font><br></center><br />';
			$GLOBAL_TITRE.="Enregistrement #".$_POST["id_axa"].' effectué';
		}
	else echo '<br/><input type="button" onClick="history.go(-1)" value="Retour"></center>';
	}


	
function client_liste() {
    global $link, $GLOBAL_TITRE;
	$GLOBAL_TITRE.="Liste";
	
	echo '
	<table border=0 cellpadding="5" cellspacing="1" bgcolor="#eeeeee" width="100%">
		<tr bgcolor="#FFFFFF"><td colspan="3">';

	
	
	if (isset($_GET["req"]) && $_GET["req"]=="search"){
		if (!isset($_GET["id_axa"])) $_GET["id_axa"]="";
		$lettre=trim(stripslashes(urldecode($_GET["id_axa"])));
		$sql="
		CONCAT_WS(' ', nom, prenom)='".sn($lettre)."' OR 		CONCAT_WS(' ', prenom, nom)='".sn($lettre)."' OR 
		prenom LIKE '%".sn($lettre)."' OR
		prenom LIKE '%".sn($lettre)."%' OR
		prenom LIKE '".sn($lettre)."%' OR
		prenom LIKE '".sn($lettre)."' OR
		nom LIKE '%".sn($lettre)."' OR
		nom LIKE '%".sn($lettre)."%' OR
		nom LIKE '".sn($lettre)."%'
		GROUP BY id
		";
		}
		
	else {
		if (!isset($_GET["lettre"])) $_GET["lettre"]="";
		$lettre=strtolower(substr($_GET["lettre"], 0 , 1));
		$test=ord($lettre);
		if ((($test< 97) || ($test> 122)) && ($test!=ord("0"))) $lettre="";
		
		if ($lettre=="") $sql="1";
		elseif ($lettre=="0") $sql="UPPER(nom) NOT REGEXP '^[a-z]'";
		else $sql="UPPER(nom) LIKE '".$lettre."%'";
		}
	
	
	
	$query="SELECT distinct(LEFT(nom, 1)) as alpha FROM clients ORDER BY alpha";
	$result = mysql_query($query, $link);
	$undone=true;
	while ($row = mysql_fetch_array($result)){
		$test=ord(strtolower($row["alpha"]));
		if (($test>= 97) && ($test<= 122)) echo '<a href="client.php?lettre='.$row["alpha"].'"><b>'.strtoupper($row["alpha"]).'</b></a>&nbsp;&nbsp;';
		else {
			if ($undone) echo '<a href="client.php?lettre=0"><b>#</b></a>&nbsp;&nbsp;';
			$undone=false;
			}
		}
		
	
	
	echo '
	</td></tr>
	';
	if ($lettre!=""){
		echo '
		
		<tr class="white">
		<td width="30" bgcolor="#000000"><b>#</b></td>
		<td bgcolor="#000000" width="60%"><b>Nom/Prénom</b></td>
		<td bgcolor="#000000" align="center"><b>Actions</b></td>
		</tr>';
		$query="SELECT id, id_axa, nom, prenom FROM clients WHERE ".$sql." ORDER by nom ASC";
		$result=mysql_query($query, $link);
		if (mysql_num_rows($result)=="1" && eregi("/contrat.php", $_SERVER["HTTP_REFERER"])) kick_me("contrat.php?req=liste&id_client=".mysql_result($result, 0, "id_axa"));
		while ($row=mysql_fetch_array($result)){
			$id=$row['id'];	
			$id_axa=$row['id_axa'];	
			$nom=html($row['nom']);
			$prenom=html($row['prenom']);
			$linked="<br>";
			/*$query2 = "SELECT id, nom FROM contrats c, relation_organisations_personnes r WHERE r.id_personne='".$id."' AND o.id=r.id_organisation ORDER by nom ASC";
			$result2 = mysql_query($query2, $link);
			while ($row2 = mysql_fetch_array($result2)){
				$linked.='&nbsp;&nbsp;<a class="sub" href="organisations.php?req=mod&id='.$row2["id"].'">'.$row2["nom"].'</a>';
				}*/
			echo '<tr bgcolor="#FFFFFF">
			<td>
			<table cellpadding="0" cellspacing="0" border="0" width="100%">
				<tr>
					<td><a href="client.php?req=mod&id='.$id.'"><img src="../images/admin/client.gif" align="absmiddle" border="0">'.$id_axa.'</a></td>
					<td align="right"><a title="Voir tous les contrats de ce client" href="contrat.php?req=liste&id_client='.$id_axa.'&incomplet=&resilie="><img align="absmiddle" src="../images/admin/filtre.gif" border="0"></a></td>
				</tr>
			</table>
			</td>
			<td>'.strtoupper($nom).' '.ucwords($prenom).'</td>
			<td align="center">
			<a title="Modifier" href="client.php?req=mod&id='.$id.'"><img src="../images/admin/mod.gif" align="absmiddle" border="0"></a>
			<a title="Supprimer" href="#" onclick="if(window.confirm(\'Etes vous sûr de vouloir supprimer le client #'.$id_axa.' ?\')) location.href=\'client.php?req=del&id='.$id.'\'; else return false;"><img src="../images/admin/del.gif" align="absmiddle" border="0"></a></td>		
			</td>		
			</tr>';
			}
		}
	

	
	echo '</table>';
	}


function client_del($id) {
    global $link;
	if (($id=="")||($id=="0")) exit();
   	mysql_query("DELETE FROM clients WHERE id='$id'", $link);
	
	$query="SELECT id FROM contrats WHERE id_client='".$id."'";
	$result=mysql_query($query, $link);
	while ($row=mysql_fetch_array($result)){
		$idc=$row["contrat"];
		mysql_query("DELETE FROM contrats_pieces WHERE id_contrat='".$idc."'", $link);
		mysql_query("DELETE FROM contrats WHERE id_contrat='".$idc."'", $link);
		mysql_query("DELETE FROM contrats WHERE id='".$idc."'", $link);
		}
	client_liste();
	}

if (!isset($req)) $req="";
switch($req) {

        case "liste":
            client_liste();
            break;
			
		case "search":
            client_liste();
            break;
	
        case "addone":
            client_new_mod(0, 0);
            break;
		
        case "mod":
			if (!isset($id)) $id="";
            if (!isset($id_axa)) $id_axa="";
            client_new_mod($id, $id_axa);
            break;
						
        case "save":
			client_save();
            break;

        case "del":
            client_del($id);
            break;

      default:
            client_liste();
            break;
	}

include ("footer.php");
?>