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

$tabInfo = $_SESSION['tabInfo'];

$identifier = $_REQUEST['identifier'];
$CompanyProductIdentifier = $_REQUEST['pidentifier'];
isset($_REQUEST['format']) ? $format = $_REQUEST['format'] : $format = 'xml';

$firephp->log($identifier, 'identifier');
$firephp->log($CompanyProductIdentifier, 'CompanyProductIdentifier');

$pays = $_SESSION['graydon']['pays'];
$mode = $_SESSION['graydon']['mode'];

require_once 'i18n/cleanchar.php';
require_once 'graydon/graydon.php';
require_once 'dbbootstrap.php';

function dVal($value){
	if($value == '' || $value == NULL){
		print 'NC';
	}else{
		print $value;
	}
}
?>
<div id="center">
<?php

$firephp->info('Fomat HTML');
	
$path = PATH_DATA.'/graydon';
$fileName = strtolower(str_replace( array(':',' '), '-', $identifier).'.'.$format);
	
//Vérifier si le rapport n'est pas disponible en base depuis une autre commandes
setDbConn('graydon');
$q = Doctrine_Query::create()
	->from('Report')
	->where('identifier = ?', $_REQUEST['identifier'])
	->andWhere('format = ?', $format);
	
$firephp->log($q->getSql(), 'Requete SQL');
$firephp->log($q->getParams(), 'Params SQL');
	    
$report = new Report();
$report = $q->fetchOne();

if($format == 'html')
{
	
	
	//@TODO : si le rapport existe déjà ou que la date n'est pas très éloigné
	if($report==FALSE)
	{
		$firephp->info('Pas en cache');
		//Vérifier si le rapport en ligne à déjà été demandé
		/*
		$q = Doctrine_Query::create()
		    ->from('Commandes')
		    ->where('companyproductidentifier = ?', $CompanyProductIdentifier);
		    
		$commandes = new Commandes();
		$commandes = $q->fetchOne();
		
		$report = new Report();
		$report = $commandes->Report;
		
		if($commandes == FALSE && !isset($report->id)){
			$html = getIdentite($identifier, $CompanyProductIdentifier, $path.'/'.$fileName, $format);
		//La commande à déjà été demandé
		}else{
			$html = $report->content;
		}
		*/
		$html = getIdentite($identifier, $CompanyProductIdentifier, $path.'/'.$fileName, $format);
	//Le rapport est disponible
	}
	else
	{
		$country = Doctrine_Query::create()
					->from('Country')
					->where('graydon_country = ?', $pays)
					->fetchOne();
					
		//Enregistrement d'une fausse commande dans la bdd
		$commandes = new Commandes();
		$commandes->reference = $placeOrderResult->OrderStatus->OrderReference;
		$commandes->servicespeed = $placeOrderResult->OrderStatus->Order->ServiceSpeed;
		$commandes->prix = 0;
		$commandes->orderstate = $placeOrderResult->OrderStatus->OrderState;
		$commandes->dateordered = $placeOrderResult->OrderStatus->DateOrdered;
		$commandes->datecompleted = $placeOrderResult->OrderStatus->DateCompleted;
		$commandes->datedue = $placeOrderResult->OrderStatus->DateDue;
		$commandes->companyproductidentifier = $CompanyProductIdentifier;
		$commandes->sessionid = $sessionID;
		$commandes->transactionidentifier = $transactionIdentifier;
		$commandes->user = $tabInfo['login'];
		$commandes->mode = $_SESSION['graydon']['mode'];
		$commandes->Country = $country;
		$commandes->save();
		$firephp->log($commandes->toArray(),'dataCommandes');
		$commandes_id = $commandes->id;
	
		//Assignation du contenu
		$html = $report->content;
	}
	
	if(isset($html) && $html!='' && $html!==FALSE)
	{
		$html = str_replace('<html><head><META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">', '', $html);
		$html = str_replace('<body>', '', $html);
		$html = str_replace('</body></html>', '', $html);
		$html = preg_replace('@<style[^>]*?>.*?</style>@si', '', $html);
		$html = preg_replace('@<h1[^>]*?>Graydon .*?</h1>@si', '', $html);
	?>
	<style type="text/css">
	h1{font-family : Verdana, Arial, Helvetica, sans-serif;font-size: 14px;font-weight : bold;color : white;padding: 3px;margin: 10px 0px 6px 0px;border-width : 1px;border-style : solid;border-color : #31659C;background-color : #31659C; text-align: left;}
	h1.header{font-size: 18px;font-weight : 900;border-width : 0px;text-align: center;color : #31659C;background-color : white;}
	h1.trailer{border-width : 0px;text-align: center;color : #31659C;background-color : white;}
	img{border-style : none;align: left;vertical-align: middle;}
	img.link{height: 11px;width: 13px}
	p{font-size: 13px;font-family: Verdana, Arial, Helvetica, sans-serif;margin : 0px;padding-left : 3px;padding-right : 3px;padding-top : 3px;padding-bottom : 3px;text-align: left;}
	p.smallSpace{font-size: 1px;}
	table{}
	table.border{border-style : solid;border-width : 1px;border-color: #31659C;}
	table.header{margin: 10px 0px 6px 0px;border-width : 1px;border-style : solid;border-color : #31659C;text-align: left; width:100%;}
	table.small{width: 300px;}
	tr{page-break-inside: avoid;}
	th{font-family : Verdana, Arial, Helvetica, sans-serif;font-weight: bold;font-size: 13px;padding-left: 3px;width: 177px;color: #31659C;background-color : #e7eff8;text-align: left;vertical-align: top;}
	th.h1{font-size: 14px;padding: 3px;font-weight : bold;background-color : #31659C;color : white;}
	th.minusBackgroundColor{background-color : white;}
	th.label{font-weight: normal;background-color : white;width: auto;}
	th.indent{padding-left: 6px;}
	th.indentDouble{padding-left: 12px;}
	td{font-size: 13px;font-family: Verdana, Arial, Helvetica, sans-serif;color: black;padding-left: 3px;vertical-align: top;text-align: left;}
	a{font-family: Verdana, Arial, Helvetica, sans-serif;text-decoration : underline;color : blue;vertical-align: middle;}
	a:HOVER{font-family: Verdana, Arial, Helvetica, sans-serif;color : gold;vertical-align: middle;}
	input{font-size 		: 13px;font-family 	: Verdana,Arial;font-weight 	: lighter;border-width 	: 1px 1px 1px 1px;border-top-style : solid;border-left-style: solid;border-right-style : solid;border-bottom-style : solid;}
	select{font-size : 7pt;font-family : "Small Fonts",Arial;border-top-width : 1px;border-left-width : 1px;border-right-width : 1px;border-bottom-width : 1px;border-top-style : solid;border-left-style : solid;border-right-style : solid;border-bottom-style : solid;border-color : black black black black;font-weight : lighter;background-color : #f1f1f1;}
	.submit{font-size : 14px;font-family : Verdana,Arial;font-weight : normal;color : #f1f1f1;background-color : black;}
	.bold{font-weight: bold;}
	.red{color: red;}
	.white{background-color: white;}
	.lightGrey{background-color: lightGrey;}.italic{font-style: italic;}.dashed{border-bottom-style : dashed;border-bottom-width : 1px;border-bottom-color: silver;padding-left: 3px;padding-right: 3px;margin-top: 3px;margin-bottom: 3px;}
	.marginTop{margin-top: 6px;}
	.paddingTop{padding-top: 3px;}
	.paddingBottom{padding-bottom: 6px;}
	.textAlignRight{text-align: right;padding-right: 3px;}
	.signature_color1{color: #31659C;}
	.border_right{border-right-style : solid;border-right-width : 1px;border-right-color: #31659C;}
	.border_left{border-right-style : solid;border-right-width : 1px;border-right-color: #31659C;}
	.border_bottom{border-bottom-style : solid;border-bottom-width : 1px;border-bottom-color: #31659C;}
	.center{text-align: center;width: auto;}
	.paymentBox {font-size: large;width: 100;height: 100;border: 1px solid #191970;text-align: center;padding-top: 35;margin-left: 70;}ul{list-style: none;padding: 0;margin: 0}li{padding-left: 10px;margin-right: 0px;background-image: url("/Creditline_core/images/tab-blue-left.gif");background-position: left top;background-repeat: no-repeat;background-color : #31659C;color: white;float: left;}li span{padding-right: 10px;padding-bottom: 3px;background-image: url("/Creditline_core/images/tab-blue-right.gif");background-position: right top;background-repeat: no-repeat;background-color : #31659C;float: left;}li a{color: white;}li a:hover{color: gold;}li a:hover span{color: gold;}</style><style media="print" type="text/css">/* HTML Elements */body{scrollbar-face-color		: #31659C;scrollbar-track-color		: #F1F1F1;scrollbar-base-color		: #F1F1F1;scrollbar-arrow-color		: #e7eff8;scrollbar-shadow-color		: #31659C;scrollbar-highlight-color	: #31659C;scrollbar-3dlight-color		: #e7eff8;scrollbar-darkshadow-color	: #e7eff8;background: #FFFFFF;font-size: 13px;font-family: Verdana, Arial, Helvetica, sans-serif;color: black;text-align: center;}h1{font-family : Verdana, Arial, Helvetica, sans-serif;font-size: 14px;font-weight : bold;color : white;padding: 3px;margin: 10px 0px 6px 0px;border-width : 1px;border-style : solid;border-color : #31659C;background-color : #31659C;text-align: left;}
	</style>
	<?php 	
		print $html;
	}
	else
	{
	?>
	<table>
	<tr>
		<td width="30">&nbsp;</td>
		<td width="500" class="StyleInfoLib">Aucune information disponible.</td>
	</tr>
	</table>
	<?php
	}
	?>
	</div>
	
<?php	
}
elseif($format == 'xml')
{
	$path = PATH_DATA.'/graydon';
	$fileName = strtolower(str_replace( array(':',' '), '-', $identifier).'.xml');
	
	//Vérifier si le rapport n'est pas disponible en base depuis une autre commandes
	setDbConn('graydon');
	$q = Doctrine_Query::create()
	    ->from('Report')
	    ->where('identifier = ?', $_REQUEST['identifier'])
	    ->andWhere('format = ?', $format);
	$report = new Report();
	$report = $q->fetchOne();
	
	//@TODO : si le rapport existe déjà ou que la date n'est pas très éloigné
	if($report==FALSE)
	{
		$firephp->info('Pas en cache');
		//Vérifier si le rapport en ligne à déjà été demandé
		$q = Doctrine_Query::create()
		    ->from('Commandes')
		    ->where('companyproductidentifier = ?', $CompanyProductIdentifier);
		$commandes = new Commandes();
		$commandes = $q->fetchOne();
		$report = new Report();
		$report = $commandes->Report;
		if($commandes == FALSE && !isset($report->id)){
			$xml = getIdentite($identifier, $CompanyProductIdentifier, $path.'/'.$fileName);
		//La commande à déjà été demandé
		}else{
			$xml = $report->content;
		}
	//Le rapport est disponible
	}
	else
	{
		$country = Doctrine_Query::create()
					->from('Country')
					->where('graydon_country = ?', $pays)
					->fetchOne();
					
		//Enregistrement d'une fausse commande dans la bdd
		$commandes = new Commandes();
		$commandes->reference = $placeOrderResult->OrderStatus->OrderReference;
		$commandes->servicespeed = $placeOrderResult->OrderStatus->Order->ServiceSpeed;
		$commandes->prix = 0;
		$commandes->orderstate = $placeOrderResult->OrderStatus->OrderState;
		$commandes->dateordered = $placeOrderResult->OrderStatus->DateOrdered;
		$commandes->datecompleted = $placeOrderResult->OrderStatus->DateCompleted;
		$commandes->datedue = $placeOrderResult->OrderStatus->DateDue;
		$commandes->companyproductidentifier = $CompanyProductIdentifier;
		$commandes->sessionid = $sessionID;
		$commandes->transactionidentifier = $transactionIdentifier;
		$commandes->user = $tabInfo['login'];
		$commandes->mode = $_SESSION['graydon']['mode'];
		$commandes->Country = $country;
		$commandes->save();
		$firephp->log($commandes->toArray(),'dataCommandes');
		$commandes_id = $commandes->id;
	
		//Assignation du contenu
		$xml = $report->content;
	}
	
	//========= Affichage des informations =========
	if(isset($xml) && $xml!='' && $xml!==FALSE)
	{
	
	$tabReport = array();
	
	//Création du DOM
	$doc = new DOMDocument;
	$doc->preserveWhiteSpace = false;
	$doc->loadxml($xml);
	$xpath = new DOMXPath($doc);
	$tabReport = parseReportXML($xml);
	$firephp->log($tabReport, 'tabReport');
	function utf8decode_deep($value)
	{
		$value = is_array($value) ?
					array_map('utf8decode_deep', $value) :
					cleanutf8($value);
		return $value;
	}
	
	?>
	
	<style type="text/css">
	.bilans { margin-left:35px; }
	div.bilans { margin-top:15px; font:bold 16px Verdana, Arial, Sans-serif; }
	span.bilans { font-style:italic; }
	table.bilans { width:545px; }
	table.bilans .right {text-align:right;}
	</style>
	
	<h1 class="titre">IDENTIT&Eacute; INTERNATIONALE DE L'ENTREPRISE</h1>
	<?php dIdentifiant($tabReport); ?>
	<h2>Raison sociale et coordonnées : </h2>
	<?php dIdentite($tabReport);?>
	<h2>Activité(s) et chiffre d'affaire de l'entreprise : </h2>
	<?php dActivite($tabReport); ?>
	<h2> Résumé financier : </h2>
	<?php dResume($tabReport); ?>
	<h2> Credit : </h2>
	<?php dCredit($tabReport); ?>
	<h2> Actionariat / Participation : </h2>
	<?php dLiens($tabReport); ?>
	<h2> Bilans : </h2>
	<?php dBilans($tabReport); ?>
	
	<?php
	}else{
	?>
	<table>
	<tr>
		<td width="30">&nbsp;</td>
		<td width="500" class="StyleInfoLib">Aucune information disponible.</td>
	</tr>
	</table>
	<?php
	}
	?>
	</div>
<?php 
}
?>