From f4e1ce4c73c8e503d5ffd54ddf422962d7073fce Mon Sep 17 00:00:00 2001 From: kkhelifa Date: Tue, 7 Mar 2023 14:58:55 +0100 Subject: [PATCH 1/4] NEW - Add a trigger when create a shipping line batch and fix propagate missing errors --- htdocs/expedition/class/expedition.class.php | 3 ++- .../class/expeditionlinebatch.class.php | 20 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/htdocs/expedition/class/expedition.class.php b/htdocs/expedition/class/expedition.class.php index 417ac943493..7b1c8ae6df7 100644 --- a/htdocs/expedition/class/expedition.class.php +++ b/htdocs/expedition/class/expedition.class.php @@ -530,6 +530,7 @@ class Expedition extends CommonObject foreach ($tab as $detbatch) { if ($detbatch->entrepot_id == $stockLocation) { if (!($detbatch->create($line_id) > 0)) { // Create an ExpeditionLineBatch + $this->errors = $detbatch->errors; $error++; } } @@ -3020,7 +3021,7 @@ class ExpeditionLigne extends CommonObjectLine $shipmentLot->qty = $this->detail_batch->qty; $shipmentLot->fk_origin_stock = $batch_id; if ($shipmentLot->create($this->id) < 0) { - $this->errors[] = $shipmentLot->errors; + $this->errors = $shipmentLot->errors; $error++; } } diff --git a/htdocs/expedition/class/expeditionlinebatch.class.php b/htdocs/expedition/class/expeditionlinebatch.class.php index ab1d9123830..b010564768f 100644 --- a/htdocs/expedition/class/expeditionlinebatch.class.php +++ b/htdocs/expedition/class/expeditionlinebatch.class.php @@ -103,11 +103,16 @@ class ExpeditionLineBatch extends CommonObject * Create an expeditiondet_batch DB record link to an expedtiondet record * * @param int $id_line_expdet rowid of expedtiondet record + * @param User $f_user User that create + * @param int $notrigger 1 = disable triggers * @return int <0 if KO, Id of record (>0) if OK */ - public function create($id_line_expdet) + public function create($id_line_expdet, $f_user = null, $notrigger = 0) { + global $user; + $error = 0; + if (!is_object($f_user)) $f_user = $user; $id_line_expdet = (int) $id_line_expdet; @@ -137,13 +142,24 @@ class ExpeditionLineBatch extends CommonObject $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element); $this->fk_expeditiondet = $id_line_expdet; + } + + if (!$error && !$notrigger) { + // Call trigger + $result = $this->call_trigger('EXPEDITIONLINEBATCH_CREATE', $f_user); + if ($result < 0) { + $error++; + } + // End call triggers + } + + if (!$error) { return $this->id; } else { foreach ($this->errors as $errmsg) { dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR); $this->error .= ($this->error ? ', '.$errmsg : $errmsg); } - $this->db->rollback(); return -1 * $error; } } From 7f10ef5215fc4c784a61f819ac3702c85b118992 Mon Sep 17 00:00:00 2001 From: kkhelifa Date: Tue, 7 Mar 2023 17:33:59 +0100 Subject: [PATCH 2/4] NEW - Provide the oldcopy value when calling setValueFrom() function with a trigger key --- htdocs/core/class/commonobject.class.php | 35 +++++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index e3c3dff2b7c..097530c6aa5 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -2089,10 +2089,6 @@ abstract class CommonObject $id_field = 'rowid'; } - $error = 0; - - $this->db->begin(); - // Special case if ($table == 'product' && $field == 'note_private') { $field = 'note'; @@ -2101,6 +2097,32 @@ abstract class CommonObject $fk_user_field = 'fk_user_mod'; } + if ($trigkey) { + $oldvalue = null; + + $sql = "SELECT " . $field; + $sql .= " FROM " . MAIN_DB_PREFIX . $table; + $sql .= " WHERE " . $id_field . " = " . ((int)$id); + + $resql = $this->db->query($sql); + if ($resql) { + if ($obj = $this->db->fetch_object($resql)) { + if ($format == 'date') { + $oldvalue = $this->db->jdate($obj->$field); + } else { + $oldvalue = $obj->$field; + } + } + } else { + $this->error = $this->db->lasterror(); + return -1; + } + } + + $error = 0; + + $this->db->begin(); + $sql = "UPDATE ".$this->db->prefix().$table." SET "; if ($format == 'text') { @@ -2133,6 +2155,11 @@ abstract class CommonObject } else { $result = $this->fetchCommon($id); } + $this->oldcopy = clone $this; + if (property_exists($this->oldcopy, $field)) { + $this->oldcopy->$field = $oldvalue; + } + if ($result >= 0) { $result = $this->call_trigger($trigkey, (!empty($fuser) && is_object($fuser)) ? $fuser : $user); // This may set this->errors } From 30aa377528c6899a56715fd2003235c74007128a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Wed, 8 Mar 2023 08:50:19 +0100 Subject: [PATCH 3/4] use isModEnabled --- htdocs/product/card.php | 8 ++++---- htdocs/variants/combinations.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/htdocs/product/card.php b/htdocs/product/card.php index 241aec60d8e..b42384e934d 100644 --- a/htdocs/product/card.php +++ b/htdocs/product/card.php @@ -207,7 +207,7 @@ if ($cancel) { $action = ''; } -$createbarcode = empty($conf->barcode->enabled) ? 0 : 1; +$createbarcode = isModEnabled('barcode'); if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->barcode->creer_advance)) { $createbarcode = 0; } @@ -1419,7 +1419,7 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action)) { } } - $showbarcode = empty($conf->barcode->enabled) ? 0 : 1; + $showbarcode = isModEnabled('barcode'); if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->barcode->lire_advance)) { $showbarcode = 0; } @@ -1984,7 +1984,7 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action)) { } // Barcode - $showbarcode = empty($conf->barcode->enabled) ? 0 : 1; + $showbarcode = isModEnabled('barcode'); if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->barcode->lire_advance)) { $showbarcode = 0; } @@ -2312,7 +2312,7 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action)) { } else { // Fiche en mode visu - $showbarcode = empty($conf->barcode->enabled) ? 0 : 1; + $showbarcode = isModEnabled('barcode'); if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->barcode->lire_advance)) { $showbarcode = 0; } diff --git a/htdocs/variants/combinations.php b/htdocs/variants/combinations.php index 4c2b77ce766..7424f7474c0 100644 --- a/htdocs/variants/combinations.php +++ b/htdocs/variants/combinations.php @@ -377,7 +377,7 @@ llxHeader("", $title); if (!empty($id) || !empty($ref)) { - $showbarcode = empty($conf->barcode->enabled) ? 0 : 1; + $showbarcode = isModEnabled('barcode'); if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->barcode->lire_advance)) { $showbarcode = 0; } From e47c395fe73688de5f4bf6491c6a33265630f95f Mon Sep 17 00:00:00 2001 From: kkhelifa Date: Fri, 10 Mar 2023 09:14:33 +0100 Subject: [PATCH 4/4] Fix stickler-ci --- htdocs/core/class/commonobject.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 097530c6aa5..bf05df918ce 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -2102,7 +2102,7 @@ abstract class CommonObject $sql = "SELECT " . $field; $sql .= " FROM " . MAIN_DB_PREFIX . $table; - $sql .= " WHERE " . $id_field . " = " . ((int)$id); + $sql .= " WHERE " . $id_field . " = " . ((int) $id); $resql = $this->db->query($sql); if ($resql) {