<?php
require 'database.inc.php';
header('Content-Type: text/html; charset=utf-8');
$annee = date('Y');
if (Test_User($lg_user, $lg_password) == TRUE) {
  switch ($Mode) {
    case 0:
      if ($Type == "O") {
        $requette = "SELECT * FROM commande" . ID_GARAGE . " WHERE Vehicule='$NoChasis' AND Type='RV' AND NoCommandeAssocie=0";
        $resultat = Connection::executeRawQueryWithfetchAll($requette);
        if ($ligne = $resultat[0]) {
          echo "alert('Vehicule déjà repris dans commande " . $ligne['NoCommande'] . "');\n";
          return;
        }
      } else {
        $requette = "SELECT * FROM commande" . ID_GARAGE . " WHERE Vehicule='$NoChasis' AND Type='VV' AND NoCommandeAssocie=0";
        $resultat = Connection::executeRawQueryWithfetchAll($requette);
        if ($ligne = $resultat[0]) {
          echo "alert('Vehicule déjà repris dans commande " . $ligne['NoCommande'] . "');\n";
          return;
        }
      }
      $ligne = Vehicule::getVehiculeByNoChassis($NoChasis);
      $err = 0;
      $NoArticle = $ligne['NoArticleAchat'];
      if (empty($NoArticle)) {
        $NoArticle = "ACH" . $Type;
      }
      if ($lignetest = Article::getArticleByNoArticle(addslashes($NoArticle))) {
      } else {
        $err++;
        echo "alert('No d\'article vente inexistant $NoArticle');\n";
      }
      if ($annee >= 2010) {
        echo "TVA=-" . get_config('TVA') . ";\n";
      } else {
        echo "TVA=0;\n";
      }
      echo "var must_abord = false;\n";
      echo "Prix=prompt('Prix achat','" . $ligne['PrixEntree'] . "');\n";
      echo "if(Prix==null)\n";
      echo "{\n";
      echo "  exit;\n";
      echo "}\n";
      echo "Km=prompt('Km','" . $ligne['Km'] . "');\n";
      echo "if(Km==null)\n";
      echo "{\n";
      echo "  exit;\n";
      echo "}\n";
      if ($err == 0) {
        echo "var texte_multilignes;\n";
        echo "var cr='\\n';\n";
        if ($Type == "O")
          echo "texte_multilignes='Reprise véhicule';\n";
        else
          echo "texte_multilignes='Achat véhicule';\n";
        //echo "add_new_text();\n";
        echo "texte_multilignes=texte_multilignes + cr + '" . $ligne['MarqueModel'] . " " . $ligne['Model'] . " " . $ligne['Couleur'] . "';\n";
        //echo "add_new_text();\n";
        if ($Type == "O") {
          echo "texte_multilignes=texte_multilignes + cr + 'année " . substr($ligne['DateCircul'], 0, 4) . ", Km '+Km;\n";
          //echo "add_new_text();\n";
        }
        echo "texte_multilignes=texte_multilignes + cr + 'no matricule " . Cree_Entree_Type('', 17, 0, $ligne['Matr'], AFFICHAGE) . "';\n";
        echo "document.info.Texte_libre.value=texte_multilignes;\n";
        echo "add_new_text_newfact();\n";
        echo "document.info.NoArticle.value='" . addslashes($NoArticle) . "';\n";
        echo "document.info.Description.value='No chasis " . addslashes($ligne['NoChasis']) . "';\n";
        echo "document.info.Prix.value = Prix;\n";
        echo "document.info.TVA.value = TVA;\n";
      }
      if ($ligne2 = Article::getArticleByNoArticle(addslashes($NoArticle))) {
        echo "document.info.GM.value = '" . $ligne2['GroupeMarchandise'] . "';\n";
      } else {
        echo "document.info.GM.value = '0';\n";
      }
      echo "document.info.Rabais.value = '0.00';\n";
      echo "document.info.Quantite.value = '1.00';\n";
      echo "add_new_newfact(false);\n";
      echo "commande_ajax_javascript('import_vhc_achat_newfact.php?Mode=1&NoCommande=$NoCommande&NoChasis=$NoChasis&Type=$Type&NoClient=$NoClient&Prix='+Prix+'&TVA='+TVA+'&Km='+Km);\n";
      break;
    case 1:
      $requette = "UPDATE vehicules SET Status='$Type', PrixEntree='$Prix', DateAchat='" . date("Y-m-d") . "', Vendeur=$NoClient, NoFacture=$NoCommande, Km=$Km, TVAAchat=$TVA WHERE NoChasis='$NoChasis'";
      Connection::executeRawQuery($requette);
      $requette = "UPDATE commande" . ID_GARAGE . " SET Vehicule='$NoChasis' WHERE NoCommande='$NoCommande'";
      Connection::executeRawQuery($requette);
      break;
  }
}
