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

class SignatureArea
{

    // PROPAL

    public function _signature_area(&$pdf, $object, $posy, $outputlangs, $db, $ref, $langs, $page_largeur, $marge_droite)
    {
        global $conf;
        $default_font_size = pdf_getPDFFontSize($outputlangs);
        $tab_top = $posy + 4;
        $tab_hl = 8;
        $posx = 120;
        $largcol = ($page_largeur - $marge_droite - $posx);
        $useborder = 0;
        $index = 0;
        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetXY($posx, $tab_top + 0);
        $pdf->SetFont('', '', $default_font_size - 2);
        $pdf->MultiCell($largcol, $tab_hl, $outputlangs->transnoentities("ProposalCustomerSignature"), 0, 'L', 1);

        $pdf->SetXY($posx, $tab_top + $tab_hl);
        $pdf->MultiCell($largcol, $tab_hl * 3, '', 1, 'R');
        if (!empty($conf->global->MAIN_PDF_PROPAL_USE_ELECTRONIC_SIGNING)) {
            $pdf->addEmptySignatureAppearance($posx, $tab_top + $tab_hl, $largcol, $tab_hl * 3);
        }

        $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $ref . "' AND entity = " . $conf->entity);
        $obj = $db->fetch_object($resql);
        if ($obj && $obj->pathoffile !== "document generated") {
            $img_base64_encoded = $obj->pathoffile;
            $img = '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '">';
            $pdf->writeHTMLCell(28,'', 122, $tab_top + 14, $img, 0, 0, false, true, '', true);
            // $pdf->Image($obj->pathoffile, 124, $tab_top + 14, 24); --> THIS ONLY WORKS ON DOLIBARR 11 ++
            if ($conf->global->SIGNATURE_DISPLAY_NAME == "true") {
                $pdf->SetXY(150, $tab_top + 12);
                $pdf->MultiCell(49, 6, $obj->name, 0, 'L', 1);
            }
            $pdf->SetXY(150, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 20 : $tab_top + 18);
            $pdf->MultiCell(38, 6, $langs->trans("Accord"), 0, 'L', 1);
            $pdf->SetXY(150, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 16 : $tab_top + 14);
            $pdf->MultiCell(38, 6, date("d/m/Y, H:i:s", strtotime($obj->tms)), 0, 'L', 1);
            $pdf->SetXY(150, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 24 : $tab_top + 22);
            $pdf->MultiCell(38, 6, 'IP: ' . $obj->ip, 0, 'L', 1);
        }

        $this->_use_certificate($conf, $pdf);

        return ($tab_hl * 7);
    }

    // MANDAT DE PRELEVEMENT SEPA

    public function _signature_area_sepa_mandate(&$pdf, $object, $posy, $outputlangs, $db, $ref, $conf, $page_largeur, $marge_droite, $marge_gauche)
    {
        global $langs;
        $default_font_size = pdf_getPDFFontSize($outputlangs);
        $tab_top = $posy + 4;
        $tab_hl = 4;

        $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $ref . "' AND entity = " . $conf->entity);
        $obj = $db->fetch_object($resql);
        if ($obj && $obj->pathoffile !== "document generated") {
            $img_base64_encoded = $obj->pathoffile;
            $img = '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '">';
            $pdf->writeHTMLCell(28,'', 122, $tab_top + 3, $img, 0, 0, false, true, '', true);
            // $pdf->Image($obj->pathoffile, 124, $tab_top + 14, 24); --> THIS ONLY WORKS ON DOLIBARR 11 ++
            $pdf->SetFillColor(255, 255, 255);
            $pdf->SetXY(152, $tab_top + 6);
            $pdf->SetFont('', '', $default_font_size - 2);
            $pdf->MultiCell(38, 8, $langs->trans("Accord"), 0, 'L', 1);
            $dateSignature = date("d/m/Y, H:i:s", strtotime($obj->tms));
            $pdf->SetFillColor(255, 255, 255);
            $pdf->SetXY(152, $tab_top + 10);
            $pdf->SetFont('', '', $default_font_size - 2);
            $pdf->MultiCell(38, 8, 'IP: ' . $obj->ip, 0, 'L', 1);
        }

        $posx = $marge_gauche;
        $pdf->SetXY($posx, $tab_top + 0);
        $pdf->SetFont('', '', $default_font_size - 2);
        $pdf->MultiCell(100, 3, $outputlangs->transnoentitiesnoconv("DateOfSignature"), 0, 'L', 0);
        $pdf->MultiCell(100, 3, ' ');
        $pdf->MultiCell(100, 3, $dateSignature, 0, 'L', 0);

        $posx = 120;
        $largcol = ($page_largeur - $marge_droite - $posx);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetXY($posx, $tab_top + 0);
        $pdf->MultiCell($largcol, $tab_hl, $outputlangs->transnoentitiesnoconv("Signature"), 0, 'L', 1);
        $pdf->SetXY($posx, $tab_top + $tab_hl);
        $pdf->MultiCell($largcol, $tab_hl * 3, '', 1, 'R');

        $this->_use_certificate($conf, $pdf);

        return ($tab_hl * 7);
    }

    // INTERVENTION, CONTRAT, NOTES DE FRAIS, DEMANDE DE CONGES

    /**
     *	Show area for the customer to sign
     *
     *	@param	PDF			$pdf            Object PDF
     *	@param  Object		$object         Object
     *	@param	int			$posy			Position depart
     *	@param	Translate	$outputlangs	Objet langs
     *	@return int							Position pour suite
     */
    public function _signature_area_double(&$pdf, $object, $posy, $outputlangs, $db, $ref, $langs, $page_largeur, $marge_droite, $emetteur_name, $thirdparty_name)
    {
        global $conf;
        $outputlangs->loadLangs(array("propal", "mbisignature", "holiday"));
        $default_font_size = pdf_getPDFFontSize($outputlangs);
        $tab_top = $posy + 4;
        $tab_hl = 8;
        $posx = 120;
        $largcol = ($page_largeur - $marge_droite - $posx);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetXY($posx, $tab_top + 0);
        $pdf->SetFont('', '', $default_font_size - 2);
        $pdf->MultiCell($largcol, $tab_hl, $outputlangs->transnoentities("ProposalCustomerSignature"), 0, 'L', 1);
        $pdf->SetXY($posx, $tab_top + 6);
        $pdf->MultiCell($largcol, $tab_hl, $thirdparty_name . " :", 0, 'L', 1);
        $pdf->SetXY($posx, $tab_top + $tab_hl + 6);
        $pdf->MultiCell($largcol, $tab_hl * 3, '', 1, 'R');
        if (!empty($conf->global->MAIN_PDF_PROPAL_USE_ELECTRONIC_SIGNING)) {
            $pdf->addEmptySignatureAppearance($posx, $tab_top + $tab_hl, $largcol, $tab_hl * 3);
        }

        $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $ref . "' AND object_id = 0 AND entity = " . $conf->entity);
        $obj = $db->fetch_object($resql);
        if ($obj && $obj->pathoffile !== "document generated") {
            $img_base64_encoded = $obj->pathoffile;
            $img = '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '">';
            $pdf->writeHTMLCell(28,'', 122, $tab_top + 20, $img, 0, 0, false, true, '', true);
            // $pdf->Image($obj->pathoffile, 124, $tab_top + 20, 24);
            if ($conf->global->SIGNATURE_DISPLAY_NAME == "true") {
                $pdf->SetXY(150, $tab_top + 18);
                $pdf->MultiCell(49, 6, $obj->name, 0, 'L', 1);
            }
            $pdf->SetXY(150, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 22 : $tab_top + 20);
            $pdf->MultiCell(38, 6, date("d/m/Y, H:i:s", strtotime($obj->tms)), 0, 'L', 1);
            $pdf->SetXY(150, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 26 : $tab_top + 24);
            $pdf->MultiCell(38, 6, $langs->trans("Accord"), 0, 'L', 1);
            $pdf->SetXY(150, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 30 : $tab_top + 28);
            $pdf->MultiCell(38, 6, 'IP: ' . $obj->ip, 0, 'L', 1);
        }

        //--//

        $tab_top = $posy + 4;
        $tab_hl = 8;
        $posx = 10;
        $largcol = 80;
        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetXY($posx, $tab_top + 0);
        $pdf->SetFont('', '', $default_font_size - 2);
        $pdf->MultiCell($largcol, $tab_hl, $outputlangs->transnoentities("ProposalCustomerSignature"), 0, 'L', 1);
        $pdf->SetXY($posx, $tab_top + 6);
        $pdf->MultiCell($largcol, $tab_hl, $emetteur_name . " :", 0, 'L', 1);
        $pdf->SetXY($posx, $tab_top + $tab_hl + 6);
        $pdf->MultiCell($largcol, $tab_hl * 3, '', 1, 'R');
        if (!empty($conf->global->MAIN_PDF_PROPAL_USE_ELECTRONIC_SIGNING)) {
            $pdf->addEmptySignatureAppearance($posx, $tab_top + $tab_hl, $largcol, $tab_hl * 3);
        }

        $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $ref . "' AND object_id = 1  AND entity = " . $conf->entity);
        $obj = $db->fetch_object($resql);
        if ($obj && $obj->pathoffile !== "document generated") {
            $img_base64_encoded = $obj->pathoffile;
            $img = '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '">';
            $pdf->writeHTMLCell(28,'', $posx + 2, $tab_top + 20, $img, 0, 0, false, true, '', true);
            // $pdf->Image($obj->pathoffile, $posx + 4, $tab_top + 20, 24);
            if ($conf->global->SIGNATURE_DISPLAY_NAME == "true") {
                $pdf->SetXY($posx + 30, $tab_top + 18);
                $pdf->MultiCell(49, 6, $obj->name, 0, 'L', 1);
            }
            $pdf->SetXY($posx + 30, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 22 : $tab_top + 20);
            $pdf->MultiCell(38, 6, date("d/m/Y, H:i:s", strtotime($obj->tms)), 0, 'L', 1);
            $pdf->SetXY($posx + 30, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 26 : $tab_top + 24);
            $pdf->MultiCell(38, 6, $langs->trans("Accord"), 0, 'L', 1);
            $pdf->SetXY($posx + 30, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 30 : $tab_top + 28);
            $pdf->MultiCell(38, 6, 'IP: ' . $obj->ip, 0, 'L', 1);

            $this->_stamp($conf, $pdf, $posx, $tab_top);
        }

        $this->_use_certificate($conf, $pdf);

        return ($tab_hl * 7);
    }

    // COMMANDE, COMMANDE FOURNISSEUR, EXPEDITION

    /**
     *	Show area for the customer to sign
     *
     *	@param	PDF			$pdf            Object PDF
     *	@param  Object		$object         Object
     *	@param	int			$posy			Position depart
     *	@param	Translate	$outputlangs	Objet langs
     *	@return int							Position pour suite
     */
    public function _signature_area_simple_or_double(&$pdf, $object, $posy, $outputlangs, $db, $ref, $langs, $page_largeur, $marge_droite, $emetteur_name)
    {
        global $conf;
        $default_font_size = pdf_getPDFFontSize($outputlangs);
        $tab_top = $posy + 4;
        $tab_hl = 8;
        $posx = 120;
        $largcol = ($page_largeur - $marge_droite - $posx);
        $useborder = 0;
        $index = 0;

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

            $pdf->SetFillColor(255, 255, 255);
            $pdf->SetXY($posx, $tab_top + 0);
            $pdf->SetFont('', '', $default_font_size - 2);
            $pdf->MultiCell($largcol, $tab_hl, $outputlangs->transnoentities("ProposalCustomerSignature"), 0, 'L', 1);
            $pdf->SetXY($posx, $tab_top + $tab_hl);
            $pdf->MultiCell($largcol, $tab_hl * 3, '', 1, 'R');
            if (!empty($conf->global->MAIN_PDF_PROPAL_USE_ELECTRONIC_SIGNING)) {
                $pdf->addEmptySignatureAppearance($posx, $tab_top + $tab_hl, $largcol, $tab_hl * 3);
            }

            $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $ref . "' AND entity = " . $conf->entity);
            $obj = $db->fetch_object($resql);
            if ($obj && $obj->pathoffile !== "document generated") {
                $img_base64_encoded = $obj->pathoffile;
                $img = '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '">';
                $pdf->writeHTMLCell(28,'', 122, $tab_top + 14, $img, 0, 0, false, true, '', true);
                // $pdf->Image($obj->pathoffile, 124, $tab_top + 14, 24); --> THIS ONLY WORKS ON DOLIBARR 11 ++
                if ($conf->global->SIGNATURE_DISPLAY_NAME == "true") {
                    $pdf->SetXY(152, $tab_top + 12);
                    $pdf->MultiCell(38, 6, $obj->name, 0, 'L', 1);
                }
                $pdf->SetXY(152, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 20 : $tab_top + 18);
                $pdf->MultiCell(38, 6, $langs->trans("Accord"), 0, 'L', 1);
                $pdf->SetXY(152, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 16 : $tab_top + 14);
                $pdf->MultiCell(38, 6, date("d/m/Y, H:i:s", strtotime($obj->tms)), 0, 'L', 1);
                $pdf->SetXY(152, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 24 : $tab_top + 22);
                $pdf->MultiCell(38, 6, 'IP: ' . $obj->ip, 0, 'L', 1);
            }
        } else {

            $pdf->SetFillColor(255, 255, 255);
            $pdf->SetXY($posx, $tab_top + 0);
            $pdf->SetFont('', '', $default_font_size - 2);
            $pdf->MultiCell($largcol, $tab_hl, $outputlangs->transnoentities("ProposalCustomerSignature"), 0, 'L', 1);
            $pdf->SetXY($posx, $tab_top + 6);
            $pdf->MultiCell($largcol, $tab_hl, $object->thirdparty->name . " :", 0, 'L', 1);
            $pdf->SetXY($posx, $tab_top + $tab_hl + 6);
            $pdf->MultiCell($largcol, $tab_hl * 3, '', 1, 'R');
            if (!empty($conf->global->MAIN_PDF_PROPAL_USE_ELECTRONIC_SIGNING)) {
                $pdf->addEmptySignatureAppearance($posx, $tab_top + $tab_hl, $largcol, $tab_hl * 3);
            }

            $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $ref . "' AND object_id = 0 AND entity = " . $conf->entity);
            $obj = $db->fetch_object($resql);
            if ($obj && $obj->pathoffile !== "document generated") {
                $img_base64_encoded = $obj->pathoffile;
                $img = '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '">';
                $pdf->writeHTMLCell(28,'', 122, $tab_top + 20, $img, 0, 0, false, true, '', true);
                // $pdf->Image($obj->pathoffile, 124, $tab_top + 20, 24);
                if ($conf->global->SIGNATURE_DISPLAY_NAME == "true") {
                    $pdf->SetXY(150, $tab_top + 18);
                    $pdf->MultiCell(49, 6, $obj->name, 0, 'L', 1);
                }
                $pdf->SetXY(150, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 22 : $tab_top + 20);
                $pdf->MultiCell(38, 6, date("d/m/Y, H:i:s", strtotime($obj->tms)), 0, 'L', 1);
                $pdf->SetXY(150, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 26 : $tab_top + 24);
                $pdf->MultiCell(38, 6, $langs->trans("Accord"), 0, 'L', 1);
                $pdf->SetXY(150, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 30 : $tab_top + 28);
                $pdf->MultiCell(38, 6, 'IP: ' . $obj->ip, 0, 'L', 1);
            }

            //--//

            $tab_top = $posy + 4;
            $tab_hl = 8;
            $posx = 10;
            $largcol = 80;
            $pdf->SetFillColor(255, 255, 255);
            $pdf->SetXY($posx, $tab_top + 0);
            $pdf->SetFont('', '', $default_font_size - 2);
            $pdf->MultiCell($largcol, $tab_hl, $outputlangs->transnoentities("ProposalCustomerSignature"), 0, 'L', 1);
            $pdf->SetXY($posx, $tab_top + 6);
            $pdf->MultiCell($largcol, $tab_hl, $emetteur_name . " :", 0, 'L', 1);
            $pdf->SetXY($posx, $tab_top + $tab_hl + 6);
            $pdf->MultiCell($largcol, $tab_hl * 3, '', 1, 'R');
            if (!empty($conf->global->MAIN_PDF_PROPAL_USE_ELECTRONIC_SIGNING)) {
                $pdf->addEmptySignatureAppearance($posx, $tab_top + $tab_hl, $largcol, $tab_hl * 3);
            }

            $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $ref . "' AND object_id = 1 AND entity = " . $conf->entity);
            $obj = $db->fetch_object($resql);
            if ($obj && $obj->pathoffile !== "document generated") {
                $img_base64_encoded = $obj->pathoffile;
                $img = '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '">';
                $pdf->writeHTMLCell(28,'', $posx + 2, $tab_top + 20, $img, 0, 0, false, true, '', true);
                // $pdf->Image($obj->pathoffile, $posx + 4, $tab_top + 20, 24);
                if ($conf->global->SIGNATURE_DISPLAY_NAME == "true") {
                    $pdf->SetFillColor(255, 255, 255);
                    $pdf->SetXY($posx + 30, $tab_top + 18);
                    $pdf->MultiCell(49, 6, $obj->name, 0, 'L', 1);
                }
                $pdf->SetXY($posx + 30, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 22 : $tab_top + 20);
                $pdf->MultiCell(38, 6, date("d/m/Y, H:i:s", strtotime($obj->tms)), 0, 'L', 1);
                $pdf->SetXY($posx + 30, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 26 : $tab_top + 24);
                $pdf->MultiCell(38, 6, $langs->trans("Accord"), 0, 'L', 1);
                $pdf->SetXY($posx + 30, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 30 : $tab_top + 28);
                $pdf->MultiCell(38, 6, 'IP: ' . $obj->ip, 0, 'L', 1);

                $this->_stamp($conf, $pdf, $posx, $tab_top);
            }
        }

        $this->_use_certificate($conf, $pdf);

        return ($tab_hl * 7);
    }

    public function _signature_area_custom(&$pdf, $object, $posx, $posy, $outputlangs, $db, $ref, $langs, $page_largeur, $marge_droite, $fk_hash)
    {
        global $conf;
        $posx -= 8;
        $posy -= 4;
        $default_font_size = pdf_getPDFFontSize($outputlangs);
        $tab_hl = 8;
        $useborder = 0;
        $index = 0;
        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetFont('', '', $default_font_size - 2);

        $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_custom WHERE object_type = '" . $ref . "' AND entity = " . $conf->entity . " AND fk_hash = " . $fk_hash);
        $obj = $db->fetch_object($resql);
        if ($obj && $obj->pathoffile !== "document generated") {
            $img_base64_encoded = $obj->pathoffile;
            $img = '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '">';
            $pdf->writeHTMLCell(28,'', $posx + 2, $posy, $img, 0, 0, false, true, '', true);
            // $pdf->Image($obj->pathoffile, 124, $tab_top + 14, 24); --> THIS ONLY WORKS ON DOLIBARR 11 ++
            if ($conf->global->SIGNATURE_DISPLAY_NAME == "true") {
                $pdf->SetXY($posx + 30, $posy - 2);
                $pdf->MultiCell(49, 6, $obj->name, 0, 'L', 1);
            }
            $pdf->SetXY($posx + 30, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $posy + 6 : $posy + 4);
            $pdf->MultiCell(38, 6, $langs->trans("Accord"), 0, 'L', 1);
            $pdf->SetXY($posx + 30, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $posy + 2 : $posy);
            $pdf->MultiCell(38, 6, date("d/m/Y, H:i:s", strtotime($obj->tms)), 0, 'L', 1);
            $pdf->SetXY($posx + 30, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $posy + 10 : $posy + 8);
            $pdf->MultiCell(38, 6, 'IP: ' . $obj->ip, 0, 'L', 1);
        }

        $this->_use_certificate($conf, $pdf);

        return ($tab_hl * 7);
    }

    // AGEFODD

    /**
     *	Show area for the customer to sign
     *
     *	@param	PDF			$pdf            Object PDF
     *	@param  Object		$object         Object
     *	@param	int			$posy			Position depart
     *	@param	Translate	$outputlangs	Objet langs
     *	@return int							Position pour suite
     */
    public function _signature_area_agefodd(&$pdf, $object, $posy, $outputlangs, $db, $ref, $langs, $page_largeur, $marge_droite, $emetteur_name, $thirdparty_name, $convid)
    {
        global $conf;
        $outputlangs->loadLangs(array("propal", "mbisignature", "holiday"));
        $default_font_size = pdf_getPDFFontSize($outputlangs);
        $tab_top = $posy + 4;
        $tab_hl = 8;
        $posx = 120;
        $largcol = ($page_largeur - $marge_droite - $posx);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetFont('', '', $default_font_size - 2);
        $pdf->SetXY($posx, $tab_top + $tab_hl + 6);
        // $pdf->MultiCell($largcol, $tab_hl * 3, '', 1, 'R');
        if (!empty($conf->global->MAIN_PDF_PROPAL_USE_ELECTRONIC_SIGNING)) {
            $pdf->addEmptySignatureAppearance($posx, $tab_top + $tab_hl, $largcol, $tab_hl * 3);
        }

        $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_agefodd WHERE object_type = '" . $ref . "' AND object_id = 0 AND convid = '" . $convid . "' AND entity = " . $conf->entity);
        $obj = $db->fetch_object($resql);
        if ($obj && $obj->pathoffile !== "document generated") {
            $img_base64_encoded = $obj->pathoffile;
            $img = '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '">';
            $pdf->writeHTMLCell(28,'', 122, $tab_top + 20, $img, 0, 0, false, true, '', true);
            // $pdf->Image($obj->pathoffile, 124, $tab_top + 20, 24);
            if ($conf->global->SIGNATURE_DISPLAY_NAME == "true") {
                $pdf->SetXY(150, $tab_top + 18);
                $pdf->MultiCell(49, 6, $obj->name, 0, 'L', 1);
            }
            $pdf->SetXY(150, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 22 : $tab_top + 20);
            $pdf->MultiCell(38, 6, date("d/m/Y, H:i:s", strtotime($obj->tms)), 0, 'L', 1);
            $pdf->SetXY(150, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 26 : $tab_top + 24);
            $pdf->MultiCell(38, 6, "LU ET APPROUVÉ", 0, 'L', 1);
            $pdf->SetXY(150, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 30 : $tab_top + 28);
            $pdf->MultiCell(38, 6, 'IP: ' . $obj->ip, 0, 'L', 1);
        }

        //--//

        $tab_top = $posy + 4;
        $tab_hl = 8;
        $posx = 10;
        $largcol = 80;
        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetFont('', '', $default_font_size - 2);
        $pdf->SetXY($posx, $tab_top + $tab_hl + 6);
        // $pdf->MultiCell($largcol, $tab_hl * 3, '', 1, 'R');
        if (!empty($conf->global->MAIN_PDF_PROPAL_USE_ELECTRONIC_SIGNING)) {
            $pdf->addEmptySignatureAppearance($posx, $tab_top + $tab_hl, $largcol, $tab_hl * 3);
        }

        $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature_agefodd WHERE object_type = '" . $ref . "' AND object_id = 1 AND convid = '" . $convid . "' AND entity = " . $conf->entity);
        $obj = $db->fetch_object($resql);
        if ($obj && $obj->pathoffile !== "document generated") {
            $img_base64_encoded = $obj->pathoffile;
            $img = '<img src="@' . preg_replace('#^data:image/[^;]+;base64,#', '', $img_base64_encoded) . '">';
            $pdf->writeHTMLCell(28,'', $posx + 2, $tab_top + 20, $img, 0, 0, false, true, '', true);
            // $pdf->Image($obj->pathoffile, $posx + 4, $tab_top + 20, 24);
            if ($conf->global->SIGNATURE_DISPLAY_NAME == "true") {
                $pdf->SetXY($posx + 30, $tab_top + 18);
                $pdf->MultiCell(49, 6, $obj->name, 0, 'L', 1);
            }
            $pdf->SetXY($posx + 30, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 22 : $tab_top + 20);
            $pdf->MultiCell(38, 6, date("d/m/Y, H:i:s", strtotime($obj->tms)), 0, 'L', 1);
            $pdf->SetXY($posx + 30, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 26 : $tab_top + 24);
            $pdf->MultiCell(38, 6, "LU ET APPROUVÉ", 0, 'L', 1);
            $pdf->SetXY($posx + 30, $conf->global->SIGNATURE_DISPLAY_NAME == "true" ? $tab_top + 30 : $tab_top + 28);
            $pdf->MultiCell(38, 6, 'IP: ' . $obj->ip, 0, 'L', 1);
        }

        $this->_use_certificate($conf, $pdf);

        return ($tab_hl * 7);
    }

    protected function _use_certificate($conf, $pdf)
    {
        if ($conf->global->SIGNATURE_USE_CERTIFICATE == "true" && file_exists($conf->global->SIGNATURE_CRT) && file_exists($conf->global->SIGNATURE_CRT_PRIVATE_KEY)) {
            $certificate = "file://" . realpath($conf->global->SIGNATURE_CRT);
            $private_key = "file://" . realpath($conf->global->SIGNATURE_CRT_PRIVATE_KEY);
            $info = array('Name' => 'TCPDF', 'Location' => 'Dolibarr', 'Reason' => 'Automatic signature with MBI Signature module', 'ContactInfo' => '');
            $pdf->setSignature($certificate, $private_key, 'mbisignature', '', 2, $info);
        }
    }

    protected function _stamp($conf, $pdf, $posx, $tab_top)
    {
        if ($conf->global->SIGNATURE_COMPANY_STAMP && file_exists($conf->global->SIGNATURE_COMPANY_STAMP)) {
            $pdf->SetAlpha(0.5);
            $pdf->StartTransform();
            $pdf->Rotate(-8, $posx + 70, $tab_top);
            $img = '<img src="' . $conf->global->SIGNATURE_COMPANY_STAMP . '">';
            $pdf->writeHTMLCell(42, '', $posx + 70, $tab_top, $img, 0, 0, false, true, '', true);
            $pdf->StopTransform();
            $pdf->SetAlpha(1);
        }
    }
}