From 7d093dc4f47a3042d7e47fb9e1e4d764e2c571bf Mon Sep 17 00:00:00 2001 From: Pierre Ardoin <32256817+mapiolca@users.noreply.github.com> Date: Sat, 27 Jun 2026 18:23:50 +0200 Subject: [PATCH] FIX category links cleanup before deleting commercial objects (#38970) * Delete linked categories on proposals and invoices Ensure category links are removed when deleting proposals, supplier proposals and supplier invoices by adding DELETE queries for categorie_propal, categorie_supplier_proposal and categorie_supplier_invoice and handling DB errors. Reorganize supplier invoice delete logic so category cleanup occurs before discount freeing. Update PHPUnit tests (PropalTest, SupplierProposalTest, FactureFournisseurTest) to create a category link, assert deletion of the category row after object delete, and require the categorie class. * Update FactureFournisseurTest.php * Always delete linked supplier invoice categories Remove the surrounding if(!$error) guard so the DELETE from categorie_supplier_invoice runs regardless of prior errors. This ensures linked categories are always cleaned up for the supplier invoice; preserved existing error handling to record DB failures. * Update PropalTest.php * Update SupplierProposalTest.php --- htdocs/comm/propal/class/propal.class.php | 13 ++++ .../fourn/class/fournisseur.facture.class.php | 61 ++++++++++--------- .../class/supplier_proposal.class.php | 13 ++++ 3 files changed, 57 insertions(+), 30 deletions(-) diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php index 22dac7fa2a9..dd80c0de89a 100644 --- a/htdocs/comm/propal/class/propal.class.php +++ b/htdocs/comm/propal/class/propal.class.php @@ -3151,6 +3151,19 @@ class Propal extends CommonObject // End call triggers } + // Remove linked categories. + if (!$error) { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_propal"; + $sql .= " WHERE fk_propal = ".((int) $this->id); + + $result = $this->db->query($sql); + if (!$result) { + $error++; + $this->error = $this->db->lasterror(); + $this->errors[] = $this->error; + } + } + // Delete extrafields of lines and lines if (!$error && !empty($this->table_element_line)) { $tabletodelete = $this->table_element_line; diff --git a/htdocs/fourn/class/fournisseur.facture.class.php b/htdocs/fourn/class/fournisseur.facture.class.php index f83b0bfaa26..8979af9ce35 100644 --- a/htdocs/fourn/class/fournisseur.facture.class.php +++ b/htdocs/fourn/class/fournisseur.facture.class.php @@ -1483,28 +1483,41 @@ class FactureFournisseur extends CommonInvoice // Fin appel triggers } - // If invoice was converted into a discount not yet consumed, we remove discount - $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'societe_remise_except'; - $sql .= ' WHERE fk_invoice_supplier_source = '.((int) $rowid); - $sql .= ' AND fk_invoice_supplier_line IS NULL'; - $resql = $this->db->query($sql); + // Remove linked categories. + $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_supplier_invoice"; + $sql .= " WHERE fk_supplier_invoice = ".((int) $this->id); - // If invoice has consumned discounts - $this->fetch_lines(); - $list_rowid_det = array(); - foreach ($this->lines as $key => $invoiceline) { - $list_rowid_det[] = $invoiceline->id; + $result = $this->db->query($sql); + if (!$result) { + $error++; + $this->error = $this->db->lasterror(); + $this->errors[] = $this->error; } - // Consumned discounts are freed - if (count($list_rowid_det)) { - $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except'; - $sql .= ' SET fk_invoice_supplier = NULL, fk_invoice_supplier_line = NULL'; - $sql .= ' WHERE fk_invoice_supplier_line IN ('.$this->db->sanitize(implode(',', $list_rowid_det)).')'; + if (!$error) { + // If invoice was converted into a discount not yet consumed, we remove discount + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'societe_remise_except'; + $sql .= ' WHERE fk_invoice_supplier_source = '.((int) $rowid); + $sql .= ' AND fk_invoice_supplier_line IS NULL'; + $resql = $this->db->query($sql); - dol_syslog(get_class($this)."::delete", LOG_DEBUG); - if (!$this->db->query($sql)) { - $error++; + // If invoice has consumned discounts + $this->fetch_lines(); + $list_rowid_det = array(); + foreach ($this->lines as $key => $invoiceline) { + $list_rowid_det[] = $invoiceline->id; + } + + // Consumned discounts are freed + if (count($list_rowid_det)) { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except'; + $sql .= ' SET fk_invoice_supplier = NULL, fk_invoice_supplier_line = NULL'; + $sql .= ' WHERE fk_invoice_supplier_line IN ('.$this->db->sanitize(implode(',', $list_rowid_det)).')'; + + dol_syslog(get_class($this)."::delete", LOG_DEBUG); + if (!$this->db->query($sql)) { + $error++; + } } } @@ -1565,18 +1578,6 @@ class FactureFournisseur extends CommonInvoice } } - // Remove linked categories. - if (!$error) { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_invoice"; - $sql .= " WHERE fk_invoice = ".((int) $this->id); - - $result = $this->db->query($sql); - if (!$result) { - $error++; - $this->errors[] = $this->db->lasterror(); - } - } - // Remove extrafields if (!$error) { $result = $this->deleteExtraFields(); diff --git a/htdocs/supplier_proposal/class/supplier_proposal.class.php b/htdocs/supplier_proposal/class/supplier_proposal.class.php index aadef3cf7a1..de3b49694e7 100644 --- a/htdocs/supplier_proposal/class/supplier_proposal.class.php +++ b/htdocs/supplier_proposal/class/supplier_proposal.class.php @@ -2136,6 +2136,19 @@ class SupplierProposal extends CommonObject // End call triggers } + // Remove linked categories. + if (!$error) { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_supplier_proposal"; + $sql .= " WHERE fk_supplier_proposal = ".((int) $this->id); + + $result = $this->db->query($sql); + if (!$result) { + $error++; + $this->error = $this->db->lasterror(); + $this->errors[] = $this->error; + } + } + if (!$error) { $main = MAIN_DB_PREFIX.'supplier_proposaldet'; $ef = $main."_extrafields";