<?php
			session_start();
	
			require('invoice.php');
		
			if($_SESSION['database']=="local")
			  {
			     $db_host    = "localhost";
			     $db_user   = "root";
			     $db_pass   = "";
		     	     $db_name   = "boutique";
		
			 }
			
			if($_SESSION['database']=="public")
			  {
		  	     $db_host    = "sql2";
		   	     $db_user   = "analogde";
		 	     $db_pass   = "rainbow";
		    	     //$db_type   = "mysql";
			     $db_name   = "analogde";
			 }


			$indice = $_GET['ref'];

			$email = $_SESSION['email'];	
									 
		 	$connexion = mysql_connect($db_host,$db_user,$db_pass);
		 	mysql_select_db($db_name, $connexion);
		
			$requete = "SELECT * FROM commandes WHERE email='$email' and id='$indice' ";
			$result = mysql_query($requete);
					
			$suivi_commandes=mysql_fetch_array($result);
			
			/*echo "<pre>";
			print_r($suivi_commandes);
			echo "</pre>";*/
			
			
			$requete = "SELECT * FROM clients WHERE email='$email' ";
			$result = mysql_query($requete);
			
			$data=mysql_fetch_array($result);
		
			$pdf = new PDF_Invoice( 'P', 'mm', 'A4' );
			$pdf->AddPage();
			$pdf->addSociete( "E-Ménager",
        	        		  "1,rue du Stade\n" .
                	  		  "81120 REALMONT - France\n".
                  	  		  "R.C.S. XXXX XXXX \n" .
                  	  		  "Capital : 15000 " . EURO );

			//$pdf->fact_dev( "Devis ", "TEMPO" );
			//$pdf->temporaire( "Devis temporaire" );
			$pdf->addFacture("fgdggd");
		
			$pdf->addDate( date("d-m-Y"));
		
		
			$pdf->addClient("CL01");
			$pdf->addPageNumber("1");
			
			$client = $data["civilite"] ." " .$data["prenom"] ." " .$data["nom"] ."\n";
			$client = $client .$data["adresse"] ."\n";
		
			if($data["complement"] != null)
			  {
				$client = $client .$data["complement"] ."\n";
			  }
		
			$client = $client .$data["cp"] ." " .$data["ville"] ."\n";
			$client = $client ."FRANCE" ."\n";

	
			//$pdf->addClientAdresse("Ste\nM. XXXX\n3ème étage\n33, rue d'ailleurs\n75000 PARIS");
			
			$pdf->addClientAdresse($client);
		
			// CB - chèque - virement
			$pdf->addReglement($suivi_commandes['reglement']);
			//$pdf->addReglement("Chèque à réception de facture");
			$pdf->addEcheance("10/10/2010");
			$pdf->addNumTVA("FR888777666");
			//$pdf->addReference("Devis ... du ....");
		
			$cols=array( "REFERENCE"    => 23,
		        	     "DESIGNATION"  => 78,
		             	     "QUANTITE"     => 22,
		             	     "PRIX TTC"      => 26,
		             	     "MONTANT TTC"  => 30,
		             	     "TVA"          => 11 );
		
			$pdf->addCols( $cols);
		
			$cols=array( "REFERENCE"    => "L",
		                     "DESIGNATION"  => "L",
		                     "QUANTITE"     => "C",
		                     "PRIX TTC"      => "R",
		                     "MONTANT TTC" => "R",
		                     "TVA"          => "R" );
		
			$pdf->addLineFormat( $cols);
			$pdf->addLineFormat($cols);
			
			$ptr = 0;
			
			while($row = mysql_fetch_assoc($result))
	    		     {
   				$id_cmd[$ptr] = $row["id"];
		    		$ptr = $ptr + 1;
	    		     }
			
			$requete = "SELECT * FROM commandes WHERE email='$email' ";
			$result = mysql_query($requete);
			$suivi_commandes=mysql_fetch_array($result);
				
			$articles = explode("," , $suivi_commandes['commande']);
	  	        
	  	        $y    = 109;
	  	        
	  	        	$ptr = 0;
			
			while($row = mysql_fetch_assoc($result))
	    		     {
   				$id_cmd[$ptr] = $row["id"];
		    		$ptr = $ptr + 1;
	    		     }
			
			$requete = "SELECT * FROM commandes WHERE email='$email' and id='$indice' ";
			$result = mysql_query($requete);
			$suivi_commandes=mysql_fetch_array($result);
				
			$articles = explode("-" , $suivi_commandes['commande']);
	  	        $nb_articles = count($articles)/7; 
	  	        
	  	       /* echo $nb_articles;
	  	        echo "<br>"; 
	  	        var_dump($articles);*/

			/*
	  	        0	PT078
	  	        1	REF78fgd
	  	        2	lave_linge Candy REF78fgd
	  	        3	washer
	  	        4	1 
	  	        5	799.00
	  	        6	645.00
	  	        7	fdg
	  	        8	hjfld
	  	        9	lave_linge Brandt hjfld
	  	        10	washer
	  	        11	1
	  	        12	700.00
	  	        13	645.00
	  	        */
//array(14) { [0]=>  string(5) "PT078" [1]=>  string(8) "REF78fgd" [2]=>  string(25) "lave_linge Candy REF78fgd" 
//[3]=>  string(6) "washer" [4]=>  string(3) " 1 " [5]=>  string(6) "799.00" [6]=>  string(6) "645.00" 
//[7]=>  string(3) "fdg" [8]=>  string(5) "hjfld" [9]=>  string(23) "lave_linge Brandt hjfld" [10]=>  string(6) "washer" 
//[11]=>  string(3) " 1 " [12]=>  string(6) "700.00" [13]=>  string(6) "645.00" } 
			
			$ptr = 0;

			for($i=0;$i<$nb_articles;$i++)
			   {
				$line = array(  "REFERENCE"    => trim($articles[$ptr+1]),
               					"DESIGNATION"  => trim($articles[$ptr+2]),
				                "QUANTITE"     => trim($articles[$ptr+4]),
               					"PRIX TTC"     => trim($articles[$ptr+5]),
               					"MONTANT TTC" => $articles[$ptr+4]*$articles[$ptr+5],
               					"TVA"          => "19.6" );
				$size = $pdf->addLine( $y, $line );
				$y   += $size + 2;
				$ptr = $ptr+7;
			}	


			
			$pdf->addCadreTVAs();
        


			$tot_prods = array( array ( "px_unit" => 600, "qte" => 1, "tva" => 1 ),
			                    array ( "px_unit" =>  10, "qte" => 1, "tva" => 1 ));
			$tab_tva = array( "1"       => 19.6,
			                  "2"       => 5.5);
			$params  = array( "RemiseGlobale" => 1,
			                      "remise_tva"     => 1,       // {la remise s'applique sur ce code TVA}
			                      "remise"         => 0,       // {montant de la remise}
			                      "remise_percent" => 10,      // {pourcentage de remise sur ce montant de TVA}
			                  "FraisPort"     => 1,
			                      "portTTC"        => 10,      // montant des frais de ports TTC
			                                                   // par defaut la TVA = 19.6 %
			                      "portHT"         => 0,       // montant des frais de ports HT
			                      "portTVA"        => 19.6,    // valeur de la TVA a appliquer sur le montant HT
			                  "AccompteExige" => 1,
			                      "accompte"         => 0,     // montant de l'acompte (TTC)
			                      "accompte_percent" => 15,    // pourcentage d'acompte (TTC)
			                  "Remarque" => "Avec un acompte, svp..." );
			
			
			
			//$pdf->addTVAs( $params, $tab_tva, $tot_prods);
			//$pdf->addCadreEurosFrancs();
			$pdf->Output();


?>
