From 10838983b50ad51afc0bb9dfb4ae32d91eb2ec74 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur <eldy@destailleur.fr> Date: Mon, 27 Feb 2017 11:38:19 +0100 Subject: [PATCH] FIX error management in bank account deletion. --- htdocs/compta/bank/card.php | 21 ++++-- htdocs/compta/bank/class/account.class.php | 73 ++++++++++++++------ htdocs/compta/bank/class/bankcateg.class.php | 7 +- 3 files changed, 71 insertions(+), 30 deletions(-) diff --git a/htdocs/compta/bank/card.php b/htdocs/compta/bank/card.php index 3977c208cce..b3bdde6d555 100644 --- a/htdocs/compta/bank/card.php +++ b/htdocs/compta/bank/card.php @@ -65,7 +65,7 @@ $extralabels=$extrafields->fetch_name_optionals_label($account->table_element); * Actions */ -if ($_POST["action"] == 'add') +if ($action == 'add') { $error=0; @@ -154,7 +154,7 @@ if ($_POST["action"] == 'add') } } -if ($_POST["action"] == 'update' && ! $_POST["cancel"]) +if ($action == 'update' && ! $_POST["cancel"]) { $error=0; @@ -235,15 +235,24 @@ if ($_POST["action"] == 'update' && ! $_POST["cancel"]) } } -if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == "yes" && $user->rights->banque->configurer) +if ($action == 'confirm_delete' && $_POST["confirm"] == "yes" && $user->rights->banque->configurer) { // Delete $account = new Account($db); $account->fetch($_GET["id"]); - $account->delete(); + $result = $account->delete($user); - header("Location: ".DOL_URL_ROOT."/compta/bank/index.php"); - exit; + if ($result > 0) + { + setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs'); + header("Location: ".DOL_URL_ROOT."/compta/bank/index.php"); + exit; + } + else + { + setEventMessages($account->error, $account->errors, 'errors'); + $action=''; + } } diff --git a/htdocs/compta/bank/class/account.class.php b/htdocs/compta/bank/class/account.class.php index 393cf619e81..96661f4074a 100644 --- a/htdocs/compta/bank/class/account.class.php +++ b/htdocs/compta/bank/class/account.class.php @@ -950,29 +950,60 @@ class Account extends CommonObject { global $conf; - $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_account"; - $sql.= " WHERE rowid = ".$this->rowid; - $sql.= " AND entity = ".$conf->entity; - - dol_syslog(get_class($this)."::delete", LOG_DEBUG); - $result = $this->db->query($sql); - if ($result) { - - // Remove extrafields - if ((empty($conf->global->MAIN_EXTRAFIELDS_DISABLED))) // For avoid conflicts if trigger used - { - $result=$this->deleteExtraFields(); - if ($result < 0) - { - dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR); - return -1; - } - } - + $error=0; + + $this->db->begin(); + + // Delete link between tag and bank account + if (! $error) + { + //$sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class"; // No more used + $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account"; + $sql.= " WHERE fk_account = ".$this->id; + + $resql = $this->db->query($sql); + if (!$resql) + { + $error++; + $this->error = "Error ".$this->db->lasterror(); + } + } + + if (! $error) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_account"; + $sql.= " WHERE rowid = ".$this->rowid; + + dol_syslog(get_class($this)."::delete", LOG_DEBUG); + $result = $this->db->query($sql); + if ($result) + { + // Remove extrafields + if ((empty($conf->global->MAIN_EXTRAFIELDS_DISABLED))) // For avoid conflicts if trigger used + { + $result=$this->deleteExtraFields(); + if ($result < 0) + { + $error++; + dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR); + } + } + } + else + { + $error++; + $this->error = "Error ".$this->db->lasterror(); + } + } + + if (! $error) + { + $this->db->commit(); return 1; } - else { - dol_print_error($this->db); + else + { + $this->db->rollback(); return -1; } } diff --git a/htdocs/compta/bank/class/bankcateg.class.php b/htdocs/compta/bank/class/bankcateg.class.php index 4f254282bb5..02d80d7429c 100644 --- a/htdocs/compta/bank/class/bankcateg.class.php +++ b/htdocs/compta/bank/class/bankcateg.class.php @@ -199,11 +199,12 @@ class BankCateg // extends CommonObject $this->db->begin(); - // Delete bank class + // Delete link between tag and bank account if (! $error) { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class"; - $sql.= " WHERE fk_categ = ".$this->id; + //$sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class"; // No more used + $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account"; + $sql.= " WHERE fk_categorie = ".$this->id; $resql = $this->db->query($sql); if (!$resql) -- GitLab