<?php
require 'database.inc.php';
header('Content-Type: text/html; charset=utf-8');
if (Test_User($lg_user, $lg_password) == TRUE) {
  $tmp = str_replace("-", "", $NoArticle);
  $tmp = str_replace(" ", "", $tmp);
  if ($tmp == "") {
    exit;
  }
  $prefix_achat = Config::getPrefixAchat();
  $len_prefix_achat = strlen($prefix_achat);
  if ($AchatMarchandise == 1) {
    if ($len_prefix_achat == 0) {
    } else if (substr(strtoupper($tmp), 0, $len_prefix_achat) != strtoupper($prefix_achat)) {
      echo "alert('Dans les achats marchandise seul les articles commançant par " . $prefix_achat . " sont acceptés !');\n";
      echo "document.info.NoArticle.value='';\n";
      echo "document.info.NoArticle.select();\n";
      return;
    }
  } else {
    if ($len_prefix_achat == 0) {
    } else if (substr(strtoupper($tmp), 0, $len_prefix_achat) == strtoupper($prefix_achat)) {
      echo "alert('Dans les factures seul les articles ne commançant pas par " . $prefix_achat . " sont acceptés !');\n";
      echo "document.info.NoArticle.value='';\n";
      echo "document.info.NoArticle.select();\n";
      return;
    }
  }
  $ok = 0;
  if ($resultat = Article::getArticleByNoArticleRR(addslashes($tmp))) {
    $nb = count($resultat);
    if ($nb == 1) {
      $ligne = $resultat[0];
      $noArticleRR = $ligne["NoArticleRR"];
      $resultat_existe_stock = Stock::getStockByNoArticleRR($noArticleRR);
      if ($resultat_existe_stock) { // stock existe
        $requette = "SELECT * FROM articles, stock" . ID_GARAGE . " WHERE articles.NoArticleRR = '" . $noArticleRR . "' AND articles.NoArticleRR=stock" . ID_GARAGE . ".NoArticleRR";
      } else { // pas de stock
        $requette = "SELECT *, 0 as EnStock FROM articles WHERE articles.NoArticleRR LIKE '" . $noArticleRR . "'";
      }
      $resultat = Connection::executeRawQueryWithfetchAll($requette);
      $ligne = $resultat[0];
      echo Affecte_Resultat_Article($ligne, $NoClient);
    } else if ($nb > 1) {
      echo "alert('Plusieurs articles avec le même numéro $tmp. Il faut corriger ceci');";
    } else {
      if ($resultat = Article::getArticleByNoArticleRR(addslashes($tmp))) {
        $nb = count($resultat);
        if ($nb == 1) // nb == 1
        {
          $ligne = $resultat[0];
          $noArticleRR = $ligne["NoArticleRR"];
          $resultat_existe_stock = Stock::getStockByNoArticleRR($noArticleRR);
          if ($resultat_existe_stock) { // stock existe
            $requette = "SELECT * FROM articles, stock" . ID_GARAGE . " WHERE articles.NoArticleRR = '" . $noArticleRR . "' AND articles.NoArticleRR=stock" . ID_GARAGE . ".NoArticleRR";
          } else { // pas de stock
            $requette = "SELECT *, 0 as EnStock FROM articles WHERE articles.NoArticleRR LIKE '" . $noArticleRR . "'";
          }
          $resultat = Connection::executeRawQueryWithfetchAll($requette);
          $ligne = $resultat[0];
          echo Affecte_Resultat_Article($ligne, $NoClient);
        } else if ($nb > 1) {
          echo "window.open('recherche_article_filter.php?NoArticle=" . urlencode($tmp) . "&NoClient=$NoClient&Commande=$Commande&AchatMarchandise=$AchatMarchandise&NbArticles=$NbArticles', '', 'width=300, height=100, resizable=yes, toolbar=no, scrollbars=yes, status=no');\n";
        } else {
          echo "document.info.NoArticle.value='';";
          echo "document.info.Description.value='';";
          echo "document.info.Prix.value ='0.00';";
          echo "document.info.TVA.value = '0.00';";
          echo "document.info.GM.value = '0';";
          echo "document.info.Stock.value = '0';";
          echo "document.info.Rabais.value = '0.00';";
          echo "document.info.Quantite.value = '0.00';";
          echo "document.info.NoArticle.focus();";
        }
      }
    }
  }
}
