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
This commit is contained in:
Pierre Ardoin 2026-06-27 18:23:50 +02:00 committed by GitHub
parent b56e5cfcb0
commit 7d093dc4f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 57 additions and 30 deletions

View file

@ -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;

View file

@ -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();

View file

@ -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";