<?php
/* Copyright (C) 2023		Laurent Destailleur			<eldy@users.sourceforge.net>
 * Copyright (C) 2025		Eduard Wisch				<data@data-it-solution.de>
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

/**
 * \file    core/triggers/interface_99_modEpcqr_EpcqrTriggers.class.php
 * \ingroup epcqr
 * \brief   Example of trigger file.
 *
 * You can create other triggered files by copying this one.
 * - File name should be either:
 *      - interface_99_modEpcqr_MyTrigger.class.php
 *      - interface_99_all_MyTrigger.class.php
 * - The file must stay in core/triggers
 * - The class name must be InterfaceMyTrigger
 */

require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';


/**
 *  Class of triggers for Epcqr module
 */
class InterfaceEpcqrTriggers extends DolibarrTriggers
{
	/**
	 * Constructor
	 *
	 * @param DoliDB $db Database handler
	 */
	public function __construct($db)
	{
		parent::__construct($db);
		$this->family = "demo";
		$this->description = "Epcqr triggers.";
		$this->version = self::VERSIONS['dev'];
		$this->picto = 'epcqr@epcqr';
	}

	/**
	 * Function called when a Dolibarr business event is done.
	 * All functions "runTrigger" are triggered if the file is inside the directory core/triggers
	 *
	 * @param string 		$action 	Event action code
	 * @param CommonObject 	$object 	Object
	 * @param User 			$user 		Object user
	 * @param Translate 	$langs 		Object langs
	 * @param Conf 			$conf 		Object conf
	 * @return int              		Return integer <0 if KO, 0 if no triggered ran, >0 if OK
	 */
	public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
	{
		if (!isModEnabled('epcqr')) {
			return 0; // If module is not enabled, we do nothing
		}

		// Put here code you want to execute when a Dolibarr business events occurs.
		// Data and type of action are stored into $object and $action

		// You can isolate code for each action in a separate method: this method should be named like the trigger in camelCase.
		// For example : COMPANY_CREATE => public function companyCreate($action, $object, User $user, Translate $langs, Conf $conf)
		$methodName = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($action)))));
		$callback = array($this, $methodName);
		if (is_callable($callback)) {
			dol_syslog(
				"Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id
			);

			return call_user_func($callback, $action, $object, $user, $langs, $conf);
		}

		// Or you can execute some code here
		switch ($action) {  // @phan-suppress-current-line PhanNoopSwitchCases
			// Bills - QR-Code Generierung bei Rechnungsvalidierung
			case 'BILL_VALIDATE':

				dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);

				// WICHTIG: Objekt neu laden, um finale Rechnungsnummer zu bekommen
				require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
				$invoice = new Facture($this->db);
				$result = $invoice->fetch($object->id);

				if ($result > 0) {
					// QR-Code lokal generieren
					require_once __DIR__.'/../../lib/epcqr.lib.php';
					$qrResult = epcqr_generateQRCodeForInvoice($invoice, $this->db);

					if ($qrResult) {
						dol_syslog("QR-Code erfolgreich generiert für Rechnung ".$invoice->ref, LOG_INFO);
					} else {
						dol_syslog("Fehler beim Generieren des QR-Codes für Rechnung ".$invoice->ref, LOG_ERR);
						// Nicht abbrechen, Rechnung ist trotzdem validiert
					}
				} else {
					dol_syslog("Fehler beim Neuladen der Rechnung", LOG_ERR);
					return -1;
				}
				break;
		}

		return 0;
	}
}
