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

View File

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

View File

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