<?php
/**
 * Controller pour les rapport
 * Prépares les données pour le Template .
 */
require_once (realpath(dirname(__FILE__)).'/Config.php');
require_once (realpath(dirname(__FILE__)).'/Client.php');
require_once (realpath(dirname(__FILE__)).'/modulesGiants/Functions.php');
require_once ('dbbootstrap.php');

/**
 * Mettre le setLog ici
 */

set_time_limit(100);

setDbConn('giantclient');
$OrderExists = Doctrine_Query::create()
			->select('InternalOrderId')
	        ->from('commande')
	        ->where('CompanyId = "'. $_GET['company'].'"')
	        ->andWhere('DataSetType ="'.$_GET['type'].'"')
	        ->fetchOne();
$InternalOrderId = $OrderExists->InternalOrderId;

$rapportExists = Doctrine_Query::create()
			->select('CompanyId, UrlObjectRapportFile, DateTimeSave')
	        ->from('rapport')
	        ->where('CompanyId = "'. $_GET['company'].'"')
	       	->andWhere('DataSetType = "'. $_GET['type'].'"')
	       	->fetchOne();

$dateTime = new DateTime();
$dateTimeBDD = new DateTime($rapportExists->DateTimeSave);
$daysOfRapport = $dateTime->diff($dateTimeBDD);

if (!file_exists($rapportExists->UrlObjectRapportFile))
{
	$soap_client = getClient($_GET['pays'], 'OrderDataSet');
	$OrderDataSet = new CreditDataPortType();
	
	$params = $OrderDataSet->OrderDataSet();
	$params->OrderDataSet->CompanyId 		= $_GET['company'];
	$params->OrderDataSet->DataSetType 		= new stdClass();
	$params->OrderDataSet->DataSetType->_ 	= $_GET['type'];
	$params->OrderDataSet->LanguageCode 	= $_GET['lang'];
	$params->OrderDataSet->DataSetVersion 	= "1.0";
	$informations 							= SwitchWSDLFunction('OrderDataSet', $params->OrderDataSet, $soap_client);
	
	$request = new commande();
	$request->Login = $_SESSION['tabInfo']['login'];
	$request->Ip = $_SESSION['tabInfo']['ip'];
	$request->ProviderOrderId = $informations->Order->ProviderOrderId;
	$request->InternalOrderId = $informations->Order->InternalOrderId;
	$request->ConsumerId = $informations->Order->ConsumerId;
	$request->ProviderId = $informations->Order->ProviderId;
	$request->CountryCode = $informations->Order->CountryCode;
	$request->CompanyId = $informations->Order->CompanyId;
	$request->DataSetType = $informations->Order->DataSetType->_;
	$request->OrderDateTime = $informations->Order->OrderDateTime;
	$request->save();
	$InternalOrderId = $informations->Order->InternalOrderId;

	$nameFileRapport = 'rapport-'.$_GET['company'].'-'.date('Ymd').'-'.$_GET['pays'].'-'.$_GET['type'];
	$soap_client2 = getClient($_GET['pays'], 'RetrieveDataSet');
	
	$RetrieveDataSet = new CreditDataPortType();
	$params = $RetrieveDataSet->RetrieveDataSet();
	
	$params->RetrieveDataSet->InternalOrderId = $InternalOrderId;
	$rapport = SwitchWSDLFunction('RetrieveDataSet', $params->RetrieveDataSet, $soap_client2);
	
	$directory = _ifDirectoryExist('/giant/'.$_GET['company'].'/');
	$saveRapport = new rapport();
	$saveRapport->CompanyId = $_GET['company'];
	$saveRapport->DataSetType = $_GET['type'];
	$saveRapport->UrlObjectRapportFile = $directory.$nameFileRapport;
	(file_put_contents(PATH_DATA.$directory.$nameFileRapport, serialize($rapport->DataSet))) ? 
		$saveRapport->Delivred = true : $saveRapport->Delivred = EXIT_FAILURE;
	$saveRapport->DateTimeSave = date('Y-m-d');
	$saveRapport->save();
	$rapport = $rapport->DataSet;
} else
	($rapport = unserialize(file_get_contents(PATH_DATA.$rapportExists->UrlObjectRapportFile))) ? true : EXIT_FAILURE;
?>