<?php
class JuridiqueController extends Zend_Controller_Action
{
	protected $siret = null;
	protected $id = 0;
	
	public function init()
	{
		$request = $this->getRequest();
		$this->siret = $request->getParam('siret');
		$this->id = $request->getParam('id', 0);
				
		require_once 'Scores/WsScores.php';
		require_once 'Scores/Utilisateur.php';
		require_once 'Scores/SessionEntreprise.php';
		require_once 'common/dates.php';
		
		$this->view->headLink()->appendStylesheet('/themes/default/styles/juridique.css', 'all');
    }
	
	public function annoncesAction()
	{
		$request = $this->getRequest();
		$source = $request->getParam('source');
		$vue = $request->getParam('vue');
		$idAnn = $request->getParam('idAnn', '');
		$siren = substr($this->siret, 0,9);
		
		$filtre = 0;
		if (!empty($vue)) {
			switch ($vue){
				case 'bodacc': 	$filtre=1; break;
				case 'balo': 	$filtre=2; break;
				case 'asso':	$filtre=3; break;
			}
		}
		if (!empty($source)) {
			switch ($source){
				case 1: 	$filtre=1; break;
				case 2: 	$filtre=2; break;
				case 3:		$filtre=3; break;
			}
		}
		$session = new SessionEntreprise($this->siret);
		$fj = $session->getFormeJuridique();
		if ($fj>9000 && $fj<9999 && intval($siren)==0) {
			$filtre=3;
		}
		
		Zend_Registry::get('firebug')->info('Filtre : '.$filtre);
		$ws = new WsScores();
		$infos = $ws->getAnnonces($siren, $filtre, $idAnn);

		require_once 'Scores/Annonces.php';
		$objAnnonces = new Annonces($infos->result->item);
		
		$typeAnnonces = array(
			'Bodacc',
			'Balo',
			'Bomp',
			'Asso',
		);
		
		$this->view->assign('siren', $siren);
		$this->view->assign('siret', $this->siret);
		$this->view->assign('raisonSociale', $session->getRaisonSociale());
		$this->view->assign('AutrePage', $request->getParam('page'));
		
		if (!empty($idAnn)) {
			$type = ucfirst($vue);
			$classType = 'annonces'.$type;
			foreach($objAnnonces->$classType as $ann) {
				if($ann->id==$idAnn) break;
			}
			
			$annonce = array(
				'Desc'		=> $objAnnonces->getAnnonceDesc($ann),
				'Entree'	=> $objAnnonces->getAnnonceEntree($ann),
				'EntreeSD'	=> $objAnnonces->getAnnonceEntreeSD($ann),
				'Even'		=> $objAnnonces->getAnnonceEven($ann),
				'Texte'		=> $objAnnonces->getAnnonceTexte($ann),
				'Type'		=> $objAnnonces->getType($ann),
			);
			Zend_Registry::get('firebug')->info($infos->result->item);
			Zend_Registry::get('firebug')->info($annonce);
			$this->view->assign('annonce', $annonce);
			$this->renderScript('juridique/annonce.phtml');
		} else {
			foreach ($typeAnnonces as $type)
			{
				$classType = 'annonces'.$type;
				$annonces = array();
				if (count($objAnnonces->$classType)>0)
				{
					foreach($objAnnonces->$classType as $ann)
	        		{
	        			$annonces[] = $objAnnonces->getAnnonceResume($ann);
	        		}
	        		$this->view->assign($classType, $annonces);
	        		Zend_Registry::get('firebug')->info($annonces);
				}
			}
		}
	}
		
	public function infosregAction()
	{
		$request = $this->getRequest();
		$idCom = $request->getParam('idCom', false);
		$siren = substr($this->siret, 0,9);
		$session = new SessionEntreprise($this->siret);
	
		$ws = new WsScores();
		$infos = $ws->getInfosReg($siren);
		Zend_Registry::get('firebug')->info($infos);
		$objAnnonces = $infos->result->item;
		$annonces = array();
		if (count($objAnnonces)>0) {
			foreach ( $objAnnonces as $item ) {
				//$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, $item->communique));
				$annonces[] = array(
					'id' 				=> $item->id,
					'titre' 			=> $item->titre,
					'communique' 		=> $item->communique,
					'source' 			=> $item->source,
					'DateParution' 		=> WDate::dateT('Y-m-d','d/m/Y',$item->DateParution),
					'dateInsertionSD'	=> WDate::dateT('Y-m-d','d/m/Y',$item->dateInsertionSD),
				);
			}
		}
		if ($idCom!=false) {
			$this->view->assign('liste', false);
			$this->view->assign('idCom', $idCom);
		} else {
			$this->view->assign('liste', true);
		}
		$this->view->assign('annonces', $annonces);
		$this->view->assign('siren', $siren);
		$this->view->assign('siret', $this->siret);
		$this->view->assign('raisonSociale', $session->getRaisonSociale());
	
	}
	
	public function competencesAction()
	{
		$request = $this->getRequest();
		$type = $request->getParam('type', '');
		$siren = substr($this->siret,0,9);
		$session = new SessionEntreprise($this->siret, $this->id);
		
		$abbrev = array(
			' TGI '		=> 'TRIBUNAL DE GRANDE INSTANCE DE ',
			' TI '		=> 'TRIBUNAL D\'INSTANCE DE ',
			' TC '		=> 'TRIBUNAL DE COMMERCE DE ',
			' TGICC '	=> 'TRIBUNAL DE GRANDE INSTANCE A COMPETENCE COMMERCIALE ',
			' TICC '	=> 'TRIBUNAL D\'INSTANCE A COMPETENCE COMMERCIALE '
		);
		
		if (!empty($type)){
			$ws = new WsScores();
			$infos = $ws->getListeCompetences($siret, $type, $session->getCodeCommune());
			$competences = $infos->result->item;
			Zend_Registry::get('firebug')->info($infos);
			if( $type=='tri' || $type=='cfe' ) {
				$i=0;
				foreach($competences as $comp){
					$competences[$i]->Nom = strtr(' '.strtoupper($comp->Nom), $abbrev);
					$i++;
				}
			}
			$this->view->assign('competences', $competences);
			$this->view->assign('type', $type);
		}
		
		$this->view->assign('siren', $siren);
		$this->view->assign('siret', $this->siret);
		$this->view->assign('raisonSociale', $session->getRaisonSociale());
	}
	
	public function conventionsAction()
	{
		$siren = substr($this->siret, 0,9);
		$session = new SessionEntreprise($this->siret);
		$this->view->assign('siren', $siren);
		$this->view->assign('siret', $this->siret);
		$this->view->assign('raisonSociale', $session->getRaisonSociale());
		$ws = new WsScores();
		$infos = $ws->getListeConventions($siren);
		$conventions = $infos->result->item;
		Zend_Registry::get('firebug')->info($conventions);
		$this->view->assign('conventions', $conventions);
	}
	
	public function marquesAction()
	{
		$this->view->headScript()->appendFile('/themes/default/scripts/marques.js', 'text/javascript');
		
		$request = $this->getRequest();
		$idObject = $request->getParam('idObject', 0);
		$siren = substr($this->siret, 0,9);
		$session = new SessionEntreprise($this->siret);
		$this->view->assign('siren', $siren);
		$this->view->assign('siret', $this->siret);
		$this->view->assign('raisonSociale', $session->getRaisonSociale());
		$ws = new WsScores();
		$infos = $ws->getMarques($siren, $idObject);
		$marques = $infos->result->item;
		$this->view->assign('marques', $marques);
		$this->view->assign('idObject', $idObject);
		Zend_Registry::get('firebug')->info($infos);
	}
	
	protected function getMarques($idObject, $directory)
	{
		require_once 'common/curl.php';
		$url = 'http://bases-marques.inpi.fr/Typo3_INPI_Marques/getPdf?idObjet='.$idObject.'_FMARK-1,FMARK-2';
		$page = getUrl($url, '', '', '', false, '', '', 7);
		$body = $page['body'];
		//On test si il y a eu une erreur
		if($page['code']==200){
			$fp=@fopen($directory.'/'.$idObject.'.pdf', 'w');
			@fwrite($fp, $body);
			@fclose($fp);
		}
	}
	
	public function getmarqueAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$request = $this->getRequest();
		$numdepot = $request->getParam('numdepot', '');
		if (empty($numdepot)){
			echo 'Paramètres incorrects';
			exit;
		}
		$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/configuration.ini', 'path');
		$directory = realpath($config->data).'/'.$config->marques;
		$file = $numdepot.'.pdf';

		//Le fichier n'existe pas alors on le télécharger
		if(!file_exists($directory.'/'.$file)
			|| filesize($directory.'/'.$file)==0
			|| date('Ymd', filemtime($directory.'/'.$file))!=date('Ymd') ) {
			$this->getMarques($numdepot, $directory);
		}
		usleep(500);
		//On vérfie que le fichier existe après le téléchargement
		if(file_exists($directory.'/'.$file) && filesize($directory.'/'.$file)>0) {
			$href = $this->view->baseUrl().'/fichier/marque/'.$file;
			echo '<a href="'.$href.'" target="_blank">Télécharger le PDF de l\'insciption au BOPI.</a>';
		} else {
			echo 'Document introuvable.';
		}

	}
	
}