From e8dae7a8dbf556b01028fcaec1ca2e02d1754840 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Thu, 24 Mar 2022 16:34:08 +0100 Subject: [PATCH 1/6] FIX Multiccompany sharings compatibility --- htdocs/core/class/commonobject.class.php | 30 ++++++++++++++++-------- htdocs/product/class/product.class.php | 14 +++++------ htdocs/societe/class/societe.class.php | 24 +++++++++---------- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index a0d1079072a..bedc17a8fc2 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -4374,9 +4374,10 @@ abstract class CommonObject * Check is done into this->childtables. There is no check into llx_element_element. * * @param int $id Force id of object + * @param int $entity Force entity to check * @return int <0 if KO, 0 if not used, >0 if already used */ - public function isObjectUsed($id = 0) + public function isObjectUsed($id = 0, $entity = 0) { global $langs; @@ -4399,11 +4400,21 @@ abstract class CommonObject // Test if child exists $haschild = 0; - foreach ($arraytoscan as $table => $elementname) { + foreach ($arraytoscan as $table => $element) { //print $id.'-'.$table.'-'.$elementname.'
'; - // Check if third party can be deleted - $sql = "SELECT COUNT(*) as nb from ".$this->db->prefix().$table; - $sql .= " WHERE ".$this->fk_element." = ".((int) $id); + // Check if element can be deleted + $sql = "SELECT COUNT(*) as nb"; + $sql.= " FROM ".$this->db->prefix().$table." as c"; + if (!empty($element['parent']) && !empty($element['parentkey'])) { + $sql.= ", ".$this->db->prefix().$element['parent']." as p"; + } + $sql.= " WHERE c.".$this->fk_element." = ".((int) $id); + if (!empty($element['parent']) && !empty($element['parentkey'])) { + $sql.= " AND c.".$element['parentkey']." = p.rowid"; + } + if (!empty($entity)) { + $sql.= " AND entity = ".((int) $entity); + } $resql = $this->db->query($sql); if ($resql) { $obj = $this->db->fetch_object($resql); @@ -4411,11 +4422,10 @@ abstract class CommonObject $langs->load("errors"); //print 'Found into table '.$table.', type '.$langs->transnoentitiesnoconv($elementname).', haschild='.$haschild; $haschild += $obj->nb; - if (is_numeric($elementname)) { // old usage - $this->errors[] = $langs->transnoentities("ErrorRecordHasAtLeastOneChildOfType", method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref, $table); - } else // new usage: $elementname=Translation key - { - $this->errors[] = $langs->transnoentities("ErrorRecordHasAtLeastOneChildOfType", method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref, $langs->transnoentitiesnoconv($elementname)); + if (is_numeric($element) || empty($element['name'])) { // old usage + $this->errors[] = $langs->trans("ErrorRecordHasAtLeastOneChildOfType", method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref, $table); + } else { // new usage: $element['name']=Translation key + $this->errors[] = $langs->trans("ErrorRecordHasAtLeastOneChildOfType", method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref, $langs->transnoentitiesnoconv($element['name'])); } break; // We found at least one, we stop here } diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 6e97e80fece..9792b11ce39 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -64,13 +64,13 @@ class Product extends CommonObject * @var array List of child tables. To test if we can delete object. */ protected $childtables = array( - 'supplier_proposaldet', - 'propaldet', - 'commandedet', - 'facturedet', - 'contratdet', - 'facture_fourn_det', - 'commande_fournisseurdet' + 'supplier_proposaldet' => array('parent' => 'supplier_proposal', 'parentkey' => 'fk_supplier_proposal'), + 'propaldet' => array('parent' => 'propal', 'parentkey' => 'fk_propal'), + 'commandedet' => array('parent' => 'commande', 'parentkey' => 'fk_commande'), + 'facturedet' => array('parent' => 'facture', 'parentkey' => 'fk_facture'), + 'contratdet' => array('parent' => 'contrat', 'parentkey' => 'fk_contrat'), + 'facture_fourn_det' => array('parent' => 'facture_fourn', 'parentkey' => 'fk_facture_fourn'), + 'commande_fournisseurdet' => array('parent' => 'commande_fournisseur', 'parentkey' => 'fk_commande') ); /** diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 89eeeaca8e5..bfcdce7d348 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -73,18 +73,18 @@ class Societe extends CommonObject * @var array List of child tables. To test if we can delete object. */ protected $childtables = array( - "supplier_proposal" => 'SupplierProposal', - "propal" => 'Proposal', - "commande" => 'Order', - "facture" => 'Invoice', - "facture_rec" => 'RecurringInvoiceTemplate', - "contrat" => 'Contract', - "fichinter" => 'Fichinter', - "facture_fourn" => 'SupplierInvoice', - "commande_fournisseur" => 'SupplierOrder', - "projet" => 'Project', - "expedition" => 'Shipment', - "prelevement_lignes" => 'DirectDebitRecord', + "supplier_proposal" => array('name' => 'SupplierProposal'), + "propal" => array('name' => 'Proposal'), + "commande" => array('name' => 'Order'), + "facture" => array('name' => 'Invoice'), + "facture_rec" => array('name' => 'RecurringInvoiceTemplate'), + "contrat" => array('name' => 'Contract'), + "fichinter" => array('name' => 'Fichinter'), + "facture_fourn" => array('name' => 'SupplierInvoice'), + "commande_fournisseur" => array('name' => 'SupplierOrder'), + "projet" => array('name' => 'Project'), + "expedition" => array('name' => 'Shipment'), + "prelevement_lignes" => array('name' => 'DirectDebitRecord'), ); /** From 6fdb140f0402a598ae7e2482f38b680859a94fb8 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Thu, 24 Mar 2022 16:54:02 +0100 Subject: [PATCH 2/6] FIX missing table alias --- 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 bedc17a8fc2..23e66ed2064 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -4413,7 +4413,7 @@ abstract class CommonObject $sql.= " AND c.".$element['parentkey']." = p.rowid"; } if (!empty($entity)) { - $sql.= " AND entity = ".((int) $entity); + $sql.= " AND p.entity = ".((int) $entity); } $resql = $this->db->query($sql); if ($resql) { From c38437f0ded7c54fad4c363f6d44ff07119b2339 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Thu, 24 Mar 2022 17:12:37 +0100 Subject: [PATCH 3/6] FIX wrong error message --- htdocs/core/class/commonobject.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 23e66ed2064..ea7d0265ec1 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -4423,9 +4423,9 @@ abstract class CommonObject //print 'Found into table '.$table.', type '.$langs->transnoentitiesnoconv($elementname).', haschild='.$haschild; $haschild += $obj->nb; if (is_numeric($element) || empty($element['name'])) { // old usage - $this->errors[] = $langs->trans("ErrorRecordHasAtLeastOneChildOfType", method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref, $table); + $this->errors[] = $langs->transnoentitiesnoconv("ErrorRecordHasAtLeastOneChildOfType", method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref, $table); } else { // new usage: $element['name']=Translation key - $this->errors[] = $langs->trans("ErrorRecordHasAtLeastOneChildOfType", method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref, $langs->transnoentitiesnoconv($element['name'])); + $this->errors[] = $langs->transnoentitiesnoconv("ErrorRecordHasAtLeastOneChildOfType", method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref, $langs->transnoentitiesnoconv($element['name'])); } break; // We found at least one, we stop here } From 391c02993d2a6d2fc8257ebe77d6f5b866eb35e0 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Thu, 24 Mar 2022 20:26:47 +0100 Subject: [PATCH 4/6] FIX better test for all usage --- htdocs/core/class/commonobject.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index ea7d0265ec1..7dcf9d09805 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -4422,8 +4422,10 @@ abstract class CommonObject $langs->load("errors"); //print 'Found into table '.$table.', type '.$langs->transnoentitiesnoconv($elementname).', haschild='.$haschild; $haschild += $obj->nb; - if (is_numeric($element) || empty($element['name'])) { // old usage + if (is_numeric($element)) { // very old usage array('table1', 'table2', ...) $this->errors[] = $langs->transnoentitiesnoconv("ErrorRecordHasAtLeastOneChildOfType", method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref, $table); + } elseif (is_string($element)) { // old usage array('table1' => 'TranslateKey1', 'table2' => 'TranslateKey2', ...) + $this->errors[] = $langs->transnoentitiesnoconv("ErrorRecordHasAtLeastOneChildOfType", method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref, $langs->transnoentitiesnoconv($element)); } else { // new usage: $element['name']=Translation key $this->errors[] = $langs->transnoentitiesnoconv("ErrorRecordHasAtLeastOneChildOfType", method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref, $langs->transnoentitiesnoconv($element['name'])); } From ffa79c59f97561f4761fbc939c2ca60e74ebfc3d Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Thu, 24 Mar 2022 20:48:43 +0100 Subject: [PATCH 5/6] FIX wrong alias of table if no parent and parentkey --- htdocs/core/class/commonobject.class.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 7dcf9d09805..3bb84eac492 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -4413,7 +4413,11 @@ abstract class CommonObject $sql.= " AND c.".$element['parentkey']." = p.rowid"; } if (!empty($entity)) { - $sql.= " AND p.entity = ".((int) $entity); + if (!empty($element['parent']) && !empty($element['parentkey'])) { + $sql.= " AND p.entity = ".((int) $entity); + } else { + $sql.= " AND c.entity = ".((int) $entity); + } } $resql = $this->db->query($sql); if ($resql) { From 4479ba801552c09e462e2346f9ec9880f4c651c7 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Sat, 26 Mar 2022 08:32:20 +0100 Subject: [PATCH 6/6] FIX missing translation keys --- htdocs/product/class/product.class.php | 14 +++---- htdocs/projet/class/task.class.php | 4 +- htdocs/societe/class/societe.class.php | 56 +++++++++++++------------- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 9792b11ce39..d308ceb8e53 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -64,13 +64,13 @@ class Product extends CommonObject * @var array List of child tables. To test if we can delete object. */ protected $childtables = array( - 'supplier_proposaldet' => array('parent' => 'supplier_proposal', 'parentkey' => 'fk_supplier_proposal'), - 'propaldet' => array('parent' => 'propal', 'parentkey' => 'fk_propal'), - 'commandedet' => array('parent' => 'commande', 'parentkey' => 'fk_commande'), - 'facturedet' => array('parent' => 'facture', 'parentkey' => 'fk_facture'), - 'contratdet' => array('parent' => 'contrat', 'parentkey' => 'fk_contrat'), - 'facture_fourn_det' => array('parent' => 'facture_fourn', 'parentkey' => 'fk_facture_fourn'), - 'commande_fournisseurdet' => array('parent' => 'commande_fournisseur', 'parentkey' => 'fk_commande') + 'supplier_proposaldet' => array('name' => 'SupplierProposal', 'parent' => 'supplier_proposal', 'parentkey' => 'fk_supplier_proposal'), + 'propaldet' => array('name' => 'Proposal', 'parent' => 'propal', 'parentkey' => 'fk_propal'), + 'commandedet' => array('name' => 'Order', 'parent' => 'commande', 'parentkey' => 'fk_commande'), + 'facturedet' => array('name' => 'Invoice', 'parent' => 'facture', 'parentkey' => 'fk_facture'), + 'contratdet' => array('name' => 'Contract', 'parent' => 'contrat', 'parentkey' => 'fk_contrat'), + 'facture_fourn_det' => array('name' => 'SupplierInvoice', 'parent' => 'facture_fourn', 'parentkey' => 'fk_facture_fourn'), + 'commande_fournisseurdet' => array('name' => 'SupplierOrder', 'parent' => 'commande_fournisseur', 'parentkey' => 'fk_commande') ); /** diff --git a/htdocs/projet/class/task.class.php b/htdocs/projet/class/task.class.php index 443e945aecb..d99b13b29c7 100644 --- a/htdocs/projet/class/task.class.php +++ b/htdocs/projet/class/task.class.php @@ -59,7 +59,9 @@ class Task extends CommonObjectLine /** * @var array List of child tables. To test if we can delete object. */ - protected $childtables = array('projet_task_time'); + protected $childtables = array( + 'projet_task_time' => array('name' => 'Task', 'parent' => 'projet_task', 'parentkey' => 'fk_task') + ); /** * @var int ID parent task diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index bfcdce7d348..7fab734a12c 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -73,18 +73,18 @@ class Societe extends CommonObject * @var array List of child tables. To test if we can delete object. */ protected $childtables = array( - "supplier_proposal" => array('name' => 'SupplierProposal'), - "propal" => array('name' => 'Proposal'), - "commande" => array('name' => 'Order'), - "facture" => array('name' => 'Invoice'), - "facture_rec" => array('name' => 'RecurringInvoiceTemplate'), - "contrat" => array('name' => 'Contract'), - "fichinter" => array('name' => 'Fichinter'), - "facture_fourn" => array('name' => 'SupplierInvoice'), - "commande_fournisseur" => array('name' => 'SupplierOrder'), - "projet" => array('name' => 'Project'), - "expedition" => array('name' => 'Shipment'), - "prelevement_lignes" => array('name' => 'DirectDebitRecord'), + 'supplier_proposal' => array('name' => 'SupplierProposal'), + 'propal' => array('name' => 'Proposal'), + 'commande' => array('name' => 'Order'), + 'facture' => array('name' => 'Invoice'), + 'facture_rec' => array('name' => 'RecurringInvoiceTemplate'), + 'contrat' => array('name' => 'Contract'), + 'fichinter' => array('name' => 'Fichinter'), + 'facture_fourn' => array('name' => 'SupplierInvoice'), + 'commande_fournisseur' => array('name' => 'SupplierOrder'), + 'projet' => array('name' => 'Project'), + 'expedition' => array('name' => 'Shipment'), + 'prelevement_lignes' => array('name' => 'DirectDebitRecord'), ); /** @@ -92,22 +92,22 @@ class Societe extends CommonObject * if name like with @ClassName:FilePathClass:ParentFkFieldName' it will call method deleteByParentField (with parentId as parameters) and FieldName to fetch and delete child object */ protected $childtablesoncascade = array( - "societe_prices", - "societe_address", - "product_fournisseur_price", - "product_customer_price_log", - "product_customer_price", - "@Contact:/contact/class/contact.class.php:fk_soc", - "adherent", - "societe_account", - "societe_rib", - "societe_remise", - "societe_remise_except", - "societe_commerciaux", - "categorie", - "notify", - "notify_def", - "actioncomm", + 'societe_prices', + 'societe_address', + 'product_fournisseur_price', + 'product_customer_price_log', + 'product_customer_price', + '@Contact:/contact/class/contact.class.php:fk_soc', + 'adherent', + 'societe_account', + 'societe_rib', + 'societe_remise', + 'societe_remise_except', + 'societe_commerciaux', + 'categorie', + 'notify', + 'notify_def', + 'actioncomm', ); /**