<?php
  if(isset($Nb))
  {
    $tab_no_commande=array();
    $tab_position=array();
    $tab_quantite=array();
    $nb_reste=$Nb;
    $tmp=str_replace("-","",$NoArticle);
    $tmp=str_replace(" ","",$tmp);
    $resultat=Article::getArticleByNoArticleRR(addslashes($tmp));
    if($ligne= $resultat[0])
    {
      $NoArt=$ligne["NoArticle"];
      $resultat=Commande::getCommandeOuverte();
      foreach($resultat as $ligne)
      {
        if($nb_reste == 0) {
          break;
        }
        $resultat2=CommandeArticle::getCommandeArticleByNoCommandeAndNoArticle($ligne["NoCommande"], addslashes($NoArt));
        foreach($resultat2 as $ligne2)
        {
          if($nb_reste == 0) {
            break;
          }
          $reste=$ligne2["Quantite"]-$ligne2["NbRecu"];
          if($reste!=0)
          {
            $tab_no_commande[]=$ligne["NoCommande"];
            $tab_position[]=$ligne2["Position"];
            if($nb_reste>$reste)
            {
              $tab_quantite[]=$reste;
              $nb_reste-=$reste;
            }
            else
            {
              $tab_quantite[]=$nb_reste;
              $nb_reste=0;
            }
          }
        }
      }
    }
    if($nb_reste!=0)
    {
      echo "<big>Vous avez recu plus de pieces que vous en avez commandé.<BR>Commandé = ".($Nb-$nb_reste)."<BR>Recu = $Nb<BR>Transaction annulée<BR></big>";
	  echo "<script>beep(100, 60, 100);</script>";
    }
    else
    {
	  $noArticleRR = Texte_Sans_Espace($NoArt);
	  $resultat_existe_stock = Stock::getStockByNoArticleRR(addslashes($noArticleRR));
	  if ($resultat_existe_stock)
	  { // UPDATE
        $requette2="UPDATE stock".ID_GARAGE." SET EnTransfert=EnTransfert-$Nb, EnStock=EnStock+$Nb WHERE NoArticleRR='".addslashes($noArticleRR)."'";
	  }
	  else
	  { // INSERT
        $requette2="INSERT INTO stock".ID_GARAGE." (`NoArticleRR` ,`LieuxStock` ,`EnStock` ,`EnTransfert`) VALUES ('".addslashes($noArticleRR)."', NULL , +(".$Nb."), -(".$Nb."));";
	  }
      Connection::executeRawQuery($requette2);
      $cpt=0;
      foreach($tab_no_commande as $no_comm)
      {
        $requette2="UPDATE commandearticles".ID_GARAGE." SET NbRecu=NbRecu+".$tab_quantite[$cpt].", DateRecu='".date("Y-m-d")."' WHERE NoCommande=$no_comm AND Position=".$tab_position[$cpt];
        Connection::executeRawQuery($requette2);
        $resultat2=CommandeArticle::getCommandeArticleOuvertByNoCommande($no_comm);
	    $Version = Commande::getVersionByNoCommande($no_comm);
	    $Version += 1;
	    $IPClient = $_SERVER["REMOTE_ADDR"];
        if($ligne2=$resultat2[0])
        {
          $requette2="UPDATE commande".ID_GARAGE." SET Version=$Version, IPClient='$IPClient' WHERE NoCommande=$no_comm";
          Connection::executeRawQuery($requette2);
        }
        else
        {
          $requette2="UPDATE commande".ID_GARAGE." SET Recu='O', Version=$Version, IPClient='$IPClient' WHERE NoCommande=$no_comm";
          Connection::executeRawQuery($requette2);
        }
      $cpt++;
      }
      echo "<font color='#FF0000'>Reçu :".$Nb."X&nbsp;&nbsp;&nbsp;$NoArticle</font><BR>\n";
    }
  }
  echo "<big>Récéption commandes</big><br>\n";
  echo "<form name='info'>";
  echo "Quantité<input type='text' name='Nb' value='1' size=3 MAXLENGTH=3><BR>";
  echo "NoArticle<input type='text' name='NoArticle' value='' size=30 MAXLENGTH=30><BR>";
  echo "<input type='hidden' name='Type' value=$Type>";
  echo "<input type='hidden' name='Sous_Type' value=$Sous_Type>";
  echo "<input type='hidden' name='Etape' value='0'>";
  echo "</form>";
  echo "<script language=JavaScript>\n";
  echo "force_submit=true;\n";
  echo "this.document.forms['info'].elements[1].focus();\n";
  echo "</script>\n";
?>
