Fix delete of invoice not complete (some fk_invoice still filled)

This commit is contained in:
Laurent Destailleur 2021-09-21 14:20:01 +02:00
parent 06f97f4f80
commit b7105d572a
2 changed files with 26 additions and 6 deletions

View File

@ -2267,7 +2267,6 @@ class Facture extends CommonInvoice
$sql .= ' SET fk_facture = NULL, fk_facture_line = NULL';
$sql .= ' WHERE fk_facture_line IN ('.$this->db->sanitize(join(',', $list_rowid_det)).')';
dol_syslog(get_class($this)."::delete", LOG_DEBUG);
if (!$this->db->query($sql)) {
$this->error = $this->db->error()." sql=".$sql;
$this->errors[] = $this->error;
@ -2276,6 +2275,30 @@ class Facture extends CommonInvoice
}
}
// Remove other links to the deleted invoice
$sql = 'UPDATE '.MAIN_DB_PREFIX.'eventorganization_conferenceorboothattendee';
$sql .= ' SET fk_facture = NULL';
$sql .= ' WHERE fk_facture = '.((int) $rowid);
if (!$this->db->query($sql)) {
$this->error = $this->db->error()." sql=".$sql;
$this->errors[] = $this->error;
$this->db->rollback();
return -5;
}
$sql = 'UPDATE '.MAIN_DB_PREFIX.'projet_task_time';
$sql .= ' SET invoice_id = NULL, invoice_line_id = NULL';
$sql .= ' WHERE invoice_id = '.((int) $rowid);
if (!$this->db->query($sql)) {
$this->error = $this->db->error()." sql=".$sql;
$this->errors[] = $this->error;
$this->db->rollback();
return -5;
}
// If we decrease stock on invoice validation, we increase back if a warehouse id was provided
if ($this->type != self::TYPE_DEPOSIT && $result >= 0 && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_BILL) && $idwarehouse != -1) {
require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
@ -2299,17 +2322,13 @@ class Facture extends CommonInvoice
// Invoice line extrafileds
$main = MAIN_DB_PREFIX.'facturedet';
$ef = $main."_extrafields";
$sqlef = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM ".$main." WHERE fk_facture = ".((int) $rowid).")";
$sqlef = "DELETE FROM ".$ef." WHERE fk_object IN (SELECT rowid FROM ".$main." WHERE fk_facture = ".((int) $rowid).")";
// Delete invoice line
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facturedet WHERE fk_facture = '.((int) $rowid);
dol_syslog(get_class($this)."::delete", LOG_DEBUG);
if ($this->db->query($sqlef) && $this->db->query($sql) && $this->delete_linked_contact()) {
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture WHERE rowid = '.((int) $rowid);
dol_syslog(get_class($this)."::delete", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
// Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive

View File

@ -21,6 +21,7 @@ CREATE TABLE llx_eventorganization_conferenceorboothattendee(
fk_soc integer,
fk_actioncomm integer,
fk_project integer NOT NULL,
fk_invoice integer NULL,
email varchar(100),
date_subscription datetime,
amount double DEFAULT NULL,