Fix delete of proposal

This commit is contained in:
Laurent Destailleur 2020-11-25 14:15:59 +01:00
parent 4917403365
commit bf7b6ee00d
2 changed files with 90 additions and 83 deletions

View File

@ -357,7 +357,7 @@ class Propal extends CommonObject
$this->products = array();
$this->duree_validite = $conf->global->PROPALE_VALIDITY_DURATION;
$this->duree_validite = ((int) $conf->global->PROPALE_VALIDITY_DURATION);
}
@ -1615,7 +1615,7 @@ class Propal extends CommonObject
if (isset($this->note_public)) $this->note_public = trim($this->note_public);
if (isset($this->model_pdf)) $this->model_pdf = trim($this->model_pdf);
if (isset($this->import_key)) $this->import_key = trim($this->import_key);
if (!empty($this->duree_validite)) $this->fin_validite = $this->date + ($this->duree_validite * 24 * 3600);
if (!empty($this->duree_validite) && is_numeric($this->duree_validite)) $this->fin_validite = $this->date + ($this->duree_validite * 24 * 3600);
// Check parameters
// Put here code to add control on parameters values
@ -2921,7 +2921,7 @@ class Propal extends CommonObject
*
* @param User $user Object user that delete
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers
* @return int 1 if ok, otherwise if error
* @return int >0 if OK, <=0 if KO
*/
public function delete($user, $notrigger = 0)
{
@ -2932,102 +2932,96 @@ class Propal extends CommonObject
$this->db->begin();
if (!$notrigger)
{
if (!$notrigger) {
// Call trigger
$result = $this->call_trigger('PROPAL_DELETE', $user);
if ($result < 0) { $error++; }
// End call triggers
}
if (!$error)
{
$main = MAIN_DB_PREFIX.'propaldet';
$ef = $main."_extrafields";
$sqlef = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_propal = ".$this->id.")";
$sql = "DELETE FROM ".MAIN_DB_PREFIX."propaldet WHERE fk_propal = ".$this->id;
if ($this->db->query($sqlef) && $this->db->query($sql))
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."propal WHERE rowid = ".$this->id;
if ($this->db->query($sql))
{
// Delete linked object
$res = $this->deleteObjectLinked();
if ($res < 0) $error++;
// Delete extrafields of lines and lines
if (!$error && !empty($this->table_element_line)) {
$tabletodelete = $this->table_element_line;
$sqlef = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete."_extrafields WHERE fk_object IN (SELECT rowid FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE fk_propal = ".$this->id.")";
$sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE fk_propal = ".$this->id;
if (! $this->db->query($sqlef) || ! $this->db->query($sql)) {
$error++;
$this->error = $this->db->lasterror();
dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
}
}
// Delete linked contacts
$res = $this->delete_linked_contact();
if ($res < 0) $error++;
// Removed extrafields of object
if (!$error) {
$result = $this->deleteExtraFields();
if ($result < 0) {
$error++;
dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
}
}
if (!$error)
{
// Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
$this->deleteEcmFiles();
// Delete main record
if (!$error) {
$sql = "DELETE FROM ".MAIN_DB_PREFIX."propal WHERE rowid = ".$this->id;
$res = $this->db->query($sql);
if (! $res) {
$error++;
dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
}
}
// We remove directory
$ref = dol_sanitizeFileName($this->ref);
if ($conf->propal->multidir_output[$this->entity] && !empty($this->ref))
{
$dir = $conf->propal->multidir_output[$this->entity]."/".$ref;
$file = $dir."/".$ref.".pdf";
if (file_exists($file))
{
dol_delete_preview($this);
if (! $error) {
// Delete linked object
$res = $this->deleteObjectLinked();
if ($res < 0) $error++;
}
if (!dol_delete_file($file, 0, 0, 0, $this)) // For triggers
{
$this->error = 'ErrorFailToDeleteFile';
$this->errors = array('ErrorFailToDeleteFile');
$this->db->rollback();
return 0;
}
}
if (file_exists($dir))
{
$res = @dol_delete_dir_recursive($dir);
if (!$res)
{
$this->error = 'ErrorFailToDeleteDir';
$this->errors = array('ErrorFailToDeleteDir');
$this->db->rollback();
return 0;
}
}
}
}
if (! $error) {
// Delete linked contacts
$res = $this->delete_linked_contact();
if ($res < 0) $error++;
}
// Removed extrafields
if (!$error)
{
$result = $this->deleteExtraFields();
if ($result < 0)
{
$error++;
$errorflag = -4;
dol_syslog(get_class($this)."::delete erreur ".$errorflag." ".$this->error, LOG_ERR);
}
}
// Delete record into ECM index and physically
if (!$error) {
$res = $this->deleteEcmFiles(); // Deleting files physically is done later with the dol_delete_dir_recursive
if (! $res) {
$error++;
}
}
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();
if (!$error) {
// We remove directory
$ref = dol_sanitizeFileName($this->ref);
if ($conf->propal->multidir_output[$this->entity] && !empty($this->ref)) {
$dir = $conf->propal->multidir_output[$this->entity]."/".$ref;
$file = $dir."/".$ref.".pdf";
if (file_exists($file)) {
dol_delete_preview($this);
if (!dol_delete_file($file, 0, 0, 0, $this)) {
$this->error = 'ErrorFailToDeleteFile';
$this->errors = array('ErrorFailToDeleteFile');
$this->db->rollback();
return 0;
}
}
if (file_exists($dir)) {
$res = @dol_delete_dir_recursive($dir);
if (!$res) {
$this->error = 'ErrorFailToDeleteDir';
$this->errors = array('ErrorFailToDeleteDir');
$this->db->rollback();
return 0;
}
} else {
$this->error = $this->db->lasterror();
$this->db->rollback();
return -3;
}
} else {
$this->error = $this->db->lasterror();
$this->db->rollback();
return -2;
}
}
if (!$error) {
dol_syslog(get_class($this)."::delete ".$this->id." by ".$user->id, LOG_DEBUG);
$this->db->commit();
return 1;
} else {
$this->db->rollback();
return -1;

View File

@ -8477,9 +8477,22 @@ abstract class CommonObject
$element = $this->element;
}
// Delete ecm_files extrafields
$sql = "DELETE FROM ".MAIN_DB_PREFIX."ecm_files_extrafields WHERE fk_object IN (";
$sql .= " SELECT rowid FROM ".MAIN_DB_PREFIX."ecm_files WHERE filename LIKE '".$this->db->escape($this->ref)."%'";
$sql .= " AND filepath = '".$this->db->escape($element)."/".$this->db->escape($this->ref)."' AND entity = ".$conf->entity; // No need of getEntity here
$sql .= ")";
if (!$this->db->query($sql)) {
$this->error = $this->db->lasterror();
$this->db->rollback();
return false;
}
// Delete ecm_files
$sql = "DELETE FROM ".MAIN_DB_PREFIX."ecm_files";
$sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%'";
$sql .= " AND filepath = '".$this->db->escape($element)."/".$this->db->escape($this->ref)."' AND entity = ".$conf->entity;
$sql .= " AND filepath = '".$this->db->escape($element)."/".$this->db->escape($this->ref)."' AND entity = ".$conf->entity; // No need of getEntity here
if (!$this->db->query($sql)) {
$this->error = $this->db->lasterror();