<?php
  if (As_Right_Fonction(6,35))
  {
  $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));
	  echo "$Genre<br/>";
      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;
      }
      echo "$requette2<BR>\n";
      $resultat2 = Connection::executeRawQuery($requette2);
    }
  }
  $requette = "DELETE FROM commande".ID_GARAGE." WHERE NoCommande=$NoCommande";
  Connection::executeRawQuery($requette);
  echo "$requette<BR>\n";
  $requette = "DELETE FROM commandearticles".ID_GARAGE." WHERE NoCommande=$NoCommande";
  Connection::executeRawQuery($requette);
  echo "$requette<BR>\n";
  $requette = "DELETE FROM dbcash".ID_GARAGE." WHERE NoFacture=$NoCommande";
  Connection::executeRawQuery($requette);
  echo "$requette<BR>\n";
  }
?>
