<?php
if (!$_SESSION['connected'])	die();

require_once 'cache/cache.php';
require_once 'partenaires/classMTva.php';
require_once 'partenaires/classMMap.php';
require_once 'partenaires/classMCoface.php';
require_once 'common/dates.php';
require_once 'scoresws/scoresws.php';
require_once 'user/user.php';

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

if (isset($_REQUEST['idan']) && $_REQUEST['idan']*1!=0)
	$idan = $_REQUEST['idan'];
else
	$idan = false;

if (($siret*1)==0 && $idan==0) die('Paramètres incorrects !');
$siren=substr($siret,0,9);

$idEntreprise = $_REQUEST['idEntreprise'];
isset($_REQUEST['vue'])? $vue = $_REQUEST['vue'] : $vue = '';
$action=$_REQUEST['action'];

$message='';
if ($action!='' && $action!='logon')	die('Paramètres incorrects !');

//Récupération des infos du webservice
$annonces = scoresws_annonces($siret, $siren, $idEntreprise, $vue, $idan);
$firephp->log($annonces, 'annonces');
if ($siren != $_SESSION['tabInfo']['entrep']['siren']) {
	$O2 = $client->getIdentite($siret, 0, false);
	$etab = $O2['result'];
	$tabEntrep = array();
	$tabEntrep['raisonSociale'] = $etab['Nom'];
	$tabEntrep['siren'] = $etab['Siren'];
	$tabEntrep['siret'] = $etab['Siret'];
	$tabEntrep['tva'] = $etab['TvaNumero'];
	$tabEntrep['isin'] = $etab['Isin'];
	$tabEntrep['AutreId'] = $etab['AutreId'];
	$tabEntrep['active'] = $etab['Statut'];
	$tabEntrep['Source'] = $etab['Source'];
	$tabEntrep['SourceId'] = $etab['SourceId'];
	$tabEntrep['codeCommune'] = $etab['Dept'].$etab['codeCommune'];
	$tabEntrep['nafEn'] = $etab['NafEnt'];
	$tabEntrep['nafEt'] = $etab['NafEtab'];
	$tabEntrep['fj'] = $etab['FJ'];
	$_SESSION['tabInfo']['entrep'] = $tabEntrep;
	$raisonSociale = $etab['Nom'];
} else
	$raisonSociale = $_SESSION['tabInfo']['entrep']['raisonSociale'];

?>
<div id="center">
<h1 class="titre">ANNONCES L&Eacute;GALES</h1>
<table>
<?php if ($message<>'') { ?>
<tr>
	<td width="30">&nbsp;</td>
	<td width="550" colspan="3" class="StyleInfoData">
		<h3><?=$message;?></h3>
	</td>
</tr>
<?php }?>
<tr>
	<td width="30">&nbsp;</td>
	<td width="200" colspan="2" class="StyleInfoLib">
	Num&eacute;ro identifiant Siren
	</td>
	<td width="350" 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" colspan="2" class="StyleInfoLib">
	Raison Sociale
	</td>
	<td width="350" class="StyleInfoData">
	<?=$raisonSociale?>
	<?php
	if(hasModeEdition())
	{
	?>
	<a href="./?page=saisie&siret=<?=$siret?>&idEntreprise=<?=$idEntreprise?>">
	(Edition)
	</a>
	<?php
	}
	?>
	</td>
</tr>
</table>
<?php
if ($idan && ($vue=='bodacc' || $vue=='abod' || $vue=='balo' || $vue=='asso'))
{
	if($vue=='asso' && $siren==0)
	{
		foreach($annonces as $ann){ if($ann['id']==$idann) break; }
	}else{
		$ann = $annonces[0];
	}

	/** AFFICHAGE D'UNE ANNONCE BODACC / BALO / ASSO **/

	// Entête BALO
	if ($vue=='balo')
	{
	?>
	<h2>BALO</h2>
	<table>
	<tr>
		<td width="30">&nbsp;</td>
		<td width="550" colspan="3" class="StyleInfoData">
		BALO n&deg;<?=$ann['BodaccNum']?>&nbsp;du <?=WDate::dateT('Y-m-d','d/m/Y',$ann['DateParution'])?>. Entr&eacute; n&deg;<?=$ann['NumAnnonce']?>. Entr&eacute;e en base S&amp;D : <?=WDate::dateT('Y-m-d','d/m/Y',$ann['dateInsertionSD'])?>.<br/>
		<a href="/?page=annonces&siret=<?=$siret?>&idan=<?=$_REQUEST['idan']?>&vue=balo&mode=pdf" target="_blank">Cliquez ici pour voir cette annonce au format PDF&nbsp;(<?=adapteOCtets(@filesize(PATH_DATA.'/jo/balo/'.WDate::dateT('Y-m-d','Y',$ann['DateParution']).'/'. $ann['Lien_Annonce_Pdf']))?>)</a>
		.</td>
	</tr>
	<tr>
		<td width="30">&nbsp;</td>
		<td width="550" colspan="3" class="StyleInfoLib"><b><?=$ann['Categorie']?></b></td>
	</tr>
	<tr>
	</table>
	<?php
		$ann['texteAnnonce']=utf8_decode($ann['texteAnnonce']);
	}
	elseif ($vue=='bodacc' || $vue=='abod')
	{
	?>
	<h2>&Eacute;V&Eacute;NEMENTS L&Eacute;GAUX</h2>
	<table>
	<tr>
		<td width="30">&nbsp;</td>
		<td width="550" colspan="3" class="StyleInfoData">
		<?php
		$tabSource=explode('-', $ann['BodaccCode']);
		$source=$tabSource[0];
		$idSource=@$tabSource[1];
		if ($source[0] == 'B'){
			if ($ann['BodaccNum']==0)
				echo '<img src="./img/logo_jo.png"/>&nbsp;&nbsp;Source BODACC '.substr($ann['BodaccCode'],-1).' du '.WDate::dateT('Y-m-d','d/m/Y',$ann['DateParution']);
			else
				echo '<img src="./img/logo_jo.png"/>&nbsp;&nbsp;&nbsp;Source BODACC n&deg;'.$ann['BodaccNum'].'&nbsp;'.substr($ann['BodaccCode'],-1).' du '.WDate::dateT('Y-m-d','d/m/Y',$ann['DateParution']).'. D&eacute;partement n&deg;'.$ann['Departement'].'. <a href="/?page=competences&siret='.$siret.'&type=tri">'.$ann['Tribunal'].'</a>';
		}elseif ($source[0] == 'G' || $source[0] == 'T'){
			echo '<img src="./img/logo_greffe.png"/>&nbsp;&nbsp;Source Greffe du Tribunal';
		}elseif ($source[0] == 'P'){
			echo '<img src="./img/logo_inpi.png"/>&nbsp;&nbsp;Source Registre National du Commerce';
		}else{
			echo '<img src="./img/logo_jal.png"/>';
			if( hasModeEdition() && substr($source,0,2)=='JT' ){
				echo ' <b>Source TESSI</b> -';
			}
			echo '&nbsp;&nbsp;Source Journal Annonces L&eacute;gales';
		}

		if ($ann['dateInsertionSD']<>'') echo ' - Entr&eacute;e en base : '. WDate::dateT('Y-m-d','d/m/Y',$ann['dateInsertionSD']);
		?>
		</td>
	</tr>
	</table>
	<?php
	}
	elseif ($vue=='asso')
	{
	?>
	<h2>Associations</h2>
	<table>
	<tr>
		<td width="30">&nbsp;</td>
		<td width="550" colspan="3" class="StyleInfoData">J.O. ASSOCIATION n&deg;<?=$ann['BodaccNum']?>&nbsp;du <?=WDate::dateT('Y-m-d','d/m/Y',$ann['DateParution'])?>. Annonce n&deg;<?=$ann['NumAnnonce']?>. Entr&eacute;e en base S&amp;D : <?=WDate::dateT('Y-m-d','d/m/Y',$ann['dateInsertionSD'])?>.</td>
	</tr>
	</table>
	<?php
	}
	?>
	<table>
	<tr>
		<td width="30">&nbsp;</td>
		<td width="550" colspan="3" class="StyleInfoLib">
		<b>
		<?php
		foreach (@$ann['evenements'] as $i=>$even) { echo @$even['LibEven'].'<br/>'; }
		?>
		</b>
		</td>
	</tr>
	<tr>
		<td colspan="4">&nbsp;</td>
	</tr>
	<tr>
		<td width="30">&nbsp;</td>
		<?php
		/**Affichage du texte de l'annonce**/
		$texte=preg_replace_callback("/((?:[0-9]{9,9})|(?:[0-9]{3,3} [0-9]{3,3} [0-9]{3,3})|(?:[0-9]{3,3}\.[0-9]{3,3}\.[0-9]{3,3})|(?:[0-9]{3,3}-[0-9]{3,3}-[0-9]{3,3}))/", replace_siren, $ann['texteAnnonce']);
		?>
		<td width="550" colspan="3"><code><?=$texte?></code></td>
	</tr>
	<tr><td colspan="4">&nbsp;</td></tr>
	<tr>
	<td colspan="4" align="center" class="StyleInfoData"><a href="Javascript:history.back()">Revenir à la liste des annonces</a></td>
	</tr>
	</table>
<?php
}
elseif (count($annonces)>0)
{
	//Tri des annonces dans des tableaux différents
	$annoncesBodacc = array();
	$annoncesBalo = array();
	$annoncesAsso = array();
	foreach ($annonces as $i => $ann) {
		if ($ann['BodaccCode']=='BODA' || $ann['BodaccCode']=='BODB' || $ann['BodaccCode']=='BODC')	{
			$annoncesBodacc[] = $ann;
		}elseif ($ann['BodaccCode']=='BALO')	{
			$annoncesBalo[] = $ann;
		}elseif ($ann['BodaccCode']=='ASSO')	{
			$annoncesAsso[] = $ann;
		}else {
			$annoncesBodacc[] = $ann;
		}
	}
	/** Affichage de la liste des annonces **/
	//Affiche annonces Bodacc
	if (count($annoncesBodacc)>0)
	{
		?>
		<h2>EV&Eacute;NEMENTS L&Eacute;GAUX</h2>
		<table>
		<?php
		foreach($annoncesBodacc as $i => $ann)
		{
		?>
		<tr>
			<td width="30">&nbsp;</td>
			<td width="140" class="StyleInfoLib">
			<div id="annoncesDate"><p>Le <?=WDate::dateT('Y-m-d','d/m/Y',$ann['DateParution']);?></p></div>
			<div id="annoncesImg">
			<?php
			$tabSource=explode('-', $ann['BodaccCode']);
			$source=$tabSource[0];
			$idSource=@$tabSource[1];
			if ($source[0] == 'B'){
				echo '<img src="./img/logo_jo.png" title="Source BODACC '.
					$ann['BodaccNum'].' '.
					substr($source,-1).' '.substr($ann['DateParution'],0,4).
					'"/>';
			}elseif ($source[0] == 'G' || $source[0] == 'T'){
				echo '<img src="./img/logo_greffe.png" title="Source Collecte Greffe"/>';
			}elseif ($source[0] == 'P'){
				echo '<img src="./img/logo_inpi.png" title="Source Collecte RNCS"/>';
			}else{
				echo '<img src="./img/logo_jal.png" title="Source Collecte JAL';
				if ($idSource>0) echo ' $idSource';
				echo '"/>';
			}
			?>
			</div>
			</td>
			<td width="450" colspan="2" class="StyleInfoData">
			<a href="/?page=annonces&siret=<?=$siret?>&idan=<?=$ann['id']?>&vue=bodacc">
			<?php
			foreach ($ann['evenements'] as $i=>$even) {
				echo $even['LibEven'];
				if( hasModeEdition() && substr($source,0,2)=='JT' ){
					echo ' <b>(Source TESSI)</b>';
				}
				$numEven=$even['CodeEven']*1;
				if ($numEven>3000 && $numEven<3999)
					echo ' (clôture au '.WDate::dateT('Y-m-d','d/m/Y',$ann['dateEffet']).')';
				echo '<br/>';
			}
			?>
			</a>
			</td>
		</tr>
		<?php
		}
		?>
		</table>
		<?php
	}

	//Affiche annonces Balo
	if (count($annoncesBalo)>0)
	{
		?>
		<h2>BALO</h2>
		<table>
		<?php
		foreach($annoncesBalo as $i => $ann){?>
		  <tr>
			<td width="30">&nbsp;</td>
			<td width="140" class="StyleInfoLib">
			<span id="annoncesDate">Le <?=WDate::dateT('Y-m-d','d/m/Y',$ann['DateParution']);?></span>
			<span id="annoncesImg">
			<?php
			if ($ann['BodaccCode'] == 'BALO'){
				echo '<img src="./img/logo_jo.png" title="Source BALO n°'.$ann['BodaccNum'].'"/>';
			}
			?>
			</span>
			</td>
			<td width="450" colspan="2" class="StyleInfoData">
			<a href="/?page=annonces&siret=<?=$siret?>&idan=<?=$ann['id']?>&vue=balo">
			<?php
			foreach ($ann['evenements'] as $i=>$even) {
				print $even['LibEven'];
				$numEven=$even['CodeEven']*1;
				if ($numEven>3000 && $numEven<3999)	print ' (clôture au '.WDate::dateT('Y-m-d','d/m/Y',$ann['dateEffet']).')';
				print '<br/>';
			}
			?>
			</a></td>
		  </tr>
		<?php
		}
		?>
		</table>
		<?php
	}

	//Affiche annonces Asso
	if (count($annoncesAsso)>0)
	{
		?>
		<h2>Associations</h2>
		<table>
		<?php
		foreach($annoncesAsso as $i => $ann)
		{
		?>
		  <tr>
			<td width="30">&nbsp;</td>
			<td width="140" class="StyleInfoLib">
			<span id="annoncesDate">Le <?=WDate::dateT('Y-m-d','d/m/Y',$ann['DateParution']);?></span>
			<span id="annoncesImg">
			<?php
			if ($ann['BodaccCode'] == 'ASSO'){
				print '<img src="./img/logo_jo.png" title="Source JO ASSOCIATION n°'.$ann['BodaccNum'].'"/>';
			}
			?>
			</span>
			</td>
			<td width="450" colspan="2" class="StyleInfoData">
			<a href="/?page=annonces&siret=<?=$siret?>&idEntreprise=<?=$idEntreprise?>&idan=<?=$ann['id']?>&vue=asso">
			<?php
			foreach ($ann['evenements'] as $i=>$even) {
				print $even['LibEven'];
				$numEven=$even['CodeEven']*1;
				if ($numEven>3000 && $numEven<3999)	print ' (clôture au '.WDate::dateT('Y-m-d','d/m/Y',$ann['dateEffet']).')';
				print '<br/>';
			}
			?>
			</a></td>
		  </tr>
		<?php
		}
		?>
		</table>
		<?php
	}
}
else
{
/** Aucune annonce bodacc pour cette entreprise **/
?>
<h2>Evénements légaux</h2>
<table>
  <tr>
	<td width="30">&nbsp;</td>
	<td width="100" class="StyleInfoLib">&nbsp;</td>
	<td width="450" colspan="2" class="StyleInfoData">N&eacute;ant</td>
  </tr>
  <tr>
	<td colspan="4">&nbsp;</td>
  </tr>
</table>
<?php
}


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

require_once 'export.php';
$parseTab = new tabExport();
$parseTab->tab = $annonces;
$parseTab->notdisplay = array('id');
$parseTab->title = array(
						'BodaccCode'=>'Code Bodacc',
						'BodaccNum'=>'Numéro Bodacc',
						'NumAnnonce'=>'Numéro annonce',
						'DateParution'=>'Date de parution',
						'TribunalSiret'=>'Siret du tribunal',
						'typeAnnonce'=>'Type annonce',
						'texteRectificatif'=>'Texte rectificatif',
						'texteAnnonce'=>'Texte annonce',
						'dateEffet'=>'Date effet',
						'dateJugement'=>'Date jugement',
						'dateInsertionSD'=>'Date insertion',
						'evenements'=>'Evenements',
					);
$parseTab->type = array();
$tabForExport = $parseTab->convertTable();

$array2csv = new ExportCSV();
$array2csv->records = $tabForExport;
$array2csv->writeCSV($fileName);

$tabForExport = $annonces;
$array2xml = new ExportXML();
$array2xml->rootName = $page.'s';
$array2xml->defaultTagName = $page;
$array2xml->records = $tabForExport;
$array2xml->writeXML($fileName);

if (!$idan)
{
?>
<table>
  <tr>
	<td width="30">&nbsp;</td>
	<td colspan="3" width="550" class="StyleInfoData">
	<a class="dialogsurv" href=""></a>
	<?php
	require_once 'surveillance/surveillance.php';
	echo getSurveillance('annonces', $siret);
	?>
	</td>
  </tr>
</table>
<?php
}
?>
</div>