<?php
  function Affiche_formulaire_liste_1($No_Formulaire)
  {
    global $Type, $Sous_Type, $lg_admin;
    $tab_description=Create_Table_Champs($No_Formulaire,$nb_champs);
    if(isset($GLOBALS['del']))
    {
      $requette="DELETE FROM listes WHERE Type=$No_Formulaire AND Nom='".addslashes($GLOBALS['del'])."'";
      Connection::executeRawQuery($requette);
    }
    $resultat=Liste::getListesByNoFormulaire($No_Formulaire);
    $path_parts = pathinfo($_SERVER['PHP_SELF']);
    if(count($resultat))
    {
      echo "<div class='div_listes_existantes'>Listes existantes</div>\n";
      echo "<table cellspacing='0' class='tab_listes_exitantes'>\n";
      foreach($resultat as $ligne)
      {
        echo "<tr class='tr_liste_existante'><td class='td_lien_liste_existante'>";
        echo "<A HREF='".$path_parts["basename"]."?Type=$Type&Sous_Type=$Sous_Type&Etape=4";
        $tab=explode(":",$ligne['Champs']);
        for($cpt=0;$cpt<30;$cpt++)
        {
          echo "&aff$cpt=".$tab[$cpt];
        }
        $tab=explode(":",$ligne['Recherche']);
        $tab2=explode(":",$ligne['Valeur']);
        for($cpt=0;$cpt<10;$cpt++)
        {
          echo "&rech$cpt=".$tab[$cpt];
          if($tab[$cpt]!=0)
          {
            echo "&val$cpt=".urlencode($tab2[$cpt]);
          }
        }
        $tab=explode(":",$ligne['Tri']);
        for($cpt=1;$cpt<=3;$cpt++)
        {
          echo "&tri$cpt=".$tab[($cpt-1)];
        }
        echo "'>".$ligne['Nom']."</A>";
        if(Is_Admin())
          echo "</td><td class='td_efface_liste_existante'><A HREF='".$path_parts["basename"]."?Type=$Type&Sous_Type=$Sous_Type&Etape=0&del=".urlencode($ligne['Nom'])."'>effacer</A>";
        echo "</td></tr>\n";
      }
      echo "</table>\n";
    }
    echo "<BIG>Champs à afficher</BIG><BR>\n";
    echo "  <form>\n";
    echo "   <input type='hidden' name='Type' value=$Type>\n";
    echo "   <input type='hidden' name='Sous_Type' value=$Sous_Type>\n";
    echo "   <input type='hidden' name='Etape' value='1'>\n";
    echo "   <table cellspacing='0' class='tab_liste_champs_afficher'>\n";
    for($cpt=0;$cpt<30;$cpt++)
    {
      echo "   <tr class='tr_liste_champs_afficher'><td class='td_liste_champs_afficher'><select  name='champ$cpt'>\n";
      for($cpt2=0;$cpt2<$nb_champs+1;$cpt2++)
      {
        echo "    <option value='$cpt2'>" . $tab_description[$cpt2][0] . "</option>\n";
      }
      echo "   </select></td></tr>\n";
    }
    echo "   </table>\n";
    echo "   <button type='submit'>Suite</button>\n";
    echo "  </form>\n";
  }
  
  function Affiche_formulaire_liste_2($No_Formulaire)
  {
    global $Type, $Sous_Type;
    $tab_description=Create_Table_Champs($No_Formulaire,$nb_champs);
    echo "  <form>\n";
    echo "   <input type='hidden' name='Type' value=$Type>\n";
    echo "   <input type='hidden' name='Sous_Type' value=$Sous_Type>\n";
    echo "   <input type='hidden' name='Etape' value='2'>\n";
    echo "<BIG>Champs à afficher</BIG><BR>\n";
    echo "   <table cellspacing='0' class='tab_liste_champs_afficher'>\n";
    for($cpt=0;$cpt<30;$cpt++)
    {
     	if($GLOBALS["champ$cpt"]!=0)
     	{
        echo "   <input type='hidden' name='aff$cpt' value='".$GLOBALS["champ$cpt"]."'>\n";
     		echo "<tr class='tr_liste_champs_afficher'><td class='td_liste_champs_afficher'>".$tab_description[$GLOBALS["champ$cpt"]][0]."</td></tr>\n";
      }
      else
      {
        echo "   <input type='hidden' name='aff$cpt' value='0'>\n";
      }
    }
    echo "   </table>\n";
    echo "<BIG>Champs de recherche</BIG><BR>\n";
    echo "   <table cellspacing='0' class='tab_liste_champs_rechercher'>\n";
    for($cpt=0;$cpt<10;$cpt++)
    {
      echo "   <tr class='tr_liste_champs_rechercher'><td class='td_liste_champs_rechercher'><select  name='champ$cpt'>\n";
      for($cpt2=0;$cpt2<$nb_champs+1;$cpt2++)
      {
        echo "    <option value='$cpt2'>" . $tab_description[$cpt2][0] . "</option>\n";
      }
      echo "   </select></td></tr>\n";
    }
    echo "   </table>\n";
    echo "   <button type='submit'>Suite</button>\n";
    echo "  </form>\n";
  }
  
  function Affiche_formulaire_liste_3($No_Formulaire)
  {
    global $Type, $Sous_Type;
    $tab_description=Create_Table_Champs($No_Formulaire,$nb_champs);
    echo "  <form>\n";
    echo "   <input type='hidden' name='Type' value=$Type>\n";
    echo "   <input type='hidden' name='Sous_Type' value=$Sous_Type>\n";
    echo "   <input type='hidden' name='Etape' value='3'>\n";
    echo "<BIG>Champs à afficher</BIG><BR>\n";
    echo "   <table cellspacing='0' class='tab_liste_champs_afficher'>\n";
    for($cpt=0;$cpt<30;$cpt++)
    {
     	if($GLOBALS["aff$cpt"]!=0)
     	{
        echo "   <input type='hidden' name='aff$cpt' value='".$GLOBALS["aff$cpt"]."'>\n";
     		echo "<tr class='tr_liste_champs_afficher'><td class='td_liste_champs_afficher'>".$tab_description[$GLOBALS["aff$cpt"]][0]."</td></tr>\n";
      }
      else
      {
        echo "   <input type='hidden' name='aff$cpt' value='0'>\n";
      }
    }
    echo "   </table>\n";
    echo "<BIG>Champs de recherche</BIG><BR>\n";
    for($cpt=0;$cpt<10;$cpt++)
    {
     	if($GLOBALS["champ$cpt"]!=0)
     	{
        echo "   <input type='hidden' name='rech$cpt' value='".$GLOBALS["champ$cpt"]."'>\n";
     		echo $tab_description[$GLOBALS["champ$cpt"]][0];
        echo Cree_Entree_Type("champ$cpt",$tab_description[$GLOBALS["champ$cpt"]][2],$tab_description[$GLOBALS["champ$cpt"]][3],"",RECHERCHE,"test$cpt")."<BR>\n";
      }
      else
      {
        echo "   <input type='hidden' name='rech$cpt' value='0'>\n";
      }
    }
    echo "   <button type='submit'>Suite</button>\n";
    echo "  </form>\n";
  }
  
  function Affiche_formulaire_liste_4($No_Formulaire)
  {
    global $Type, $Sous_Type;
    $tab_description=Create_Table_Champs($No_Formulaire,$nb_champs);
    echo "  <form>\n";
    echo "   <input type='hidden' name='Type' value=$Type>\n";
    echo "   <input type='hidden' name='Sous_Type' value=$Sous_Type>\n";
    echo "   <input type='hidden' name='Etape' value='4'>\n";
    echo "<BIG>Champs à afficher</BIG><BR>\n";
    for($cpt=0;$cpt<30;$cpt++)
    {
     	if($GLOBALS["aff$cpt"]!=0)
     	{
        echo "   <input type='hidden' name='aff$cpt' value='".$GLOBALS["aff$cpt"]."'>\n";
     		echo $tab_description[$GLOBALS["aff$cpt"]][0]."<BR>\n";
      }
      else
      {
        echo "   <input type='hidden' name='aff$cpt' value='0'>\n";
      }
    }
    echo "<BIG>Champs de recherche</BIG><BR>\n";
    for($cpt=0;$cpt<10;$cpt++)
    {
     	if($GLOBALS["rech$cpt"]!=0)
     	{
        echo "   <input type='hidden' name='rech$cpt' value='".$GLOBALS["rech$cpt"]."'>\n";
        $champ=$GLOBALS["champ".$cpt];
        $special=$GLOBALS["test".$cpt];
     		echo $tab_description[$GLOBALS["rech$cpt"]][0]." ".Requette_Recherche($tab_description[$GLOBALS["rech$cpt"]][1],$tab_description[$GLOBALS["rech$cpt"]][2],$champ,$special);
			if ($No_Formulaire == 1)
			{
			  $type_interne=$tab_description[$GLOBALS["rech$cpt"]][2] % 100;
			  if ($type_interne == 39 || $type_interne == 40)
			  {
     		    echo "   <input type='hidden' name='val$cpt' value=".'"'.Requette_Recherche("stock".ID_GARAGE.".".$tab_description[$GLOBALS["rech$cpt"]][1],$tab_description[$GLOBALS["rech$cpt"]][2],$champ,$special).'"'.">\n";
			  }
			  else
			  {
     		    echo "   <input type='hidden' name='val$cpt' value=".'"'.Requette_Recherche($tab_description[$GLOBALS["rech$cpt"]][1],$tab_description[$GLOBALS["rech$cpt"]][2],$champ,$special).'"'.">\n";
			  }
			}
			else
			{
     		  echo "   <input type='hidden' name='val$cpt' value=".'"'.Requette_Recherche($tab_description[$GLOBALS["rech$cpt"]][1],$tab_description[$GLOBALS["rech$cpt"]][2],$champ,$special).'"'.">\n";
			}
        echo "<BR>\n";
      }
      else
      {
        echo "   <input type='hidden' name='rech$cpt' value='0'>\n";
      }
    }
    echo "<BIG>Tri</BIG><BR>\n";
    for($cpt=1;$cpt<=3;$cpt++)
    {
      echo "   TRI$cpt<select name='tri$cpt'>\n";
      echo "<option value='0'>-</option>";
      for($cpt2=0;$cpt2<30;$cpt2++)
      {
        if($GLOBALS["aff$cpt2"]!=0)
        {
          echo "   <option value='".$GLOBALS["aff$cpt2"]."'>".$tab_description[$GLOBALS["aff$cpt2"]][0]."</option>\n";
        }
      }
      echo "   </select><br>\n";
    }
    echo "   <button type='submit'>Suite</button>\n";
    echo "  </form>\n";
  }
  
  function Affiche_formulaire_liste_5($No_Formulaire)
  {
    global $Type, $Sous_Type;
    $tab_description=Create_Table_Champs($No_Formulaire,$nb_champs);
    echo "  <form>\n";
    echo "   <input type='hidden' name='Type' value=$Type>\n";
    echo "   <input type='hidden' name='Sous_Type' value=$Sous_Type>\n";
    echo "<BIG>Champs à afficher</BIG><BR>\n";
    for($cpt=0;$cpt<30;$cpt++)
    {
     	if($GLOBALS["aff$cpt"]!=0)
     	{
        echo "   <input type='hidden' name='aff$cpt' value='".$GLOBALS["aff$cpt"]."'>\n";
     		echo $tab_description[$GLOBALS["aff$cpt"]][0]."<BR>\n";
      }
      else
      {
        echo "   <input type='hidden' name='aff$cpt' value='0'>\n";
      }
    }
    echo "<BIG>Champs de recherche</BIG><BR>\n";
    for($cpt=0;$cpt<10;$cpt++)
    {
     	if($GLOBALS["rech$cpt"]!=0)
     	{
        echo "   <input type='hidden' name='rech$cpt' value='".$GLOBALS["rech$cpt"]."'>\n";
     		echo "   <input type='hidden' name='val$cpt' value=".'"'.$GLOBALS["val$cpt"].'"'.">\n";
     		echo $tab_description[$GLOBALS["rech$cpt"]][0]." ".$GLOBALS["val$cpt"];
        echo "<BR>\n";
      }
      else
      {
        echo "   <input type='hidden' name='rech$cpt' value='0'>\n";
     		echo "   <input type='hidden' name='val$cpt' value=''>\n";
      }
    }
    echo "<BIG>Tri</BIG><BR>\n";
    for($cpt=1;$cpt<=3;$cpt++)
    {
      echo "   <input type='hidden' name='tri$cpt' value='".$GLOBALS["tri$cpt"]."'>\n";
      if($GLOBALS["tri$cpt"]!=0)
      {
       	echo $tab_description[$GLOBALS["tri$cpt"]][0]."<BR>\n";
      }
    }
    echo "<BIG>Sortie</BIG><BR>\n";
    echo "   <select name='Etape'>\n";
    echo "    <option value='5'>Écran</option>\n";
    echo "    <option value='6'>Fichier</option>\n";
    echo "    <option value='7'>PDF</option>\n";
    echo "    <option value='8'>Enregistrer</option>\n";
    echo "   </select><br>\n";
    echo "   <button type='submit'>Suite</button>\n";
    echo "  </form>\n";
  }
  
  function Cree_requette_liste($No_Formulaire)
  {
    $special_vhc = false;
    $tab_description=Create_Table_Champs($No_Formulaire,$nb_champs);
      if ($ligne_nom = TableData::getTableDataByNoRechercheAndNoVisu($No_Formulaire,0))
      {
        $requette = "SELECT ";
        $vide = true;
        for($cpt=0;$cpt<30;$cpt++)
        {
          if($GLOBALS["aff$cpt"]!=0)
          {
            if($vide)
            {
              $vide = false;
            }
            else
            {
              $requette .= ", ";
            }
            if (($tab_description[$GLOBALS["aff$cpt"]][2]==137)||($tab_description[$GLOBALS["aff$cpt"]][2]==126))
            {
              $requette .= "NoArticle AS Vendu";
            }
			else if ($tab_description[$GLOBALS["aff$cpt"]][2]==128)
			{
			  $special_vhc = true;
			  $requette .= 'GROUP_CONCAT(CONCAT(NoChasis,", ",MarqueModel," ",Model,", (",YEAR(DateCircul),")") SEPARATOR "<br />") AS Vehicules';
			}
			else
			{
			  if ($No_Formulaire == 1)
			  {
			    //echo $GLOBALS["aff$cpt"]."<br/>\n";
			    if (is_special_stock($GLOBALS["aff$cpt"]))
				{
                  $requette .= "stock".ID_GARAGE.".".$tab_description[$GLOBALS["aff$cpt"]][1];
				}
				else
				{
                  $requette .= $ligne_nom['NomTable'].".".$tab_description[$GLOBALS["aff$cpt"]][1];
				}
			  }
			  else
			  {
                $requette .= "T.".$tab_description[$GLOBALS["aff$cpt"]][1];
			  }
			}
          }
        }
		if ($No_Formulaire == 1)
		{
		  $requette .=" FROM " . $ligne_nom['NomTable'] . ", stock" . ID_GARAGE;
		}
		else if ($No_Formulaire == 4)
		{
          $requette .=" FROM " . $ligne_nom['NomTable'] . ID_GARAGE . " as T";
		}
		else
		{
          $requette .=" FROM " . $ligne_nom['NomTable'] . " as T";
		}
		if ($special_vhc)
		{
		  $requette .= " LEFT JOIN vehicules ON T.NoClient = vehicules.Client";
		}
        $vide = true;
		if ($special_vhc)
		{
		  //$requette .= " WHERE clients.NoClient = vehicules.Client";
		  //$vide = false;
		}
        for($cpt=0;$cpt<10;$cpt++)
        {
          if($GLOBALS["rech$cpt"]!=0)
          {
            if(!empty($GLOBALS["val$cpt"]))
            {
              if($vide)
              {
                $requette .= " WHERE ";
			    if ($No_Formulaire == 1)
				{
				  $requette .= "articles.NoArticleRR = stock".ID_GARAGE.".NoArticleRR AND "; 
				}
                $vide = false;
              }
              else
              {
                $requette .= " AND ";
              }
              $requette .= $GLOBALS["val$cpt"];
            }
          }
          else
          {
          }
        }
		if ($vide && ($No_Formulaire == 1))
		{
		  $requette .= " WHERE articles.NoArticleRR = stock".ID_GARAGE.".NoArticleRR";
		}
		
		if ($special_vhc)
		{
		  $requette .= " GROUP BY NoClient";
		}
        $vide = true;
        for($cpt=1;$cpt<=3;$cpt++)
        {
          if($GLOBALS["tri$cpt"]!=0)
          {
            if($vide)
            {
              $requette .= " ORDER BY ";
              $vide = false;
            }
            else
            {
              $requette .= ", ";
            }
			if ($No_Formulaire == 1)
			{
			  if (is_special_stock($GLOBALS["tri$cpt"]))
			  {
			    $requette .= "stock".ID_GARAGE.".";
			  }
			  else
			  {
			    $requette .= "articles.";
			  }
			}
            $requette .= $tab_description[$GLOBALS["tri$cpt"]][1];
          }
        }
        return $requette;
      }
  }
  
  function is_special_stock($pos)
  {
    switch ($pos)
	{
	  case 18:
	  case 19:
	  case 6:
	    return true;
	  break;
	}
    return false;
  }
  
  function Affiche_liste_ecran($No_Formulaire)
  {
    $requette=Cree_requette_liste($No_Formulaire);
    echo "$requette<BR>\n";
	//return;
    $tab_description=Create_Table_Champs($No_Formulaire,$nb_champs);
    echo "<TABLE border=1>\n";
    echo "<TR>";
    for($cpt=0;$cpt<30;$cpt++)
    {
     	if($GLOBALS["aff$cpt"]!=0)
     	{
     	  echo "<TH>".$tab_description[$GLOBALS["aff$cpt"]][0]."</TH>";
      }
	  }
	  echo "</TR>\n";
	  $resultat=Connection::executeRawQueryWithfetchAll($requette);
	  foreach($resultat as $ligne)
	  {
      echo "<TR>";
      for($cpt=0;$cpt<30;$cpt++)
      {
        if($GLOBALS["aff$cpt"]!=0)
        {
		  if($tab_description[$GLOBALS["aff$cpt"]][2]==128)
		  {
		    echo "<TD>".$ligne["Vehicules"]."</TD>";
		  }
		  else
		  {
			echo "<TD>".Cree_Entree_Type("",$tab_description[$GLOBALS["aff$cpt"]][2],$tab_description[$GLOBALS["aff$cpt"]][3],$ligne[$tab_description[$GLOBALS["aff$cpt"]][1]],AFFICHAGE)."</TD>";
		  }
        }
	    }
	    echo "</TR>\n";
	  }
    echo "</TABLE>\n";
  }
  
  function Affiche_liste_fichier($No_Formulaire)
  {
    $requette=Cree_requette_liste($No_Formulaire);
    $tab_description=Create_Table_Champs($No_Formulaire,$nb_champs);
    $fichier=fopen("listes/liste.xls","w");
    $vide=true;
    for($cpt=0;$cpt<30;$cpt++)
    {
      if($GLOBALS["aff$cpt"]!=0)
      {
        if($vide)
        {
          $vide=false;
        }
        else
        {
          fwrite($fichier,"\t");
        }
        fwrite($fichier,$tab_description[$GLOBALS["aff$cpt"]][0]);
      }
    }
    fwrite($fichier,"\n");
    $resultat=Connection::executeRawQueryWithfetchAll($requette);
	  foreach($resultat as $ligne)
    {
      $vide=true;
      for($cpt=0;$cpt<30;$cpt++)
      {
        if($GLOBALS["aff$cpt"]!=0)
        {
          if($vide)
          {
            $vide=false;
          }
          else
          {
            fwrite($fichier,"\t");
          }
		  if($tab_description[$GLOBALS["aff$cpt"]][2]==128)
		  {
		    $vhc = '"'.str_replace("<br />","\n",$ligne["Vehicules"]).'"';
		    fwrite($fichier,$vhc);
		  }
		  else
		  {
            fwrite($fichier,Cree_Entree_Type("",$tab_description[$GLOBALS["aff$cpt"]][2],$tab_description[$GLOBALS["aff$cpt"]][3],$ligne[$tab_description[$GLOBALS["aff$cpt"]][1]],AFFICHAGE));
		  }
        }
      }
      fwrite($fichier,"\n");
    }
    fclose($fichier);
    echo '<iframe height="0" width="0" src="listes/liste.xls"></iframe>'."\n";
  }
  
  function Affiche_liste_pdf($No_Formulaire)
  {
  }
  
  function Enregistrer_liste_1($No_Formulaire)
  {
    global $Type, $Sous_Type, $Etape;
    $tab_description=Create_Table_Champs($No_Formulaire,$nb_champs);
    echo "  <form>\n";
    echo "   <input type='hidden' name='Type' value=$Type>\n";
    echo "   <input type='hidden' name='Sous_Type' value=$Sous_Type>\n";
    echo "   <input type='hidden' name='Etape' value=".($Etape+1).">\n";
    echo "<BIG>Champs à afficher</BIG><BR>\n";
    for($cpt=0;$cpt<30;$cpt++)
    {
     	if($GLOBALS["aff$cpt"]!=0)
     	{
        echo "   <input type='hidden' name='aff$cpt' value='".$GLOBALS["aff$cpt"]."'>\n";
     		echo $tab_description[$GLOBALS["aff$cpt"]][0]."<BR>\n";
      }
      else
      {
        echo "   <input type='hidden' name='aff$cpt' value='0'>\n";
      }
    }
    echo "<BIG>Champs de recherche</BIG><BR>\n";
    for($cpt=0;$cpt<10;$cpt++)
    {
     	if($GLOBALS["rech$cpt"]!=0)
     	{
        echo "   <input type='hidden' name='rech$cpt' value='".$GLOBALS["rech$cpt"]."'>\n";
     		echo "   <input type='hidden' name='val$cpt' value=".'"'.$GLOBALS["val$cpt"].'"'.">\n";
     		echo $tab_description[$GLOBALS["rech$cpt"]][0]." ".$GLOBALS["val$cpt"];
        echo "<BR>\n";
      }
      else
      {
        echo "   <input type='hidden' name='rech$cpt' value='0'>\n";
     		echo "   <input type='hidden' name='val$cpt' value=''>\n";
      }
    }
    echo "<BIG>Tri</BIG><BR>\n";
    for($cpt=1;$cpt<=3;$cpt++)
    {
      echo "   <input type='hidden' name='tri$cpt' value='".$GLOBALS["tri$cpt"]."'>\n";
      if($GLOBALS["tri$cpt"]!=0)
      {
       	echo $tab_description[$GLOBALS["tri$cpt"]][0]."<BR>\n";
      }
    }
    echo "   Nom<input type='text' name='nom'>\n";
    echo "   <button type='submit'>Sauver</button>\n";
    echo "  </form>\n";
  }
  
  function Enregistrer_liste_2($No_Formulaire)
  {
    global $nom;
    $requette_liste=Cree_requette_liste($No_Formulaire);
    $requette="INSERT INTO listes (Type, Nom, Requette, Champs, Recherche, Valeur, Tri) VALUES ($No_Formulaire, '".addslashes($nom)."', '".addslashes($requette_liste)."', '";
    for($cpt=0;$cpt<30;$cpt++)
    {
      $requette.=$GLOBALS["aff$cpt"].":";
    }
    $requette.="', '";
    for($cpt=0;$cpt<10;$cpt++)
    {
      $requette.=$GLOBALS["rech$cpt"].":";
    }
    $requette.="', '";
    for($cpt=0;$cpt<10;$cpt++)
    {
      $requette.=addslashes($GLOBALS["val$cpt"]).":";
    }
    $requette.="', '";
    for($cpt=1;$cpt<=3;$cpt++)
    {
      $requette.=$GLOBALS["tri$cpt"].":";
    }
    $requette.="')";
    echo "$requette";
    Connection::executeRawQuery($requette);
  }

?>