<?php
require 'database.inc.php';
if ((!empty($User)) && (!empty($Passwd))) {
	$lg_user = $User;
	$lg_password = $Passwd;
}
if (Test_User($lg_user, $lg_password) == TRUE) {
	$annee = date("Y");
	if (!isset($NoClient)) {
		$NoClient = 0;
	}
	switch ($etape) {
		case 0:
			if (!isset($new)) {
				$date_debut = "$Date_annee-" . Chiffre_2_Car($Date_mois) . "-" . Chiffre_2_Car($Date_jour);
				$heure_debut = Chiffre_2_Car($HD_heure) . ":" . Chiffre_2_Car($HD_minute) . ":00";
				$date_fin = "$HF_annee-" . Chiffre_2_Car($HF_mois) . "-" . Chiffre_2_Car($HF_jour) . " " . Chiffre_2_Car($HF_heure) . ":" . Chiffre_2_Car($HF_minute) . ":00";
				if (empty($NoClient)) {
					$NoClient2 = "NULL";
				} else {
					$NoClient2 = $NoClient;
				}
				$requette = "UPDATE atelier" . ID_GARAGE . " SET NoClient=$NoClient2, Nom='" . Connection::getConnection()->quote($Nom) . "', Prenom='" . Connection::getConnection()->quote($Prenom) . "', Telephone='$Telephone', NoChasis='$NoChasis'," .
					" Vehicule='$Vehicule', Km='$Km', Date='$date_debut', Heure='$heure_debut', Temps='$Temps', Description='" . addslashes($Description) . "'," .
					" Restitution='$date_fin', Reception='" . addslashes($Reception) . "', Etape=0, Mecano='$Mecano', Id_Vhc='$vhc', Id_Service='$service' WHERE NoAtelier=$NoAtelier";
				if ($resultat = Connection::executeRawQuery($requette)) {
					if ($Acompte != 0) {
						$NoCommande = Trouve_No_Commande_Libre("FD");
						$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, 'FD', 'F', '$lg_user', $nc, '$date', '$date', 0, -$Acompte, '" . date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") + 30, date("Y"))) . "')";
						if ($resultat = 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, ModePayement, NoCaisse) VALUES" .
								" ($NoCommande, '$lg_user', '$date " . date("H:i") . ":00', $Acompte, $mode_payement, $no_caisse)";
							echo $requette . "<BR />\n";
							Connection::executeRawQuery($requette);
							$no_cash = Connection::getConnection()->lastInsertId();
							print_quitance($no_cash);
						}
						Connection::executeRawQuery("UPDATE atelier" . ID_GARAGE . " SET NoCommande = $NoCommande WHERE NoAtelier = $NoAtelier");
					}
					echo "<script language=JavaScript>\n";
					echo "this.opener.location.reload();\n";
					echo "window.close();\n";
					echo "</script>\n";
				} else {
					echo $requette;
					echo "<BR>KO<BR>";
					echo Connection::getConnection()->errorInfo();
				}
			} else {
				$err = false;
				$date_debut = "$Date_annee-" . Chiffre_2_Car($Date_mois) . "-" . Chiffre_2_Car($Date_jour);
				$heure_debut = Chiffre_2_Car($HD_heure) . ":" . Chiffre_2_Car($HD_minute) . ":00";
				$date_fin = "$HF_annee-" . Chiffre_2_Car($HF_mois) . "-" . Chiffre_2_Car($HF_jour) . " " . Chiffre_2_Car($HF_heure) . ":" . Chiffre_2_Car($HF_minute) . ":00";
				if (!empty($vhc_pret)) {
					$resultat_loc = Location::getByNoChassisAndDate(addslashes($vhc_pret), $date_debut . ' ' . $heure_debut, $date_fin);
					$ligne_loc = count($resultat_loc);
					if ($ligne_loc != 0) {
						$err = true;
						echo "le véhicule de prêt est indisponible durant cette période";
					} else {
						$requette = "INSERT INTO location" . ID_GARAGE . " (NoChasis, Nom, Prenom, Telephone, Debut, Fin, TypeTarif, Tarif, NbUnites, Rabais, Total, Etape, NoClient, Pret) values ('";
						$requette .= addslashes($vhc_pret) . "', '" . addslashes($Nom) . "', '" . addslashes($Prenom) . "', '" . addslashes($Telephone) . "', " .
							"'$date_debut $heure_debut', '$date_fin', 0, 0, 0, 0, 0, 0, ";
						if (empty($NoClient)) {
							$requette .= "NULL";
						} else {
							$requette .= $NoClient;
						}
						$requette .= ", 1)";
						Connection::executeRawQuery($requette);
					}
				}
				$requette = "INSERT INTO atelier" . ID_GARAGE . " (NoClient, Nom, Prenom, Telephone, NoChasis, Vehicule, Km, Date, Heure, Temps, Description, Restitution, Reception, Etape, Mecano, Id_Vhc, Id_Service) values (";
				if (empty($NoClient)) {
					$requette .= "NULL";
				} else {
					$requette .= "$NoClient";
				}
				$requette .= ", '" . Connection::getConnection()->quote($Nom) . "', '" . Connection::getConnection()->quote($Prenom) . "', '$Telephone', '$NoChasis', '$Vehicule', '$Km', '$date_debut', '$heure_debut', $Temps, '" . addslashes($Description) . "', '$date_fin', '" . addslashes($Reception) . "', 0, '$Mecano', '$vhc', '$service')";
				if ($resultat = Connection::executeRawQuery($requette)) {
					if ($Acompte != 0) {
						$NoAtelier = Connection::getConnection()->lastInsertId();
						$NoCommande = Trouve_No_Commande_Libre("FD");
						$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, 'FD', 'F', '$lg_user', $nc, '$date', '$date', 0, -$Acompte, '" . date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") + 30, date("Y"))) . "')";
						if ($resultat = 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, ModePayement, NoCaisse) VALUES" .
								" ($NoCommande, '$lg_user', '$date " . date("H:i") . ":00', $Acompte, $mode_payement, $no_caisse)";
							echo $requette . "<BR />\n";
							Connection::executeRawQuery($requette);
							$no_cash = Connection::getConnection()->lastInsertId();
							print_quitance($no_cash);
						}
						Connection::executeRawQuery("UPDATE atelier" . ID_GARAGE . " SET NoCommande = $NoCommande WHERE NoAtelier = $NoAtelier");
					}
					echo "<script language=JavaScript>\n";
					echo "this.opener.location.reload();\n";
					if ($err)
						echo "alert('le véhicule de prêt est indisponible durant cette période');\n";
					echo "window.close();\n";
					echo "</script>\n";
				} else {
					echo $requette;
					echo "<BR>KO<BR>";
					echo Connection::getConnection()->errorInfo();
				}
			}
			break;
		case 1:
			if (!isset($new)) {
				$date_debut = "$Date_annee-" . Chiffre_2_Car($Date_mois) . "-" . Chiffre_2_Car($Date_jour);
				$heure_debut = Chiffre_2_Car($HD_heure) . ":" . Chiffre_2_Car($HD_minute) . ":00";
				$date_fin = "$HF_annee-" . Chiffre_2_Car($HF_mois) . "-" . Chiffre_2_Car($HF_jour) . " " . Chiffre_2_Car($HF_heure) . ":" . Chiffre_2_Car($HF_minute) . ":00";
				if (empty($NoClient)) {
					$NoClient2 = "NULL";
				} else {
					$NoClient2 = $NoClient;
				}
				$requette = "UPDATE atelier" . ID_GARAGE . " SET NoClient=$NoClient2, Nom='" . Connection::getConnection()->quote($Nom) . "', Prenom='" . Connection::getConnection()->quote($Prenom) . "', Telephone='$Telephone', NoChasis='$NoChasis'," .
					" Vehicule='$Vehicule', Km='$Km', Date='$date_debut', Heure='$heure_debut', Temps='$Temps', Description='" . addslashes($Description) . "'," .
					" Restitution='$date_fin', Reception='" . addslashes($Reception) . "', Etape=1, Mecano='$Mecano', Id_Vhc='$vhc', Id_Service='$service' WHERE NoAtelier=$NoAtelier";
				if ($resultat = Connection::executeRawQuery($requette)) {
					if ($Acompte != 0) {
						$NoCommande = Trouve_No_Commande_Libre("FD");
						$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, 'FD', 'F', '$lg_user', $nc, '$date', '$date', 0, -$Acompte, '" . date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") + 30, date("Y"))) . "')";
						if ($resultat = 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, ModePayement, NoCaisse) VALUES" .
								" ($NoCommande, '$lg_user', '$date " . date("H:i") . ":00', $Acompte, $mode_payement, $no_caisse)";
							echo $requette . "<BR />\n";
							Connection::executeRawQuery($requette);
							$no_cash = Connection::getConnection()->lastInsertId();
							print_quitance($no_cash);
						}
						Connection::executeRawQuery("UPDATE atelier" . ID_GARAGE . " SET NoCommande = $NoCommande WHERE NoAtelier = $NoAtelier");
					}
					if ($pri == 1) {
						echo "<script language=JavaScript>\n";
						echo "this.opener.location.reload();\n";
						echo "document.URL='print_fiche_atelier.php?NoAtelier=$NoAtelier&Direct=1';\n";
						echo "</script>\n";
					} else {
						echo "<script language=JavaScript>\n";
						echo "this.opener.location.reload();\n";
						echo "window.close();\n";
						echo "</script>\n";
					}
				} else {
					echo $requette;
					echo "<BR>KO<BR>";
					echo Connection::getConnection()->errorInfo();
				}
			} else {
				$err = false;
				$date_debut = "$Date_annee-" . Chiffre_2_Car($Date_mois) . "-" . Chiffre_2_Car($Date_jour);
				$heure_debut = Chiffre_2_Car($HD_heure) . ":" . Chiffre_2_Car($HD_minute) . ":00";
				$date_fin = "$HF_annee-" . Chiffre_2_Car($HF_mois) . "-" . Chiffre_2_Car($HF_jour) . " " . Chiffre_2_Car($HF_heure) . ":" . Chiffre_2_Car($HF_minute) . ":00";
				if (!empty($vhc_pret)) {
					$resultat_loc = Location::getByNoChassisAndDate(addslashes($vhc_pret), $date_debut . ' ' . $heure_debut, $date_fin);
					$ligne_loc = count($resultat_loc);
					if ($ligne_loc != 0) {
						$err = true;
						echo "le véhicule de prêt est indisponible durant cette période";
					} else {
						$requette = "INSERT INTO location" . ID_GARAGE . " (NoChasis, Nom, Prenom, Telephone, Debut, Fin, TypeTarif, Tarif, NbUnites, Rabais, Total, Etape, NoClient, Pret) values ('";
						$requette .= addslashes($vhc_pret) . "', '" . addslashes($Nom) . "', '" . addslashes($Prenom) . "', '" . addslashes($Telephone) . "', " .
							"'$date_debut $heure_debut', '$date_fin', 0, 0, 0, 0, 0, 1, ";
						if (empty($NoClient)) {
							$requette .= "NULL";
						} else {
							$requette .= $NoClient;
						}
						$requette .= ", 1)";
						Connection::executeRawQuery($requette);
					}
				}
				$requette = "INSERT INTO atelier" . ID_GARAGE . " (NoClient, Nom, Prenom, Telephone, NoChasis, Vehicule, Km, Date, Heure, Temps, Description, Restitution, Reception, Etape, Mecano, Id_Vhc, Id_Service) values (";
				if (empty($NoClient)) {
					$requette .= "NULL";
				} else {
					$requette .= "$NoClient";
				}
				$requette .= ", '" . Connection::getConnection()->quote($Nom) . "', '" . Connection::getConnection()->quote($Prenom) . "', '$Telephone', '$NoChasis', '$Vehicule', '$Km', '$date_debut', '$heure_debut', $Temps, '" . addslashes($Description) . "', '$date_fin', '" . addslashes($Reception) . "', 1, '$Mecano', '$vhc', '$service')";
				if ($resultat = Connection::executeRawQuery($requette)) {
					if ($pri == 1) {
						$resultat2 = Atelier::getAll();
						$ligne2 = $resultat2[0];
						$NoAtelier = $ligne2['NoAtelier'];
						echo "<script language=JavaScript>\n";
						echo "this.opener.location.reload();\n";
						if ($err)
							echo "alert('le véhicule de prêt est indisponible durant cette période');\n";
						echo "document.URL='print_fiche_atelier.php?NoAtelier=$NoAtelier&Direct=1';\n";
						echo "</script>\n";
					} else {
						echo "<script language=JavaScript>\n";
						echo "this.opener.location.reload();\n";
						if (!$err)
							echo "window.close();\n";
						echo "</script>\n";
					}
				} else {
					echo $requette;
					echo "<BR>KO<BR>";
					echo Connection::getConnection()->errorInfo();
				}
			}
			break;
		case 2:
			$date_debut = "$Date_annee-" . Chiffre_2_Car($Date_mois) . "-" . Chiffre_2_Car($Date_jour);
			if (!empty($NoChasis)) {
				$requette = "UPDATE vehicules SET Km=$Km, DateService='$date_debut' WHERE NoChasis='$NoChasis'";
				echo $requette . "<BR>\n";
				if ($resultat = Connection::executeRawQuery($requette)) {
				}
			}
			$heure_debut = Chiffre_2_Car($HD_heure) . ":" . Chiffre_2_Car($HD_minute) . ":00";
			$date_fin = "$HF_annee-" . Chiffre_2_Car($HF_mois) . "-" . Chiffre_2_Car($HF_jour) . " " .
				Chiffre_2_Car($HF_heure) . ":" . Chiffre_2_Car($HF_minute) . ":00";
			if ($ligne = Atelier::getAtelierByNoAtelier($NoAtelier)) {
				$prev_etape = $ligne['Etape'];
				if (!empty($NoCommande)) {
					if ($ligne = Commande::getCommandeByNoCommande($NoCommande)) {
						$Type = $ligne['Type'];
					}
				} else if (empty($ligne['NoCommande'])) {
					switch ($fact) {
						case 1:
							$Type = "FD";
							$Genre = "F";
							break;
						case 2:
							$Type = "GF";
							$Genre = "G";
							break;
						case 3:
							$Type = "GG";
							$Genre = "G";
							break;
						case 4:
							$Type = "DP";
							$Genre = "D";
							break;
						case 5:
							$Type = "FI";
							$Genre = "F";
							break;
					}
					if ($service != 0) {
						echo "document.URL='service.php?NoAtelier=$NoAtelier&Type=$Type';\n";
						$NoCommande = 0;
					} else {
						$NoCommande = Trouve_No_Commande_Libre($Type);
						$no_plaque = "";
						if (!empty($NoChasis)) {
							$ligne = Vehicule::getVehiculeByNoChassis(addslashes($NoChasis));
							$no_plaque = $ligne['NoPlaque'];
						}
						$requette = "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 $requette;
						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, '', '" . addslashes($Description) . "', 0, 0, 0, 0, 0, 0)";
							if ($resultat = Connection::executeRawQuery($requette)) {
							}
							echo "OK";
						} else {
							echo "KO";
						}
					}
				} else {
					$NoCommande = $ligne['NoCommande'];
					if ($ligne = Commande::getCommandeByNoCommande($NoCommande)) {
						$Type = $ligne['Type'];
					}
				}
			}
			if (empty($NoClient)) {
				$NoClient2 = "NULL";
			} else {
				$NoClient2 = $NoClient;
			}
			if ($service != 0 && $prev_etape != 2) {
				$requette = "UPDATE atelier" . ID_GARAGE . " SET NoClient=$NoClient2, Nom='" . Connection::getConnection()->quote($Nom) . "', Prenom='" . Connection::getConnection()->quote($Prenom) . "', Telephone='$Telephone', NoChasis='$NoChasis'," .
					" Vehicule='$Vehicule', Km='$Km', Date='$date_debut', Heure='$heure_debut', Temps='$Temps', Description='" . addslashes($Description) . "'," .
					" Restitution='$date_fin', Etape=1, NoCommande=$NoCommande, Mecano='$Mecano', Id_Vhc='$vhc', Id_Service='$service' WHERE NoAtelier=$NoAtelier";
			} else {
				$requette = "UPDATE atelier" . ID_GARAGE . " SET NoClient=$NoClient2, Nom='" . Connection::getConnection()->quote($Nom) . "', Prenom='" . Connection::getConnection()->quote($Prenom) . "', Telephone='$Telephone', NoChasis='$NoChasis'," .
					" Vehicule='$Vehicule', Km='$Km', Date='$date_debut', Heure='$heure_debut', Temps='$Temps', Description='" . addslashes($Description) . "'," .
					" Restitution='$date_fin', Etape=2, NoCommande=$NoCommande, Mecano='$Mecano', Id_Vhc='$vhc', Id_Service='$service' WHERE NoAtelier=$NoAtelier";
			}
			if ($resultat = Connection::executeRawQuery($requette)) {
				$Sous_Type = 1;
				echo "<script language=JavaScript>\n";
				if ($pri == 1) {
					echo "this.opener.location.reload();\n";
					echo "document.URL='print_fiche_atelier.php?NoAtelier=$NoAtelier&Direct=1';\n";
				} else {
					if ($service != 0 && $prev_etape != 2) {
						echo "document.URL='service.php?NoAtelier=$NoAtelier&Type=$Type&Genre=$Genre';\n";
					} else {
						echo "this.opener.location='default.php?Type=4&Sous_Type=$Sous_Type&Etape=3&New=0&NoCommande=$NoCommande&NoClient=$NoClient2';\n";
						echo "window.close();\n";
					}
				}
				echo "</script>\n";
			} else {
				echo $requette;
				echo "<BR>KO<BR>";
				echo Connection::getConnection()->errorInfo();
			}
			break;
		case 3:
			$date_debut = "$Date_annee-" . Chiffre_2_Car($Date_mois) . "-" . Chiffre_2_Car($Date_jour);
			if (!empty($NoChasis)) {
				$requette = "UPDATE vehicules SET Km=$Km, DateService='$date_debut' WHERE NoChasis='$NoChasis'";
				echo $requette . "<BR>\n";
				if ($resultat = Connection::executeRawQuery($requette)) {
				}
			}
			$heure_debut = Chiffre_2_Car($HD_heure) . ":" . Chiffre_2_Car($HD_minute) . ":00";
			$date_fin = "$HF_annee-" . Chiffre_2_Car($HF_mois) . "-" . Chiffre_2_Car($HF_jour) . " " . Chiffre_2_Car($HF_heure) . ":" . Chiffre_2_Car($HF_minute) . ":00";
			if (empty($NoClient)) {
				$NoClient2 = "NULL";
			} else {
				$NoClient2 = $NoClient;
			}
			$requette = "UPDATE atelier" . ID_GARAGE . " SET NoClient=$NoClient2, Nom='" . Connection::getConnection()->quote($Nom) . "', Prenom='" . Connection::getConnection()->quote($Prenom) . "', Telephone='$Telephone', NoChasis='$NoChasis'," .
				" Vehicule='$Vehicule', Km='$Km', Date='$date_debut', Heure='$heure_debut', Temps='$Temps', Description='" . addslashes($Description) . "'," .
				" Restitution='$date_fin', Etape=3, NoCommande=NULL, Mecano='$Mecano', Id_Vhc='$vhc', Id_Service='$service' WHERE NoAtelier=$NoAtelier";
			if ($resultat = Connection::executeRawQuery($requette)) {
				echo "<script language=JavaScript>\n";
				echo "this.opener.location.reload();\n";
				if ($pri == 1) {
					echo "document.URL='print_fiche_atelier.php?NoAtelier=$NoAtelier&Direct=1';\n";
				} else {
					echo "window.close();\n";
				}
				echo "</script>\n";
			} else {
				echo $requette;
				echo "<BR>KO<BR>";
				echo Connection::getConnection()->errorInfo();
			}
			break;
	}
}
