<script type="text/javascript" src="../js/synthese_perso_img.js"></script>
<script type="text/javascript">
function changeCouleur(nouvelleCouleur, cnt) {

	elem = document.getElementById("idColor"+cnt);
	elem.style.backgroundColor = nouvelleCouleur;
}

function changeStyle(nouveauStyle, cnt) {

	document.images["idStyle"+cnt].src= 'img/ratios/' + nouveauStyle + '.png';
	document.images["idStyle"+cnt].alt= nouveauStyle;
}
</script>
<?php
if (!$_SESSION['connected'])	die();


/*
 * Si on recharge la page
 */
if(isset($_SESSION['imgcache'])) { // Si la session exite

	if ((file_exists(PATH_SITE.'/cache/imgcache/'.$_SESSION['imgcache']))) { // Si le fichier existe

	    @unlink(PATH_SITE.'/cache/imgcache/'.$_SESSION['imgcache']); // On supprime le fichier
	}
	unset($_SESSION['imgcache']); // On détruit la session
	$_SESSION['graphperso']->init(); // On vide le graphe
}

require_once 'cache/cache.php';
require_once 'ratios/ratios.php';

$siret=trim(preg_replace('/[^0-9]/', '', $_REQUEST['siret']));
if (strlen($siret)<>0 && strlen($siret)<>9 && strlen($siret)<>14) die('Paramètres incorrects !');

$idEntreprise=trim(preg_replace('/[^0-9]/', '', $_REQUEST['idEntreprise']))*1;	// Si id=0 alors non communiqué
if (($siret*1)==0 && $idEntreprise==0) die('Paramètres incorrects !');
$siren=substr($siret,0,9);

isset($_REQUEST['ratio'])? $ratio=$_REQUEST['ratio'] : $ratio='';

if (($siret*1)==0 || ($siren*1)<100){ $fileName = $page2.'-'.$idEntreprise;
}else{ $fileName = $page2.'-'.$siret; }

$ratiosEntrep = array();
$ratiosInfos = array();
$bilansInfos = array();
$rationsEntrepEvol = array();


try {
	$O = $client->getRatios($siren, 'synthese');
	$bilansInfos = $O['result']['BilansInfos'];
	$ratiosInfos = $O['result']['RatiosInfos'];
	$ratiosEntrep = $O['result']['RatiosEntrep'];
	$ratiosEntrepEvol = $O['result']['RatiosEntrepEvol'];
}catch (SoapFault $fault) {
	require_once 'soaperror.php';
	processSoapFault($client,$fault,$_SESSION['tabInfo']);
	die();
}


//Liste des ratios à  afficher
$tabRatio = array(
					'r5' => array('evol'=>'r6', 'op' => 1000, 'titre'=>'CHIFFRE D\'AFFAIRES'),
					'r7' => array('evol'=>'r8', 'op' => 1000, 'titre'=>'RESULTAT COURANT AVANT IMPOTS'),
					'r10' => array('evol'=>'r11', 'op' => 1000, 'titre'=>'RESULTAT NET'),
					'r18' => array('evol'=>'r19', 'op' => 1000, 'titre'=>'FONDS PROPRES'),
					'r22' => array('evol'=>'r23', 'op' => 1000, 'titre'=>'TOTAL BILAN'),
					'r231' => array('evol'=>'r235', 'op' => 1000, 'titre'=>'FONDS DE ROULEMENT'),
					'r232' => array('evol'=>'r236', 'op' => 1000, 'titre'=>'BESOIN EN FONDS DE ROULEMENT'),
					'r62' => array('evol'=>'r64', 'op' => 1000, 'titre'=>'TRESORERIE'),
					'r24' => array('evol'=>'r24', 'op' => 1, 'titre'=>'EFFECTIF', 'unite' => 1),
				);

/******************************************
 * 				Fonction				  *
 *****************************************/

function Date_to_DateFr($Date){
	if(!empty($Date))
	{
		$jour = substr($Date,6,2);
		$mois =substr($Date,4,2);
		$annee = substr($Date,0,4);
		$Date_to_DateFr = $jour."/".$mois."/".$annee;
	}
	return $Date_to_DateFr;
}


function synthese_datagraph_perso($bilansInfos, $ratiosEntrep, $tabRatioGraph, $tabRatioGraphEvol){
	global $firephp;
	$dataGraph = array();
	$dataEvol = array();

	$nbrAnnees = count($bilansInfos)-1;
	$i=0;
	foreach($bilansInfos as $dateCloture){
		//Données pour le graphique
		if(isset($dateCloture['dateCloture'])){
			$dataGraph[$i]['date'] = $dateCloture['dateCloture'];
			$dataGraph[$i]['duree'] = $dateCloture['duree'];
		}
		//Données pour les graphiques évolutions
		if(isset($bilansInfos[$nbrAnnees-$i]['dateCloture'])){
			foreach($tabRatioGraphEvol as $ratio => $info){
				$dataEvol[$ratio][] = array(
					'date'=>$bilansInfos[$nbrAnnees-$i]['dateCloture'],
					'value'=>(($ratiosEntrep[$nbrAnnees-$i][$ratio]!='NS') ? $ratiosEntrep[$nbrAnnees-$i][$ratio]/$info['op'] : 0 )
				);
			}
		}
		$i++;
	}
	return array("graph" => $dataGraph, "evol" => $dataEvol);
}

$data = synthese_datagraph_perso($bilansInfos, $ratiosEntrep, $tabRatioGraph, $tabRatio);

$dataGraph = $data['graph'];
$dataEvol = $data['evol'];

//$firephp->log($dataEvol, 'evol');

?>
<script type="text/javascript" src="./js/jquery.qtip.js"></script>
<script type="text/javascript">
	$(document).ready(function()
	{
		$('#synthese a[tooltip]').each(function()
		{
			$(this).qtip({
				content: $(this).attr('tooltip'),
	         	style: { width: 500, name: 'dark' },
	         	position: { corner: { target: 'bottomMiddle', tooltip: 'topMiddle' } }
	      	});
	   	});

		$('.jTip').each(function()
		{
	      $(this).qtip(
	      {
	         content: {
	            text: 'Chargement...',
	            url: $(this).attr('rel'),
	            title: { text: 'Evolution - ' + $(this).attr('name')}
	         },
	         position: {
	            corner: {
	               target: 'leftMiddle',
	               tooltip: 'rightMiddle'
	            }
	         },
	         show: { solo: true},
	         style: {
	            tip: true,
	            border: { width: 1, radius: 0 },
	            name: 'light',
	            width: 400
	         }
	      })
		});
	});
</script>
<style>
#synthese { clear:both; font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif; font-size: 12px; margin: 10px 0 0 0; width:100%; text-align: left; border-collapse: collapse; }
#synthese th { padding: 4px; font-weight: normal; font-size: 13px; color: #039; background: #b9c9fe; border: 1px solid #fff; }
#synthese th.date { font-size: 11px; }
#synthese td { padding: 4px; background: #e8edff; border: 1px solid #fff; color: #669; }
#synthese td.right { text-align: right; }
#synthese tbody tr:hover td { background: #d0dafd; }
#entete { margin:1px; }
#synthese .head { font-weight:bold; }
#graphInfo { font-family: Arial, Helvetica, sans-serif; font-size:  11px; font-weight: bold; margin-left:10px; }
p, label{font-size: 12px;}
ul{list-style: none;}
ul li{padding-top:3px;}
.qtip-title {font-size:11px;}
.qtip-content {font-size:12px;}
#titreEvol {margin:0 130px;}
#imgEvol {margin:0 100px;}
</style>

<div id="center">
<h1>SYNTH&Egrave;SE</h1>

<table width="570" border="0" align="left" bgcolor="#FFFFFF">
<tr>
	<td width="30">&nbsp;</td>
	<td width="200" class="StyleInfoLib">Num&eacute;ro identifiant Siren</td>
	<td width="340" class="StyleInfoData"><?=substr($siren,0,3).' '.substr($siren,3,3).' '.substr($siren,6,3)?></td>
</tr>
<tr>
	<td width="30">&nbsp;</td>
	<td width="200" class="StyleInfoLib">Raison Sociale</td>
	<td width="340" class="StyleInfoData"><?=$_SESSION['tabInfo']['entrep']['raisonSociale'];?></td>
</tr>
</table>

<?php
if (count($ratiosEntrep)==0 || !isset($ratiosEntrep))
{
?>
<table>
<tr><td width="30">&nbsp;</td><td><b>Aucun bilan disponible pour cette entreprise !</b></td></tr>
</table>
<?php
}
else
{

$tab_dateCloture = array(Date_to_DateFr($bilansInfos[2]['dateCloture']), Date_to_DateFr($bilansInfos[1]['dateCloture']), Date_to_DateFr($bilansInfos[0]['dateCloture']));
?>

<p><a href="Javascript:;" onClick="vider('vide');">Vider le graphe</a></p>
<table id="synthese">
<thead>
<tr>
	<th align="center"><?php if($ratio!=''){?><a href="/?page=synthese&siret=<?=$_REQUEST['siret']?>&idEntreprise=<?=$_REQUEST['idEntreprise']?>" title="Retour à la page complète"><img src="./img/synthese/chart_bar.png" alt="Visionner le graphique"></a><?php } ?></th>
	<th class="date"><?=$tab_dateCloture[0]."<br />".$bilansInfos[2]['duree']." mois"; ?></th>
	<th class="date"><?=$tab_dateCloture[1]."<br />".$bilansInfos[1]['duree']." mois"; ?></th>
	<th class="date"><?=$tab_dateCloture[2]."<br />".$bilansInfos[0]['duree']." mois"; ?></th>
	<th class="date" align="center">Actions</th>
	<th class="date" align="center">Couleur</th>
	<th class="date" align="center">Style</th>
</tr>
</thead>
<tbody>
<?php
//print synthese_tablerow($ratiosInfos, $dataEvol, $tabRatio)
$ligne_style = array('Line', 'DashLine', 'DotLine', 'DotDashLine', 'AltDashLine', 'Line', 'DashLine', 'DotLine', 'DotDashLine');
$color = array('#ff0000', '#3333ff', '#000000', '#808080', '#333366', '#c3c3e6', '#8080ff', '#80ff80', '#ff8080');
$j = 0;
foreach($tabRatio as $ratio => $info) {

	$comment = empty($ratiosInfos[$ratio]['commentaires']) ? "" : " tooltip=\"".wrapComment($ratiosInfos[$ratio]['commentaires'])."\" ";
	$tab_chiffre = array(dRatio(2,$ratio), dRatio(1,$ratio), dRatio(0,$ratio));

	$chaine_date_cloture = $tab_dateCloture[0]."@".$tab_dateCloture[1]."@".$tab_dateCloture[2];
	$chaine_date_dure = $bilansInfos[0]['duree']."@".$bilansInfos[1]['duree']."@".$bilansInfos[2]['duree'];
	$chaine_chiffre = $tab_chiffre[0]."@".$tab_chiffre[1]."@".$tab_chiffre[2];

	// <span onClick=\"alert(document.getElementById('idColor$j').style.backgroundColor);\">Test</span>
	// <span onClick=\"alert(document.images['idStyle$j'].alt);\">Test2</span>


	echo "<tr>\n";
		echo "<td class=\"head\"><a $comment >".$info['titre']."</a></td>\n";
		echo "<td class=\"right\">".$tab_chiffre[0]."</td>\n";
		echo "<td class=\"right\">".$tab_chiffre[1]."</td>\n";
		echo "<td class=\"right\">".$tab_chiffre[2]."</td>\n";
		echo "<td  align=\"center\">

		 <a href=\"Javascript:;\" onClick=\"ajouter('ajoute'
			 								, '".$_REQUEST['siret']."'
			 								, '".$chaine_date_cloture."'
			 								, '".$chaine_date_dure."'
			 								, '".$chaine_chiffre."'
			 								, '".addslashes($info['titre'])."'
			 								, '".$info['evol']."'
			 								, document.getElementById('idColor$j').style.backgroundColor
			 								, document.images['idStyle$j'].alt
			 							  );\">Ajouter</a>

		 <a href=\"Javascript:;\" onClick=\"enlever('enleve', '".$info['evol']."');\">Enlever</a>
		 </td>\n";

		echo "<td  class=\"right\" >
		<div id=\"idColor".$j."\" style=\"background-color:".$color[$j].";\">&nbsp;</div>
		<form>
		<select name=\"color\" onchange=\"changeCouleur(this.options[this.selectedIndex].value, '".$j."');\">
			<option value=\"#000000\">Couleur</option>
			<option value=\"#ff0000\">Rouge</option>
			<option value=\"#3333ff\">Bleu</option>
			<option value=\"#000000\">Noire</option>
			<option value=\"#808080\">Gris</option>
			<option value=\"#333366\">Violet</option>
			<option value=\"#c3c3e6\">---</option>
			<option value=\"#8080ff\">Mauve</option>
			<option value=\"#80ff80\">Vert</option>
			<option value=\"#ff8080\">Orange</option>
		</select>
		</form>
		</td>\n";
		echo "<td align=\"center\">
		<form>
		<div><img style=\"margin-bottom:8px;\" name=\"idStyle".$j."\" src=\"img/ratios/".$ligne_style[$j].".png\" alt=\"".$ligne_style[$j]."\" width=\"90\" height=\"1\" /></div>

		<select name=\"style\" onchange=\"changeStyle(this.options[this.selectedIndex].value, '".$j."');\">
			<option value=\"".$ligne_style[$j]."\">Style</option>
			<option value=\"Line\">Line</option>
			<option value=\"DashLine\">DashLine</option>
			<option value=\"DotLine\">DotLine</option>
			<option value=\"DotDashLine\">DotDashLine</option>
			<option value=\"AltDashLine\">AltDashLine</option>
		</select>
		</form>
		</td>\n";

	echo "</tr>\n";
$j++;
}

?>
</tbody>
</table>
<br/>
<?php
print "<div id=\"img\">Bla bla bla bla bla bla bla bla bla bla bla bla.</div>";

//echo "<pre>";
//print_r($_SESSION['graphperso']);
unset($_SESSION['graphperso']);
//print_r($bilansInfos);
}
?>
</div>