Fix: Test if child exists to avoid error

This commit is contained in:
Laurent Destailleur 2011-01-26 13:45:05 +00:00
parent 20dea64979
commit e4b445fcb1

View File

@ -791,19 +791,22 @@ class Societe extends CommonObject
dol_syslog("Societe::Delete", LOG_DEBUG); dol_syslog("Societe::Delete", LOG_DEBUG);
$sqr = 0; $sqr = 0;
// Test if child exists
$listtable=array("propal","commande","facture","contrat","facture_fourn","commande_fournisseur");
$haschild=0;
foreach($listtable as $table)
{
// Check if third party can be deleted // Check if third party can be deleted
$nbpropal=0; $nb=0;
$sql = "SELECT COUNT(*) as nb from ".MAIN_DB_PREFIX."propal"; $sql = "SELECT COUNT(*) as nb from ".MAIN_DB_PREFIX.$table;
$sql.= " WHERE fk_soc = " . $id; $sql.= " WHERE fk_soc = " . $id;
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if ($resql)
{ {
$obj=$this->db->fetch_object($resql); $obj=$this->db->fetch_object($resql);
$nbpropal=$obj->nb; if ($obj->nb > 0)
if ($nbpropal > 0)
{ {
$this->error="ErrorRecordHasChildren"; $haschild+=$obj->nb;
return -1;
} }
} }
else else
@ -812,7 +815,12 @@ class Societe extends CommonObject
dol_syslog("Societe::Delete erreur -1 ".$this->error, LOG_ERR); dol_syslog("Societe::Delete erreur -1 ".$this->error, LOG_ERR);
return -1; return -1;
} }
}
if ($haschild > 0)
{
$this->error="ErrorRecordHasChildren";
return -1;
}
if ($this->db->begin()) if ($this->db->begin())