Fix delete of expense report

This commit is contained in:
Laurent Destailleur 2020-11-25 15:29:18 +01:00
parent 54913c6a75
commit 944e2a8032
3 changed files with 102 additions and 80 deletions

View File

@ -2942,8 +2942,8 @@ class Propal extends CommonObject
// Delete extrafields of lines and lines // Delete extrafields of lines and lines
if (!$error && !empty($this->table_element_line)) { if (!$error && !empty($this->table_element_line)) {
$tabletodelete = $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.")"; $sqlef = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete."_extrafields WHERE fk_object IN (SELECT rowid FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE ".$this->fk_element." = ".$this->id.")";
$sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE fk_propal = ".$this->id; $sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE ".$this->fk_element." = ".$this->id;
if (! $this->db->query($sqlef) || ! $this->db->query($sql)) { if (! $this->db->query($sqlef) || ! $this->db->query($sql)) {
$error++; $error++;
$this->error = $this->db->lasterror(); $this->error = $this->db->lasterror();
@ -2962,7 +2962,7 @@ class Propal extends CommonObject
// Delete main record // Delete main record
if (!$error) { if (!$error) {
$sql = "DELETE FROM ".MAIN_DB_PREFIX."propal WHERE rowid = ".$this->id; $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".$this->id;
$res = $this->db->query($sql); $res = $this->db->query($sql);
if (! $res) { if (! $res) {
$error++; $error++;

View File

@ -1074,39 +1074,89 @@ class ExpenseReport extends CommonObject
/** /**
* delete * Delete object in database
* *
* @param User $fuser User that delete * @param User $fuser User that delete
* @param bool $notrigger false=launch triggers after, true=disable triggers
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
public function delete(User $fuser = null) public function delete(User $fuser = null, $notrigger = false)
{ {
global $user, $langs, $conf; global $conf;
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$error = 0; $error = 0;
$this->db->begin(); $this->db->begin();
if (!$rowid) $rowid = $this->id; if (!$notrigger) {
// Call trigger
$result = $this->call_trigger('EXPENSEREPORT_DELETE', $user);
if ($result < 0) { $error++; }
// End call triggers
}
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element_line.' WHERE '.$this->fk_element.' = '.$rowid; // Delete extrafields of lines and lines
if ($this->db->query($sql)) { if (!$error && !empty($this->table_element_line)) {
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE rowid = '.$rowid; $tabletodelete = $this->table_element_line;
$resql = ; //$sqlef = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete."_extrafields WHERE fk_object IN (SELECT rowid FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE ".$this->fk_element." = ".$this->id.")";
if ($this->db->query($sql)) { $sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE ".$this->fk_element." = ".$this->id;
// Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive if (! $this->db->query($sql)) {
$this->deleteEcmFiles(); $error++;
$this->error = $this->db->lasterror();
dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
}
}
// Removed extrafields of object
if (!$error) {
$result = $this->deleteExtraFields();
if ($result < 0) {
$error++;
dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
}
}
// Delete main record
if (!$error) {
$sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".$this->id;
$res = $this->db->query($sql);
if (! $res) {
$error++;
dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
}
}
if (! $error) {
// Delete linked object
$res = $this->deleteObjectLinked();
if ($res < 0) $error++;
}
if (! $error) {
// Delete linked contacts
$res = $this->delete_linked_contact();
if ($res < 0) $error++;
}
// 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) {
// We remove directory // We remove directory
$ref = dol_sanitizeFileName($this->ref); $ref = dol_sanitizeFileName($this->ref);
if ($conf->expensereport->multidir_output[$this->entity] && !empty($this->ref)) { if ($conf->propal->multidir_output[$this->entity] && !empty($this->ref)) {
$dir = $conf->expensereport->multidir_output[$this->entity]."/".$ref; $dir = $conf->propal->multidir_output[$this->entity]."/".$ref;
$file = $dir."/".$ref.".pdf"; $file = $dir."/".$ref.".pdf";
if (file_exists($file)) { if (file_exists($file)) {
dol_delete_preview($this); dol_delete_preview($this);
if (!dol_delete_file($file, 0, 0, 0, $this)) // For triggers if (!dol_delete_file($file, 0, 0, 0, $this)) {
{
$this->error = 'ErrorFailToDeleteFile'; $this->error = 'ErrorFailToDeleteFile';
$this->errors = array('ErrorFailToDeleteFile'); $this->errors = array('ErrorFailToDeleteFile');
$this->db->rollback(); $this->db->rollback();
@ -1123,15 +1173,6 @@ class ExpenseReport extends CommonObject
} }
} }
} }
// 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);
}
} }
if (!$error) { if (!$error) {
@ -1139,21 +1180,8 @@ class ExpenseReport extends CommonObject
$this->db->commit(); $this->db->commit();
return 1; return 1;
} else { } else {
$this->error = $this->db->lasterror();
$this->db->rollback(); $this->db->rollback();
return 0; return -1;
}
} else {
$this->error = $this->db->error()." sql=".$sql;
dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
$this->db->rollback();
return -6;
}
} else {
$this->error = $this->db->error()." sql=".$sql;
dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
$this->db->rollback();
return -4;
} }
} }
@ -2466,7 +2494,7 @@ class ExpenseReport extends CommonObject
{ {
$obj = $this->db->fetch_object($resql); $obj = $this->db->fetch_object($resql);
$this->db->free($resql); $this->db->free($resql);
return $obj->amount; return (empty($obj->amount) ? 0 : $obj->amount);
} else { } else {
$this->error = $this->db->lasterror(); $this->error = $this->db->lasterror();
return -1; return -1;

View File

@ -281,15 +281,9 @@ class ExpenseReportTest extends PHPUnit\Framework\TestCase
$langs=$this->savlangs; $langs=$this->savlangs;
$db=$this->savdb; $db=$this->savdb;
/*$result=$localobject->setstatus(0); $result=$localobject->getSumPayments();
print __METHOD__." id=".$localobject->id." result=".$result."\n"; print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0); $this->assertGreaterThanOrEqual(0, $result);
*/
/*$localobject->info($localobject->id);
print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
$this->assertNotEquals($localobject->date_creation, '');
*/
return $localobject->id; return $localobject->id;
} }
@ -316,7 +310,7 @@ class ExpenseReportTest extends PHPUnit\Framework\TestCase
$result=$localobject->delete($user); $result=$localobject->delete($user);
print __METHOD__." id=".$id." result=".$result."\n"; print __METHOD__." id=".$id." result=".$result."\n";
$this->assertLessThan($result, 0); $this->assertGreaterThan(0, $result);
return $result; return $result;
} }
} }