<?php
  switch($Etape)
  {
    case 0:
      $requette="SELECT * FROM commande".ID_GARAGE." WHERE MontantTotal=0 AND Paye!='O' ORDER BY NoCommande";
      $resultat=Connection::executeRawQueryWithfetchAll($requette);
      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 border=1><TR><TH>No</TH><TH>Client</TH><TH>nb lignes</TH><TH>supprimer</TH></TR>\n";
      foreach($resultat as $ligne)
      {
        echo "<TR><TD>".$ligne['NoCommande']."</TD><TD>".$ligne['NoClient'];
        $resultat2=CommandeArticle::getCommandeArticleByNoCommande($ligne['NoCommande']);
        $ligne2=count($resultat2);
        if($ligne2==0)
        {
          echo "</TD><TD>vide</TD>";
        }
        else
        {
          echo "</TD><TD>".$ligne2."</TD>";
        }
        echo "<TD><input type='checkbox' name='c".$ligne['NoCommande']."' value='1'";
        if($ligne['NoClient']==0 && $ligne2==0)
        {
          echo "checked";
        }
        echo "></TD></TR>\n";
      }
      echo "</TABLE>\n";
      echo "   <button type='submit'>Supprimer</button>\n";
      echo "  </form>\n";
    break;
    case 1:
      $requettel="SELECT * FROM commande".ID_GARAGE." WHERE MontantTotal=0 AND Paye!='O' ORDER BY NoCommande";
      $resultatl=Connection::executeRawQueryWithfetchAll($requettel);
      foreach($resultatl as $lignel)
      {
        if(isset(${"c".$lignel['NoCommande']}))
        {
          $NoCommande=$lignel['NoCommande'];
          $ligne=Commande::getCommandeByNoCommande($NoCommande);
          $Genre=$ligne['Genre'];
          $Date=substr($ligne['DateCommande'],0,4);
          $resultat = CommandeArticle::getCommandeArticleByNoCommande($NoCommande);
          foreach($resultat as $ligne)
          {
            if (!empty($ligne['NoArticle']))
            {
			  $noArticleRR = Texte_Sans_Espace($ligne['NoArticle']);
			  $resultat_existe_stock = Stock::getStockByNoArticleRR(addslashes($noArticleRR));
              switch ($Genre)
              {
                case "F":
                case "B":
                case "G":
				  if ($resultat_existe_stock)
				  { // UPDATE
                    $requette2 = "UPDATE stock".ID_GARAGE." SET EnStock = EnStock + (" . $ligne['Quantite'] . ") WHERE NoArticleRR = '" . addslashes($noArticleRR) . "'";
				  }
				  else
				  { // INSERT
					$requette2="INSERT INTO stock".ID_GARAGE." (`NoArticleRR` ,`LieuxStock` ,`EnStock` ,`EnTransfert`) VALUES ('".addslashes($noArticleRR)."', NULL , +(".$ligne['Quantite']."), 0);";
				  }
                break;
                case "N":
                case "A":
				  if ($resultat_existe_stock)
				  { // UPDATE
                    $requette2 = "UPDATE stock".ID_GARAGE." SET EnStock = EnStock - (" . $ligne['Quantite'] . ") WHERE NoArticleRR = '" . addslashes($noArticleRR) . "'";
				  }
				  else
				  { // INSERT
					$requette2="INSERT INTO stock".ID_GARAGE." (`NoArticleRR` ,`LieuxStock` ,`EnStock` ,`EnTransfert`) VALUES ('".addslashes($noArticleRR)."', NULL , -(".$ligne['Quantite']."), 0);";
				  }
                break;
                case "C":
				  if ($resultat_existe_stock)
				  { // UPDATE
                    $requette2 = "UPDATE stock".ID_GARAGE." SET EnStock = EnStock - (" . $ligne['NbRecu'] . "), EnTransfert = EnTransfert - (".($ligne['Quantite']-$ligne['NbRecu']).") WHERE NoArticleRR = '" . addslashes($noArticleRR) . "'";
				  }
				  else
				  { // INSERT
					$requette2="INSERT INTO stock".ID_GARAGE." (`NoArticleRR` ,`LieuxStock` ,`EnStock` ,`EnTransfert`) VALUES ('".addslashes($noArticleRR)."', NULL , - (" . $ligne['NbRecu'] . "), - (".($ligne['Quantite']-$ligne['NbRecu'])."));";
				  }
                break;
              }
              $resultat2 = Connection::executeRawQuery($requette2);
            }
          }
          $requette = "DELETE FROM commande".ID_GARAGE." WHERE NoCommande=$NoCommande";
          Connection::executeRawQuery($requette);
          $requette = "DELETE FROM commandearticles".ID_GARAGE." WHERE NoCommande=$NoCommande";
          Connection::executeRawQuery($requette);
          $requette = "DELETE FROM dbcash".ID_GARAGE." WHERE NoFacture=$NoCommande";
          Connection::executeRawQuery($requette);
        }
      }
    break;
  }
?>
