<?php
require 'database.inc.php';
if (Test_User($lg_user, $lg_password) == TRUE) {
  $table_textes =  array();
  $table_textes2 =  array();
  $table_groupes = array();
  $table_articles = array();
  if ($ligne = Atelier::getAtelierByNoAtelier($NoAtelier)) {
    $NoArticleMecano = "A1";
    if ($ligne["Mecano"] != 0) {
      if ($ligneMecano = Employee::getEmployeeByClient($ligne["Mecano"])) {
        $arrArt = explode(";", $ligneMecano["ArticlesAtelier"]);
        $NoArticleMecano = $arrArt[0];
      }
    }

    $requetteRemplacement = "SELECT voa.* FROM atelier_option" . ID_GARAGE . " AS ao, vhc_option AS vo, vhc_option_action_remplacement AS voa WHERE ao.Id_atelier = " . $NoAtelier . " AND ao.Id_option = vo.Id AND vo.Id_option = voa.Id_option";
    $resultatRemplacement = Connection::executeRawQueryWithfetchAll($requetteRemplacement);
    $req_Remplacement = "";
    foreach ($resultatRemplacement as $ligneRemplacement) {
      if ($req_Remplacement != "") $req_Remplacement .= ", ";
      $req_Remplacement .= $ligneRemplacement["Id_action"];
    }
    echo $requetteRemplacement . "<br/>\n";
    echo $req_Remplacement . "<br/>\n";
    if ($req_Remplacement != "") {
      $req_Remplacement = " AND action.Id NOT IN (" . $req_Remplacement . ")";
    }
    $requette2 = "SELECT DISTINCT action_type.Description, action_type.Avec_Article, action.Texte, action.Id AS Id_Action, action_type.Valeurs, action.Id_Rubrique, action_rubrique.Nom_Rubrique FROM service_action, action, action_type, action_rubrique WHERE service_action.Id_Service = " . $ligne['Id_Service'] . " AND (service_action.Id_Action = action.Id$req_Remplacement) AND service_action.Id_Action_Type = action_type.Id AND action.Id_Rubrique = action_rubrique.Id ORDER BY action.Id_Rubrique, action.Position";
    echo $requette2 . "<br/>\n";
    //$requette2 = "SELECT action_type.Description, action_type.Avec_Article, action.Texte, service_action.Id_Action, action_type.Valeurs, action.Id_Rubrique, action_rubrique.Nom_Rubrique FROM service_action, action, action_type, action_rubrique WHERE service_action.Id_Service = ".$ligne['Id_Service']." AND service_action.Id_Action = action.Id AND service_action.Id_Action_Type = action_type.Id AND action.Id_Rubrique = action_rubrique.Id ORDER BY action.Id_Rubrique, action.Position";
    $resultat2 = Connection::executeRawQueryWithfetchAll($requette2);
    foreach ($resultat2 as $ligne2) {
      $avec_article = false;
      if (!isset($table_groupes[$ligne2["Nom_Rubrique"]])) {
        $table_groupes[$ligne2["Nom_Rubrique"]] = array();
      }
      if (isset(${"act_" . $ligne2["Id_Action"]})) {
        $valeures = explode(";", $ligne2["Valeurs"]);
        if (count($valeures) > 1) {
          $val = $valeures[${"act_exec_" . $ligne2["Id_Action"]}];
          $pos = strpos($val, "(1)");
          if ($pos === false) {
          } else {
            $avec_article = true;
          }
          $val = str_replace("(0)", "", $val);
          $val = str_replace("(1)", "", $val);
          if (!isset($table_groupes[$ligne2["Nom_Rubrique"]][$val])) {
            $table_groupes[$ligne2["Nom_Rubrique"]][$val] = array();
          }
          $table_groupes[$ligne2["Nom_Rubrique"]][$val][] = $ligne2["Texte"];
          $table_textes[] = "$val " . $ligne2["Texte"];
        } else {
          if ($ligne2["Avec_Article"] == 1) {
            $avec_article = true;
          }
          if (!isset($table_groupes[$ligne2["Nom_Rubrique"]][$valeures[0]])) {
            $table_groupes[$ligne2["Nom_Rubrique"]][$valeures[0]] = array();
          }
          $table_groupes[$ligne2["Nom_Rubrique"]][$valeures[0]][] = $ligne2["Texte"];
          $table_textes[] = $valeures[0] . " " . $ligne2["Texte"];
        }
        if ($avec_article) {
          $requette3 = "SELECT service_article.* FROM service_vhc, service_article WHERE service_vhc.Id = " . $ligne['Id_Service'] . " AND service_vhc.Id_vhc = service_article.Id_Vhc AND service_article.Id_Action = " . $ligne2["Id_Action"];
          $resultat3 = Connection::executeRawQueryWithfetchAll($requette3);
          if ($ligne3 = $resultat3[0]) {
            $no_article = $ligne3["NoArticle"];
            $pos_p = strpos($no_article, "(");
            if ($pos_p !== false) {
              $no_article = substr($no_article, 0, $pos_p);
            }
            $table_articles[] = array(${"act_quantite_" . $ligne2["Id_Action"]}, $no_article);
          }
        } else {
        }
      }
    }
  }
  foreach ($table_groupes as $rubrique => $groupe) {
    $table_textes2[] = $rubrique;
    foreach ($groupe as $action => $textes) {
      $tmp = $action . " ";
      $first = true;
      foreach ($textes as $texte) {
        if (strlen($texte)) {
          if (strlen($tmp) + 3 + strlen($texte) > 120) {
            $table_textes2[] = $tmp;
            $tmp = $action . " ";
            $first = true;
          }
          if ($first) {
            $tmp .= ": ";
            $first = false;
          } else {
            $tmp .= ", ";
          }
          $tmp .= $texte;
        }
      }
      $table_textes2[] = $tmp;
    }
  }
  echo "<pre>\n";
  print_r($table_textes);
  print_r($table_articles);
  print_r($table_groupes);
  print_r($table_textes2);
  $position = 0;
  if ($ligne["NoCommande"] != 0) {
    $NoCommande = $ligne["NoCommande"];
    $resultat2 = CommandeArticle::getCommandeArticleByNoCommande($NoCommande);
    foreach ($resultat2 as $ligne2) {
      if ($ligne2["Position"] > $position) {
        $position = $ligne2["Position"];
      }
    }
  } else {
    //$NoCommande="1234567";
    $NoCommande = Trouve_No_Commande_Libre($Type);
    $no_plaque = "";
    $NoChasis = $ligne["NoChasis"];
    if (!empty($NoChasis)) {
      $ligne2 = Vehicule::getVehiculeByNoChassis(addslashes($NoChasis));
      $no_plaque = $ligne2['NoPlaque'];
    }
    $NoClient = $ligne["NoClient"];
    $Km = $ligne["Km"];
    $requette3 = "INSERT INTO commande" . ID_GARAGE . " (NoCommande,Type,Genre,PersonneModif,NoClient,Vehicule,KmVhc,NoPlaque,DateCommande,DateModification,DateDelaisPayement)" .
      "values ($NoCommande, '$Type', '$Genre', '$lg_user', '$NoClient', '" . addslashes($NoChasis) . "', '$Km', '$no_plaque', '" . date("Y-m-d") . "', '" . date("Y-m-d") . "', '" . date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") + 30, date("Y"))) . "')";
    echo $requette3 . "\n";
    Connection::executeRawQuery($requette3);
  }
  $requette3 = "UPDATE atelier" . ID_GARAGE . " SET NoCommande = $NoCommande, Etape = 2 WHERE NoAtelier = $NoAtelier";
  echo $requette3 . "\n";
  Connection::executeRawQuery($requette3);
  $ligne3 = ServiceVHC::getServiceVHCByID($ligne["Id_Service"]);
  $position++;
  if ($ligne3['Nom'] != "") {
    $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, $position, 6, '', '" . addslashes($ligne3["Nom"]) . "', 0, 0, 0, 0, 0, 0)";
  } else {
    $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, $position, 6, '', '" . addslashes("Service " . $ligne3["Km"]) . "', 0, 0, 0, 0, 0, 0)";
  }
  echo $requette3 . "\n";
  Connection::executeRawQuery($requette3);
  foreach ($table_textes2 as $texte) {
    $position++;
    $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, $position, 1, '', '" . addslashes($texte) . "', 0, 0, 0, 0, 0, 0)";
    echo $requette3 . "\n";
    Connection::executeRawQuery($requette3);
  }
  $ligne5 = Client::getClientByNoClient($NoClient);
  $total = 0;
  $requetteMo = "SELECT * FROM service_vhc WHERE Id = " . $ligne["Id_Service"] . " AND Id_vhc = " . $ligne["Id_Vhc"];
  $resultatMo = Connection::executeRawQueryWithfetchAll($requetteMo);
  if ($ligneMo =  $resultatMo[0]) {
    $NoArticle = $NoArticleMecano; // TODO No article MO
    if ($ligne4 = Article::getArticleByNoArticleRR(addslashes($NoArticle))) {
      $position++;
      $TVA = $ligne4["TVA"];
      $Quantite = $ligneMo["Duree"];
      if ($ligne4["PrixPropre"] != 0) {
        $prix_u = $ligne4["PrixPropre"];
      } else {
        $prix_u = $ligne4["PrixImportateur"];
      }
      $prix_m = $prix_u * $Quantite;
      if ($TVA > 0) {
        $prix_m = $prix_m * (1 + ($TVA / 100));
      }
      $rabais = $ligne4["RabaisType" . $ligne5["TypeRabais"]];
      $prix_m = $prix_m * ((100 + $rabais) / 100);
      $prix_m = Arrondi_5_ct($prix_m);
      $total += $prix_m;
      $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
        "$NoCommande, $position, 0, '" . addslashes($ligne4["NoArticle"]) . "', '" . addslashes($ligne4["Description"]) . "', $TVA, " . $ligne4["GroupeMarchandise"] . ", $Quantite, $prix_u, $rabais, $prix_m)";
      echo $requette3 . "\n";
      Connection::executeRawQuery($requette3);
      $noArticleRR = Texte_Sans_Espace($ligne4["NoArticle"]);
      $resultat_existe_stock = Stock::getStockByNoArticleRR(addslashes($noArticleRR));
      if ($resultat_existe_stock) { // UPDATE
        $requette5 = "UPDATE stock" . ID_GARAGE . " SET EnStock = EnStock - $Quantite WHERE NoArticleRR = '" . addslashes($noArticleRR) . "'";
      } else { // INSERT
        $requette5 = "INSERT INTO stock" . ID_GARAGE . " (`NoArticleRR` ,`LieuxStock` ,`EnStock` ,`EnTransfert`) VALUES ('" . addslashes($noArticleRR) . "', NULL , -($Quantite), 0);";
      }
      echo $requette5 . "<br/>\n";
      Connection::executeRawQuery($requette5);
      $position++;
      $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
        "$NoCommande, $position, 5, '', '" . addslashes("Total main d'oeuvre") . "', 0, 0, 0, 0, 0, 0)";
      echo $requette3 . "\n";
      Connection::executeRawQuery($requette3);
    }
  }
  foreach ($table_articles as $article) {
    $NoArticle = $article[1];
    $NoArticle = str_replace(array(" ", "-"), "", $NoArticle);
    if ($ligne4 = Article::getArticleByNoArticleRR(addslashes($NoArticle))) {
      $position++;
      $TVA = $ligne4["TVA"];
      $Quantite = $article[0];
      if ($ligne4["PrixPropre"] != 0) {
        $prix_u = $ligne4["PrixPropre"];
      } else {
        $prix_u = $ligne4["PrixImportateur"];
      }
      $prix_m = $prix_u * $Quantite;
      if ($TVA > 0) {
        $prix_m = $prix_m * (1 + ($TVA / 100));
      }
      $rabais = $ligne4["RabaisType" . $ligne5["TypeRabais"]];
      $prix_m = $prix_m * ((100 + $rabais) / 100);
      $prix_m = Arrondi_5_ct($prix_m);
      $total += $prix_m;
      $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
        "$NoCommande, $position, 0, '" . addslashes($ligne4["NoArticle"]) . "', '" . addslashes($ligne4["Description"]) . "', $TVA, " . $ligne4["GroupeMarchandise"] . ", $Quantite, $prix_u, $rabais, $prix_m)";
      echo $requette3 . "\n";
      Connection::executeRawQuery($requette3);
      $noArticleRR = Texte_Sans_Espace($ligne4["NoArticle"]);
      $resultat_existe_stock = Stock::getStockByNoArticleRR(addslashes($noArticleRR));
      if ($resultat_existe_stock) { // UPDATE
        $requette5 = "UPDATE stock" . ID_GARAGE . " SET EnStock = EnStock - $Quantite WHERE NoArticleRR = '" . addslashes($noArticleRR) . "'";
      } else { // INSERT
        $requette5 = "INSERT INTO stock" . ID_GARAGE . " (`NoArticleRR` ,`LieuxStock` ,`EnStock` ,`EnTransfert`) VALUES ('" . addslashes($noArticleRR) . "', NULL , -($Quantite), 0);";
      }
      echo $requette5 . "<br/>\n";
      Connection::executeRawQuery($requette5);
    }
  }
  if (count($table_articles) > 0) {
    $position++;
    $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, $position, 5, '', 'Total pièces', 0, 0, 0, 0, 0, 0)";
    echo $requette3 . "\n";
    Connection::executeRawQuery($requette3);
  }
  $position++;
  $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
    "$NoCommande, $position, 7, '', 'Total service', 0, 0, 0, 0, 0, 0)";
  echo $requette3 . "\n";
  Connection::executeRawQuery($requette3);
  $requetteOptions = "SELECT vo.*, so.Nom FROM atelier_option" . ID_GARAGE . " AS ao, service_option AS so, vhc_option AS vo WHERE ao.Id_atelier = " . $NoAtelier . " AND vo.Id_option = so.Id AND ao.Id_option = vo.Id";
  $resultatOptions = Connection::executeRawQueryWithfetchAll($requetteOptions);
  foreach ($resultatOptions as $ligneOption) {
    // Options --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    $table_textes =  array();
    $table_textes2 =  array();
    $table_groupes = array();
    $table_articles = array();
    $position++;
    $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, $position, 6, '', 'Option " . addslashes($ligneOption["Nom"]) . "', 0, 0, 0, 0, 0, 0)";
    echo $requette3 . "\n";
    Connection::executeRawQuery($requette3);
    $requette2 = "SELECT at.Description, at.Avec_Article, a.Texte, a.Id AS Id_Action, at.Valeurs, a.Id_Rubrique, ar.Nom_Rubrique, voa.Id_vhc FROM vhc_option_action AS voa, action AS a, action_type AS at, action_rubrique AS ar WHERE voa.Id_vhc = " . $ligne["Id_Vhc"] . " AND voa.Id_option = " . $ligneOption["Id_option"] . " AND voa.Id_Action = a.Id AND a.Id_Type = at.Id AND a.Id_Rubrique = ar.Id ORDER BY a.Id_Rubrique, a.Position";
    echo $requette2 . "<br/>\n";
    $resultat2 = Connection::executeRawQueryWithfetchAll($requette2);
    foreach ($resultat2 as $ligne2) {
      $avec_article = false;
      if (!isset($table_groupes[$ligne2["Nom_Rubrique"]])) {
        $table_groupes[$ligne2["Nom_Rubrique"]] = array();
      }
      if (isset(${"act_" . $ligne2["Id_Action"]})) {
        $valeures = explode(";", $ligne2["Valeurs"]);
        if (count($valeures) > 1) {
          $val = $valeures[${"act_exec_" . $ligne2["Id_Action"]}];
          $pos = strpos($val, "(1)");
          if ($pos === false) {
          } else {
            $avec_article = true;
          }
          $val = str_replace("(0)", "", $val);
          $val = str_replace("(1)", "", $val);
          if (!isset($table_groupes[$ligne2["Nom_Rubrique"]][$val])) {
            $table_groupes[$ligne2["Nom_Rubrique"]][$val] = array();
          }
          $table_groupes[$ligne2["Nom_Rubrique"]][$val][] = $ligne2["Texte"];
          $table_textes[] = "$val " . $ligne2["Texte"];
        } else {
          if ($ligne2["Avec_Article"] == 1) {
            $avec_article = true;
          }
          if (!isset($table_groupes[$ligne2["Nom_Rubrique"]][$valeures[0]])) {
            $table_groupes[$ligne2["Nom_Rubrique"]][$valeures[0]] = array();
          }
          $table_groupes[$ligne2["Nom_Rubrique"]][$valeures[0]][] = $ligne2["Texte"];
          $table_textes[] = $valeures[0] . " " . $ligne2["Texte"];
        }
        if ($avec_article) {
          $requette3 = "SELECT service_option_article.* FROM service_option_article WHERE service_option_article.Id_Vhc = " . $ligne2["Id_vhc"] . " AND service_option_article.Id_Action = " . $ligne2["Id_Action"];
          $resultat3 = Connection::executeRawQueryWithfetchAll($requette3);
          echo "ICI " . $requette3 . "\n";
          if ($ligne3 = $resultat3[0]) {
            $no_article = $ligne3["NoArticle"];
            $pos_p = strpos($no_article, "(");
            if ($pos_p !== false) {
              $no_article = substr($no_article, 0, $pos_p);
            }
            $table_articles[] = array(${"act_quantite_" . $ligne2["Id_Action"]}, $no_article);
          }
        } else {
        }
      }
    }
    foreach ($table_groupes as $rubrique => $groupe) {
      $table_textes2[] = $rubrique;
      foreach ($groupe as $action => $textes) {
        $tmp = $action . " ";
        $first = true;
        foreach ($textes as $texte) {
          if (strlen($texte)) {
            if (strlen($tmp) + 3 + strlen($texte) > 120) {
              $table_textes2[] = $tmp;
              $tmp = $action . " ";
              $first = true;
            }
            if ($first) {
              $tmp .= ": ";
              $first = false;
            } else {
              $tmp .= ", ";
            }
            $tmp .= $texte;
          }
        }
        $table_textes2[] = $tmp;
      }
    }
    print_r($table_textes);
    print_r($table_articles);
    print_r($table_groupes);
    print_r($table_textes2);
    //$requette3="SELECT * FROM service_vhc WHERE Id = ".$ligne["Id_Service"];
    //$resultat3=mysql_query($requette3);
    //$ligne3=mysql_fetch_array($resultat3);
    //$position++;
    //if ($ligne3['Nom']!="")
    //{
    //  $requette3="INSERT INTO commandearticles".ID_GARAGE." (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
    //            "$NoCommande, $position, 1, '', '".addslashes($ligne3["Nom"])."', 0, 0, 0, 0, 0, 0)";
    //}
    //else
    //{
    //  $requette3="INSERT INTO commandearticles".ID_GARAGE." (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
    //            "$NoCommande, $position, 1, '', '".addslashes("Service ".$ligne3["Km"])."', 0, 0, 0, 0, 0, 0)";
    //}
    //echo $requette3."\n";
    //mysql_query($requette3);
    foreach ($table_textes2 as $texte) {
      $position++;
      $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
        "$NoCommande, $position, 1, '', '" . addslashes($texte) . "', 0, 0, 0, 0, 0, 0)";
      echo $requette3 . "\n";
      Connection::executeRawQuery($requette3);
    }
    $ligne5 = Client::getClientByNoClient($NoClient);
    //$total=0;
    echo $requetteMo . "\n";
    if ($ligneMo = VHCOptions::getByIdVHCAndIdOption($ligne["Id_Vhc"], $ligneOption["Id_option"])) {
      if ($ligneMo["Duree"] != 0) {
        $NoArticle = $NoArticleMecano; // TODO No article MO
        if ($ligne4 = Article::getArticleByNoArticleRR(addslashes($NoArticle))) {
          $position++;
          $TVA = $ligne4["TVA"];
          $Quantite = $ligneMo["Duree"];
          if ($ligne4["PrixPropre"] != 0) {
            $prix_u = $ligne4["PrixPropre"];
          } else {
            $prix_u = $ligne4["PrixImportateur"];
          }
          $prix_m = $prix_u * $Quantite;
          if ($TVA > 0) {
            $prix_m = $prix_m * (1 + ($TVA / 100));
          }
          $rabais = $ligne4["RabaisType" . $ligne5["TypeRabais"]];
          $prix_m = $prix_m * ((100 + $rabais) / 100);
          $prix_m = Arrondi_5_ct($prix_m);
          $total += $prix_m;
          $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
            "$NoCommande, $position, 0, '" . addslashes($ligne4["NoArticle"]) . "', '" . addslashes($ligne4["Description"]) . "', $TVA, " . $ligne4["GroupeMarchandise"] . ", $Quantite, $prix_u, $rabais, $prix_m)";
          echo $requette3 . "\n";
          Connection::executeRawQuery($requette3);
          $noArticleRR = Texte_Sans_Espace($ligne4["NoArticle"]);
          $resultat_existe_stock = Stock::getStockByNoArticleRR(addslashes($noArticleRR));
          if ($resultat_existe_stock) { // UPDATE
            $requette5 = "UPDATE stock" . ID_GARAGE . " SET EnStock = EnStock - $Quantite WHERE NoArticleRR = '" . addslashes($noArticleRR) . "'";
          } else { // INSERT
            $requette5 = "INSERT INTO stock" . ID_GARAGE . " (`NoArticleRR` ,`LieuxStock` ,`EnStock` ,`EnTransfert`) VALUES ('" . addslashes($noArticleRR) . "', NULL , -($Quantite), 0);";
          }
          echo $requette5 . "<br/>\n";
          Connection::executeRawQuery($requette5);
          $position++;
          $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
            "$NoCommande, $position, 5, '', '" . addslashes("Total main d'oeuvre option") . "', 0, 0, 0, 0, 0, 0)";
          echo $requette3 . "\n";
          Connection::executeRawQuery($requette3);
        }
      }
    }
    foreach ($table_articles as $article) {
      $NoArticle = $article[1];
      $NoArticle = str_replace(array(" ", "-"), "", $NoArticle);
      if ($ligne4 = Article::getArticleByNoArticleRR(addslashes($NoArticle))) {
        $position++;
        $TVA = $ligne4["TVA"];
        $Quantite = $article[0];
        if ($ligne4["PrixPropre"] != 0) {
          $prix_u = $ligne4["PrixPropre"];
        } else {
          $prix_u = $ligne4["PrixImportateur"];
        }
        $prix_m = $prix_u * $Quantite;
        if ($TVA > 0) {
          $prix_m = $prix_m * (1 + ($TVA / 100));
        }
        $rabais = $ligne4["RabaisType" . $ligne5["TypeRabais"]];
        $prix_m = $prix_m * ((100 + $rabais) / 100);
        $prix_m = Arrondi_5_ct($prix_m);
        $total += $prix_m;
        $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
          "$NoCommande, $position, 0, '" . addslashes($ligne4["NoArticle"]) . "', '" . addslashes($ligne4["Description"]) . "', $TVA, " . $ligne4["GroupeMarchandise"] . ", $Quantite, $prix_u, $rabais, $prix_m)";
        echo $requette3 . "\n";
        Connection::executeRawQuery($requette3);
        $noArticleRR = Texte_Sans_Espace($ligne4["NoArticle"]);
        $resultat_existe_stock = Stock::getStockByNoArticleRR(addslashes($noArticleRR));
        if ($resultat_existe_stock) { // UPDATE
          $requette5 = "UPDATE stock" . ID_GARAGE . " SET EnStock = EnStock - $Quantite WHERE NoArticleRR = '" . addslashes($noArticleRR) . "'";
        } else { // INSERT
          $requette5 = "INSERT INTO stock" . ID_GARAGE . " (`NoArticleRR` ,`LieuxStock` ,`EnStock` ,`EnTransfert`) VALUES ('" . addslashes($noArticleRR) . "', NULL , -($Quantite), 0);";
        }
        echo $requette5 . "<br/>\n";
        Connection::executeRawQuery($requette5);
      }
    }
    if (count($table_articles) > 0) {
      $position++;
      $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
        "$NoCommande, $position, 5, '', 'Total pièces option', 0, 0, 0, 0, 0, 0)";
      echo $requette3 . "\n";
      Connection::executeRawQuery($requette3);
    }
    $position++;
    $requette3 = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, $position, 7, '', 'Total option', 0, 0, 0, 0, 0, 0)";
    echo $requette3 . "\n";
    Connection::executeRawQuery($requette3);
  }

  $ligne0 = Commande::getCommandeByNoCommande($NoCommande);
  $Version = $ligne0["Version"];
  $Version += 1;
  $IPClient = $_SERVER["REMOTE_ADDR"];
  $requette6 = "UPDATE commande" . ID_GARAGE . " SET MontantTotal = MontantTotal + $total, APayer = APayer + $total, Version=$Version, IPClient='$IPClient' WHERE NoCommande = $NoCommande";
  echo $requette6 . "\n";
  Connection::executeRawQuery($requette6);
  echo "Total = $total";
  echo "</pre>\n";
  echo "<script language=JavaScript>\n";
  echo "this.opener.location='default.php?Type=4&Sous_Type=1&Etape=3&New=0&NoCommande=$NoCommande&NoClient=$NoClient';\n";
  echo "window.close();\n";
  echo "</script>\n";
}
