<SCRIPT language=JavaScript src='popup.js' type=text/javascript></SCRIPT>
<?php
require 'database.inc.php';
function get_imprimante_location()
{
  return Config::getImprimanteLocation();
}

function create_commande_acompte($vhc, $Start, $acompte)
{
  global $lg_user, $NoClient, $ModePayement;
  $NoCommande = Trouve_No_Commande_Libre("FL");
  $date = date("Y-m-d");
  if (empty($NoClient)) {
    $nc = "NULL";
  } else {
    $nc = $NoClient;
  }
  $requette = "INSERT INTO commande" . ID_GARAGE . " (NoCommande,Type,Genre,PersonneModif,NoClient,DateCommande,DateModification,MontantTotal,APayer,DateDelaisPayement) VALUES " .
    "($NoCommande, 'FL', 'F', '$lg_user', $nc, '$date', '$date', 0, -$acompte, '" . date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") + 30, date("Y"))) . "')";
  //echo $requette."<BR />\n";
  if ($resultat = Connection::executeRawQuery($requette)) {
    $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, 1, 1, '', 'Acompte Location $acompte CHF payé le " . date("d-m-Y") . "', 0, 0, 0, 0, 0, 0)";
    Connection::executeRawQuery($requette);
    $no_caisse = 1;
    $mode_payement = 1;
    $TabModePayement = explode(" ", $ModePayement);
    if (count($TabModePayement) == 2) {
      $mode_payement = $TabModePayement[0];
      $no_caisse = $TabModePayement[1];
    }
    $requette = "INSERT INTO dbcash" . ID_GARAGE . " (NoFacture, PersonneEncaissement, Date, Somme, TVA, ModePayement, NoCaisse) VALUES" .
      " ($NoCommande, '$lg_user', '$date " . date("H:i") . ":00', $acompte, -" . get_config('TVA') . ", $mode_payement, $no_caisse)";
    //echo $requette . "<BR />\n";
    Connection::executeRawQuery($requette);
    $no_cash = Connection::getConnection()->lastInsertId();
    $requette = "UPDATE location" . ID_GARAGE . " SET NoCommande = $NoCommande, Acompte = '$acompte' WHERE NoChasis='" . addslashes($vhc) . "' AND Debut='$Start'";
    //echo $requette . "<BR />\n";
    Connection::executeRawQuery($requette);
    print_quitance($no_cash);
  }
  return $NoCommande;
}
function create_commande_fact($vhc, $Start)
{
  global $lg_user, $NoClient, $date_debut, $new_jour_debut, $new_mois_debut, $new_annee_debut, $new_heure_debut, $new_minutes_debut, $date_fin,
    $new_jour_fin, $new_mois_fin, $new_annee_fin, $new_heure_fin, $new_minutes_fin, $nb_jours, $tarif, $total, $new_tarif_type, $KmRetour, $KmDepart, $rabais;
  $NoCommande = Trouve_No_Commande_Libre("FL");
  $requette = "INSERT INTO commande" . ID_GARAGE . " (NoCommande,Type,Genre,PersonneModif,NoClient,DateCommande,DateModification,DateDelaisPayement) VALUES " .
    "($NoCommande, 'FL', 'F', '$lg_user', '$NoClient', '" . date("Y-m-d") . "', '" . date("Y-m-d") . "', '" . date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") + 30, date("Y"))) . "')";
  //echo $requette;
  if ($resultat = Connection::executeRawQuery($requette)) {
    $ligne2 = Vehicule::getVehiculeByNoChassis($vhc);
    $ligne3 = VehiculesLocation::getVehiculeLocationByNoChassis($vhc);
    $ligne4 = Article::getArticleByNoArticle($ligne3['ArticleLocation']);
    $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, 1, 1, '', 'Location " . $ligne2['Model'] . " " . $ligne2['NoPlaque'] . "', 0, 0, 0, 0, 0, 0)";
    $resultat2 = Connection::executeRawQuery($requette);
    $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, 2, 1, '', 'Du " . Cree_Entree_Type("", 2, 0, $date_debut, AFFICHAGE) . " " . Chiffre_2_Car($new_heure_debut) . ":" . Chiffre_2_Car($new_minutes_debut) . "', 0, 0, 0, 0, 0, 0)";
    $resultat2 = Connection::executeRawQuery($requette);
    $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, 3, 1, '', 'au " . Cree_Entree_Type("", 2, 0, $date_fin, AFFICHAGE) . " " . Chiffre_2_Car($new_heure_fin) . ":" . Chiffre_2_Car($new_minutes_fin) . "', 0, 0, 0, 0, 0, 0)";
    $resultat2 = Connection::executeRawQuery($requette);
    $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, 4, 0, '" . $ligne3['ArticleLocation'] . "', 'Location', -" . get_config('TVA') . ", " . $ligne4['GroupeMarchandise'] . ", " . Min_Zero($nb_jours) . ", " . Min_Zero($tarif) . ", $rabais, " . Min_Zero($total) . ")";
    $resultat2 = Connection::executeRawQuery($requette);
    $noArticleRR = Texte_Sans_Espace($ligne3['ArticleLocation']);
    $resultat_existe_stock = Stock::getStockByNoArticleRR(addslashes($noArticleRR));
    if ($resultat_existe_stock) { // UPDATE
      $requette = "UPDATE stock" . ID_GARAGE . " SET EnStock = EnStock - (" . Min_Zero($nb_jours) . ") WHERE NoArticleRR = '" . addslashes($noArticleRR) . "'";
    } else { // INSERT
      $requette = "INSERT INTO stock" . ID_GARAGE . " (`NoArticleRR` ,`LieuxStock` ,`EnStock` ,`EnTransfert`) VALUES ('" . addslashes($noArticleRR) . "', NULL , -(" . Min_Zero($nb_jours) . "), 0);";
    }
    Connection::executeRawQuery($requette);
    $diff1 = $KmRetour - $KmDepart;
    $KmUnit = $ligne3[db_kil($new_tarif_type)];
    $KmSup = $ligne3[db_sup($new_tarif_type)];
    $diff2 = $KmUnit * $nb_jours;
    $diff3 = $diff1 - $diff2;
    if ($diff3 > 0 && $KmSup > 0) {
      $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
        "$NoCommande, 5, 1, '', 'Km départ $KmDepart, Km retour $KmRetour, Km parcourus $diff1', 0, 0, 0, 0, 0, 0)";
      $resultat2 = Connection::executeRawQuery($requette);
      $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
        "$NoCommande, 6, 1, '', 'Km unitaires autorisés $KmUnit, nb unités $nb_jours, Km autorisé $diff2', 0, 0, 0, 0, 0, 0)";
      $resultat2 = Connection::executeRawQuery($requette);
      $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
        "$NoCommande, 7, 1, '', 'Km supplemetaires $diff3, prix au km supplémentaire $KmSup', 0, 0, 0, 0, 0, 0)";
      $resultat2 = Connection::executeRawQuery($requette);
      $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
        "$NoCommande, 8, 0, '" . $ligne3['ArticleKm'] . "', 'KmSup', -" . get_config('TVA') . ", 0, " . Min_Zero($diff3) . ", " . Min_Zero($KmSup) . ", 0, " . Arrondi_5_ct($diff3 * $KmSup) . ")";
      $resultat2 = Connection::executeRawQuery($requette);
      $noArticleRR = Texte_Sans_Espace($ligne3['ArticleKm']);
      $resultat_existe_stock = Stock::getStockByNoArticleRR(addslashes($noArticleRR));
      if ($resultat_existe_stock) { // UPDATE
        $requette = "UPDATE stock" . ID_GARAGE . " SET EnStock = EnStock - (" . Min_Zero($KmSup) . ") WHERE NoArticleRR = '" . addslashes($noArticleRR) . "'";
      } else { // INSERT
        $requette = "INSERT INTO stock" . ID_GARAGE . " (`NoArticleRR` ,`LieuxStock` ,`EnStock` ,`EnTransfert`) VALUES ('" . addslashes($noArticleRR) . "', NULL , -(" . Min_Zero($KmSup) . "), 0);";
      }
      Connection::executeRawQuery($requette);
      $icitot = $total + Arrondi_5_ct($diff3 * $KmSup);
      Connection::executeRawQuery("UPDATE commande" . ID_GARAGE . " SET MontantTotal = " . Min_Zero($icitot) . ", APayer = " . Min_Zero($icitot) . " WHERE NoCommande = $NoCommande");
    }
    Connection::executeRawQuery("UPDATE commande" . ID_GARAGE . " SET MontantTotal = " . Min_Zero($total) . ", APayer = " . Min_Zero($total) . " WHERE NoCommande = $NoCommande");
    $requette = "UPDATE location" . ID_GARAGE . " SET NoCommande = $NoCommande, Facture = 1 WHERE NoChasis='" . addslashes($vhc) . "' AND Debut='$Start'";
    Connection::executeRawQuery($requette);
  } else {
    echo "KO";
  }
  return $NoCommande;
}
function add_commande_fact($vhc, $Start, $NoCommande)
{
  global $lg_user, $NoClient, $date_debut, $new_jour_debut, $new_mois_debut, $new_annee_debut, $new_heure_debut, $new_minutes_debut, $date_fin,
    $new_jour_fin, $new_mois_fin, $new_annee_fin, $new_heure_fin, $new_minutes_fin, $nb_jours, $tarif, $total, $new_tarif_type, $KmRetour, $KmDepart, $rabais;
  if ($ligne_commande = Commande::getCommandeByNoCommande($NoCommande)) {
    $ligne2 = Vehicule::getVehiculeByNoChassis($vhc);
    $ligne3 = VehiculesLocation::getVehiculeLocationByNoChassis($vhc);
    $ligne4 = Article::getArticleByNoArticle($ligne3['ArticleLocation']);
    $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, 2, 1, '', 'Location " . $ligne2['Model'] . " " . $ligne2['NoPlaque'] . "', 0, 0, 0, 0, 0, 0)";
    $resultat2 = Connection::executeRawQuery($requette);
    $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, 3, 1, '', 'Du " . Cree_Entree_Type("", 2, 0, $date_debut, AFFICHAGE) . " " . Chiffre_2_Car($new_heure_debut) . ":" . Chiffre_2_Car($new_minutes_debut) . "', 0, 0, 0, 0, 0, 0)";
    $resultat2 = Connection::executeRawQuery($requette);
    $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, 4, 1, '', 'au " . Cree_Entree_Type("", 2, 0, $date_fin, AFFICHAGE) . " " . Chiffre_2_Car($new_heure_fin) . ":" . Chiffre_2_Car($new_minutes_fin) . "', 0, 0, 0, 0, 0, 0)";
    $resultat2 = Connection::executeRawQuery($requette);
    $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
      "$NoCommande, 5, 0, '" . $ligne3['ArticleLocation'] . "', 'Location', -" . get_config('TVA') . ", " . $ligne4['GroupeMarchandise'] . ", " . Min_Zero($nb_jours) . ", " . Min_Zero($tarif) . ", $rabais, " . Min_Zero($total) . ")";
    $resultat2 = Connection::executeRawQuery($requette);
    $noArticleRR = Texte_Sans_Espace($ligne3['ArticleLocation']);
    $resultat_existe_stock = Stock::getStockByNoArticleRR(addslashes($noArticleRR));
    if ($resultat_existe_stock) { // UPDATE
      $requette = "UPDATE stock" . ID_GARAGE . " SET EnStock = EnStock - (" . Min_Zero($nb_jours) . ") WHERE NoArticleRR = '" . addslashes($noArticleRR) . "'";
    } else { // INSERT
      $requette = "INSERT INTO stock" . ID_GARAGE . " (`NoArticleRR` ,`LieuxStock` ,`EnStock` ,`EnTransfert`) VALUES ('" . addslashes($noArticleRR) . "', NULL , -(" . Min_Zero($nb_jours) . "), 0);";
    }
    Connection::executeRawQuery($requette);
    $diff1 = $KmRetour - $KmDepart;
    $KmUnit = $ligne3[db_kil($new_tarif_type)];
    $KmSup = $ligne3[db_sup($new_tarif_type)];
    $diff2 = $KmUnit * $nb_jours;
    $diff3 = $diff1 - $diff2;
    $ligne0 = Commande::getCommandeByNoCommande($NoCommande);
    $Version = $ligne0["Version"];
    $Version += 1;
    $IPClient = $_SERVER["REMOTE_ADDR"];
    if ($diff3 > 0 && $KmSup > 0) {
      $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
        "$NoCommande, 6, 1, '', 'Km départ $KmDepart, Km retour $KmRetour, Km parcourus $diff1', 0, 0, 0, 0, 0, 0)";
      $resultat2 = Connection::executeRawQuery($requette);
      $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
        "$NoCommande, 7, 1, '', 'Km unitaires autorisés $KmUnit, nb unités $nb_jours, Km autorisé $diff2', 0, 0, 0, 0, 0, 0)";
      $resultat2 = Connection::executeRawQuery($requette);
      $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
        "$NoCommande, 8, 1, '', 'Km supplemetaires $diff3, prix au km supplémentaire $KmSup', 0, 0, 0, 0, 0, 0)";
      $resultat2 = Connection::executeRawQuery($requette);
      $requette = "INSERT INTO commandearticles" . ID_GARAGE . " (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti) VALUES (" .
        "$NoCommande, 9, 0, '" . $ligne3['ArticleKm'] . "', 'KmSup', -" . get_config('TVA') . ", 0, " . Min_Zero($diff3) . ", " . Min_Zero($KmSup) . ", 0, " . Arrondi_5_ct($diff3 * $KmSup) . ")";
      $resultat2 = Connection::executeRawQuery($requette);
      $noArticleRR = Texte_Sans_Espace($ligne3['ArticleKm']);
      $resultat_existe_stock = Stock::getStockByNoArticleRR(addslashes($noArticleRR));
      if ($resultat_existe_stock) { // UPDATE
        $requette = "UPDATE stock" . ID_GARAGE . " SET EnStock = EnStock - (" . Min_Zero($KmSup) . ") WHERE NoArticleRR = '" . addslashes($noArticleRR) . "'";
      } else { // INSERT
        $requette = "INSERT INTO stock" . ID_GARAGE . " (`NoArticleRR` ,`LieuxStock` ,`EnStock` ,`EnTransfert`) VALUES ('" . addslashes($noArticleRR) . "', NULL , -(" . Min_Zero($KmSup) . "), 0);";
      }
      Connection::executeRawQuery($requette);
      $icitot = $total + Arrondi_5_ct($diff3 * $KmSup);
      Connection::executeRawQuery("UPDATE commande" . ID_GARAGE . " SET MontantTotal = " . Min_Zero($icitot) . ", APayer = APayer + " . Min_Zero($icitot) . ", Version=$Version, IPClient='$IPClient' WHERE NoCommande = $NoCommande");
    } else {
      Connection::executeRawQuery("UPDATE commande" . ID_GARAGE . " SET MontantTotal = " . Min_Zero($total) . ", APayer = APayer + " . Min_Zero($total) . ", Version=$Version, IPClient='$IPClient' WHERE NoCommande = $NoCommande");
    }
    $requette = "UPDATE location" . ID_GARAGE . " SET Facture = 1 WHERE NoChasis='" . addslashes($vhc) . "' AND Debut='$Start'";
    Connection::executeRawQuery($requette);
  }
}
function print_contrat_location($NoChasis, $Debut, $nb_copies)
{
  require('../config/ficheatt.php');
  if ($resultat = Location::getByNoChassisAndDateDebutExact(addslashes($NoChasis), $Debut)) {
    if ($ligne = $resultat[0]) {
      $pdf = new FICHEATT('P', 'mm', 'A4');
      $pdf->SetTitle("Fiche atelier");
      $pdf->SetSubject("Fiche atelier");
      $pdf->SetAuthor("Laurent");
      $pdf->SetCreator("Laurent");
      $pdf->SetDisplayMode("fullpage");
      //$pdf->Open();
      $pdf->AddPage();
      $Adresse = "";
      $MarqueModel = "";
      $NoPlaque = "";
      $Etat = "";
      if ($ligne['NoClient'] != 0) {
        if ($ligne2 = Client::getClientByNoClient($ligne['NoClient'])) {
          $Adresse = $ligne2['Adresse'] . " " . $ligne2['NPA'] . " " . $ligne2['Localite'];
        }
      }
      if ($ligne2 = Vehicule::getVehiculeByNoChassis(addslashes($NoChasis))) {
        $MarqueModel = $ligne2['MarqueModel'] . " " . $ligne2['Model'] . " " . $ligne2['Couleur'];
        $NoPlaque = $ligne2['NoPlaque'];
      }
      $Debut = $ligne['DebutEffectif'];
      $Fin = $ligne['Fin'];
      if ($ligne2 = VehiculesLocation::getVehiculeLocationByNoChassis(addslashes($NoChasis))) {
        $Etat = $ligne2['EtatVhc'];
        $Caution = $ligne2['Caution'];
        $NoPlaque = $ligne2['NoPlaque'];
        $TypeVhc = $ligne2['Categorie'];
        switch ($ligne['TypeTarif']) {
          case 1:
            $KmUnit = $ligne2['Kil1_2'];
            $PrixKmSup = $ligne2['Sup1_2'];
            break;
          case 2:
            $KmUnit = $ligne2['Kil3_5'];
            $PrixKmSup = $ligne2['Sup3_5'];
            break;
          case 3:
            $KmUnit = $ligne2['Kil6_9'];
            $PrixKmSup = $ligne2['Sup6_9'];
            break;
          case 4:
            $KmUnit = $ligne2['Kil10_12'];
            $PrixKmSup = $ligne2['Sup10_12'];
            break;
          case 5:
            $KmUnit = $ligne2['Kil13_15'];
            $PrixKmSup = $ligne2['Sup13_15'];
            break;
          default:
            $KmUnit = 0;
            $PrixKmSup = 0;
            break;
        }
      }
      $pdf->addContractLoc($ligne['Nom'], $ligne['Prenom'], $ligne['Telephone'], $Adresse, $MarqueModel, $NoChasis, $NoPlaque, "", Cree_Entree_Type("", 2, 0, $Debut, AFFICHAGE), substr($Debut, 11, 5), Cree_Entree_Type("", 2, 0, $Fin, AFFICHAGE), substr($Fin, 11, 5), $ligne['KmDepart'], $Etat, $ligne['TypeTarif'], $ligne['Tarif'], $ligne['NbUnites'], $ligne['Rabais'], $ligne['Total'], $KmUnit, $PrixKmSup, $Caution, $ligne['TypeCaution'], $ligne['NoCC'], $ligne['ExpCC'], $TypeVhc);
      $printer = get_imprimante_location();
      if ($printer == "ecran" || $nb_copies == 3) {
        header('Content-type: application/pdf');
        header('Content-Disposition: inline; filename="' . $NoCommande . '.pdf"');
        $pdf->Output();
      } else {
        $pdf->Output("../files/atelier/loc.pdf", "F");
        $cmd = "lp -d contratloc ../files/atelier/loc.pdf";
        for ($cpt = 0; $cpt < $nb_copies; $cpt++) {
          exec($cmd);
        }
        unlink("../files/atelier/loc.pdf");
      }
    }
  }
}
if (Test_User($lg_user, $lg_password) == TRUE) {
  /* ON VERIFIE SI AUCUNE LOCATION n'est définie sur le même intarvalle de temps */
  //echo "Etape = ".$etape;
  $old_acompte = 0;
  $err = 0;
  $annee = date("Y");
  $date_debut = Decode_Entree_Type($date_debut, 2) . " " . Chiffre_2_Car($new_heure_debut) . ":" . Chiffre_2_Car($new_minutes_debut) . ":00";
  $date_fin = Decode_Entree_Type($date_fin, 2) . " " . Chiffre_2_Car($new_heure_fin) . ":" . Chiffre_2_Car($new_minutes_fin) . ":00";
  //$date_debut="$new_annee_debut-".Chiffre_2_Car($new_mois_debut)."-".Chiffre_2_Car($new_jour_debut)." ".Chiffre_2_Car($new_heure_debut) . ":" . Chiffre_2_Car($new_minutes_debut) . ":00";
  //$date_fin="$new_annee_fin-".Chiffre_2_Car($new_mois_fin)."-".Chiffre_2_Car($new_jour_fin)." ".Chiffre_2_Car($new_heure_fin) . ":" . Chiffre_2_Car($new_minutes_fin) . ":00";
  $date_effectif_debut = "$de_annee-" . Chiffre_2_Car($de_mois) . "-" . Chiffre_2_Car($de_jour) . " " . Chiffre_2_Car($de_heure) . ":" . Chiffre_2_Car((floor($de_minute / 15)) * 15) . ":00";
  $date_effectif_fin = "$re_annee-" . Chiffre_2_Car($re_mois) . "-" . Chiffre_2_Car($re_jour) . " " . Chiffre_2_Car($re_heure) . ":" . Chiffre_2_Car((floor($re_minute / 15)) * 15) . ":00";
  if (isset($upd)) {
    switch ($etape) {
      case 0:
        $requette = "SELECT * FROM location" . ID_GARAGE . " WHERE NoChasis='" . addslashes($new_vhc) . "' AND ((Debut>'$date_debut' AND Debut<'$date_fin')OR(Fin>'$date_debut' AND Fin<'$date_fin')OR(Debut<'$date_debut' AND Fin>'$date_fin')) AND Debut <> '$Start'";
        break;
      case 1:
        $requette = "SELECT * FROM location" . ID_GARAGE . " WHERE NoChasis='" . addslashes($new_vhc) . "' AND ((Debut>'$date_effectif_debut' AND Debut<'$date_fin')OR(Fin>'$date_effectif_debut' AND Fin<'$date_fin')OR(Debut<'$date_effectif_debut' AND Fin>'$date_fin')) AND Debut <> '$Start'";
        break;
      case 2:
      case 3:
        $requette = "SELECT * FROM location" . ID_GARAGE . " WHERE NoChasis='" . addslashes($new_vhc) . "' AND ((Debut>'$date_effectif_debut' AND Debut<'$date_effectif_fin')OR(Fin>'$date_effectif_debut' AND Fin<'$date_effectif_fin')OR(Debut<'$date_effectif_debut' AND Fin>'$date_effectif_fin')) AND Debut <> '$Start'";
        break;
    }
    if ($resultat = Connection::executeRawQueryWithfetchAll($requette)) {
      if ($ligne = $resultat[0]) {
        echo "Locations déjà existantes dans cet intervalle de temps<BR>";
        echo "<TABLE border=1><TR><TH>Nom</TH><TH>Début</TH><TH>Fin</TH></TR>";
        echo "<TR><TD><A HREF='location.php?new=0&Start=" . $ligne['Debut'] . "&vhc=" . $ligne['NoChasis'] . "' TARGET='_blank' rel='opener'>" . $ligne['Nom'] . " " . $ligne['Prenom'] . "</A></TD><TD>" . Cree_Entree_Type("", 18, 0, $ligne['Debut'], AFFICHAGE) . "</TD><TD>" . Cree_Entree_Type("", 18, 0, $ligne['Fin'], AFFICHAGE) . "</TD></TR>";
        foreach ($resultat as $ligne) {
          echo "<TR><TD><A HREF='location.php?new=0&Start=" . $ligne['Debut'] . "&vhc=" . $ligne['NoChasis'] . "' TARGET='_blank' rel='opener'>" . $ligne['Nom'] . " " . $ligne['Prenom'] . "</A></TD><TD>" . Cree_Entree_Type("", 18, 0, $ligne['Debut'], AFFICHAGE) . "</TD><TD>" . Cree_Entree_Type("", 18, 0, $ligne['Fin'], AFFICHAGE) . "</TD></TR>";
        }
        echo "</TABLE>";
        return;
      }
    }
    if ($ligne = Location::getByNoChassisAndDateDebutExact($vhc, $Start)) {
      $old_acompte = $ligne["Acompte"];
      $NoCommande = $ligne["NoCommande"];
    }
  } else {
    switch ($etape) {
      case 0:
        $requette = "SELECT * FROM location" . ID_GARAGE . " WHERE NoChasis='" . addslashes($new_vhc) . "' AND ((Debut>'$date_debut' AND Debut<'$date_fin')OR(Fin>'$date_debut' AND Fin<'$date_fin')OR(Debut<'$date_debut' AND Fin>'$date_fin'))";
        break;
      case 1:
        $requette = "SELECT * FROM location" . ID_GARAGE . " WHERE NoChasis='" . addslashes($new_vhc) . "' AND ((Debut>'$date_effectif_debut' AND Debut<'$date_fin')OR(Fin>'$date_effectif_debut' AND Fin<'$date_fin')OR(Debut<'$date_effectif_debut' AND Fin>'$date_fin'))";
        break;
      case 2:
      case 3:
        $requette = "SELECT * FROM location" . ID_GARAGE . " WHERE NoChasis='" . addslashes($new_vhc) . "' AND ((Debut>'$date_effectif_debut' AND Debut<'$date_effectif_fin')OR(Fin>'$date_effectif_debut' AND Fin<'$date_effectif_fin')OR(Debut<'$date_effectif_debut' AND Fin>'$date_effectif_fin'))";
        break;
    }
    if ($resultat = Connection::executeRawQueryWithfetchAll($requette)) {
      if ($ligne = $resultat[0]) {
        echo "Locations déjà existantes dans cet intervalle de temps<BR>";
        echo "<TABLE border=1><TR><TH>Nom</TH><TH>Début</TH><TH>Fin</TH></TR>";
        echo "<TR><TD><A HREF='location.php?new=0&Start=" . $ligne['Debut'] . "&vhc=" . $ligne['NoChasis'] . "' TARGET='_blank' rel='opener'>" . $ligne['Nom'] . " " . $ligne['Prenom'] . "</A></TD><TD>" . Cree_Entree_Type("", 18, 0, $ligne['Debut'], AFFICHAGE) . "</TD><TD>" . Cree_Entree_Type("", 18, 0, $ligne['Fin'], AFFICHAGE) . "</TD></TR>";
        foreach ($resultat as $ligne) {
          echo "<TR><TD><A HREF='location.php?new=0&Start=" . $ligne['Debut'] . "&vhc=" . $ligne['NoChasis'] . "' TARGET='_blank' rel='opener'>" . $ligne['Nom'] . " " . $ligne['Prenom'] . "</A></TD><TD>" . Cree_Entree_Type("", 18, 0, $ligne['Debut'], AFFICHAGE) . "</TD><TD>" . Cree_Entree_Type("", 18, 0, $ligne['Fin'], AFFICHAGE) . "</TD></TR>";
        }
        echo "</TABLE>";
        return;
      }
    }
    $Start = $date_debut;
    $vhc = $new_vhc;
    $requette = "INSERT INTO location" . ID_GARAGE . " (NoChasis, Debut, Fin, Nom, Etape, EmployeRdv, EmployeDepart) VALUES ('" . addslashes($new_vhc) . "','$date_debut','$date_fin','TEMP','0','$EmployeRDV','$EmployeDepart')";
    if ($resultat = Connection::executeRawQuery($requette)) {
    } else {
      echo $requette;
      echo "<BR>KO<BR>";
      $err++;
    }
  }
  if (isset($caution)) {
    $cc = $caution;
    if ((strlen($ecc1) == 2) && (strlen($ecc2) == 2)) {
      $ecc = "20$ecc2-" . Chiffre_2_Car($ecc1) . "-01";
    } else {
      $ecc = "0000-00-00";
    }
  } else {
    $cc = 0;
    $ncc = "0000000000000000";
    $ecc = "0000-00-00";
    $scc = "000";
  }
  switch ($etape) {
    case 0:
      $encCC = "$ncc-$scc-$ecc1-$ecc2";
      $pattern = "/^[0-9]{16}\-[0-9]{3}\-[0-9]{2}\-[0-9]{2}$/";
      if (preg_match($pattern, $encCC)) {
        $fp = fopen("./public.key", "r");
        $pub_key = fread($fp, 8192);
        fclose($fp);
        openssl_public_encrypt($encCC, $crypttext, $pub_key);
        $encCC = base64_encode($crypttext);
        $spCC = ", EncCC='$encCC'";
      } else {
        $spCC = "";
      }
      $requette = "UPDATE location" . ID_GARAGE . " SET NoChasis='" . addslashes($new_vhc) . "', Nom='" . addslashes($Nom) . "', Prenom='" . addslashes($Prenom) . "', " .
        "Telephone='" . addslashes($Telephone) . "', Debut='$date_debut', Fin='$date_fin', " .
        "TypeTarif=$new_tarif_type, Tarif=" . Min_Zero($tarif) . ", NbUnites=" . Min_Zero($nb_jours) . ", Total=" . Min_Zero($total) . ", " .
        "Etape=0 , Pret=" . Min_Zero($Pret) . ", Rabais=" . Min_Zero($rabais) . ", TypeCaution=" . Min_Zero($cc) . "$spCC, EmployeRdv='$EmployeRDV', EmployeDepart='$EmployeDepart'";
      if (empty($NoClient)) {
        $requette .= ", NoClient=NULL";
      } else {
        $requette .= ", NoClient=$NoClient";
      }
      $requette .= " WHERE NoChasis='$vhc' AND Debut='$Start'";
      if ($resultat = Connection::executeRawQuery($requette)) {
        $vhc = $new_vhc;
        $Start = $date_debut;
      } else {
        echo $requette;
        echo "<BR>KO<BR>";
        $err++;
      }
      if ($acompte != 0) {
        $NoCommande = create_commande_acompte($vhc, $Start, $acompte);
      }
      break;
    case 1:
      $encCC = "$ncc-$scc-$ecc1-$ecc2";
      $pattern = "/^[0-9]{16}\-[0-9]{3}\-[0-9]{2}\-[0-9]{2}$/";
      if (preg_match($pattern, $encCC)) {
        $fp = fopen("./public.key", "r");
        $pub_key = fread($fp, 8192);
        fclose($fp);
        openssl_public_encrypt($encCC, $crypttext, $pub_key);
        $encCC = base64_encode($crypttext);
        $spCC = ", EncCC='$encCC'";
      } else {
        $spCC = "";
      }
      $date_effectif_debut = "$de_annee-" . Chiffre_2_Car($de_mois) . "-" . Chiffre_2_Car($de_jour) . " " . Chiffre_2_Car($de_heure) . ":" . Chiffre_2_Car($de_minute) . ":00";
      $date_effectif_debut2 = "$de_annee-" . Chiffre_2_Car($de_mois) . "-" . Chiffre_2_Car($de_jour) . " " . Chiffre_2_Car($de_heure) . ":" . Chiffre_2_Car((floor($de_minute / 15)) * 15) . ":00";
      $requetteu = "UPDATE location" . ID_GARAGE . " SET NoChasis='" . addslashes($new_vhc) . "', Nom='" . addslashes($Nom) . "', Prenom='" . addslashes($Prenom) . "', " .
        "Telephone='" . addslashes($Telephone) . "', Debut='$date_effectif_debut2', Fin='$date_fin', " .
        "TypeTarif=$new_tarif_type, Tarif=" . Min_Zero($tarif) . ", NbUnites=" . Min_Zero($nb_jours) . ", Total=" . Min_Zero($total) . ", " .
        "Etape=1, KmDepart=" . Min_Zero($KmDepart) . ", DebutEffectif='$date_effectif_debut', Pret=" . Min_Zero($Pret) . ", Rabais=" . Min_Zero($rabais) . ", TypeCaution=" . Min_Zero($cc) . "$spCC, EmployeRdv='$EmployeRDV', EmployeDepart='$EmployeDepart', EtatDepart='$etat'";
      if (empty($NoClient)) {
        $requetteu .= ", NoClient=NULL";
      } else {
        $requetteu .= ", NoClient=$NoClient";
      }
      $requetteu .= " WHERE NoChasis='$vhc' AND Debut='$Start'";
      if ($resultat = Connection::executeRawQuery($requetteu)) {
        $requette = "UPDATE vehiculeslocation" . ID_GARAGE . " SET KmActuel=$KmDepart WHERE NoChasis='" . addslashes($new_vhc) . "'";
        Connection::executeRawQuery($requette);
        $vhc = $new_vhc;
        $Start = $date_effectif_debut2;
      }
      if ($acompte != 0) {
        $NoCommande = create_commande_acompte($vhc, $Start, $acompte);
        if ($fact == 1) {
          add_commande_fact($vhc, $Start, $NoCommande);
        }
      } else if ($old_acompte != 0) {
        if ($fact == 1) {
          add_commande_fact($vhc, $Start, $NoCommande);
        }
      } else if ($fact == 1) {
        $NoCommande = create_commande_fact($vhc, $Start);
      }
      break;
    case 2:
      $encCC = "$ncc-$scc-$ecc1-$ecc2";
      $pattern = "/^[0-9]{16}\-[0-9]{3}\-[0-9]{2}\-[0-9]{2}$/";
      if (preg_match($pattern, $encCC)) {
        $fp = fopen("./public.key", "r");
        $pub_key = fread($fp, 8192);
        fclose($fp);
        openssl_public_encrypt($encCC, $crypttext, $pub_key);
        $encCC = base64_encode($crypttext);
        $spCC = ", EncCC='$encCC'";
      } else {
        $spCC = "";
      }
      $date_effectif_debut = "$de_annee-" . Chiffre_2_Car($de_mois) . "-" . Chiffre_2_Car($de_jour) . " " . Chiffre_2_Car($de_heure) . ":" . Chiffre_2_Car($de_minute) . ":00";
      $date_effectif_fin = "$re_annee-" . Chiffre_2_Car($re_mois) . "-" . Chiffre_2_Car($re_jour) . " " . Chiffre_2_Car($re_heure) . ":" . Chiffre_2_Car($re_minute) . ":00";
      $date_effectif_debut2 = "$de_annee-" . Chiffre_2_Car($de_mois) . "-" . Chiffre_2_Car($de_jour) . " " . Chiffre_2_Car($de_heure) . ":" . Chiffre_2_Car((floor($de_minute / 15)) * 15) . ":00";
      $date_effectif_fin2 = "$re_annee-" . Chiffre_2_Car($re_mois) . "-" . Chiffre_2_Car($re_jour) . " " . Chiffre_2_Car($re_heure) . ":" . Chiffre_2_Car((floor($re_minute / 15)) * 15) . ":00";
      $requette = "UPDATE location" . ID_GARAGE . " SET NoChasis='" . addslashes($new_vhc) . "', Nom='" . addslashes($Nom) . "', Prenom='" . addslashes($Prenom) . "', " .
        "Telephone='" . addslashes($Telephone) . "', Debut='$date_effectif_debut2', Fin='$date_effectif_fin2', " .
        "TypeTarif=$new_tarif_type, Tarif=" . Min_Zero($tarif) . ", NbUnites=" . Min_Zero($nb_jours) . ", Total=" . Min_Zero($total) . ", " .
        "Etape=2, KmDepart=" . Min_Zero($KmDepart) . ", DebutEffectif='$date_effectif_debut', KmRetour=" . Min_Zero($KmRetour) . ", FinEffectif='$date_effectif_fin', RemarqueRetour='$EtatRetour', Pret=" . Min_Zero($Pret) . ", Rabais=" . Min_Zero($rabais) . ", TypeCaution=" . Min_Zero($cc) . "$spCC, EmployeRdv='$EmployeRDV', EmployeDepart='$EmployeDepart', EtatDepart='$etat'";
      if (empty($NoClient)) {
        $requette .= ", NoClient=NULL";
      } else {
        $requette .= ", NoClient=$NoClient";
      }
      $requette .= " WHERE NoChasis='$vhc' AND Debut='$Start'";
      $vhc = $new_vhc;
      $Start = $date_effectif_debut2;
      if ($resultat = Connection::executeRawQuery($requette)) {
        echo $requette;
        $requette = "UPDATE vehiculeslocation" . ID_GARAGE . " SET KmActuel=$KmRetour WHERE NoChasis='" . addslashes($new_vhc) . "'";
        Connection::executeRawQuery($requette);
        if ($fact == 1) {
          $ligne = Location::getByNoChassisAndDateDebutExact($vhc, $Start)[0];
          if (empty($ligne['NoCommande'])) {
            $NoCommande = create_commande_fact($vhc, $Start);
          } else {
            $NoCommande = $ligne['NoCommande'];
            add_commande_fact($vhc, $Start, $NoCommande);
          }
        } else {
        }
      } else {
        echo $requette;
        echo "<BR>KO";
      }
      break;
    case 3:
      $encCC = "$ncc-$scc-$ecc1-$ecc2";
      $pattern = "/^[0-9]{16}\-[0-9]{3}\-[0-9]{2}\-[0-9]{2}$/";
      if (preg_match($pattern, $encCC)) {
        $fp = fopen("./public.key", "r");
        $pub_key = fread($fp, 8192);
        fclose($fp);
        openssl_public_encrypt($encCC, $crypttext, $pub_key);
        $encCC = base64_encode($crypttext);
        $spCC = ", EncCC='$encCC'";
        echo $spCC;
      } else {
        $spCC = "";
      }
      $date_effectif_debut = "$de_annee-" . Chiffre_2_Car($de_mois) . "-" . Chiffre_2_Car($de_jour) . " " . Chiffre_2_Car($de_heure) . ":" . Chiffre_2_Car($de_minute) . ":00";
      $date_effectif_fin = "$re_annee-" . Chiffre_2_Car($re_mois) . "-" . Chiffre_2_Car($re_jour) . " " . Chiffre_2_Car($re_heure) . ":" . Chiffre_2_Car($re_minute) . ":00";
      $date_effectif_debut2 = "$de_annee-" . Chiffre_2_Car($de_mois) . "-" . Chiffre_2_Car($de_jour) . " " . Chiffre_2_Car($de_heure) . ":" . Chiffre_2_Car((floor($de_minute / 15)) * 15) . ":00";
      $date_effectif_fin2 = "$re_annee-" . Chiffre_2_Car($re_mois) . "-" . Chiffre_2_Car($re_jour) . " " . Chiffre_2_Car($re_heure) . ":" . Chiffre_2_Car((floor($re_minute / 15)) * 15) . ":00";
      $requette = "UPDATE location" . ID_GARAGE . " SET NoChasis='" . addslashes($new_vhc) . "', Nom='" . addslashes($Nom) . "', Prenom='" . addslashes($Prenom) . "', " .
        "Telephone='" . addslashes($Telephone) . "', Debut='$date_effectif_debut2', Fin='$date_effectif_fin2', " .
        "TypeTarif=$new_tarif_type, Tarif=" . Min_Zero($tarif) . ", NbUnites=" . Min_Zero($nb_jours) . ", Total=" . Min_Zero($total) . ", " .
        "Etape=3, KmDepart=" . Min_Zero($KmDepart) . ", DebutEffectif='$date_effectif_debut', KmRetour=" . Min_Zero($KmRetour) . ", FinEffectif='$date_effectif_fin', RemarqueRetour='$EtatRetour', NoClient=" . Min_Zero($NoClient) . ", Pret=" . Min_Zero($Pret) . ", Rabais=" . Min_Zero($rabais);
      $requette .= ", EmployeRdv='$EmployeRDV', EmployeDepart='$EmployeDepart', EtatDepart='$etat' WHERE NoChasis='$vhc' AND Debut='$Start'";
      if ($resultat = Connection::executeRawQuery($requette)) {
        echo $requette;
        $requette = "UPDATE vehiculeslocation" . ID_GARAGE . " SET KmActuel=$KmRetour WHERE NoChasis='" . addslashes($new_vhc) . "'";
        Connection::executeRawQuery($requette);
        $vhc = $new_vhc;
        $Start = $date_effectif_debut2;
      } else {
        echo $requette;
        echo "<BR>KO";
      }
      break;
  }
  if ($pri != 0) {
    print_contrat_location($vhc, $Start, $pri);
  }
  if ($fact == 1) {
    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 "close_popup();\n";
    echo "</script>\n";
  } else {
    echo "<script language=JavaScript>\n";
    echo "this.opener.location.reload();\n";
    echo "close_popup();\n";
    echo "</script>\n";
  }
}
?>