$createdBy = $_SESSION['userMerlaTrav']->login();
         $created = date('Y-m-d h:i:s');
         $history = new History(array('action' => "Modification", 'target' => "Table des appartements", 'description' => "Modifier un appartement", 'created' => $created, 'createdBy' => $createdBy));
         //add it to db
         $historyManager->add($history);
         $actionMessage = "Opération Valide : Appartement Modifié avec succès.";
         $typeMessage = "success";
     } else {
         $actionMessage = "Erreur Modification Appartement : Vous devez remplir le champ <strong>Code</strong>.";
         $typeMessage = "error";
     }
 } else {
     if ($action == "updateStatus") {
         $idAppartement = $_POST['idAppartement'];
         $status = htmlentities($_POST['status']);
         $appartementManager->changeStatus($idAppartement, $status);
         //add history data to db
         $createdBy = $_SESSION['userMerlaTrav']->login();
         $created = date('Y-m-d h:i:s');
         $history = new History(array('action' => "Modification Status", 'target' => "Table des appartements", 'description' => "Changer le status de l'appartement", 'created' => $created, 'createdBy' => $createdBy));
         //add it to db
         $historyManager->add($history);
         $actionMessage = "Opération Valide : Appartement Status Modifié avec succès.";
         $typeMessage = "success";
     } else {
         if ($action == "updateClient") {
             $idAppartement = $_POST['idAppartement'];
             $par = htmlentities($_POST['par']);
             $appartementManager->updatePar($par, $idAppartement);
             //add history data to db
             $createdBy = $_SESSION['userMerlaTrav']->login();
    if (file_exists('../model/' . $myClass . '.php')) {
        include '../model/' . $myClass . '.php';
    } elseif (file_exists('../controller/' . $myClass . '.php')) {
        include '../controller/' . $myClass . '.php';
    }
}
spl_autoload_register("classLoad");
include '../config.php';
//classes loading end
session_start();
//post input processing
$idProjet = $_POST['idProjet'];
$idContrat = $_POST['idContrat'];
$contratManager = new ContratManager($pdo);
$contrat = $contratManager->getContratById($idContrat);
$contratManager->delete($idContrat);
if ($contrat->typeBien() == "appartement") {
    $appartementManager = new AppartementManager($pdo);
    $appartementManager->changeStatus($contrat->idBien(), "Non");
} else {
    if ($contrat->typeBien() == "localCommercial") {
        $locauxManager = new LocauxManager($pdo);
        $locauxManager->changeStatus($contrat->idBien(), "Non");
    }
}
$_SESSION['contrat-delete-success'] = "<strong>Opération valide : </strong>Contrat supprimé avec succès.";
$redirectLink = 'Location:../contrats-list.php?idProjet=' . $idProjet;
if (isset($_GET['p']) and $_GET['p'] == 99) {
    $redirectLink = 'Location:../clients-search.php';
}
header($redirectLink);
     $echeance = htmlentities($_POST['echeance']);
     $note = htmlentities($_POST['note']);
     $idClient = htmlentities($_POST['idClient']);
     $codeContrat = uniqid() . date('YmdHis');
     $created = date('Y-m-d h:i:s');
     $createdBy = $_SESSION['userMerlaTrav']->login();
     $numeroCheque = '0';
     if (isset($_POST['numeroCheque'])) {
         $numeroCheque = htmlentities($_POST['numeroCheque']);
     }
     $contratManager = new ContratManager($pdo);
     $contrat = new Contrat(array('numero' => $numero, 'dateCreation' => $dateCreation, 'prixVente' => $prixNegocie, 'avance' => $avance, 'modePaiement' => $modePaiement, 'dureePaiement' => $dureePaiement, 'nombreMois' => $nombreMois, 'echeance' => $echeance, 'note' => $note, 'idClient' => $idClient, 'idProjet' => $idProjet, 'idBien' => $idBien, 'typeBien' => $typeBien, 'code' => $codeContrat, 'numeroCheque' => $numeroCheque, 'created' => $created, 'createdBy' => $createdBy));
     $contratManager->add($contrat);
     if ($typeBien == "appartement") {
         $appartementManager = new AppartementManager($pdo);
         $appartementManager->changeStatus($idBien, "Vendu");
     } else {
         if ($typeBien == "localCommercial") {
             $locauxManager = new LocauxManager($pdo);
             $locauxManager->changeStatus($idBien, "Vendu");
         }
     }
     //add note client into db and show it in the dashboard
     $notesClientManager = new NotesClientManager($pdo);
     $notesClient = new NotesClient(array('note' => $note, 'created' => date('Y-m-d'), 'idProjet' => $idProjet, 'codeContrat' => $codeContrat));
     $notesClientManager->add($notesClient);
     header('Location:../contrat.php?codeContrat=' . $codeContrat);
 } else {
     $_SESSION['contrat-add-error'] = "<strong>Erreur Création Contrat : </strong>Vous devez choisir un 'Type de bien'.";
     header('Location:../contrats-add.php?idProjet=' . $idProjet . '&codeClient=' . $codeClient);
     exit;
        include '../controller/' . $myClass . '.php';
    }
}
spl_autoload_register("classLoad");
include '../config.php';
//classes loading end
session_start();
//post input processing
$idProjet = $_POST['idProjet'];
$idContrat = $_POST['idContrat'];
//create classes managers
$locauxManager = new LocauxManager($pdo);
$contratManager = new ContratManager($pdo);
$appartementManager = new AppartementManager($pdo);
//create classes
$contrat = $contratManager->getContratById($idContrat);
//change status of the old contrat Bien from reservé to non reservé
if ($contrat->typeBien() == "appartement") {
    $appartementManager->changeStatus($contrat->idBien(), "Disponible");
} else {
    if ($contrat->typeBien() == "localCommercial") {
        $locauxManager->changeStatus($contrat->idBien(), "Disponible");
    }
}
$contratManager->desisterContrat($idContrat);
$_SESSION['contrat-desister-success'] = "<strong>Opération valide : </strong>Le contrat est désisté avec succès.";
$redirectLink = 'Location:../contrats-list.php?idProjet=' . $idProjet;
if (isset($_GET['p']) and $_GET['p'] == 99) {
    $redirectLink = 'Location:../clients-search.php';
}
header($redirectLink);