<?php
  switch($Etape)
  {
    case 0:
      if ($resultat = DefaultRabais::getAll())
      {
        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 "   <select name='CodeRabais'>\n";
        echo "   <option value=" . '"Tous"' . ">Tous</option>\n";
        foreach($resultat as $ligne)
        {
          echo "    <option value=" . '"' . htmlspecialchars($ligne['CodeRabais']) . '"' . ">" . htmlspecialchars($ligne['CodeRabais']) . "</option>\n";
        }
        echo "   </select><BR>\n";
        echo "   <button type='submit'>Suite</button>\n";
        echo "  </form>\n";
      }
    break;
    case 1:
      set_time_limit ( 300 );
      if ($CodeRabais == "Tous")
      {
        if ($resultat = DefaultRabais::getAll())
        {
          foreach($resultat as $ligne)
          {
            $resultat2=TypeRabais::getActiveTypeRabais();            
			$vide=true;
			$valeurs="";
			foreach($resultat2 as $ligne2)
			{
			  if($vide)
			  {
			    $vide=false;
			  }
			  else
			  {
			    $valeurs .= ", ";
			  }
			  $valeurs .= "RabaisType".$ligne2[0]."=".$ligne["RabaisType".$ligne2[0]];
			}
            $requette3="UPDATE articles SET $valeurs WHERE CodeRabais='".$ligne["CodeRabais"]."'";
	        echo $requette3."<br />";
          Connection::executeRawQuery($requette3);
          }
        }
      }
      else 
      {
      $resultat=Article::getArticleByCodeRabais($CodeRabais);
      $ligne=count($resultat);
      echo "CR=$CodeRabais NB=".$ligne;
      echo "  <form method='POST' action='default.php'>\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 "   <input type='hidden' name='CodeRabais' value='$CodeRabais'>\n";
      if($ligne>200)
      {
        echo "   <input type='hidden' name='Tous' value='1'>\n";
      }
      else
      {
        echo "   <input type='hidden' name='Tous' value='0'>\n";
        $resultat=Article::getArticleByCodeRabais($CodeRabais);
        echo "<TABLE border=1><TR><TD>Modif</TD><TH>No</TH><TH>Desc</TH></TR>\n";
        foreach($resultat as $ligne)
        {
          echo "<TR><TD><input type='checkbox' name='CH".$ligne['NoArticleRR']."' value='1' checked></TD><TD>".$ligne['NoArticleRR']."</TD><TD>".$ligne['Description']."</TD></TR>\n";
        }
        echo "</TABLE>\n";
      }
      echo "   <button type='submit'>Terminer</button>\n";
      echo "  </form>\n";
      }
    break;
    case 2:
      set_time_limit ( 300 );
      $resultat=TypeRabais::getActiveTypeRabais();
      $ligne2=DefaultRabais::getByCodeRabais($CodeRabais);
      $vide=true;
      $valeurs="";
      foreach($resultat as $ligne)
      {
        if($vide)
        {
          $vide=false;
        }
        else
        {
          $valeurs .= ", ";
        }
        $valeurs .= "RabaisType".$ligne[0]."=".$ligne2["RabaisType".$ligne[0]];
      }
      if($Tous==1)
      {
        $requette2="UPDATE articles SET $valeurs WHERE CodeRabais='$CodeRabais'";
	echo $requette2;
  Connection::executeRawQuery($requette2);
      }
      else
      {
        $resultat=Article::getArticleByCodeRabais($CodeRabais);
        foreach($resultat as $ligne)
        {
          if(isset(${"CH".$ligne['NoArticleRR']}))
          {
            $requette2="UPDATE articles SET $valeurs WHERE NoArticle='".$ligne['NoArticle']."'";
            Connection::executeRawQuery($requette2);
          }
        }
      }
      echo "TERMINÉ<BR>\n";
    break;
  }
?>
