//classes loading end
session_start();
//post input processing
$idProjet = htmlentities($_POST['idProjet']);
$idFournisseur = htmlentities($_POST['idFournisseur']);
if (!empty($_POST['montant']) && !empty($_POST['idFournisseur'])) {
    $dateReglement = htmlentities($_POST['dateReglement']);
    $montant = htmlentities($_POST['montant']);
    $modePaiement = htmlentities($_POST['modePaiement']);
    $numeroCheque = 0;
    if (isset($_POST['numeroCheque'])) {
        $numeroCheque = $_POST['numeroCheque'];
    }
    //create a new Operation object
    $reglementFournisseur = new ReglementFournisseur(array('dateReglement' => $dateReglement, 'montant' => $montant, 'idProjet' => $idProjet, 'idFournisseur' => $idFournisseur, 'modePaiement' => $modePaiement, 'numeroCheque' => $numeroCheque));
    $reglementFournisseurManager = new ReglementFournisseurManager($pdo);
    $reglementFournisseurManager->add($reglementFournisseur);
    $_SESSION['reglement-add-success'] = "<strong>Opération valide</strong> : Le réglement du fournisseur est réalisé avec succès.";
    $redirectLink = 'Location:../fournisseurs-reglements.php?idFournisseur=' . $idFournisseur;
    if (isset($_GET['p']) and $_GET['p'] == 99) {
        $redirectLink = 'Location:../livraisons.php';
    }
    header($redirectLink);
} else {
    $_SESSION['reglement-add-error'] = "<strong>Erreur Ajout Réglement Fournisseur</strong> : Vous devez remplir le champ 'Montant'.";
    $redirectLink = 'Location:../fournisseurs-reglements.php?idFournisseur=' . $idFournisseur;
    if (isset($_GET['p']) and $_GET['p'] == 99) {
        $redirectLink = 'Location:../livraisons.php';
    }
    header($redirectLink);
}
//process begins
//The History Component is used in all ActionControllers to mention a historical version of each action
$historyManager = new HistoryManager($pdo);
$reglementManager = new ReglementFournisseurManager($pdo);
if ($action == "add") {
    if (!empty($_POST['montant'])) {
        $idFournisseur = htmlentities($_POST['idFournisseur']);
        $idProjet = htmlentities($_POST['idProjet']);
        $dateReglement = htmlentities($_POST['dateReglement']);
        $montant = htmlentities($_POST['montant']);
        $modePaiement = htmlentities($_POST['modePaiement']);
        $numeroOperation = htmlentities($_POST['numeroCheque']);
        $createdBy = $_SESSION['userMerlaTrav']->login();
        $created = date('Y-m-d h:i:s');
        $reglement = new ReglementFournisseur(array('idFournisseur' => $idFournisseur, 'idProjet' => $idProjet, 'dateReglement' => $dateReglement, 'montant' => $montant, 'modePaiement' => $modePaiement, 'numeroCheque' => $numeroOperation, 'createdBy' => $createdBy, 'created' => $created));
        $reglementManager->add($reglement);
        //add History data
        $history = new History(array('action' => "Ajout", 'target' => "Table des réglements fournisseurs", 'description' => "Ajouter un réglement fournisseur", 'created' => $created, 'createdBy' => $createdBy));
        //add it to db
        $historyManager->add($history);
        $actionMessage = "<strong>Opération Valide</strong> : Réglement Ajouté avec succès.";
        $typeMessage = "success";
    } else {
        $actionMessage = "<strong>Erreur Ajout Réglement</strong> : Vous devez remplir le champ <strong>Montant</strong>.";
        $typeMessage = "error";
    }
    //in this line we specify the response url basing on the source of our request
    $redirectLink = "Location:../reglements.php";
    if (isset($_POST['source'])) {
        if ($_POST['source'] == 'livraisons-group') {
            $redirectLink = "Location:../livraisons-group.php";