<?php
/* Copyright (C) 2020-2021       MB Informatique         <info@mb-informatique.fr> */

function showOnlineSignatureUrlText($type, $ref, $id = 0, $custom = false, $fk_file = 0, $page = 0, $posx = 0, $posy = 0) { // Fonction qui permet l'affichage du lien de signature
    $url = getOnlineSignatureUrlNew($type, $ref, $id, $custom, $fk_file, $page, $posx, $posy);
    $out = $custom ? $url . "&custom=1" : $url;
    return $out;
}

function insertSignatureHash($db, $ref, $hashp, $conf, $custom, $fk_file, $page, $posx, $posy) {
    if ($custom) {
        $db->begin();
        $db->query("INSERT INTO " . MAIN_DB_PREFIX . "mbi_signature_hash_custom (ref, hash, entity, fk_file, page, posx, posy) VALUES ('" . $ref . "', '" . $hashp . "', '" . $conf->entity . "', " . $fk_file . ", '" . $page . "', '" . $posx . "', '" . $posy . "')");
        $db->commit();
    } else {
        $resqlhash = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_hash WHERE ref = '" . $ref . "' AND entity = '" . $conf->entity . "'");
        if ($resqlhash) {
            $num = $db->num_rows($resqlhash);
            if ($num === 0) {
                $db->begin();
                $db->query("INSERT INTO " . MAIN_DB_PREFIX . "mbi_signature_hash (ref, hash, entity) VALUES ('" . $ref . "', '" . $hashp . "', '" . $conf->entity . "')");
                $db->commit();
            }
        }
    }
}

function getOnlineSignatureUrlNew($type, $ref = '', $id, $custom, $fk_file, $page, $posx, $posy) { // Fonction qui génère le lien de signature
    global $conf, $db;

    $ref = str_replace(' ', '', $ref);
    if ($type == "agefodd_convention") {
        $ref2 = $ref;
        $ref = $id;
    }
    $out = '';

    if ($custom) {
        try {
            if (!file_exists(DOL_DOCUMENT_ROOT . '/custom/mbisignaturecustomdocs/newonlinesign.php'))
                throw new Exception ('Does not exist');
            else
                $out = $conf->global->SIGNATURE_URL . '/custom/mbisignaturecustomdocs/newonlinesign.php?source=' . $type . '&ref=' . urlencode($ref);
        } catch (Exception $e) {
            $out = $conf->global->SIGNATURE_URL . '/mbisignaturecustomdocs/newonlinesign.php?source=' . $type . '&ref=' . urlencode($ref);
        }
    } else {
        try {
            if (!file_exists(DOL_DOCUMENT_ROOT . '/custom/mbisignature/newonlinesign.php'))
                throw new Exception ('Does not exist');
            else
                $out = $conf->global->SIGNATURE_URL . '/custom/mbisignature/newonlinesign.php?source=' . $type . '&ref=' . urlencode($ref);
        } catch (Exception $e) {
            $out = $conf->global->SIGNATURE_URL . '/mbisignature/newonlinesign.php?source=' . $type . '&ref=' . urlencode($ref);
        }
    }

    if ($type == 'proposal') {
        include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
        $propaltmp = new Propal($db);
        $res = $propaltmp->fetch(0, $ref);
        if ($res <= 0) return 'FailedToGetProposal';
    }
    else if ($type == 'order') {
        include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
        $propaltmp = new Commande($db);
        $res = $propaltmp->fetch(0, $ref);
        if ($res <= 0) return 'FailedToGetProposal';
    }
    else if ($type == 'ordersupplier') {
        include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
        $propaltmp = new CommandeFournisseur($db);
        $res = $propaltmp->fetch(0, $ref);
        if ($res <= 0) return 'FailedToGetProposal';
    }
    else if ($type == 'expedition') {
        include_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
        $propaltmp = new Expedition($db);
        $res = $propaltmp->fetch(0, $ref);
        if ($res <= 0) return 'FailedToGetProposal';
    }
    else if ($type == 'delivery') {
        include_once DOL_DOCUMENT_ROOT.'/delivery/class/delivery.class.php';
        $propaltmp = new Delivery($db);
        $res = $propaltmp->fetch($id);
        if ($res <= 0) return 'FailedToGetProposal';
    }
    else if ($type == 'intervention') {
        include_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
        $propaltmp = new Fichinter($db);
        $res = $propaltmp->fetch(0, $ref);
        if ($res <= 0) return 'FailedToGetProposal';
    }
    else if ($type == 'contract') {
        include_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
        $propaltmp = new Contrat($db);
        $res = $propaltmp->fetch(0, $ref);
        if ($res <= 0) return 'FailedToGetProposal';
    }

    include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
    $hashp = getRandomPassword(true);
    $out .= '&hashp='.$hashp;
    if ($type == "agefodd_convention") {
        insertSignatureHash($db, $ref2, $hashp, $conf, $custom, $fk_file, $page, $posx, $posy);
    } else {
        insertSignatureHash($db, $ref, $hashp, $conf, $custom, $fk_file, $page, $posx, $posy);
    }

    // For multicompany
    if (!empty($out)) $out .= "&entity=".$conf->entity;

    return $out;
}
