* Copyright (C) 2025 Eduard Wisch * * 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 . */ /** * \file supplierlink3/class/actions_supplierlink3.class.php * \ingroup supplierlink3 * \brief Example hook overload. * * TODO: Write detailed description here. */ require_once DOL_DOCUMENT_ROOT.'/core/class/commonhookactions.class.php'; /** * Class ActionsSupplierLink3 */ class ActionsSupplierLink3 extends CommonHookActions { /** * @var DoliDB Database handler. */ public $db; /** * @var int Debug mode (0=off, 1=on) */ private $debug = 0; /** * @var string Debug log file path */ private $debugLogFile = '/tmp/supplierlink3_debug.log'; /** * @var string Error code (or message) */ public $error = ''; /** * @var string[] Errors */ public $errors = array(); /** * @var mixed[] Hook results. Propagated to $hookmanager->resArray for later reuse */ public $results = array(); /** * @var ?string String displayed by executeHook() immediately after return */ public $resprints; /** * @var int Priority of hook (50 is used if value is not defined) */ public $priority; /** * Constructor * * @param DoliDB $db Database handler */ /** * @var string Shop icon class (FontAwesome) */ private $shopIcon = 'fas fa-store'; /** * Constructor * * @param DoliDB $db Database handler */ public function __construct($db) { $this->db = $db; // Debug-Modus aus Dolibarr-Konfiguration laden (Standard: AUS) $this->debug = getDolGlobalInt('SUPPLIERLINK3_DEBUG_MODE', 0); // Shop-Icon aus Konfiguration laden $this->shopIcon = getDolGlobalString('SUPPLIERLINK3_SHOP_ICON', 'fas fa-store'); // Nur loggen wenn Debug aktiviert if ($this->debug) { error_log('['.date('Y-m-d H:i:s').'] [CONSTRUCTOR] ActionsSupplierLink3 instanziiert'."\n", 3, $this->debugLogFile); } } /** * Debug-Log schreiben * * @param string $message Nachricht * @param string $method Methodenname */ private function debugLog($message, $method = '') { if (!$this->debug) { return; } $logLine = '['.date('Y-m-d H:i:s').']'; if ($method) { $logLine .= ' ['.$method.']'; } $logLine .= ' '.$message."\n"; error_log($logLine, 3, $this->debugLogFile); } /** * Execute action * * @param array $parameters Array of parameters * @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 'add', 'update', 'view' * @return int Return integer <0 if KO, * =0 if OK but we want to process standard actions too, * >0 if OK and we want to replace standard actions. */ public function getNomUrl($parameters, &$object, &$action) { global $db, $langs, $conf, $user; $this->resprints = ''; return 0; } /** * Overload the doActions function : replacing the parent's function with the one below * * @param array $parameters Hook metadata (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 Return integer < 0 on error, 0 on success, 1 to replace standard code */ public function doActions($parameters, &$object, &$action, $hookmanager) { $error = 0; // Error counter /* print_r($parameters); print_r($object); echo "action: " . $action; */ if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2' // Do what you want here... // You can for example load and use call global vars like $fieldstosearchall to overwrite them, or update the database depending on $action and GETPOST values. if (!$error) { $this->results = array('myreturn' => 999); $this->resprints = 'A text to show'; return 0; // or return 1 to replace standard code } else { $this->errors[] = 'Error message'; return -1; } } return 0; } /** * Overload the doMassActions function : replacing the parent's function with the one below * * @param array $parameters Hook metadata (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 Return integer < 0 on error, 0 on success, 1 to replace standard code */ public function doMassActions($parameters, &$object, &$action, $hookmanager) { global $conf, $user, $langs; $error = 0; // Error counter /* print_r($parameters); print_r($object); echo "action: " . $action; */ if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2' // @phan-suppress-next-line PhanPluginEmptyStatementForeachLoop foreach ($parameters['toselect'] as $objectid) { // Do action on each object id } if (!$error) { $this->results = array('myreturn' => 999); $this->resprints = 'A text to show'; return 0; // or return 1 to replace standard code } else { $this->errors[] = 'Error message'; return -1; } } return 0; } /** * Overload the addMoreMassActions function : replacing the parent's function with the one below * * @param array $parameters Hook metadata (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 Return integer < 0 on error, 0 on success, 1 to replace standard code */ public function addMoreMassActions($parameters, &$object, &$action, $hookmanager) { global $conf, $user, $langs; $error = 0; // Error counter $disabled = 1; /* print_r($parameters); print_r($object); echo "action: " . $action; */ if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2' $this->resprints = ''; } if (!$error) { return 0; // or return 1 to replace standard code } else { $this->errors[] = 'Error message'; return -1; } } /** * Execute action before PDF (document) creation * * @param array $parameters Array of parameters * @param CommonObject $object Object output on PDF * @param string $action 'add', 'update', 'view' * @return int Return integer <0 if KO, * =0 if OK but we want to process standard actions too, * >0 if OK and we want to replace standard actions. */ public function beforePDFCreation($parameters, &$object, &$action) { global $conf, $user, $langs; global $hookmanager; $outputlangs = $langs; $ret = 0; $deltemp = array(); dol_syslog(get_class($this).'::executeHooks action='.$action); /* print_r($parameters); print_r($object); echo "action: " . $action; */ // @phan-suppress-next-line PhanPluginEmptyStatementIf if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2' } return $ret; } /** * Execute action after PDF (document) creation * * @param array $parameters Array of parameters * @param CommonDocGenerator $pdfhandler PDF builder handler * @param string $action 'add', 'update', 'view' * @return int Return integer <0 if KO, * =0 if OK but we want to process standard actions too, * >0 if OK and we want to replace standard actions. */ public function afterPDFCreation($parameters, &$pdfhandler, &$action) { global $conf, $user, $langs; global $hookmanager; $outputlangs = $langs; $ret = 0; $deltemp = array(); dol_syslog(get_class($this).'::executeHooks action='.$action); /* print_r($parameters); print_r($object); echo "action: " . $action; */ // @phan-suppress-next-line PhanPluginEmptyStatementIf if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2' } return $ret; } /** * Overload the loadDataForCustomReports function : returns data to complete the customreport tool * * @param array $parameters Hook metadata (context, 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 Return integer < 0 on error, 0 on success, 1 to replace standard code */ public function loadDataForCustomReports($parameters, &$action, $hookmanager) { global $langs; $langs->load("supplierlink3@supplierlink3"); $this->results = array(); $head = array(); $h = 0; if ($parameters['tabfamily'] == 'supplierlink3') { $head[$h][0] = dol_buildpath('/module/index.php', 1); $head[$h][1] = $langs->trans("Home"); $head[$h][2] = 'home'; $h++; $this->results['title'] = $langs->trans("SupplierLink3"); $this->results['picto'] = 'supplierlink3@supplierlink3'; } $head[$h][0] = 'customreports.php?objecttype='.$parameters['objecttype'].(empty($parameters['tabfamily']) ? '' : '&tabfamily='.$parameters['tabfamily']); $head[$h][1] = $langs->trans("CustomReports"); $head[$h][2] = 'customreports'; $this->results['head'] = $head; $arrayoftypes = array(); //$arrayoftypes['supplierlink3_myobject'] = array('label' => 'MyObject', 'picto'=>'myobject@supplierlink3', 'ObjectClassName' => 'MyObject', 'enabled' => isModEnabled('supplierlink3'), 'ClassPath' => "/supplierlink3/class/myobject.class.php", 'langs'=>'supplierlink3@supplierlink3') $this->results['arrayoftype'] = $arrayoftypes; return 0; } /** * Overload the restrictedArea function : check permission on an object * * @param array $parameters Hook metadata (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 Return integer <0 if KO, * =0 if OK but we want to process standard actions too, * >0 if OK and we want to replace standard actions. */ public function restrictedArea($parameters, $object, &$action, $hookmanager) { global $user; if ($parameters['features'] == 'myobject') { if ($user->hasRight('supplierlink3', 'myobject', 'read')) { $this->results['result'] = 1; return 1; } else { $this->results['result'] = 0; return 1; } } return 0; } /** * Execute action completeTabsHead * * @param array $parameters Array of parameters * @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 'add', 'update', 'view' * @param Hookmanager $hookmanager Hookmanager * @return int Return integer <0 if KO, * =0 if OK but we want to process standard actions too, * >0 if OK and we want to replace standard actions. */ public function completeTabsHead(&$parameters, &$object, &$action, $hookmanager) { global $langs, $conf, $user; if (!isset($parameters['object']->element)) { return 0; } if ($parameters['mode'] == 'remove') { // used to make some tabs removed return 0; } elseif ($parameters['mode'] == 'add') { $langs->load('supplierlink3@supplierlink3'); // used when we want to add some tabs $counter = count($parameters['head']); $element = $parameters['object']->element; $id = $parameters['object']->id; // verifier le type d'onglet comme member_stats où ça ne doit pas apparaitre // if (in_array($element, ['societe', 'member', 'contrat', 'fichinter', 'project', 'propal', 'commande', 'facture', 'order_supplier', 'invoice_supplier'])) { if (in_array($element, ['context1', 'context2'])) { $datacount = 0; $parameters['head'][$counter][0] = dol_buildpath('/supplierlink3/supplierlink3_tab.php', 1) . '?id=' . $id . '&module='.$element; $parameters['head'][$counter][1] = $langs->trans('SupplierLink3Tab'); if ($datacount > 0) { $parameters['head'][$counter][1] .= '' . $datacount . ''; } $parameters['head'][$counter][2] = 'supplierlink3emails'; $counter++; } if ($counter > 0 && (int) DOL_VERSION < 14) { // @phpstan-ignore-line $this->results = $parameters['head']; // return 1 to replace standard code return 1; } else { // From V14 onwards, $parameters['head'] is modifiable by reference return 0; } } else { // Bad value for $parameters['mode'] return -1; } } /** * Overload the showLinkToObjectBlock function : add or replace array of object linkable * * @param array $parameters Hook metadata (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 Return integer < 0 on error, 0 on success, 1 to replace standard code */ /*public function showLinkToObjectBlock($parameters, &$object, &$action, $hookmanager) { $myobject = new MyObject($object->db); $this->results = array('myobject@supplierlink3' => array( 'enabled' => isModEnabled('supplierlink3'), 'perms' => 1, 'label' => 'LinkToMyObject', 'sql' => "SELECT t.rowid, t.ref, t.ref as 'name' FROM " . $this->db->prefix() . $myobject->table_element. " as t "),); return 1; }*/ /* Add other hook methods here... */ /** * Holt alle Lieferanten mit Shop-URL für ein Produkt * * @param int $fk_product Produkt-ID * @return array Array mit Lieferanten-Daten, sortiert nach Preis aufsteigend */ private function getAllSuppliersForProduct($fk_product) { $suppliers = array(); $sql = "SELECT DISTINCT pfp.fk_soc as supplier_id, pfp.ref_fourn, pfp.price, pfp.quantity as min_qty, s.nom as supplier_name, se.shop_url FROM ".MAIN_DB_PREFIX."product_fournisseur_price pfp INNER JOIN ".MAIN_DB_PREFIX."societe s ON s.rowid = pfp.fk_soc LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields se ON se.fk_object = pfp.fk_soc WHERE pfp.fk_product = ".(int)$fk_product." AND se.shop_url IS NOT NULL AND se.shop_url != '' ORDER BY pfp.price ASC"; $resql = $this->db->query($sql); if ($resql) { while ($obj = $this->db->fetch_object($resql)) { $suppliers[] = array( 'supplier_id' => $obj->supplier_id, 'ref_fourn' => $obj->ref_fourn, 'price' => $obj->price, 'min_qty' => $obj->min_qty, 'supplier_name' => $obj->supplier_name, 'shop_url' => rtrim($obj->shop_url, '/'), 'full_url' => rtrim($obj->shop_url, '/') . '/' . $obj->ref_fourn ); } } return $suppliers; } /** * Generiert HTML-Badge für Lagerbestand * * 4 Zustände: * - ROT: stock <= 0 (Nicht verfügbar) * - ORANGE: stock < seuil_stock_alerte (Unter Mindestmenge) * - GRAU: stock < desiredstock (Unter Wunschmenge) * - GRÜN: stock >= desiredstock (Ausreichend) * * @param float $qtyStock Aktueller Lagerbestand * @param float $desiredQty Wunschbestand * @param float $alertQty Mindestbestand (seuil_stock_alerte) * @return string HTML-Badge */ private function getStockBadgeHtml($qtyStock, $desiredQty, $alertQty = 0) { if ($qtyStock <= 0) { // ROT: Nicht verfügbar (0 oder negativ) $badgeClass = 'badge-danger'; $icon = 'fa-times-circle'; $tooltip = 'Nicht auf Lager'; } elseif ($alertQty > 0 && $qtyStock < $alertQty) { // ORANGE: Unter Mindestmenge (Alarm-Schwelle) $badgeClass = 'badge-warning'; $icon = 'fa-exclamation-triangle'; $tooltip = 'Unter Mindestmenge ('.(int)$alertQty.')'; } elseif ($desiredQty > 0 && $qtyStock < $desiredQty) { // GRAU: Unter Wunschmenge $badgeClass = 'badge-secondary'; $icon = 'fa-box-open'; $tooltip = 'Unter Wunschmenge ('.(int)$desiredQty.')'; } else { // GRÜN: Ausreichend auf Lager $badgeClass = 'badge-success'; $icon = 'fa-check-circle'; $tooltip = 'Ausreichend auf Lager'; } $html = ''; $html .= ''; $html .= number_format($qtyStock, 0, ',', '.'); $html .= ''; return $html; } /** * Generiert Shop-Link HTML (Icon oder Modal-Trigger für mehrere Lieferanten) * * @param array $suppliers Array der Lieferanten * @param int $currentSupplierId Aktueller Lieferant der Bestellung * @param string $refFourn Lieferanten-Artikelnummer * @param int $lineId Zeilen-ID für eindeutige Modal-ID * @return string HTML für Shop-Link */ private function getShopLinkHtml($suppliers, $currentSupplierId, $refFourn, $lineId) { if (empty($suppliers)) { return ''; } // Fall 1: Nur ein Lieferant mit Shop-URL - direkter Link if (count($suppliers) == 1) { $supplier = $suppliers[0]; $html = 'shopIcon.'">'; $html .= ''; return $html; } // Fall 2: Mehrere Lieferanten - Modal-Trigger $modalId = 'supplier_modal_'.$lineId; // Trigger-Button mit Dropdown-Pfeil $html = 'shopIcon.'">'; $html .= ''; $html .= ''; // Modal-Container (versteckt) $html .= '