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

class ActionsMBISignature
{
    /**
     * Overloading the addMoreActionsButtons function : replacing the parent's function with the one below
     *
     * @param array()         $parameters     Hook metadatas (context, etc...)
     * @param CommonObject    &$object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
     * @param string          &$action Current action (if set). Generally create or edit or null
     * @param HookManager $hookmanager Hook manager propagated to allow calling another hook
     * @return  int                             < 0 on error, 0 on success, 1 to replace standard code
     */

    function addMoreActionsButtons($parameters, &$object, &$action, $hookmanager)
    {
        dol_include_once('/mbisignature/lib/signatureToText.lib.php');
        global $db, $langs, $conf, $user;
        $langs->load("mbisignature@mbisignature");

        // Si l'objet est une proposition commerciale

        if (in_array('propalcard', explode(':', $parameters['context']))) {

            dol_syslog("PROPAL DETECTED - " . $object->ref, LOG_INFO);

            // Si n'est pas un brouillon

            if ($object->statut == Propal::STATUS_VALIDATED && $conf->global->SIGNATURE_ENABLE_PROPAL == "true") {

                dol_syslog("PROPAL IS NOT A DRAFT - " . $object->ref, LOG_INFO);

                if (strpos($object->ref, ' ') !== false || strpos($object->ref, '/') !== false) {
                    setEventMessages($langs->trans("REFSPECIALCHAR"), null, 'warnings');
                    dol_syslog("REFSPECIALCHAR " . $object->ref, LOG_INFO);
                } else {

                    // On insère le lien de signature dans l'attribut supplémentaire signature_link

                    $resqlhash = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_hash WHERE ref = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                    if ($resqlhash) {
                        $num = $db->num_rows($resqlhash);
                        if ($num === 0) {
                            $extrafields = $object->array_options;
                            $extrafields['signature_link'] = strval(showOnlineSignatureUrlText('proposal', $object->ref));
                            $object->array_options = $extrafields;
                            $result = $object->insertExtraFields();
                            if ($result < 0) {
                                setEventMessages("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN PROPAL " . $object->ref, null, 'errors');
                                dol_syslog("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN PROPAL " . $object->ref, LOG_ERR);
                            } else {
                                echo "<script>window.location.reload();</script>";
                            }
                        } else {
                            dol_syslog("REF ALREADY EXISTS IN " . MAIN_DB_PREFIX . "mbi_signature_hash IN PROPAL " . $object->ref, LOG_INFO);
                        }
                    }
                    $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                    $obj = $db->fetch_object($resql);

                    if ($obj->pathoffile !== 'document generated') { // Affichage des boutons
                        $signature = $object->array_options;
                        echo "<a class='butAction' href='" . $signature['options_signature_link'] . "' target='_blank'>" . $langs->trans("SignButton") . "</a>";
                        dol_syslog("DOCUMENT NOT GENERATED - SIGN BUTTON EXISTS - " . $object->ref, LOG_INFO);
                    }
                }
            }

            // Si l'objet est une commande

        } else if (in_array('ordercard', explode(':', $parameters['context']))) {

            dol_syslog("ORDER DETECTED - " . $object->ref, LOG_INFO);

            // Si n'est pas un brouillon

            if ($object->statut != Commande::STATUS_DRAFT && $conf->global->SIGNATURE_ENABLE_ORDER == "true") {

                dol_syslog("ORDER IS NOT A DRAFT - " . $object->ref, LOG_INFO);

                if (strpos($object->ref, ' ') !== false || strpos($object->ref, '/') !== false) {
                    setEventMessages($langs->trans("REFSPECIALCHAR"), null, 'warnings');
                    dol_syslog("REFSPECIALCHAR " . $object->ref, LOG_INFO);
                } else {

                    // On insère le lien de signature dans l'attribut supplémentaire signature_link

                    $resqlhash = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_hash WHERE ref = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                    if ($resqlhash) {
                        $num = $db->num_rows($resqlhash);
                        if ($num === 0) {
                            $extrafields = $object->array_options;
                            $extrafields['signature_link'] = strval(showOnlineSignatureUrlText('order', $object->ref));
                            $object->array_options = $extrafields;
                            $result = $object->insertExtraFields();
                            if ($result < 0) {
                                setEventMessages("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN ORDER " . $object->ref, null, 'errors');
                                dol_syslog("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN ORDER " . $object->ref, LOG_ERR);
                            } else {
                                echo "<script>window.location.reload();</script>";
                            }
                        } else {
                            dol_syslog("REF ALREADY EXISTS IN " . MAIN_DB_PREFIX . "mbi_signature_hash IN ORDER " . $object->ref, LOG_INFO);
                        }
                    }
                    $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                    $obj = $db->fetch_object($resql);

                    if ($obj->pathoffile !== 'document generated') { // Affichage des boutons
                        $signature = $object->array_options;
                        echo "<a class='butAction' href='" . $signature['options_signature_link'] . "' target='_blank'>" . $langs->trans("SignButton") . "</a>";
                        if ($conf->global->DOUBLE_SIGNATURE == "true") {
                            echo "<a class='butAction' href='" . $signature['options_signature_link'] . "&company=1' target='_blank'>" . $langs->trans("SignButton2") . "</a>";
                        }
                        dol_syslog("DOCUMENT NOT GENERATED - SIGN BUTTON EXISTS - " . $object->ref, LOG_INFO);
                    }
                }
            }

            // Si l'objet est une commande fournisseur

        } else if (in_array('ordersuppliercard', explode(':', $parameters['context']))) {

            dol_syslog("ORDER SUPPLIER DETECTED - " . $object->ref, LOG_INFO);

            // Si n'est pas un brouillon

            if ($object->statut != CommandeFournisseur::STATUS_DRAFT && $conf->global->SIGNATURE_ENABLE_ORDER_SUPPLIER == "true") {

                dol_syslog("ORDER SUPPLIER IS NOT A DRAFT - " . $object->ref, LOG_INFO);

                if (strpos($object->ref, ' ') !== false || strpos($object->ref, '/') !== false) {
                    setEventMessages($langs->trans("REFSPECIALCHAR"), null, 'warnings');
                    dol_syslog("REFSPECIALCHAR " . $object->ref, LOG_INFO);
                } else {

                    // On insère le lien de signature dans l'attribut supplémentaire signature_link

                    $resqlhash = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_hash WHERE ref = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                    if ($resqlhash) {
                        $num = $db->num_rows($resqlhash);
                        if ($num === 0) {
                            $extrafields = $object->array_options;
                            $extrafields['signature_link'] = strval(showOnlineSignatureUrlText('ordersupplier', $object->ref));
                            $object->array_options = $extrafields;
                            $result = $object->insertExtraFields();
                            if ($result < 0) {
                                setEventMessages("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN ORDER " . $object->ref, null, 'errors');
                                dol_syslog("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN ORDER " . $object->ref, LOG_ERR);
                            } else {
                                echo "<script>window.location.reload();</script>";
                            }
                        } else {
                            dol_syslog("REF ALREADY EXISTS IN " . MAIN_DB_PREFIX . "mbi_signature_hash IN ORDER " . $object->ref, LOG_INFO);
                        }
                    }
                    $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                    $obj = $db->fetch_object($resql);

                    if ($obj->pathoffile !== 'document generated') { // Affichage des boutons
                        $signature = $object->array_options;
                        echo "<a class='butAction' href='" . $signature['options_signature_link'] . "' target='_blank'>" . $langs->trans("SignButton") . "</a>";
                        if ($conf->global->DOUBLE_SIGNATURE == "true") {
                            echo "<a class='butAction' href='" . $signature['options_signature_link'] . "&company=1' target='_blank'>" . $langs->trans("SignButton2") . "</a>";
                        }
                        dol_syslog("DOCUMENT NOT GENERATED - SIGN BUTTON EXISTS - " . $object->ref, LOG_INFO);
                    }
                }
            }

            // Si l'objet est une expedition

        } else if (in_array('expeditioncard', explode(':', $parameters['context']))) {

            dol_syslog("SHIPMENT DETECTED - " . $object->ref, LOG_INFO);

            // Si n'est pas un brouillon

            if ($object->statut != Expedition::STATUS_DRAFT && $conf->global->SIGNATURE_ENABLE_EXPEDITION == "true") {

                dol_syslog("SHIPMENT IS NOT A DRAFT - " . $object->ref, LOG_INFO);

                if (strpos($object->ref, ' ') !== false || strpos($object->ref, '/') !== false) {
                    setEventMessages($langs->trans("REFSPECIALCHAR"), null, 'warnings');
                    dol_syslog("REFSPECIALCHAR " . $object->ref, LOG_INFO);
                } else {

                    // On insère le lien de signature dans l'attribut supplémentaire signature_link

                    $resqlhash = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_hash WHERE ref = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                    if ($resqlhash) {
                        $num = $db->num_rows($resqlhash);
                        if ($num === 0) {
                            $extrafields = $object->array_options;
                            $extrafields['signature_link'] = strval(showOnlineSignatureUrlText('expedition', $object->ref));
                            $object->array_options = $extrafields;
                            $result = $object->insertExtraFields();
                            if ($result < 0) {
                                setEventMessages("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN SHIPMENT " . $object->ref, null, 'errors');
                                dol_syslog("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN SHIPMENT " . $object->ref, LOG_ERR);
                            } else {
                                echo "<script>window.location.reload();</script>";
                            }
                        } else {
                            dol_syslog("REF ALREADY EXISTS IN " . MAIN_DB_PREFIX . "mbi_signature_hash IN SHIPMENT " . $object->ref, LOG_INFO);
                        }
                    }
                    $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                    $obj = $db->fetch_object($resql);

                    if ($obj->pathoffile !== 'document generated') { // Affichage des boutons
                        $signature = $object->array_options;
                        echo "<a class='butAction' href='" . $signature['options_signature_link'] . "' target='_blank'>" . $langs->trans("SignButton") . "</a>";
                        if ($conf->global->DOUBLE_SIGNATURE == "true") {
                            echo "<a class='butAction' href='" . $signature['options_signature_link'] . "&company=1' target='_blank'>" . $langs->trans("SignButton2") . "</a>";
                        }
                        dol_syslog("DOCUMENT NOT GENERATED - SIGN BUTTON EXISTS - " . $object->ref, LOG_INFO);
                    }
                }
            }
        } else if (in_array('interventioncard', explode(':', $parameters['context']))) {

            dol_syslog("INTERVENTION DETECTED - " . $object->ref, LOG_INFO);

            // Si n'est pas un brouillon

            if ($object->statut != Fichinter::STATUS_DRAFT && $conf->global->SIGNATURE_ENABLE_INTERVENTION == "true") {

                dol_syslog("INTERVENTION IS NOT A DRAFT - " . $object->ref, LOG_INFO);

                if (strpos($object->ref, ' ') !== false || strpos($object->ref, '/') !== false) {
                    setEventMessages($langs->trans("REFSPECIALCHAR"), null, 'warnings');
                    dol_syslog("REFSPECIALCHAR " . $object->ref, LOG_INFO);
                } else {

                    // On insère le lien de signature dans l'attribut supplémentaire signature_link

                    $resqlhash = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_hash WHERE ref = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                    if ($resqlhash) {
                        $num = $db->num_rows($resqlhash);
                        if ($num === 0) {
                            $extrafields = $object->array_options;
                            $extrafields['signature_link'] = strval(showOnlineSignatureUrlText('intervention', $object->ref));
                            $object->array_options = $extrafields;
                            $result = $object->insertExtraFields();
                            if ($result < 0) {
                                setEventMessages("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN INTERVENTION " . $object->ref, null, 'errors');
                                dol_syslog("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN INTERVENTION " . $object->ref, LOG_ERR);
                            } else {
                                echo "<script>window.location.reload();</script>";
                            }
                        } else {
                            dol_syslog("REF ALREADY EXISTS IN " . MAIN_DB_PREFIX . "mbi_signature_hash IN INTERVENTION " . $object->ref, LOG_INFO);
                        }
                    }
                    $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                    $obj = $db->fetch_object($resql);

                    if ($obj->pathoffile !== 'document generated') { // Affichage des boutons
                        $signature = $object->array_options;
                        echo "<a class='butAction' href='" . $signature['options_signature_link'] . "' target='_blank'>" . $langs->trans("SignButton") . "</a>";
                        echo "<a class='butAction' href='" . $signature['options_signature_link'] . "&company=1' target='_blank'>" . $langs->trans("SignButton2") . "</a>";
                        dol_syslog("DOCUMENT NOT GENERATED - SIGN BUTTON EXISTS - " . $object->ref, LOG_INFO);
                    }
                }
            }
        } else if (in_array('contractcard', explode(':', $parameters['context']))) {

            dol_syslog("CONTRACT DETECTED - " . $object->ref, LOG_INFO);

            // Si n'est pas un brouillon

            if (version_compare(DOL_VERSION, '11.0.0', '<')) { // Contrat:STATUS_DRAFT non supporté avant Dolibarr 11.0.0

                if ($conf->global->SIGNATURE_ENABLE_CONTRACT == "true") {

                    // On insère le lien de signature dans l'attribut supplémentaire signature_link

                    if (strpos($object->ref, ' ') !== false || strpos($object->ref, '/') !== false) {
                        setEventMessages($langs->trans("REFSPECIALCHAR"), null, 'warnings');
                        dol_syslog("REFSPECIALCHAR " . $object->ref, LOG_INFO);
                    } else {

                        $resqlhash = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_hash WHERE ref = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                        if ($resqlhash) {
                            $num = $db->num_rows($resqlhash);
                            if ($num === 0) {
                                $extrafields = $object->array_options;
                                $extrafields['signature_link'] = strval(showOnlineSignatureUrlText('contract', $object->ref));
                                $object->array_options = $extrafields;
                                $result = $object->insertExtraFields();
                                if ($result < 0) {
                                    setEventMessages("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN CONTRACT " . $object->ref, null, 'errors');
                                    dol_syslog("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN CONTRACT " . $object->ref, LOG_ERR);
                                } else {
                                    echo "<script>window.location.reload();</script>";
                                }
                            } else {
                                dol_syslog("REF ALREADY EXISTS IN " . MAIN_DB_PREFIX . "mbi_signature_hash IN CONTRACT " . $object->ref, LOG_INFO);
                            }
                        }
                        $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                        $obj = $db->fetch_object($resql);

                        if ($obj->pathoffile !== 'document generated') { // Affichage des boutons
                            $signature = $object->array_options;
                            echo "<a class='butAction' href='" . $signature['options_signature_link'] . "' target='_blank'>" . $langs->trans("SignButton") . "</a>";
                            echo "<a class='butAction' href='" . $signature['options_signature_link'] . "&company=1' target='_blank'>" . $langs->trans("SignButton2") . "</a>";
                            dol_syslog("DOCUMENT NOT GENERATED - SIGN BUTTON EXISTS - " . $object->ref, LOG_INFO);
                        }
                    }
                }
            } else {

                if ($object->statut != Contrat::STATUS_DRAFT && $conf->global->SIGNATURE_ENABLE_CONTRACT == "true") { // Dolibarr 11.0.0 ++

                    dol_syslog("CONTRACT IS NOT A DRAFT - " . $object->ref, LOG_INFO);

                    if (strpos($object->ref, ' ') !== false || strpos($object->ref, '/') !== false) {
                        setEventMessages($langs->trans("REFSPECIALCHAR"), null, 'warnings');
                        dol_syslog("REFSPECIALCHAR " . $object->ref, LOG_INFO);
                    } else {

                        // On insère le lien de signature dans l'attribut supplémentaire signature_link

                        $resqlhash = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_hash WHERE ref = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                        if ($resqlhash) {
                            $num = $db->num_rows($resqlhash);
                            if ($num === 0) {
                                $extrafields = $object->array_options;
                                $extrafields['signature_link'] = strval(showOnlineSignatureUrlText('contract', $object->ref));
                                $object->array_options = $extrafields;
                                $result = $object->insertExtraFields();
                                if ($result < 0) {
                                    setEventMessages("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN CONTRACT " . $object->ref, null, 'errors');
                                    dol_syslog("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN CONTRACT " . $object->ref, LOG_ERR);
                                } else {
                                    echo "<script>window.location.reload();</script>";
                                }
                            } else {
                                dol_syslog("REF ALREADY EXISTS IN " . MAIN_DB_PREFIX . "mbi_signature_hash IN CONTRACT " . $object->ref, LOG_INFO);
                            }
                        }
                        $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                        $obj = $db->fetch_object($resql);

                        if ($obj->pathoffile !== 'document generated') { // Affichage des boutons
                            $signature = $object->array_options;
                            echo "<a class='butAction' href='" . $signature['options_signature_link'] . "' target='_blank'>" . $langs->trans("SignButton") . "</a>";
                            echo "<a class='butAction' href='" . $signature['options_signature_link'] . "&company=1' target='_blank'>" . $langs->trans("SignButton2") . "</a>";
                            dol_syslog("DOCUMENT NOT GENERATED - SIGN BUTTON EXISTS - " . $object->ref, LOG_INFO);
                        }
                    }
                }
            }
        } else if (in_array('thirdpartycard', explode(':', $parameters['context']))) {
            dol_syslog("SOCIETE DETECTED - " . $object->ref, LOG_INFO);

            require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
            $companybankaccount = new CompanyBankAccount($db);
            $companybankaccount->fetch('', $object->id);

            if (!empty($companybankaccount->id) && $companybankaccount->id !== "" && $conf->global->SIGNATURE_ENABLE_SEPA_MANDATE == "true") {

                if (strpos($object->ref, ' ') !== false || strpos($object->ref, '/') !== false) {
                    setEventMessages($langs->trans("REFSPECIALCHAR"), null, 'warnings');
                    dol_syslog("REFSPECIALCHAR " . $object->ref, LOG_INFO);
                } else {

                    // On insère le lien de signature dans l'attribut supplémentaire signature_link

                    $resqlhash = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_hash WHERE ref = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                    if ($resqlhash) {
                        $num = $db->num_rows($resqlhash);
                        if ($num === 0) {
                            $extrafields = $object->array_options;
                            $extrafields['signature_link'] = strval(showOnlineSignatureUrlText('sepamandate', $object->ref));
                            $object->array_options = $extrafields;
                            $result = $object->insertExtraFields();
                            if ($result < 0) {
                                setEventMessages("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN SOCIETE " . $object->ref, null, 'errors');
                                dol_syslog("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN SOCIETE " . $object->ref, LOG_ERR);
                            } else {
                                echo "<script>window.location.reload();</script>";
                            }
                        } else {
                            dol_syslog("REF ALREADY EXISTS IN " . MAIN_DB_PREFIX . "mbi_signature_hash IN SOCIETE " . $object->ref, LOG_INFO);
                        }
                    }
                    $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                    $obj = $db->fetch_object($resql);

                    if ($obj->pathoffile !== 'document generated') { // Affichage des boutons
                        $signature = $object->array_options;
                        echo "<a class='butAction' href='" . $signature['options_signature_link'] . "' target='_blank'>" . $langs->trans("SignButton") . "</a>";
                        dol_syslog("DOCUMENT NOT GENERATED - SIGN BUTTON EXISTS - " . $object->ref, LOG_INFO);
                    }
                }
            }
        } else if (in_array('expensereportcard', explode(':', $parameters['context']))) {

            dol_syslog("EXPENSE REPORT DETECTED - " . $object->ref, LOG_INFO);

            // Si n'est pas un brouillon

            if ($object->status != ExpenseReport::STATUS_DRAFT && $conf->global->SIGNATURE_ENABLE_EXPENSE_REPORT == "true") { // object->status à la place de object->statut sur Expense Report Dolibarr 13.0.2

                dol_syslog("EXPENSE REPORT IS NOT A DRAFT - " . $object->ref, LOG_INFO);

                if (strpos($object->ref, ' ') !== false || strpos($object->ref, '/') !== false) {
                    setEventMessages($langs->trans("REFSPECIALCHAR"), null, 'warnings');
                    dol_syslog("REFSPECIALCHAR " . $object->ref, LOG_INFO);
                } else {

                    // On insère le lien de signature dans l'attribut supplémentaire signature_link

                    $resqlhash = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_hash WHERE ref = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                    if ($resqlhash) {
                        $num = $db->num_rows($resqlhash);
                        if ($num === 0) {
                            $extrafields = $object->array_options;
                            $extrafields['signature_link'] = strval(showOnlineSignatureUrlText('expensereport', $object->ref));
                            $object->array_options = $extrafields;
                            $result = $object->insertExtraFields();
                            if ($result < 0) {
                                setEventMessages("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN EXPENSE REPORT " . $object->ref, null, 'errors');
                                dol_syslog("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN EXPENSE REPORT " . $object->ref, LOG_ERR);
                            } else {
                                echo "<script>window.location.reload();</script>";
                            }
                        } else {
                            dol_syslog("REF ALREADY EXISTS IN " . MAIN_DB_PREFIX . "mbi_signature_hash IN EXPENSE REPORT " . $object->ref, LOG_INFO);
                        }
                    }
                    $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $object->ref . "' AND entity = '" . $conf->entity . "'");
                    $obj = $db->fetch_object($resql);

                    if ($obj->pathoffile !== 'document generated') { // Affichage des boutons
                        $signature = $object->array_options;
                        if ($user->id == $object->fk_user_author && $user->id == $object->fk_user_validator) {
                            echo "<a class='butAction' href='" . $signature['options_signature_link'] . "' target='_blank'>" . $langs->trans("AskingUserSignButton") . "</a>";
                            echo "<a class='butAction' href='" . $signature['options_signature_link'] . "&company=1' target='_blank'>" . $langs->trans("HierarchicalResponsibleSignButton") . "</a>";
                        } else if ($user->id == $object->fk_user_author && $user->id !== $object->fk_user_validator) {
                            echo "<a class='butAction' href='" . $signature['options_signature_link'] . "' target='_blank'>" . $langs->trans("AskingUserSignButton") . "</a>";
                            echo "<a class='butActionRefused'>" . $langs->trans("HierarchicalResponsibleSignButton") . "</a>";
                        } else if ($user->id !== $object->fk_user_author && $user->id == $object->fk_user_validator) {
                            echo "<a class='butActionRefused'>" . $langs->trans("AskingUserSignButton") . "</a>";
                            echo "<a class='butAction' href='" . $signature['options_signature_link'] . "&company=1' target='_blank'>" . $langs->trans("HierarchicalResponsibleSignButton") . "</a>";
                        }
                        dol_syslog("DOCUMENT NOT GENERATED - SIGN BUTTON EXISTS - " . $object->ref, LOG_INFO);
                    }
                }
            }
        }
        return 0;
    }

    function printTopRightMenu($parameters, &$object, &$action, $hookmanager)
    {
        global $db, $langs, $conf, $user;

        dol_include_once('/mbisignature/lib/signatureToText.lib.php');
        $langs->load("mbisignature@mbisignature");

        if (strpos($_SERVER['REQUEST_URI'],'holiday/card.php') !== false) {

            $holiday = new Holiday($db);
            $holiday->fetch($_GET["id"]);

            dol_syslog("HOLIDAY DETECTED - " . $holiday->ref, LOG_INFO);

            // Si n'est pas un brouillon

            if ($holiday->statut == 2 && $conf->global->SIGNATURE_ENABLE_HOLIDAY == "true") {
                dol_syslog("HOLIDAY IS NOT A DRAFT - " . $holiday->ref, LOG_INFO);

                if (strpos($holiday->ref, ' ') !== false || strpos($holiday->ref, '/') !== false) {
                    setEventMessages($langs->trans("REFSPECIALCHAR"), null, 'warnings');
                    dol_syslog("REFSPECIALCHAR " . $holiday->ref, LOG_INFO);
                } else {

                    // On insère le lien de signature dans l'attribut supplémentaire signature_link

                    $resqlhash = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_hash WHERE ref = '" . $holiday->ref . "' AND entity = '" . $conf->entity . "'");
                    if ($resqlhash) {
                        $num = $db->num_rows($resqlhash);
                        if ($num === 0) {
                            $extrafields = $holiday->array_options;
                            $extrafields['signature_link'] = strval(showOnlineSignatureUrlText('holiday', $holiday->ref));
                            $holiday->array_options = $extrafields;
                            $result = $holiday->insertExtraFields();
                            if ($result < 0) {
                                setEventMessages("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN HOLIDAY " . $holiday->ref, null, 'errors');
                                dol_syslog("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN HOLIDAY " . $holiday->ref, LOG_ERR);
                            } else {
                                echo "<script>window.location.reload();</script>";
                            }
                        } else {
                            dol_syslog("REF ALREADY EXISTS IN " . MAIN_DB_PREFIX . "mbi_signature_hash IN HOLIDAY " . $holiday->ref, LOG_INFO);
                        }
                    }
                    $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $holiday->ref . "' AND entity = '" . $conf->entity . "'");
                    $obj = $db->fetch_object($resql);

                    if ($obj->pathoffile !== 'document generated') { // Affichage des boutons
                        $signature = $holiday->array_options;
                        if ($user->id == $holiday->fk_user && $user->id == $holiday->fk_validator) {
                            echo "<script>$(document).ready(function() { $('.tabsAction').children(':first').text('" . $langs->trans("HierarchicalResponsibleSignButton") . "'); $('.tabsAction').children(':first').attr('href', '" . $signature['options_signature_link'] . "&company=1'); $('.tabsAction').children(':first').attr('target', '_blank'); $('.tabsAction').prepend('<a href=\"" . $signature['options_signature_link'] . "\" class=\"butAction classfortooltip\" target=\"_blank\">" . $langs->trans("AskingUserSignButton") ."</a>'); });</script>";
                        } else if ($user->id == $holiday->fk_user && $user->id !== $holiday->fk_validator) {
                            echo "<script>$(document).ready(function() { $('.tabsAction').children(':first').text('" . $langs->trans("HierarchicalResponsibleSignButton") . "'); $('.tabsAction').prepend('<a href=\"" . $signature['options_signature_link'] . "\" class=\"butAction classfortooltip\" target=\"_blank\">" . $langs->trans("AskingUserSignButton") ."</a>'); });</script>";
                        } else if ($user->id !== $holiday->fk_user && $user->id == $holiday->fk_validator) {
                            echo "<script>$(document).ready(function() { $('.tabsAction').children(':first').text('" . $langs->trans("HierarchicalResponsibleSignButton") . "'); $('.tabsAction').children(':first').attr('href', '" . $signature['options_signature_link'] . "&company=1'); $('.tabsAction').children(':first').attr('target', '_blank'); $('.tabsAction').prepend('<a class=\"butActionRefused classfortooltip\">" . $langs->trans("AskingUserSignButton") ."</a>'); });</script>";
                        }
                        dol_syslog("DOCUMENT NOT GENERATED - SIGN BUTTON EXISTS - " . $holiday->ref, LOG_INFO);
                    }
                }
            }
        }
        if (strpos($_SERVER['REQUEST_URI'],'delivery/card.php') !== false) {

            $delivery = new Delivery($db);
            $delivery->fetch($_GET["id"]);

            dol_syslog("DELIVERY DETECTED - " . $delivery->ref, LOG_INFO);

            // Si n'est pas un brouillon

            if ($delivery->statut == 1 && $conf->global->SIGNATURE_ENABLE_DELIVERY == "true") {
                dol_syslog("DELIVERY IS NOT A DRAFT - " . $delivery->ref, LOG_INFO);

                if (strpos($delivery->ref, ' ') !== false || strpos($delivery->ref, '/') !== false) {
                    setEventMessages($langs->trans("REFSPECIALCHAR"), null, 'warnings');
                    dol_syslog("REFSPECIALCHAR " . $delivery->ref, LOG_INFO);
                } else {

                    // On insère le lien de signature dans l'attribut supplémentaire signature_link

                    $resqlhash = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_hash WHERE ref = '" . $delivery->ref . "' AND entity = '" . $conf->entity . "'");
                    if ($resqlhash) {
                        $num = $db->num_rows($resqlhash);
                        if ($num === 0) {
                            $extrafields = $delivery->array_options;
                            $extrafields['signature_link'] = strval(showOnlineSignatureUrlText('delivery', $delivery->ref, $delivery->id));
                            $delivery->array_options = $extrafields;
                            $result = $delivery->insertExtraFields();
                            if ($result < 0) {
                                setEventMessages("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN DELIVERY " . $delivery->ref, null, 'errors');
                                dol_syslog("ERROR WHILE INSERTING EXTRAFIELD SIGNATURE_LINK IN DELIVERY " . $delivery->ref, LOG_ERR);
                            } else {
                                echo "<script>window.location.reload();</script>";
                            }
                        } else {
                            dol_syslog("REF ALREADY EXISTS IN " . MAIN_DB_PREFIX . "mbi_signature_hash IN DELIVERY " . $delivery->ref, LOG_INFO);
                        }
                    }
                    $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $delivery->ref . "' AND entity = '" . $conf->entity . "'");
                    $obj = $db->fetch_object($resql);

                    if ($obj->pathoffile !== 'document generated') { // Affichage des boutons
                        $signature = $delivery->array_options;
                        echo "<script>$(document).ready(function() { $('.tabsAction').prepend('<a href=\"" . $signature['options_signature_link'] . "\" class=\"butAction classfortooltip\" target=\"_blank\">" . $langs->trans("SignButton") ."</a><a href=\"" . $signature['options_signature_link'] . "&company=1\" class=\"butAction classfortooltip\" target=\"_blank\">" . $langs->trans("SignButton2") ."</a>'); });</script>";
                        dol_syslog("DOCUMENT NOT GENERATED - SIGN BUTTON EXISTS - " . $object->ref, LOG_INFO);
                    }
                }
            }
        }
        return 0;
    }
}
