Accountancyexport - Système de Hook

Bonjour Dolibarriens !

Je développe un module qui vient ajouter un type d’export en comptabilité mais j’ai du mal à cerner le système de hook…

Voici les éléments de la page (accountancyexport.class.php) en question :

Ligne 111 → $hookmanager->initHooks(array(‹ accountancyexport ›));

Ligne 148 → $reshook = $hookmanager->executeHooks(‹ getType ›, $parameters, $listofexporttypes);

Dans le fichier actions_mymodule.class.php je voulais savoir comment utiliser les contextes, actuellement je voudrai utiliser pour 2 contextes de accountancyexport → GetType et getFormatCode

  • voici le code que j’ai :

      function doActions($parameters, &$object, &$action, $hookmanager)
    

    {
    $error = 0; // Error counter
    $myvalue = ‹ test ›; // A result value

      print_r($parameters);
      echo "action: " . $action;
      print_r($object);
    
      if (in_array('getType', explode(':', $parameters['accountancyexport'])))
      {
        // ZONE POUR MON SCRIPT POUR LE HOOK GetType
    
      }
      if (in_array('getFormatCode', explode(':', 
    

$parameters[‹ accountancyexport ›])))
{
// ZONE POUR MON SCRIPT POUR LE HOOK getFormatCode

	}

	if (! $error)
	{
		$this->results = array('myreturn' => $myvalue);
		$this->resprints = 'A text to show';
		return 0; // or return 1 to replace standard code
	}
	else
	{
		$this->errors[] = 'Error message';
		return -1;
	}
}

Merci d’avance pour le temps que vous prendrez à m’aider,
Bonne journée,
Paul

Bonjour,

Votre fonction ne doit pas s’appeler doActions mais getType ou getFormatCode

Bonjour et bonne année.
Un petit tour sur le wiki ? Système de Hooks - Dolibarr ERP CRM Wiki

Bonjour Ksar,

Excusez-moi du délais de réponse, je n’étais pas disponible.
Merci pour votre réponse.

J’ai changé et créer 4 fonctions distinctes pour éviter de potentiels conflits.

      function getType($parameters, &$object, &$action, $hookmanager)
{
$error = 0; // Error counter
$myvalue = "test"; // A result value

  print_r($parameters);
  echo "action: " . $action;
  print_r($object);

  if (in_array('getType', explode(':', $parameters['accountancyexport'])))
  {
			self::$EXPORT_CODE_CHANTIER => $langs->trans('Modelcsv_codechantier'),

  }

	if (! $error)
	{
		$this->results = array('myreturn' => $myvalue);
		$this->resprints = 'A text to show';
		return 0; // or return 1 to replace standard code
	}
	else
	{
		$this->errors[] = 'Error message';
		return -1;
	}
}
      function getFormatCode($parameters, &$object, &$action, $hookmanager)
{
$error = 0; // Error counter
$myvalue = "test"; // A result value

  print_r($parameters);
  echo "action: " . $action;
  print_r($object);

  if (in_array('getFormatCode', explode(':', $parameters["accountancyexport"])))
{
// ZONE POUR MON SCRIPT POUR LE HOOK getFormatCode
			self::$EXPORT_CODE_CHANTIER => 'csv',

	}

	if (! $error)
	{
		$this->results = array('myreturn' => $myvalue);
		$this->resprints = 'A text to show';
		return 0; // or return 1 to replace standard code
	}
	else
	{
		$this->errors[] = 'Error message';
		return -1;
	}
}
     function getTypeConfig($parameters, &$object, &$action, $hookmanager)
{
$error = 0; // Error counter
$myvalue = "test"; // A result value

  print_r($parameters);
  echo "action: " . $action;
  print_r($object);

  if (in_array('getTypeConfig', explode(':', $parameters["accountancyexport"])))
{
// ZONE POUR MON SCRIPT POUR LE HOOK getTypeConfig
				self::$EXPORT_CODE_CHANTIER => array(
					'label' => $langs->trans('Modelcsv_codechantier'),
					'ACCOUNTING_EXPORT_FORMAT' => empty($conf->global->ACCOUNTING_EXPORT_FORMAT) ? 'txt' : $conf->global->ACCOUNTING_EXPORT_FORMAT,
					'ACCOUNTING_EXPORT_SEPARATORCSV' => empty($conf->global->ACCOUNTING_EXPORT_SEPARATORCSV) ? ',' : $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV,
					'ACCOUNTING_EXPORT_ENDLINE' => empty($conf->global->ACCOUNTING_EXPORT_ENDLINE) ? 1 : $conf->global->ACCOUNTING_EXPORT_ENDLINE,
					'ACCOUNTING_EXPORT_DATE' => getDolGlobalString('ACCOUNTING_EXPORT_DATE', '%Y-%m-%d'),
				),
	}

	if (! $error)
	{
		$this->results = array('myreturn' => $myvalue);
		$this->resprints = 'A text to show';
		return 0; // or return 1 to replace standard code
	}
	else
	{
		$this->errors[] = 'Error message';
		return -1;
	}
}
     function export($parameters, &$object, &$action, $hookmanager)
{
$error = 0; // Error counter
$myvalue = "test"; // A result value

  print_r($parameters);
  echo "action: " . $action;
  print_r($object);

  if (in_array('export', explode(':', $parameters["accountancyexport"])))
{
// ZONE POUR MON SCRIPT POUR LE HOOK getTypeConfig
public function exportConfigurable($objectLines, $exportFile = null)
{
    require_once DOL_DOCUMENT_ROOT.'/main.inc.php';

    global $conf;

    $separator = $this->separator;
    dol_include_once('/fourn/class/fournisseur.facture.class.php');
        dol_include_once('/fourn/class/fournisseur.facture-rec.class.php');


    foreach ($objectLines as $line) {
        $date_document = dol_print_date($line->doc_date, $conf->global->ACCOUNTING_EXPORT_DATE);

        $tab = array();

        $codechantier = ''; // Initialisation de la variable pour stocker la valeur de 'codechantier'

        // Récupération de la valeur de 'rowid' depuis la propriété 'fk_facture_fourn'
        $roo = $line->fk_docdet; // Assurez-vous que cette propriété est correcte

        // Construction de la requête SQL pour récupérer la valeur de 'codechantier'
        $sql = "SELECT e.codechantier
                FROM llx_facture_fourn f
                JOIN llx_facture_fourn_det d ON f.rowid = d.fk_facture_fourn
                JOIN llx_accounting_bookkeeping b ON d.fk_facture_fourn = b.fk_doc
                JOIN llx_facture_fourn_det_extrafields e ON d.rowid = e.fk_object
                WHERE d.rowid = $roo";

        // Exécution de la requête SQL
        $res = $this->db->query($sql);

        if ($res) {
            if ($this->db->num_rows($res) > 0) {
                $row = $this->db->fetch_array($res);
                $codechantier = $row['codechantier']; // Récupération de la valeur de 'codechantier'
            }
        } else {
            // Gestion de l'erreur si la requête échoue
            dol_syslog('Erreur lors de l\'exécution de la requête SQL : ' . $this->db->lasterror());
        }

        // Export configurable
        $tab[] = $line->piece_num;
        $tab[] = $date_document;
        $tab[] = $line->doc_ref;
        $tab[] = preg_match('/'.$separator.'/', $line->label_operation) ? "'".$line->label_operation."'" : $line->label_operation;
        $tab[] = length_accountg($line->numero_compte);
        $tab[] = length_accounta($line->subledger_account);
        $tab[] = price2num($line->debit);
        $tab[] = price2num($line->credit);
        $tab[] = price2num($line->debit - $line->credit);
        $tab[] = $line->code_journal;
        $tab[] =  $codechantier;

        $output = implode($separator, $tab) . $this->end_line;
        if ($exportFile) {
            fwrite($exportFile, $output);
        } else {
            print $output;
        }
    }
}
	}

	if (! $error)
	{
		$this->results = array('myreturn' => $myvalue);
		$this->resprints = 'A text to show';
		return 0; // or return 1 to replace standard code
	}
	else
	{
		$this->errors[] = 'Error message';
		return -1;
	}
}

Voici les 4 fonctions que j’ai déclaré en suivant les 4 executehook du fichier, mais je n’ai pas vu de changements…
À savoir que la page où j’aimerai injecter mes hooks, est accountancyexport.class.php soit une page contenant des types d’export étant appelé uniquement lors de l’export comptable sous forme d’une petite fenêtre lors d’un clic sur la page accountancy/bookkeeping/export.php afin de sélectionner un type d’export.

Pour info je me réfère déjà au wiki « Système de Hooks » mais sans réussir à bien cerner le fonctionnement.

Merci pour vos retours,
Paul