From 131fa0d52b98b260fb8fce5c7b970c960b3c1609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Fali=C3=A8re?= <121813548+altairis-benjamin@users.noreply.github.com> Date: Thu, 12 Oct 2023 17:50:18 +0200 Subject: [PATCH] NEW subproduct triggers in product class (#25142) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * NEW subproduct triggers in product class * add notrigger parameter to functions * add doc comment for new parameters --------- Co-authored-by: Benjamin Falière --- htdocs/product/class/product.class.php | 52 ++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 6c75e7d0fb2..fce56903456 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -15,7 +15,8 @@ * Copyright (C) 2016-2018 Ferran Marcet * Copyright (C) 2017 Gustavo Novaro * Copyright (C) 2019-2023 Frédéric France - * + * Copyright (C) 2023 Benjamin Falière + * * 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 @@ -4254,10 +4255,13 @@ class Product extends CommonObject * @param int $id_fils Id of child product/service * @param int $qty Quantity * @param int $incdec 1=Increase/decrease stock of child when parent stock increase/decrease + * @param int $notrigger Disable triggers * @return int < 0 if KO, > 0 if OK */ - public function add_sousproduit($id_pere, $id_fils, $qty, $incdec = 1) + public function add_sousproduit($id_pere, $id_fils, $qty, $incdec = 1, $notrigger = 0) { + global $user; + // phpcs:enable // Clean parameters if (!is_numeric($id_pere)) { @@ -4296,6 +4300,17 @@ class Product extends CommonObject dol_print_error($this->db); return -1; } else { + if (!$notrigger) { + // Call trigger + $result = $this->call_trigger('PRODUCT_SUBPRODUCT_ADD', $user); + if ($result < 0) { + $this->error = $this->db->lasterror(); + dol_syslog(get_class($this).'::addSubproduct error='.$this->error, LOG_ERR); + return -1; + } + } + // End call triggers + return 1; } } else { @@ -4313,10 +4328,13 @@ class Product extends CommonObject * @param int $id_fils Id of child product/service * @param int $qty Quantity * @param int $incdec 1=Increase/decrease stock of child when parent stock increase/decrease + * @param int $notrigger Disable triggers * @return int < 0 if KO, > 0 if OK */ - public function update_sousproduit($id_pere, $id_fils, $qty, $incdec = 1) + public function update_sousproduit($id_pere, $id_fils, $qty, $incdec = 1, $notrigger = 0) { + global $user; + // phpcs:enable // Clean parameters if (!is_numeric($id_pere)) { @@ -4341,6 +4359,17 @@ class Product extends CommonObject dol_print_error($this->db); return -1; } else { + if (!$notrigger) { + // Call trigger + $result = $this->call_trigger('PRODUCT_SUBPRODUCT_UPDATE', $user); + if ($result < 0) { + $this->error = $this->db->lasterror(); + dol_syslog(get_class($this).'::updateSubproduct error='.$this->error, LOG_ERR); + return -1; + } + // End call triggers + } + return 1; } } @@ -4351,10 +4380,13 @@ class Product extends CommonObject * * @param int $fk_parent Id of parent product (child will no more be linked to it) * @param int $fk_child Id of child product + * @param int $notrigger Disable triggers * @return int < 0 if KO, > 0 if OK */ - public function del_sousproduit($fk_parent, $fk_child) + public function del_sousproduit($fk_parent, $fk_child, $notrigger = 0) { + global $user; + // phpcs:enable if (!is_numeric($fk_parent)) { $fk_parent = 0; @@ -4391,6 +4423,18 @@ class Product extends CommonObject } } } + + if (!$notrigger) { + // Call trigger + $result = $this->call_trigger('PRODUCT_SUBPRODUCT_DELETE', $user); + if ($result < 0) { + $this->error = $this->db->lasterror(); + dol_syslog(get_class($this).'::delSubproduct error='.$this->error, LOG_ERR); + return -1; + } + // End call triggers + } + return 1; }