<?php
  $Type_default="DP";
  switch($Etape)
  {
    case 0:
      Create_Recherche_Form(4,$NoVisu);
	  echo $_SESSION["memres4"];
    break;
    case 1:
      Create_Recherche_Form(4,$NoVisu);
      Affiche_Resultat_Recherche_Form(4,$NoVisu);
    break;
    case 2:
      $ligne = Commande::getCommandeByNoCommande($NoCommande);;
	  if ($ligne["Paye"] == "O")
	  {
        echo "<big>".$NoCommande." déjà transformé en facture</big>\n";
	  }
	  else
	  {
        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='checkbox' name='Facture' CHECKED> Facture<BR>\n";
        echo "<input type='checkbox' name='Rdv' CHECKED> Rdv atelier<BR>\n";
        echo "<button type='submit'>Transformer</button>\n";
        echo "</form>\n";
	  }
    break;
    case 3:
      $requette0 = "UPDATE commande".ID_GARAGE." SET Paye = 'O' WHERE NoCommande=$NoCommande";
      $resultat0 = Connection::executeRawQuery($requette0);
      $ligne = Commande::getCommandeByNoCommande($NoCommande);
      $NoCommande2 = 0;
      if(isset($Facture))
      {
        echo "Facture<BR>\n";
        $NoCommande2=Trouve_No_Commande_Libre("FD");
        $requette2 = "INSERT INTO commande".ID_GARAGE." (NoCommande,Type,Genre,PersonneModif,NoClient,NoClient2,Vehicule,KmVhc,NoPlaque,DateCommande,DateSaisie,DateModification,MontantTotal,APayer,DateDelaisPayement) VALUES " .
                     "($NoCommande2, 'FD', '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")))."')";
        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(isset($Rdv))
      {
        echo "Rdv<BR>\n";
        if($ligne2 = Client::getClientByNoClient($ligne['NoClient']))
        {
          $Nom = $ligne2['Nom'];
          $Prenom = $ligne2['Prenom'];
          if(!empty($ligne2['Natel']))
            $Telephone = $ligne2['Natel'];
          else if(!empty($ligne2['TelPriv']))
            $Telephone = $ligne2['TelPriv'];
          else if(!empty($ligne2['TelProf']))
            $Telephone = $ligne2['TelProf'];
          else
            $Telephone = "";
        }
        else
        {
          $Nom = "";
          $Prenom = "";
          $Telephone = "";
        }
        if($ligne2 = Vehicule::getVehiculeByNoChassis(addslashes($ligne['Vehicule'])))
        {
          $Vehicule = $ligne2['MarqueModel']." ".$ligne2['Model'];
          $Km = $ligne2['Km'];
        }
        else
        {
          $Vehicule = "";
          $Km = "";
        }
        $curr_date = date("Y-m-d");
        $requette2 = "INSERT INTO atelier".ID_GARAGE." (Date, Heure, NoClient, Nom, Prenom, Telephone, NoChasis, Vehicule, Km, Restitution, Etape, NoCommande) " .
                     "VALUES ('$curr_date', '08:00:00', ".$ligne['NoClient'].", '$Nom', '$Prenom', '$Telephone', '".$ligne['Vehicule']."', '$Vehicule', " .
                     "'$Km', '$curr_date 18:00:00', 0, $NoCommande2)";
        Connection::executeRawQuery($requette2);
        echo $requette2."<BR>\n";
        if($ligne2=Atelier::getAtelierByDateNoClientNoChassisNoCommande($curr_date, $ligne['NoClient'], $ligne['Vehicule'], $NoCommande2))
        {
          $NoAtelier=$ligne2['NoAtelier'];
          echo "<script language=JavaScript>\n";
          echo "window.open('atelier.php?NoAtelier=$NoAtelier&new=0', '', 'top=0, left=0, width=800, height=600');\n";
          echo "window.location.href='default.php?Type=5&Sous_Type=2&NoVisu=0'\n";
          echo "</script>\n";
        }
      }
      else
      {
        if(isset($Facture))
        {
          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;
  }
?>
