Génération d'un mandat de prélèvement depuis l'api

Bonjour à vous,
J’aimerais, depuis l’api, générer puis envoyer au client qui se sert de l’api un mandat sepa.
Le téléchargement et l’envoie du mandat sepa pour moi n’est pas un problème. Il me suffira de le transformer en base 64 et de l’envoyer dans un json.
Cependant, je souhaite générer le mandat sepa depuis l’api. je veux générer le même fichier que celui qui est généré à cette page societe/rib.php?socid={id}#builddoc.
Quelqu’un pourrait-il me venir en aide s’il vous plaît?

voici mon code.

PS: si le message n’est pas approprié pour le forum, merci de m’indiquer où je pourrais me renseigner d’avantage.

Merci d’avance pour vos réponses

<?php
/* Copyright (C) 2015   Jean-François Ferry     <[email protected]>
 * Copyright (C) 2018 CHENE Pierre
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

use Luracast\Restler\RestException;
require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';



/**
 * API class for apigenrib 
 *
 * @smart-auto-routing false
 * @access protected
 * @class  DolibarrApiAccess {@requires user,external}
 */
class ApiGenRibApi extends DolibarrApi
{
    public function __construct()
    {
        global $db;
        $this->db = $db;
    }

    /**
     * Generate a sepamandate Document
     * 
     * @param int $socid thirdparty id
     * 
     * @return array Check success
     * 
     * @url GET /generateMandat/{socid}
     */
    public function generateMandat($socid){
        $model = "sepamandate";

        $object = new Societe($this->db);
        $object->fetch($socid);

        $action = 'builddoc';
        $upload_dir = $conf->societe->multidir_output[$object->entity];
        if(! DolibarrApiAccess::$user->rights->societe->creer)
            throw new RestException(401);
    
    
        // Reload to get all modified line records and be ready for hooks
        $ret = $object->fetch($id);
        $ret = $object->fetch_thirdparty();


        $object->setDocModel($user, $model);

        $object->fk_bank = $object->fk_account;

        $outputlangs = $langs;
        $newlang='';

        

        if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang=GETPOST('lang_id','aZ09');
        if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($object->thirdparty->default_lang)) $newlang=$object->thirdparty->default_lang;  // for proposal, order, invoice, ...
        if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($object->default_lang)) $newlang=$object->default_lang;                  // for thirdparty
        if (! empty($newlang))
        {
            $outputlangs = new Translate("",$conf);
            $outputlangs->setDefaultLang($newlang);
        }

        // To be sure vars is defined
        if (empty($hidedetails)) $hidedetails=0;
        if (empty($hidedesc)) $hidedesc=0;
        if (empty($hideref)) $hideref=0;
        if (empty($moreparams)) $moreparams=null;


        $sql = "SELECT rowid";
		$sql.= " FROM ".MAIN_DB_PREFIX."societe_rib";
		if ($socid) $sql.= " WHERE fk_soc  = ".$socid." ";

	
		$result = $this->db->query($sql);

		if($result->num_rows == 0 ){
			throw new RestException(404, 'Account not found');
		}	

		$i=0;

		$accounts =[];

		if ($result)
		{
			$num = $this->db->num_rows($result);
			while ($i < $num)
			{
				$obj = $this->db->fetch_object($result);
				$account = new CompanyBankAccount($this->db);
				if($account->fetchFromApi($obj->rowid)) {
					$accounts[] = $account;
				}
				$i++;
			}
		}
		else{
			throw new RestException(404, 'Account not found');
		}

        $moreparams = array(
			'use_companybankid'=>$accounts[0]->id,
			'force_dir_output'=>$conf->societe->multidir_output[$object->entity].'/'.dol_sanitizeFileName($object->id)
        );

        


        $result= $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);

        return array("result" => $result);
        if ($result <= 0)
        {
            throw new RestException(500);
        }
        else
        {
            return array("success" => true);
        }
    }


    
}

Pour commencer la fonction generateDocument() retourne un entier selon la réussite ou non de la fonction. Du coup tu ne retourne pas les infos du fichier créer. L’API document utilise du php de base pour lire le contenu a voir ce que tu peux faire.

Pour avoir une reference des fonctions regarde ici https://doxygen.dolibarr.org/dc/dbc/class_societe.html#a7d588d0f43e0211dbf2467093c7a317b
Par exemple pour recuperer les comptes tu as $ribs = $object->get_all_rib();

Enfin tout ce qu’il y a après le premier return ne sert plus à rien puisque la fonction s’arrete dès qu’il y a un return.

Bon courage et n’hésite pas à proposer ton api sur Github pour en faire profiter tout le monde.

1 « J'aime »

Bonjour Timmy63

Merci pour ta réponse.

Je sais bien que la fonction me renvoie un entier.
C’est d’alleur mon but, d’ailleur récupérer l’entier pour savoir si le document a été créé.
Une fois que l’entier me dit que le document a bien été créé, là j’irai télécharger le document.
Le problème est que la fonction generateDocument() me retourne 1, donc théoriquement le fichier est généré, sauf que bah non, il est pas généré.
C’est là mon problème.
Le premier return n’était que temporaire, pour vérifier si mon if fonctionnait bien

Mon objectif est d’intégrer toutes les fonctionnalités que j’aurai faîtes à Dolibarr