<?
global $tabOrt2Sd;
$tabOrt2Sd=array(	'_130000001'=>'SIREN',
					'_103000040'=>'CONSOLIDE',
					'_103000033'=>'DATE_CLOTURE',
					'_103000023'=>'DUREE_MOIS',
					'_103000202'=>'MONNAIE_ORI',
					'_103000205'=>'MONNAIE_ORI_UNITE',
					'_103000211'=>'MONNAIE',
					'_103000214'=>'MONNAIE_LIV_UNITE',
					'_103000029'=>'AVIS_CAC',
					'_130000009'=>'CODE_NAF',
					'_103101001'=>'AA',
					'_103101002'=>'AB',
					'_103101003'=>'AC',
					'_103101004'=>'AC1',
					'_103101005'=>'AD',
					'_103101006'=>'AE',
					'_103101007'=>'AE1',
					'_103101008'=>'AF',
					'_103101009'=>'AG',
					'_103101010'=>'AG1',
					'_103101011'=>'AH',
					'_103101012'=>'AI',
					'_103101013'=>'AI1',
					'_103101014'=>'AJ',
					'_103101015'=>'AK',
					'_103101016'=>'AK1',
					'_103101017'=>'AL',
					'_103101018'=>'AM',
					'_103101019'=>'AM1',
					'_103101020'=>'AN',
					'_103101021'=>'AO',
					'_103101022'=>'AO1',
					'_103101023'=>'AP',
					'_103101024'=>'AQ',
					'_103101025'=>'AQ1',
					'_103101026'=>'AR',
					'_103101027'=>'AS',
					'_103101028'=>'AS1',
					'_103101029'=>'AT',
					'_103101030'=>'AU',
					'_103101031'=>'AU1',
					'_103101032'=>'AV',
					'_103101033'=>'AW',
					'_103101034'=>'AW1',
					'_103101035'=>'AX',
					'_103101036'=>'AY',
					'_103101037'=>'AY1',
					'_103101038'=>'CS',
					'_103101039'=>'CT',
					'_103101040'=>'CT1',
					'_103101041'=>'CU',
					'_103101042'=>'CV',
					'_103101043'=>'CV1',
					'_103101044'=>'BB',
					'_103101045'=>'BC',
					'_103101046'=>'BC1',
					'_103101047'=>'BD',
					'_103101048'=>'BE',
					'_103101049'=>'BE1',
					'_103101050'=>'BF',
					'_103101051'=>'BG',
					'_103101052'=>'BG1',
					'_103101053'=>'BH',
					'_103101054'=>'BI',
					'_103101055'=>'BI1',
					'_103101056'=>'BJ',
					'_103101057'=>'BK',
					'_103101058'=>'BK1',
					'_103101059'=>'BL',
					'_103101060'=>'BM',
					'_103101061'=>'BM1',
					'_103101062'=>'BN',
					'_103101063'=>'BO',
					'_103101064'=>'BO1',
					'_103101065'=>'BP',
					'_103101066'=>'BQ',
					'_103101067'=>'BQ1',
					'_103101068'=>'BR',
					'_103101069'=>'BS',
					'_103101070'=>'BS1',
					'_103101071'=>'BT',
					'_103101072'=>'BU',
					'_103101073'=>'BU1',
					'_103101074'=>'BV',
					'_103101075'=>'BW',
					'_103101076'=>'BW1',
					'_103101077'=>'BX',
					'_103101078'=>'BY',
					'_103101079'=>'BY1',
					'_103101080'=>'BZ',
					'_103101081'=>'CA',
					'_103101082'=>'CA1',
					'_103101083'=>'CB',
					'_103101084'=>'CC',
					'_103101085'=>'CC1',
					'_103101086'=>'CD',
					'_103101087'=>'CE',
					'_103101088'=>'CE1',
					'_103101089'=>'CF',
					'_103101090'=>'CG',
					'_103101091'=>'CG1',
					'_103101092'=>'CH',
					'_103101093'=>'CI',
					'_103101094'=>'CI1',
					'_103101095'=>'CJ',
					'_103101096'=>'CK',
					'_103101097'=>'CK1',
					'_103101098'=>'CL',
					'_103101099'=>'CM',
					'_103101100'=>'CN',
					'_103101101'=>'CO',
					'_103101102'=>'1A',
					'_103101103'=>'1A1',
					'_103101104'=>'CP',
					'_103101105'=>'CR',
					'_103102001'=>'DA',
					'_103102002'=>'DB',
					'_103102003'=>'EK',
					'_103102004'=>'DC',
					'_103102005'=>'DD',
					'_103102006'=>'DE',
					'_103102007'=>'DF',
					'_103102008'=>'DG',
					'_103102009'=>'DH',
					'_103102010'=>'DI',
					'_103102011'=>'DJ',
					'_103102012'=>'DK',
					'_103102013'=>'DL',
					'_103102014'=>'DM',
					'_103102015'=>'DN',
					'_103102016'=>'DO',
					'_103102017'=>'DP',
					'_103102018'=>'DQ',
					'_103102019'=>'DR',
					'_103102020'=>'DS',
					'_103102021'=>'DT',
					'_103102022'=>'DU',
					'_103102023'=>'DV',
					'_103102024'=>'DW',
					'_103102025'=>'DX',
					'_103102026'=>'DY',
					'_103102027'=>'DZ',
					'_103102028'=>'EA',
					'_103102029'=>'EB',
					'_103102030'=>'EC',
					'_103102031'=>'ED',
					'_103102032'=>'EE',
					'_103102033'=>'EF',
					'_103102034'=>'EG',
					'_103102035'=>'EH',
					'_103102036'=>'EI',
					'_103103001'=>'FA',
					'_103103002'=>'FB',
					'_103103003'=>'FC',
					'_103103004'=>'FD',
					'_103103005'=>'FE',
					'_103103006'=>'FF',
					'_103103007'=>'FG',
					'_103103008'=>'FH',
					'_103103009'=>'FI',
					'_103103010'=>'FJ',
					'_103103011'=>'FK',
					'_103103012'=>'FL',
					'_103103013'=>'FM',
					'_103103014'=>'FN',
					'_103103015'=>'FO',
					'_103103016'=>'FP',
					'_103103017'=>'FQ',
					'_103103018'=>'FR',
					'_103103019'=>'FS',
					'_103103020'=>'FT',
					'_103103021'=>'FU',
					'_103103022'=>'FV',
					'_103103023'=>'FW',
					'_103103024'=>'FX',
					'_103103025'=>'FY',
					'_103103026'=>'FZ',
					'_103103027'=>'GA',
					'_103103028'=>'GB',
					'_103103029'=>'GC',
					'_103103030'=>'GD',
					'_103103031'=>'GE',
					'_103103032'=>'GF',
					'_103103033'=>'GG',
					'_103103034'=>'GH',
					'_103103035'=>'GI',
					'_103103036'=>'GJ',
					'_103103037'=>'GK',
					'_103103038'=>'GL',
					'_103103039'=>'GM',
					'_103103040'=>'GN',
					'_103103041'=>'GO',
					'_103103042'=>'GP',
					'_103103043'=>'GQ',
					'_103103044'=>'GR',
					'_103103045'=>'GS',
					'_103103046'=>'GT',
					'_103103047'=>'GU',
					'_103103048'=>'GV',
					'_103103049'=>'GW',
					'_103103050'=>'HA',
					'_103103051'=>'HB',
					'_103103052'=>'HC',
					'_103103053'=>'HD',
					'_103103054'=>'HE',
					'_103103055'=>'HF',
					'_103103056'=>'HG',
					'_103103057'=>'HH',
					'_103103058'=>'HI',
					'_103103059'=>'HJ',
					'_103103060'=>'HK',
					'_103103061'=>'HL',
					'_103103062'=>'HM',
					'_103103063'=>'HN',
					'_103103064'=>'HP',
					'_103103065'=>'HQ',
					'_103104077'=>'A1',
					'_103104078'=>'A2',
					'_103104079'=>'A3',
					'_103104080'=>'A4',
					'_103104001'=>'KF',
					'_103104002'=>'LP',
					'_103104003'=>'0J',
					'_103104004'=>'MY',
					'_103104005'=>'NC',
					'_103104006'=>'2E',
					'_103104007'=>'0K',
					'_103104008'=>'PD',
					'_103104009'=>'PH',
					'_103104010'=>'QX',
					'_103104011'=>'ON',
					'_103104012'=>'OP',
					'_103104013'=>'OQ',
					'_103104014'=>'OR',
					'_103104015'=>'SM',
					'_103104016'=>'SP',
					'_103104017'=>'5R',
					'_103104018'=>'5S',
					'_103104019'=>'5T',
					'_103104020'=>'5U',
					'_103104021'=>'7B',
					'_103104022'=>'TY',
					'_103104023'=>'TZ',
					'_103104024'=>'UA',
					'_103104025'=>'UF',
					'_103104026'=>'UH',
					'_103104027'=>'UK',
					'_103104028'=>'UM',
					'_103104029'=>'UR',
					'_103104030'=>'UV',
					'_103104031'=>'VA',
					'_103104032'=>'UY',
					'_103104033'=>'UZ',
					'_103104034'=>'VB',
					'_103104035'=>'VN',
					'_103104036'=>'VT',
					'_103104037'=>'VU',
					'_103104038'=>'VV',
					'_103104039'=>'7Y1',
					'_103104040'=>'7Z1',
					'_103104041'=>'VG',
					'_103104042'=>'VG1',
					'_103104043'=>'VH',
					'_103104044'=>'VH1',
					'_103104045'=>'8A1',
					'_103104046'=>'8B1',
					'_103104047'=>'8C',
					'_103104048'=>'8C1',
					'_103104049'=>'8D',
					'_103104050'=>'8D1',
					'_103104051'=>'8E1',
					'_103104052'=>'VW',
					'_103104053'=>'VW1',
					'_103104054'=>'VX',
					'_103104055'=>'VX1',
					'_103104056'=>'VQ',
					'_103104057'=>'VQ1',
					'_103104058'=>'8J1',
					'_103104059'=>'VI',
					'_103104060'=>'VI1',
					'_103104061'=>'8K1',
					'_103104062'=>'SZ1',
					'_103104063'=>'8L1',
					'_103104064'=>'VY',
					'_103104065'=>'VZ',
					'_103104066'=>'VZ1',
					'_103104067'=>'VZ2',
					'_103104068'=>'YQ',
					'_103104069'=>'YR',
					'_103104070'=>'YS',
					'_103104071'=>'YT',
					'_103104072'=>'YU',
					'_103104073'=>'YV',
					'_103104074'=>'YY',
					'_103104075'=>'YZ',
					'_103104076'=>'YP',
					'_103104081'=>'CZ',
					'_103104082'=>'KB',
					'_103104083'=>'KC',
					'_103104084'=>'KD',
					'_103104085'=>'KE',
					'_103104086'=>'LN',
					'_103104087'=>'LO',
					'_103104088'=>'LQ',
					'_103104089'=>'LR',
					'_103104090'=>'LS',
					'_103104091'=>'0G',
					'_103104092'=>'0H',
					'_103104093'=>'NL',
					'_103104094'=>'LT',
					'_103104095'=>'LU',
					'_103104096'=>'NO',
					'_103104097'=>'LV',
					'_103104098'=>'LW',
					'_103104099'=>'NN',
					'_103104100'=>'NG',
					'_103104101'=>'NH',
					'_103104102'=>'NM',
					'_103104103'=>'NJ',
					'_103104104'=>'NK',
					'_103104105'=>'NP',
					'_103104106'=>'0L',
					'_103104107'=>'PA',
					'_103104108'=>'PB',
					'_103104109'=>'PC',
					'_103104110'=>'PE',
					'_103104111'=>'PF',
					'_103104112'=>'PG',
					'_103104113'=>'QU',
					'_103104114'=>'QV',
					'_103104115'=>'QW',
					'_103104116'=>'Z91',
					'_103104117'=>'Z92',
					'_103104118'=>'SN',
					'_103104119'=>'SP_1',
					'_103104120'=>'SP_2',
					'_103104121'=>'SR',
					'_103104122'=>'TO',
					'_103104123'=>'3Z',
					'_103104124'=>'TS',
					'_103104125'=>'TT',
					'_103104126'=>'TU',
					'_103104127'=>'4D',
					'_103104128'=>'4H',
					'_103104129'=>'4M',
					'_103104130'=>'4S',
					'_103104131'=>'4W',
					'_103104132'=>'5A',
					'_103104133'=>'5E',
					'_103104134'=>'5K',
					'_103104135'=>'5P',
					'_103104136'=>'5Y',
					'_103104137'=>'5Z',
					'_103104138'=>'TV',
					'_103104139'=>'TW',
					'_103104140'=>'TX',
					'_103104141'=>'6A',
					'_103104142'=>'6B',
					'_103104143'=>'6C',
					'_103104144'=>'6D',
					'_103104145'=>'6E',
					'_103104146'=>'6F',
					'_103104147'=>'6G',
					'_103104148'=>'6H',
					'_103104149'=>'02',
					'_103104150'=>'03',
					'_103104151'=>'04',
					'_103104152'=>'05',
					'_103104153'=>'06',
					'_103104154'=>'07',
					'_103104155'=>'08',
					'_103104156'=>'09',
					'_103104157'=>'6N',
					'_103104158'=>'6P',
					'_103104159'=>'6R',
					'_103104160'=>'6S',
					'_103104161'=>'6T',
					'_103104162'=>'6U',
					'_103104163'=>'6V',
					'_103104164'=>'6W',
					'_103104165'=>'6X',
					'_103104166'=>'6Y',
					'_103104167'=>'6Z',
					'_103104168'=>'7A',
					'_103104169'=>'7C',
					'_103104170'=>'UB',
					'_103104171'=>'UC',
					'_103104172'=>'UD',
					'_103104173'=>'UE',
					'_103104174'=>'UG',
					'_103104175'=>'UJ',
					'_103104176'=>'UL',
					'_103104177'=>'UP',
					'_103104178'=>'UT',
					'_103104179'=>'UX',
					'_103104180'=>'UQ',
					'_103104181'=>'UU',
					'_103104182'=>'VM',
					'_103104183'=>'VP',
					'_103104184'=>'VC',
					'_103104185'=>'VR',
					'_103104186'=>'VS',
					'_103104187'=>'7Y',
					'_103104188'=>'7Y2',
					'_103104189'=>'7Z',
					'_103104190'=>'7Z2',
					'_103104191'=>'VG2',
					'_103104192'=>'VH2',
					'_103104193'=>'8A',
					'_103104194'=>'8A2',
					'_103104195'=>'8B',
					'_103104196'=>'8B2',
					'_103104197'=>'8C2',
					'_103104198'=>'8D2',
					'_103104199'=>'8E',
					'_103104200'=>'8E2',
					'_103104201'=>'VW2',
					'_103104202'=>'VX2',
					'_103104203'=>'VQ2',
					'_103104204'=>'8J',
					'_103104205'=>'8J2',
					'_103104206'=>'8J3',
					'_103104207'=>'VI2',
					'_103104208'=>'VI3',
					'_103104209'=>'8K',
					'_103104210'=>'8K2',
					'_103104211'=>'SZ',
					'_103104212'=>'SZ2',
					'_103104213'=>'8L',
					'_103104214'=>'8L2',
					'_103104215'=>'VJ',
					'_103104216'=>'VK',
					'_103104217'=>'ZE',
					'_103104218'=>'XQ',
					'_103104219'=>'SS',
					'_103104220'=>'ST',
					'_103104221'=>'ZJ',
					'_103104222'=>'YW',
					'_103104223'=>'9Z',
					'_103104224'=>'YX',
					'_103104225'=>'9X',
					'_103102037'=>'B1',
					'_103102038'=>'EJ',
					);

require_once 'framework/common/curl.php';

define('RNCS_WS_URL', 'http://www.services.ort.fr/ProtocoleRNCS/access');
define('RNCS_WS_USER', '167260');
define('RNCS_WS_PASS', '100356');

class MRncsFlux {

	public $body = '';
	public $header = '';
	public $codeRetour = 0;
	public $errMsgInpi='';
	public $errNumInpi='';
	public $cookie='';
	public $iDb;
	public $tabVitrine=array();
	
	
	public $tabErreurInpi=array(0=>"Aucune erreur",
								10=>"Erreur de protocole avec système distant",
								11=>"Système distant inaccessible",
								101=>"Type de requête invalide",
								102=>"Code d'erreur appelant invalide",
								103=>"Identification non reconnue",
								107=>"Date invalide",
								108=>"Heure invalide",
								110=>"Code produit inconnu",
								115=>"Numéro de champ invalide",
								116=>"Longueur de champ invalide",
								121=>"Code de protocole invalide",
								122=>"Code base de données invalide",
								900=>"Erreur applicative",
								990100=>"+ de 50 entreprises trouvées",
								990101=>"Recherche complexe",
								990102=>"Critères saisis insuffisants",
								990103=>"Requête trop imprécise",
								990104=>"Dénomination non significative",
								990105=>"Critère commune non générall",
								990106=>"Critère voie trop général",
								990107=>"Commune obligatoire avec rue",
								990108=>"Commune non identifiée",
								990109=>"Entreprise non identifiée",
								990110=>"Erreur système",
								990111=>"Devise incorrecte",
								990112=>"Autre erreur",
								990113=>"Donnée incorrecte",
								990114=>"Valeur incohérente",
								990115=>"Surveillance impossible",
								990116=>"Entreprise non surveillée",
								990117=>"Entreprise déjà surveillée",
								990118=>"Produit indisponible",
								990119=>"Erreur base de données",
								990120=>"Erreur facturation",
							);

	function __construct() {
		$this->iDb=new WDB();
	}
	
	function getVitrine($siren) {
		$this->tabVitrine=$this->query($siren, array('Type'=>'QVIT'));
		return $this->tabVitrine;
	}
	
/*
	$cmdPages=$cmdNom='';

	if (isset($_REQUEST['pages']))	$cmdPages=$_REQUEST['pages'];
	if (isset($_REQUEST['nom']))	$cmdNom=$_REQUEST['nom'];
	if (isset($_REQUEST['vue'])) 	$vue=$_REQUEST['vue'];

	if ($vue=='bilans')	$presta='12004';
	else 				$presta='12003';
			
	if ($cmdNom && $cmdPages)	$type='QDOC';
	else 						$type='QVIT';
	$prod=array('Type'=>$type,
				'Presta'=>$presta,
				'Num'=>'2',//2,
				'Ver'=>'2',//2,
				'Siren'=>$siren,
				'CmdNom'=>$cmdNom,
				'CmdPages'=>$cmdPages,
				'CmdBilan'=>'',
			   );

	$ref='';
	*/

	public  function query($siren, $prod=array()) {
		/*$prod=array('Type'=>$type,
				'Presta'=>$presta,
				'Siren'=>$siren,
				'CmdNom'=>$cmdNom,
				'CmdPages'=>$cmdPages,
				'CmdBilan'=>'',
				'Ref'=>'',
			   );
		*/
		$option='';
		if ( ($prod['Type']=='QDOC' || $prod['Presta']==12004 || $prod['Presta']==12003) && !isset($prod['Num'])) {
			$prod['Num']=2;
			$prod['Ver']=2;
		} elseif (!isset($prod['Num'])) {
			$prod['Num']=1;
			$prod['Ver']=3;
		}
			
		$req=	'<?xml version="1.0" ?>'.								"\n".
				'<ort:message xmlns:ort="http://www.ort.fr/NEWPORT/V2">'.		"\n".
				' <ort:_1 type="110">'.											"\n".
				'  <ort:_7 type="010">NPORT</ort:_7>'.							"\n".
				'  <ort:_8 type="010">2</ort:_8>'.								"\n".	// Version du protocole
				'  <ort:_9 type="010">1</ort:_9>'.								"\n".	// Version du dictionnaire
				'  <ort:_10 type="010">1</ort:_10>'.							"\n".	// Code protocole/version application
				'  <ort:_11 type="010">PROTORNCS</ort:_11>'.					"\n".
				'  <ort:_12 type="010">'.$prod['Type'].'</ort:_12>'.			"\n".	// Prestation souhaitée QVIT / QDOC
				'  <ort:_13 type="010">0</ort:_13>'.							"\n".	// Code retour/erreur
				'  <ort:_14 type="010">'.RNCS_WS_USER.RNCS_WS_PASS.'</ort:_14>'."\n".	// User Password concaténés
				'  <ort:_15 type="010">'.$prod['Ref'].'</ort:_15>'.						"\n".	// Réf.
				'  <ort:_16 type="010">'.date('Ymd').'</ort:_16>'.				"\n".	// Date SSAAMMJJ
				'  <ort:_17 type="010">'.date('His').'</ort:_17>'.				"\n".	// Heure HHMMSS
				'  <ort:_19 type="010">ORT</ort:_19>'.							"\n".
				' </ort:_1>'.													"\n".
				' <ort:_2 type="110">'.											"\n".
				'  <ort:_1 type="110">'.										"\n".
				'   <ort:_26 type="010">'.$prod['Prod'].'</ort:_26>'.			"\n".	// Type de produit (vide pour QVIT)
				'   <ort:_27 type="010">'.$prod['Num'].'</ort:_27>'.			"\n".	// Numéro de produit souhaité	1	2
				'   <ort:_28 type="010">'.$prod['Ver'].'</ort:_28>'.			"\n".	// Version du produit 			3	2
				'   <ort:_31 type="010">'.$prod['Presta'].'</ort:_31>'.			"\n".	// Code prestation (vide pour QVIT)
				'  </ort:_1>'.													"\n";		
if ($prod['Presta']==12004 || $prod['Presta']==12003) {
$req.='
	<ort:_2 type="110">
		<ort:_100 type="111">
			<ort:_101 type="110">
				<ort:_120000031 type="010">'.$prod['Presta'].'</ort:_120000031> 
				<ort:_100700010 type="010">FTP</ort:_100700010> 
				<ort:_100701000 type="010">NTIF</ort:_100701000>
				<ort:_120000015 type="010" /> 
			</ort:_101>
		</ort:_100>
		<ort:_200 type="111">
			<ort:_210 type="110">
				<ort:_130000001 type="010">'.$siren.'</ort:_130000001> 
				<ort:_103000402 type="010">'.$prod['CmdNom'].'</ort:_103000402> 
				<ort:_103000403 type="010">'.$prod['CmdPages'].'</ort:_103000403> 
				<ort:_190000025 type="010">'.$prod['CmdBilan'].'</ort:_190000025> 
			</ort:_210>
		</ort:_200>
	</ort:_2>';	//
	$option=$prod['CmdNom'].'-'.$prod['CmdPages'].'-'.$prod['CmdBilan'];
} elseif ($prod['Type']=='QINF') {
$req.='  <ort:_2 type="110">'."\n".'<ort:_130000001 type="010">'.$siren.'</ort:_130000001>';
if ($prod['Presta']=='2000' || $prod['Presta']=='2001' || $prod['Presta']=='2002' || $prod['Presta']=='2003') {
	// Liste années bilans
	$req.='<ort:_190000025 type="010">'.$prod['Millesime'].'</ort:_190000025>'."\n";
	$option=$prod['Millesime'];
}
elseif ($prod['Presta']=='12001')
	// Flag limitation du nombre d’établissement
	$req.='<ort:_109105002 type="010">N</ort:_109105002>'."\n";
$req.='</ort:_2>';
} else {
$req.='  <ort:_2 type="110">
<ort:_130000001 type="010">'.$siren.'</ort:_130000001>
<ort:_120000031 type="010">'.$prod['Presta'].'</ort:_120000031>
</ort:_2>';// 12003 Actes et statuts  12004 Bilans
}
$req.=' </ort:_2>
</ort:message>';
echo $req.'<hr/>';
$postData=array(''=>$req);
$cookie=$referer='';
$enCache=false;
$params=md5(serialize($prod));
//die($params);
/*if ($prod['CmdNom']<>'' || $prod['CmdPages']<>'' || $prod['CmdBilan']<>'' || $prod['Millesime']<>'')
	$strOption=*/
//AND params='$params'
$res=$this->iDb->select('rncs_requetes', 'dateRequete, strXml, codeErreur, libErreur', "siren=$siren AND params='$params' ORDER BY dateRequete DESC");
//type='".$prod['Type']."' AND presta ='".$prod['Presta']."' $strOption ORDER BY dateRequete DESC");
if (count($res)>0) {
	$tabRes=$res[0];
	$dateReq=$tabRes['dateRequete'];
	$this->body=$tabRes['strXml'];
	$this->errNumInpi=$tabRes['codeErreur'];
	$this->errMsgInpi=$tabRes['libErreur'];
	$enCache=true;
	echo 'Une requête dans le cache <br/>';
	//die();	
} else {
	echo 'Pas de requête dans le cache <br/>';
	//die();
	$page=getUrl(RNCS_WS_URL, $cookie, $postData, $referer, false, 'www.services.ort.fr');
	$this->body=$page['body'];
	/**
 	 ** ERREUR PROTOCOL
	 **/
	preg_match('/<ort:_13 type="020">(.*)<\/ort:_13>/Uis',$this->body, $matches);
	if ($matches[1]*1<>0) {
		$this->errNumInpi=$matches[1];
		$this->errMsgInpi=$tabErreurInpi[$this->errNumInpi];
	//echo "<!-- ERREUR INPI 13 n°$errNumInpi = $errMsgInpi !!! -->\n";
	}
	
	/**
	 ** ERREUR APPLICATIVES NUMERIQUES
	 **/
	preg_match('/<ort:_96 type="020">(.*)<\/ort:_96>/Uis',$this->body, $matches);
	if ($matches[1]*1<>0) {
		$this->errNumInpi=$matches[1];
		$this->errMsgInpi=$tabErreurInpi[$this->errNumInpi];
	//echo "<!-- ERREUR INPI 96 n°$errNumInpi = $errMsgInpi !!! -->\n";
	}
	
	/**
	 ** ERREUR APPLICATIVES TEXTE
	 **/
	preg_match('/<ort:_97 type="010">(.*)<\/ort:_97>/Uis',$this->body, $matches);
	if ($matches[1]*1<>0) {
		$this->errMsgInpi=$matches[1];
	//echo "<!-- ERREUR INPI 97 : $errMsgInpi !!! -->\n";
	}
	
	$tabInsert=array(	'siren'			=> $siren,
						'dateRequete'	=> date('YmdHis'),
						'strXml'		=> $this->body,
						'params'		=> md5(serialize($prod)),
						'type'			=> $prod['Type'],
						'presta'		=> $prod['Presta'],
						'option'		=> $option,
						'codeErreur'	=> $this->errNumInpi,
						'libErreur'		=> $this->errMsgInpi,
					);
	$this->iDb->insert('rncs_requetes', $tabInsert, true);		
}

if ($prod['Presta']==12004)
 	// bilans image
	$pregmatch=preg_match_all('/<ort\:_201 type="110">(?:.*)<ort\:_103000040 type="010">(.*)<\/ort:_103000040>(?:.*)<ort\:_103000452 type="010">(.*)<\/ort:_103000452>(?:.*)<ort\:_103000402 type="010">(.*)<\/ort:_103000402>(?:.*)<ort\:_103000403 type="010">(.*)<\/ort:_103000403>(?:.*)<ort\:_103000404 type="010">(.*)<\/ort:_103000404>(?:.*)<\/ort:_201>/Uis',$this->body, $matches);
elseif ($prod['Presta']==12003)
	// actes image
	$pregmatch=preg_match_all('/<ort\:_201 type="110">(?:.*)<ort\:_103000452 type="010">(.*)<\/ort:_103000452>(?:.*)<ort\:_103000402 type="010">(.*)<\/ort:_103000402>(?:.*)<ort\:_103000403 type="010">(.*)<\/ort:_103000403>(?:.*)<ort\:_103000404 type="010">(.*)<\/ort:_103000404>(?:.*)<\/ort:_201>/Uis',$this->body, $matches);
elseif ($prod['Presta']==2000)
	// bilan simplifiés ou normaux saisi
	$pregmatch=preg_match('/<ort\:_2 type="110">(?:.*)<ort\:_2 type="110">(.*)<\/ort\:_2>(?:.*)<\/ort\:_2>/Uis',$this->body, $matches);
elseif ($prod['Presta']==12001)
	// Fiche Identite
	$pregmatch=preg_match('/<ort\:_2 type="110">(?:.*)<ort\:_2 type="110">(.*)<\/ort\:_2>(?:.*)<\/ort\:_2>/Uis',$this->body, $matches);
else {
	// Prestation Vitrine
	$tabVitrine=array();
	
	// Bilans dispos
	$tabBilans=array();
	// Type de Bilans saisis disponible
	for ($typeBilan=2000; $typeBilan<2004; $typeBilan++) {
		$pregmatch=preg_match('/<ort:_'.$typeBilan.' type="110">(?:.*)<ort\:_120000026 type="010">(.*)<\/ort\:_120000026>(?:.*)<ort\:_120000027 type="010">(.*)<\/ort\:_120000027>(?:.*)<ort\:_120000028 type="010">(.*)<\/ort\:_120000028>(?:.*)<ort\:_120000029 type="010">(.*)<\/ort:_120000029>(?:.*)<ort\:_120000031 type="010">(.*)<\/ort\:_120000031>(?:.*)<ort\:_109104002 type="010">(.*)<\/ort\:_109104002>(?:.*)<ort\:_109104003 type="010">(.*)<\/ort\:_109104003>(.*)<\/ort:_'.$typeBilan.'>/Uis',$this->body, $matches);
		$numProduit=$matches[1]; // 52
		$numProduit=$matches[2]; // 52
    	$verProduit=$matches[3]; // 1
	    $libProduit=$matches[4]; // Comptes sociaux
    	$typProduit=$matches[5]; // 2000 : Bilans sociaux 
    							 // 2001 : Bilans consolidés (70)
								 // 2002 : Bilans de banques (71)
								 // 2003 : Bilans d'assurances (72)
     	$synProduit=$matches[6]; // 109104002 : Disponibilité synchrone
     	$asyProduit=$matches[7]; // 109104003 : Disponibilité asynchrone
		$pregmatch=preg_match_all('/<ort\:_103000038 type="010">(.*)<\/ort:_103000038>/Uis',$matches[8], $matches);
		if ($typeBilan==2000)		$tabBilans['N']=$matches[1];	// Réel normal	  SC
		elseif ($typeBilan==2001)	$tabBilans['C']=$matches[1];	// Consolidé	  CC
		elseif ($typeBilan==2002)	$tabBilans['B']=$matches[1];	// Banque		  SB
		elseif ($typeBilan==2003)	$tabBilans['A']=$matches[1];	// Assurance	  SA
		else						$tabBilans['S']=$matches[1];	// Réel Simplifié SS
	}
	$tabVitrine['bilans']=$tabBilans;
	
	// Fiche d'identité
	$pregmatch=preg_match('/<ort:_12001 type="110">(?:.*)<ort\:_120000027 type="010">(.*)<\/ort\:_120000027>(?:.*)<ort\:_120000028 type="010">(.*)<\/ort\:_120000028>(?:.*)<ort\:_120000029 type="010">(.*)<\/ort:_120000029>(?:.*)<ort\:_120000031 type="010">(.*)<\/ort\:_120000031>(?:.*)<ort\:_109104002 type="010">(.*)<\/ort\:_109104002>(?:.*)<ort\:_109104003 type="010">(.*)<\/ort\:_109104003>(?:.*)<ort\:_130000401 type="010">(.*)<\/ort\:_130000401>(?:.*)<\/ort:_12001>/Uis',$this->body, $matches);
	$numProduit=$matches[1]; // 52
   	$verProduit=$matches[2]; // 1
    $libProduit=$matches[3]; // Comptes sociaux
   	$typProduit=$matches[4]; // 2000 : Bilans sociaux 
   	$synProduit=$matches[5]; // 109104002 : Disponibilité synchrone
   	$asyProduit=$matches[6]; // 109104003 : Disponibilité asynchrone
   	$nbEtab=$matches[7]; // Nombre d'établissements recensés
	$tabVitrine['identite']=array(	'numProduit'=>$numProduit,
   									'verProduit'=>$verProduit,
   									'libProduit'=>$libProduit,
									'typProduit'=>$typProduit,
 									'synProduit'=>$synProduit,
 									'asyProduit'=>$asyProduit,
 									'nbEtab'	=>$nbEtab);
 									
 	return $tabVitrine;
}

echo $this->body;

//print_r($tabBilans);
return $matches;
	}
	
	function getListeBilans($siren, $type='*') {
		$tabVitrine=$this->getVitrine($siren);
		foreach ($tabVitrine['bilans'] as $type => $tabMillesime) {
			foreach ($tabMillesime as $num=>$millesime) {
				$delta1=round((Wdate::dateT('d/m/Y','Y',$millesime)-Wdate::dateT('d/m/Y','Y',$tabMillesime[$num+1]))*12)
						+round(Wdate::dateT('d/m/Y','m',$millesime)-Wdate::dateT('d/m/Y','m',$tabMillesime[$num+1]));
				if ($delta1>23)	$delta1=0;
				$delta2=round((Wdate::dateT('d/m/Y','Y',$tabMillesime[$num+1])-Wdate::dateT('d/m/Y','Y',$tabMillesime[$num+2]))*12)
						+round(Wdate::dateT('d/m/Y','m',$tabMillesime[$num+1])-Wdate::dateT('d/m/Y','m',$tabMillesime[$num+2]));
				if ($delta2>23)	$delta2=0;
				$tabRet[$type.$millesime]=array(	'dateProvPartenaire'=>date('Ymd'),
												  	'dureeExercice'		=>$delta1,
													'dateExercicePre'	=>Wdate::dateT('d/m/Y','Ymd',$tabMillesime[$num+1]),
													'dureeExercicePre'	=>$delta2,
													'monnaie'			=>'EUR',
													'source'			=>100,
													'ref'				=>'',
											   );
			}
		}
		return $tabRet;
	}
	
	function getBilan($siren, $millesime, $type='N') {
		global $tabOrt2Sd;
		$tabRet=$tabPostes=array();
		if ($type=='N')		$presta=2000;
		elseif ($type=='C')	$presta=2001;
		elseif ($type=='B')	$presta=2002;
		elseif ($type=='A')	$presta=2003;
		$prod=array(	'Prod'=>'RNCS',
						'Num'=>'52',
						'Ver'=>'1',
						'Presta'=>$presta,
						'Millesime'=>Wdate::dateT('d/m/Y','mY',$millesime),
						'Type'=>'QINF');
		$ret=$this->query($siren, $prod);
		
	/*	$fp=fopen(LOG_PATH.'/debugBilansRncs.log','a');
		fwrite($fp, print_r($ret,true));
		fclose($fp);*/
		
		$pregmatch=preg_match_all('/<ort\:_(.*) type="010">(.*)<\/ort\:_(.*)>/Uis', $ret[1], $matches);
		foreach ($matches[1] as $i=>$poste) {
			//echo "$i : $poste<br/>";
			if ('_'.$poste=='_103000040') { // CONSOLIDE
				switch ($matches[2][$i]) {
					case 'SC':	$tabRet[$tabOrt2Sd['_'.$poste]]='N';	break;
					case 'CC':	$tabRet[$tabOrt2Sd['_'.$poste]]='C';	break;
					case 'SB':	$tabRet[$tabOrt2Sd['_'.$poste]]='B';	break;
					case 'SA':	$tabRet[$tabOrt2Sd['_'.$poste]]='A';	break;
					case 'SS':	$tabRet[$tabOrt2Sd['_'.$poste]]='S';	break;
					default:											break;
				}
			} elseif ('_'.$poste=='_103000214') {	// MONNAIE_LIV_UNITE
				switch ($matches[2][$i]*1) {
					case 0:		$tabRet[$tabOrt2Sd['_'.$poste]]='';	break;
					case 3:		$tabRet[$tabOrt2Sd['_'.$poste]]='k';	break;
					case 6:		$tabRet[$tabOrt2Sd['_'.$poste]]='m';	break;
					default:											break;
				}
			} elseif ('_'.$poste=='_130000001' || 	// SIREN
					  '_'.$poste=='_103000033' ||	// DATE_CLOTURE
					  '_'.$poste=='_103000023' ||	// DUREE_MOIS
					  '_'.$poste=='_103000202' ||	// MONNAIE_ORI
					  '_'.$poste=='_103000205' ||	// MONNAIE_ORI_UNITE
					  '_'.$poste=='_103000211' ||	// MONNAIE
					  '_'.$poste=='_103000029' ||	// AVIS_CAC
					  '_'.$poste=='_130000009') {	// CODE_NAF
				$tabRet[$tabOrt2Sd['_'.$poste]]=$matches[2][$i];
			} else
				$tabPostes[$tabOrt2Sd['_'.$poste]]=$matches[2][$i];
		}
		$tabRet['POSTES']=$tabPostes;
		$tabRet['DATE_FRAICHE_BILAN']=date('Ymd');
		$tabRet['TYPE_BILAN']=$tabRet['CONSOLIDE'];
		//$tabRet['DATE_CLOTURE_PRE']
		//$tabRet['DUREE_MOIS_PRE']
		$fp=fopen(LOG_PATH.'/debugBilansRncs.log','a');
		fwrite($fp, print_r($tabRet,true));
		fclose($fp);
		
		return $tabRet;
	}
	
	function getIdentite($siren) {
		$tabVitrine=$this->getVitrine($siren);
		
		$fp=fopen(LOG_PATH.'/debugVitrineRncs.log','a');
		fwrite($fp, print_r($tabVitrine,true));
		fclose($fp);
		
		$prod=array(	'Prod'=>'RNCS',
						'Num'=>$tabVitrine['identite']['numProduit'],
						'Ver'=>$tabVitrine['identite']['verProduit'],
						'Presta'=>12001,
						'Type'=>'QINF');
		$ret=$this->query($siren, $prod);
		if ($this->errNumInpi>0 && $this->errMsgInpi<>'') {
			return false;
		} else {
			$pregmatch=preg_match_all('/<ort\:_((?:1300|13011|13016|130100403).*) type="010">(.*)<\/ort\:_((?:1300|13011|13016|130100403).*)>/Uis', $ret[1], $matchesIden);
			foreach ($matchesIden[1] as $i=>$rubrique) {
				    if ($rubrique==130000300)	$tabIdentite['Nom']=$matchesIden[2][$i];
				elseif ($rubrique==130000008)	$tabIdentite['FJ']=$matchesIden[2][$i];
				elseif ($rubrique==130000303)	$tabIdentite['Sigle']=$matchesIden[2][$i];
				elseif ($rubrique==130100015)	$tabIdentite['Enseigne']=$matchesIden[2][$i];
				elseif ($rubrique==130000500)	$tabIdentite['CapitalMont']=$matchesIden[2][$i];
				elseif ($rubrique==130000501)	$tabIdentite['CapitalType']=$matchesIden[2][$i];
				elseif ($rubrique==130000508)	$tabIdentite['CapitalDev']=$matchesIden[2][$i];
				elseif ($rubrique==130000509)	$tabIdentite['CapitalUnite']=$matchesIden[2][$i];
				elseif ($rubrique==130000513)	$tabIdentite['CapitalMontCts']=$matchesIden[2][$i];
				elseif ($rubrique==130000102)	$tabIdentite['DateCrea']=$matchesIden[2][$i];
				elseif ($rubrique==130001002)	$tabIdentite['DateImma']=$matchesIden[2][$i];
				elseif ($rubrique==130000013)	$tabIdentite['CodeGreffe']=$matchesIden[2][$i];
				elseif ($rubrique==130000014)	$tabIdentite['NumRC']=$matchesIden[2][$i];
				elseif ($rubrique==130001102)	$tabIdentite['DateRad']=$matchesIden[2][$i];
				elseif ($rubrique==130000025)	$tabIdentite['Provisoire']=$matchesIden[2][$i];
				elseif ($rubrique==130000401)	$tabIdentite['NbEtab']=$matchesIden[2][$i];
			}

			/** DIRIGEANTS **/
			$tabDir=array();
			if (preg_match('/<ort\:_130300015 type="010">(.*)<\/ort:_130300015>(?:.*)<ort:_200 type="111">(.*)<\/ort\:_200>/Uis', $ret[1], $matchesDirs)) {
				$nbDirs=$matchesDirs[1];
				$strDir=$matchesDirs[2];
				$strMatch=	'<ort\:_210 type="110">(?:.*)'.
							'<ort\:_130300005 type="010">(.*)<\/ort\:_130300005>(?:.*)'.
							'<ort\:_130301003 type="010">(.*)<\/ort\:_130301003>(?:.*)'.
							'<ort\:_130300016 type="010">(.*)<\/ort\:_130300016>(?:.*)'.
							'<ort\:_130301006 type="010">(.*)<\/ort\:_130301006>(?:.*)'.
							'<ort\:_130301007 type="010">(.*)<\/ort\:_130301007>(?:.*)'.
							'<ort\:_130301009 type="010">(.*)<\/ort\:_130301009>(?:.*)'.
							'<ort\:_130301004 type="010">(.*)<\/ort\:_130301004>(?:.*)'.
							'<ort\:_130301008 type="010">(.*)<\/ort\:_130301008>(?:.*)'.
							'<ort\:_130301012 type="010">(.*)<\/ort\:_130301012>(?:.*)'.
							'<ort\:_130301100 type="010">(.*)<\/ort\:_130301100>(?:.*)'.
							'<ort\:_130301102 type="010">(.*)<\/ort\:_130301102>(?:.*)'.
							'<ort\:_130302002 type="010">(.*)<\/ort\:_130302002>(?:.*)'.
							'<ort\:_130302003 type="010">(.*)<\/ort\:_130302003>(?:.*)'.
							'<ort\:_130302004 type="010">(.*)<\/ort\:_130302004>(?:.*)<\/ort\:_210>';
				$pregmatch=preg_match_all("/$strMatches/Uis", $strDir, $matchesDirs);
				foreach ($matchesDirs[1] as $i=>$dir) {
					$tabDir[$i]=array(	'Fonc'=>$dir[1],
										'Sexe'=>$dir[2],
										'Nom'=>$dir[3],
										'Prenom'=>$dir[4],
										'Prenom2'=>$dir[5],
										'NomJF'=>$dir[6],
										'NaissDept'=>$dir[7],
										'NaissLieu'=>$dir[8],
										'NaissNati'=>$dir[9],
										'NaissDate'=>$dir[10],
										'ReprNom'=>$dir[11],
										'ReprPrenom'=>$dir[12],
										'ReprPrenom2'=>$dir[13],
									 );
				}
			}
 
			$fp=fopen(LOG_PATH.'/debugIdentiteRncs.log','a');
			fwrite($fp, print_r($matchesIden,true).EOL.print_r($matchesEtab,true).EOL.print_r($matchesDirs,true));
			fclose($fp);

			return $tabIdentite;
		}
	}
	
}

 ?>