<?php
error_reporting(0);
if (isset($_POST["req"]) && $_POST["req"]=="save") {
	include("../master.php");
	$contrats=explode(":", $_POST["contrats"]);
	foreach ($contrats as $infos){
		$info=explode("-", $infos);
		$id_contrat=$info[0];
		if ($id_contrat!=""){
			$relance_num=$info[1];
			$relance=contrat_relance_get($id_contrat);
			$relance[$relance_num]=date("Y-m-d H:i:s");
			contrat_relance_set($id_contrat, $relance);
			}
		}
	echo '<center>Les infos ont été enregistrées !</center>';
	}

if (!isset($_GET["req"])) {
	?>

<html>
<head>
<title>Impression de courrier</title>
<link rel="stylesheet" href="../style/axa.css">
</head>

 <frameset rows="17%, 83%">
      <frame id="print_top" name="print_top" src="print_all.php?req=top&id=<?php echo $_GET["id"];?>&idr=<?php echo $_GET["idr"];?>">
      <frame id="print_print" name="print_print" src="print_all.php?req=print&id=<?php echo $_GET["id"];?>&idr=<?php echo $_GET["idr"];?>">
  </frameset>
</html><?php }
else {
	include("../master.php");

	
	if ($_GET["req"]!="print") 
		echo '<html>
			<head>
			<link rel="stylesheet" href="../style/axa.css">
			</head>
			<body>
			';
			
	switch($_GET["req"]){
		
		case "top":
			echo '
			<center>
			<form name="save_me" id="save_me" method="post" action="print_all.php" target="iframe_save">
			<input type="hidden" name="req" value="save">
			<input type="hidden" name="contrats" id="contrats" value="">
			
			
			<iframe id="iframe_save" name="iframe_save" src="" scrolling="no" frameborder="0" width="500" height="30"></iframe><br />
			<input type="button" id="my_button" style="display:none;" value="Imprimer tous les contrats" onclick="parent.print_print.print_me(); document.getElementById(\'save_me\').submit();">
			</form>
			</center>
			';
			break;

		
		case "print":
			echo '	<html>
			<head>
			<style>
			table, body, div, span {
			font-family:  Eras Light ITC;
				font-size:14px;
				color:black;
				line-height:16px;
				text-align:justify;
				}
			</style>
			</head>
			<body>
			';
			$result_all=mysql_query("SELECT * FROM contrats WHERE incomplet='1'", $link);
			$first=true;
			$relances="";
			while ($row_all=mysql_fetch_array($result_all)){
				foreach ($row_all as $key=>$value) ${$key}=html($value);
				if (!$resilie && !contrat_is_resilie(contrat_id_contrat($id))){	
					$relance=unserialize($row_all["relance"]);
					$idr=0;
					$date_tmp=array();
					$date_tmp=explode("-", $date_souscription);
					$date_tmp[4]=$date_tmp[2];
					$date_tmp[2]=$date_tmp[0];
					$date_tmp[0]=$date_tmp[4];
					if (!empty($relance)){
						$date_relance=array();
						ksort($relance);
						foreach ($relance as $key=>$value){
							$date_relance[$key]=dateshort($value);
							}
						
						$idr=$key+1;
						$date_tmp=explode("/", $date_relance[$key]);
						if ($idr>=3) $date_suspension=date("d/m/Y", mktime()+24*3600*15);
						}
						
					if ($idr<=3 && ((mktime(0, 0, 0, $date_tmp[1], $date_tmp[0], $date_tmp[2])+(24*3600*15))<mktime())){
						if ($id_contrat!="0") {
							$id_client=contrat_id_client($id_contrat);
							$categorie=contrat_type_nom(contrat_id_type($id_contrat));
							$courrier="contrat_remplacement";
							}
						else {
							if ($id_type=="16" && ($idr=="" || $idr=="0")) $courrier="contrat_nouveau_juridique"; // 16 => juridique
							else $courrier="contrat_nouveau";
							}
						
						$categorie=contrat_type_nom($id_type);
						
						$result=mysql_query("SELECT id_axa, civilite, nom, prenom, adresse1, adresse2, adresse3, code_postal, ville FROM clients WHERE id='".$id_client."'", $link);
						while ($row=mysql_fetch_array($result)){
							if ($row["civilite"]=="") $row["civilite"]=3;
							$id_axa_client=$row["id_axa"];
							$titre=$CLIENTS["civilite"][$row["civilite"]];
							$nom=$row["nom"];
							$prenom=$row["prenom"];
							$adresse1=$row["adresse1"];
							$adresse2=$row["adresse2"];
							$adresse3=$row["adresse3"];
							$code_postal=$row["code_postal"];
							$ville=$row["ville"];
							}
						
						$pieces="";
						$need_contrat=false;
						$need_prime=false;
						$nothing=true;
			
						
						$result=mysql_query("SELECT p.id, p.nom, cp.fournie FROM contrats_pieces cp, pieces p WHERE cp.id_contrat='".$id."' AND p.id=cp.id_piece AND fournie='0'", $link);
						while ($row=mysql_fetch_array($result)){
							if ($idr=="" || $idr=="0"){
								$nothing=false;
									if ($row["id"]==1) $need_contrat=true; 
								elseif ($row["id"]==2) $need_prime=true;
								else $pieces.="<b>".$row["nom"]."</b><br />";
								}
							else {
								if ($row["id"]==2){
									if ($montant!="0")
										$pieces.="<b>RÈGLEMENT DE ".$montant." €</b><br />";
									}
								else $pieces.="<b>".$row["nom"]."</b><br />";
								}
							}
						
						if (!$need_contrat) $contrat=false; else $contrat=true;
						if (!$need_prime) $montant="0";
												
						if (!$first) echo '<div style="page-break-before: always;">';
						include("../courriers/courrier_modele_header.php");
						echo '<div style="width:550px; text-align:justify; margin-left:45px;"><br /><br />';
						
						if ($resiliation!="") include("../courriers/contrat_resiliation.php");
						elseif ($idr=="" || $idr=="0") include("../courriers/".$courrier.".php");
						else include("../courriers/relance_".$idr.".php");
						echo '</div>';
						include("../courriers/courrier_modele_footer.php");
						$first=false;
						$relances.=$id."-".$idr.":";
						}
					}
				}
			if ($first) echo '<center><br /><br />Aucun contrat n\'est à relancer !</center>';
			echo '
				<input type="hidden" id="relances" value="'.$relances.'">
				</div>
			<script>
			load_me();
			function load_me(){
				if (!parent.print_top.document.getElementById(\'my_button\'))
					setTimeout(\'load_me()\', 100);
				else {
					parent.print_top.document.getElementById(\'contrats\').value=\''.$relances.'\';
					parent.print_top.document.getElementById(\'my_button\').style.display=\'inline\';	
					}
				}
						
			function print_me(){
				this.focus();
				window.print();
				}
			</script>
			';
			break;
		
		default : break;
		}
	echo '
		</body>
		</html>';
	}

?>