diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index be5272cf7c2..7ef28b3ee76 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -331,16 +331,40 @@ class ExtraFields $table=$elementtype.'_extrafields'; + $error=0; + if (! empty($attrname) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname)) { - $result=$this->db->DDLDropField(MAIN_DB_PREFIX.$table,$attrname); // This also drop the unique key + $result=$this->delete_label($attrname,$elementtype); if ($result < 0) { - $this->error=$this->db->lasterror(); + $this->error=$this->db->lasterror(); + $error++; } - $result=$this->delete_label($attrname,$elementtype); - + if (! $error) + { + $sql = "SELECT COUNT(rowid) as nb"; + $sql.= " FROM ".MAIN_DB_PREFIX."extrafields"; + $sql.= " WHERE elementtype = '".$elementtype."'"; + $sql.= " AND name = '".$attrname."'"; + //$sql.= " AND entity IN (0,".$conf->entity.")"; Do not test on entity here. We want to see if there is still on field remaning in other entities before deleting field in table + $resql = $this->db->query($sql); + if ($resql) + { + $obj = $this->db->fetch_object($resql); + if ($obj->nb <= 0) + { + $result=$this->db->DDLDropField(MAIN_DB_PREFIX.$table,$attrname); // This also drop the unique key + if ($result < 0) + { + $this->error=$this->db->lasterror(); + $error++; + } + } + } + } + return $result; } else diff --git a/htdocs/expensereport/class/expensereport.class.php b/htdocs/expensereport/class/expensereport.class.php index 631d1ca548b..e4a37cb5f62 100644 --- a/htdocs/expensereport/class/expensereport.class.php +++ b/htdocs/expensereport/class/expensereport.class.php @@ -1207,15 +1207,14 @@ class ExpenseReport extends CommonObject $expld_car = (empty($conf->global->NDF_EXPLODE_CHAR))?"-":$conf->global->NDF_EXPLODE_CHAR; $num_car = (empty($conf->global->NDF_NUM_CAR_REF))?"5":$conf->global->NDF_NUM_CAR_REF; - $sql = 'SELECT de.ref_number_int'; + $sql = 'SELECT MAX(de.ref_number_int) as max'; $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' de'; - $sql.= ' ORDER BY de.ref_number_int DESC'; - + $result = $this->db->query($sql); if($this->db->num_rows($result) > 0): $objp = $this->db->fetch_object($result); - $this->ref = $objp->ref_number_int; + $this->ref = $objp->max; $this->ref++; while(strlen($this->ref) < $num_car): $this->ref = "0".$this->ref; diff --git a/htdocs/filefunc.inc.php b/htdocs/filefunc.inc.php index 7e9503d7bb2..7407984a0d4 100644 --- a/htdocs/filefunc.inc.php +++ b/htdocs/filefunc.inc.php @@ -1,7 +1,7 @@ * Copyright (C) 2003 Xavier Dutoit - * Copyright (C) 2004-2016 Laurent Destailleur + * Copyright (C) 2004-2017 Laurent Destailleur * Copyright (C) 2004 Sebastien Di Cintio * Copyright (C) 2004 Benoit Mortier * Copyright (C) 2005-2011 Regis Houssin