Fix: delete linked object in propal delete function and uniformize code

This commit is contained in:
Regis Houssin 2012-02-13 18:27:42 +01:00
parent f07b6397fc
commit 6f3f1204fe
3 changed files with 64 additions and 45 deletions

View File

@ -1789,50 +1789,52 @@ class Propal extends CommonObject
$this->db->begin();
$sql = "DELETE FROM ".MAIN_DB_PREFIX."propaldet WHERE fk_propal = ".$this->id;
if ( $this->db->query($sql) )
if ($this->db->query($sql))
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."propal WHERE rowid = ".$this->id;
if ( $this->db->query($sql) )
if ($this->db->query($sql))
{
// Delete linked object
$res = $this->deleteObjectLinked();
if ($res < 0) $error++;
// Delete linked contacts
$res = $this->delete_linked_contact();
if ($res < 0)
if ($res < 0) $error++;
if (! $error)
{
$this->error='ErrorFailToDeleteLinkedContact';
$this->db->rollback();
return 0;
}
// We remove directory
$propalref = dol_sanitizeFileName($this->ref);
if ($conf->propale->dir_output)
{
$dir = $conf->propale->dir_output . "/" . $propalref ;
$file = $conf->propale->dir_output . "/" . $propalref . "/" . $propalref . ".pdf";
if (file_exists($file))
// We remove directory
$propalref = dol_sanitizeFileName($this->ref);
if ($conf->propale->dir_output)
{
dol_delete_preview($this);
if (!dol_delete_file($file))
$dir = $conf->propale->dir_output . "/" . $propalref ;
$file = $conf->propale->dir_output . "/" . $propalref . "/" . $propalref . ".pdf";
if (file_exists($file))
{
$this->error='ErrorFailToDeleteFile';
$this->db->rollback();
return 0;
dol_delete_preview($this);
if (!dol_delete_file($file))
{
$this->error='ErrorFailToDeleteFile';
$this->db->rollback();
return 0;
}
}
}
if (file_exists($dir))
{
$res=@dol_delete_dir($dir);
if (! $res)
if (file_exists($dir))
{
$this->error='ErrorFailToDeleteDir';
$this->db->rollback();
return 0;
$res=@dol_delete_dir($dir);
if (! $res)
{
$this->error='ErrorFailToDeleteDir';
$this->db->rollback();
return 0;
}
}
}
}
if (! $notrigger)
if (! $error && ! $notrigger)
{
// Call triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
@ -1842,27 +1844,33 @@ class Propal extends CommonObject
// End call triggers
}
if (!$error)
if (! $error)
{
dol_syslog("Suppression de la proposition $this->id par $user->id", LOG_DEBUG);
dol_syslog(get_class($this)."::delete $this->id by $user->id", LOG_DEBUG);
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
$this->db->rollback();
return 0;
}
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
$this->db->rollback();
return -2;
}
}
else
{
$this->db->rollback();
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
$this->db->rollback();
return -1;
}
}

View File

@ -2304,9 +2304,10 @@ class Commande extends CommonObject
* Delete the customer order
*
* @param User $user User object
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <=0 if KO, >0 if OK
*/
function delete($user)
function delete($user, $notrigger=0)
{
global $conf, $langs;
require_once(DOL_DOCUMENT_ROOT."/core/lib/files.lib.php");
@ -2369,7 +2370,7 @@ class Commande extends CommonObject
}
}
if (! $error)
if (! $error && ! $notrigger)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
@ -2377,12 +2378,18 @@ class Commande extends CommonObject
$result=$interface->run_triggers('ORDER_DELETE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
$this->db->commit();
return 1;
}
if (! $error)
{
dol_syslog(get_class($this)."::delete $this->id by $user->id", LOG_DEBUG);
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
$this->db->rollback();
return -1;
}

View File

@ -1116,9 +1116,10 @@ class Facture extends CommonObject
* Delete invoice
*
* @param int $rowid Id of invoice to delete. If empty, we delete current instance of invoice
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 if KO, >0 if OK
*/
function delete($rowid=0)
function delete($rowid, $notrigger=0)
{
global $user,$langs,$conf;
@ -1176,12 +1177,15 @@ class Facture extends CommonObject
$resql=$this->db->query($sql);
if ($resql)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('BILL_DELETE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
if (! $notrigger)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('BILL_DELETE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
}
$this->db->commit();
return 1;