<?php
  $Type_default="OV";
  switch($Etape)
  {
    case 0:
      Create_Recherche_Form(4,$NoVisu);
    break;
    case 1:
      Create_Recherche_Form(4,$NoVisu);
      Affiche_Resultat_Recherche_Form(4,$NoVisu);
    break;
    case 2:
      echo $NoCommande."<BR>\n";
      echo "<form name='info'>\n";
      echo "<input type='hidden' name='Type' value=$Type>\n";
      echo "<input type='hidden' name='Sous_Type' value=$Sous_Type>\n";
      echo "<input type='hidden' name='Etape' value='3'>\n";
      echo "<input type='hidden' name='NoCommande' value=$NoCommande>\n";
      echo "<input type='radio' name='Facture' value='1'> Facture vhc neuf<BR>\n";
      echo "<input type='radio' name='Facture' value='2'> Facture vhc occasion<BR>\n";
	  $resultat2=Employee::getActifEmployee();
	  echo "<SELECT name='Vendeur'>\n";
	  echo "<option value='0'>-</option>\n";
	  foreach($resultat2 as $ligne2)
	  {
	    if($ligne3=Client::getClientByNoClient($ligne2['NoClient']))
	    {
	      echo "<option value='".$ligne3['NoClient']."'";
          echo ">".$ligne3['Prenom']." ".$ligne3['Nom']."</option>\n";
        }
      }
      echo "</SELECT><BR>\n";
      echo "<button type='button' onclick='test_vendeur();'>Transformer</button>";
      //echo "<button type='submit'>Transformer</button>\n";
      echo "</form>\n";
    break;
    case 3:
      $ligne = Commande::getCommandeByNoCommande($NoCommande);
      $NoCommande2 = 0;
      if(isset($Facture))
      {
        echo "Facture<BR>\n";
        if($Facture==1)
          $Type_fact="FV";
        else
          $Type_fact="FO";
        $NoCommande2=Trouve_No_Commande_Libre($Type_fact);
        $requette2 = "INSERT INTO commande".ID_GARAGE." (NoCommande,Type,Genre,PersonneModif,NoClient,NoClient2,Vehicule,KmVhc,NoPlaque,DateCommande,DateSaisie,DateModification,MontantTotal,APayer,DateDelaisPayement,Vendeur) VALUES " .
                     "($NoCommande2, '$Type_fact', 'F', '$lg_user', ".$ligne['NoClient'].", ".$ligne['NoClient2'].", '".$ligne['Vehicule']."', ".$ligne['KmVhc'].", " .
                     "'".$ligne['NoPlaque']."', '" . date("Y-m-d") . "', '" . date("Y-m-d") . "', '" . date("Y-m-d") . "', ".$ligne['MontantTotal'].", ".$ligne['MontantTotal'].", '".date("Y-m-d",mktime(0,0,0,date("m"),date("d")+30,date("Y")))."',$Vendeur)";
        echo $requette2."<BR>\n";
        Connection::executeRawQuery($requette2);
        $resultat2 = CommandeArticle::getCommandeArticleByNoCommande($NoCommande);
        foreach($resultat2 as $ligne2)
        {
          $requette3 = "INSERT INTO commandearticles".ID_GARAGE." (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES " .
                       "($NoCommande2, ".$ligne2['Position'].", ".$ligne2['TypeData'].", '".addslashes($ligne2['NoArticle'])."', '".addslashes($ligne2['Description'])."', " .
                       $ligne2['TVA'].", ".$ligne2['GM'].", ".$ligne2['Quantite'].", ".$ligne2['PrixBase'].", ".$ligne2['Rabais'].", ".$ligne2['PrixMulti'].")";
                       Connection::executeRawQuery($requette3);
          echo $requette3."<BR>\n";
		  if ($ligne2['TypeData'] == 0)
		  {
		    $noArticleRR = Texte_Sans_Espace($ligne2['NoArticle']);
			if ($noArticleRR != "")
			{
	          $resultat_existe_stock = Stock::getStockByNoArticleRR(addslashes($noArticleRR));
              if ($resultat_existe_stock)
              { // UPDATE
	            $requette4 = "UPDATE stock".ID_GARAGE." SET EnStock = EnStock - (" . $ligne2['Quantite'] . ") WHERE NoArticleRR = '" . addslashes($noArticleRR) . "'";
              }
              else
              { // INSERT
	            $requette4="INSERT INTO stock".ID_GARAGE." (`NoArticleRR` ,`LieuxStock` ,`EnStock` ,`EnTransfert`) VALUES ('".addslashes($noArticleRR)."', NULL , -(".$ligne2['Quantite']."), 0);";
              }
              Connection::executeRawQuery($requette4);
			}
          }
          if(substr($ligne2['Description'],0,11)=="No châssis ");
          {
            $NoChasis=substr($ligne2['Description'],12);
            $requette3="UPDATE vehicules SET Status='C', PrixActuel='".$ligne2['PrixMulti']."', DateVente='".date("Y-m-d")."', Client=".$ligne['NoClient'].", Leasing=".$ligne['NoClient2'].", NoFacture=$NoCommande2 WHERE NoChasis='$NoChasis'";
			echo $requette3."<BR>\n";
			Connection::executeRawQuery($requette3);
			// Associe la reprise avec la facture occasion
			$ligne4=Vehicule::getVehiculeByNoChassis($NoChasis);
			if($ligne4['Status']=='O')
			{
			  if ($ligne5 = Vehicule::getVehiculeByNoChassisAndTypeAndCommandAssocie($NoChasis))
			  {
				$requette6 = "UPDATE commande".ID_GARAGE." SET NoCommandeAssocie = ".$ligne5["NoCommande"]." WHERE NoCommande = $NoCommande";
				Connection::executeRawQuery($requette6);
				$requette6 = "UPDATE commande".ID_GARAGE." SET NoCommandeAssocie = $NoCommande WHERE NoCommande = ".$ligne5["NoCommande"];
				Connection::executeRawQuery($requette6);        
			  }
			  else
			  {
				echo "alert('Aucune reprise pour ce vehicule disponible');\n";
			  }
			}
          }
        }
        echo "<script language=JavaScript>\n";
        echo "window.location.href='default.php?Type=4&Sous_Type=1&Etape=3&NoCommande=$NoCommande2&NoClient=".$ligne['NoClient']."&NoClient2=".$ligne['NoClient2']."'\n";
        echo "</script>\n";
      }
    break;
    case 4:
    break;
  }
?>
